04-13-2023, 10:53 PM
(03-31-2023, 06:59 PM)Petr Wrote: The following is a demonstration of how to texture a cube with different textures from different sides. Notice the ever-repeating _glTexCoord2f, followed by _glVertex3f... over and over again. That's literally says - use field! This way it's better for understanding, but in practice I would rather create a data file with the coordinates and then compile the whole thing in a loop using only these two commands.
Try applying a texture with transparency to one position, it's quite interesting. Don't forget that even in this case you can mix color as in the previous example.
Do not forgot to change texture file names!
Code: (Select All)Type GL_Loader
PointerGL As Long
TextureName As String
End Type
ReDim Shared GLL(0) As GL_Loader, t As Long
Dim Shared GL_InitInfo As _Byte
Dim Shared ExitSignal As _Byte
_Title "Use texture!"
Screen _NewImage(1024, 768, 32)
Do
If ExitSignal Then System
_Limit 40
Loop
Sub _GL ()
Static Xrot, Yrot, Zrot, t
t1 = LoadTexture("Phoenix.png") 'function load texture from valid file and return OpenGL Handle for this texture,
t2 = LoadTexture("X.png")
t3 = LoadTexture("cat.gif")
t4 = LoadTexture("banan.gif")
t5 = LoadTexture("dum03.jpg")
t6 = LoadTexture("new sob.gif")
' if handle exists, return it and do not next.
Init2
_glClear _GL_COLOR_BUFFER_BIT And _GL_DEPTH_BUFFER_BIT 'Clear screen and depth buffer
_glMatrixMode _GL_PROJECTION ' Set projection matrix - TRY comment this five rows and then run it. Black screen occur. For view just something then must be depth set to -1 (Z parameter in _glTranslateF)
_glLoadIdentity '();// Reset matice
_gluPerspective 90.0F, _Width / _Height, 0.1F, 100.0F ' Perspective calculation
_glMatrixMode _GL_MODELVIEW ' set modelview matrix
_glLoadIdentity
_glTranslatef 0.0F, 0.0F, -5F 'Shift to depth - without projection matrix settings if Z is -5 just black screen occur!
_glRotatef Xrot, 1.0F, 0.0F, 0.0F 'rotation in axis X
_glRotatef Yrot, 0.0F, 1.0F, 0.0F ' Y
_glRotatef Zrot, 0.0F, 0.0F, 1.0F ' Z
_glBindTexture _GL_TEXTURE_2D, t1 'set texture - in this case phoenix logo to this wall
_glBegin _GL_QUADS
'Front Wall
_glTexCoord2f 0.0F, 0.0F: _glVertex3f -1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f 1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f 1.0F, 1.0F, 1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.0F, 1.0F, 1.0F
_glEnd '<---------------- for more texture FIRST MUST BE _glEnd used and then
_glBindTexture _GL_TEXTURE_2D, t2 'set character "X" as texture to this wall
_glBegin _GL_QUADS '<----------- after new texture settings, continue
' Rear Wall
_glTexCoord2f 1.0F, 0.0F: _glVertex3f -1.0F, -1.0F, -1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f -1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f 1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f 1.0F, -1.0F, -1.0F
_glEnd
_glBindTexture _GL_TEXTURE_2D, t3 'set cat image as texture to this wall
_glBegin _GL_QUADS '<----------- after new texture settings, continue
' Upper Wall
_glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f -1.0F, 1.0F, 1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f 1.0F, 1.0F, 1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f 1.0F, 1.0F, -1.0
_glEnd
_glBindTexture _GL_TEXTURE_2D, t4 'set banana image as texture to this wall
_glBegin _GL_QUADS '<----------- after new texture settings, continue
' Bottom Wall
_glTexCoord2f 1.0F, 1.0F: _glVertex3f -1.0F, -1.0F, -1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f 1.0F, -1.0F, -1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f 1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f -1.0F, -1.0F, 1.0F
_glEnd
_glBindTexture _GL_TEXTURE_2D, t5 'set house image as texture to this wall
_glBegin _GL_QUADS '<----------- after new texture settings, continue
' Right Wall
_glTexCoord2f 1.0F, 0.0F: _glVertex3f 1.0F, -1.0F, -1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f 1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f 1.0F, 1.0F, 1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f 1.0F, -1.0F, 1.0F
_glEnd
_glBindTexture _GL_TEXTURE_2D, t6 'set reindeer as texture to this wall
_glBegin _GL_QUADS '<----------- after new texture settings, continue
' Left Wall
_glTexCoord2f 0.0F, 0.0F: _glVertex3f -1.0F, -1.0F, -1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f -1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f -1.0F, 1.0F, 1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.0F, 1.0F, -1.0F
_glEnd
Xrot = Xrot + 0.3F
Yrot = Yrot + 0.2F
Zrot = Zrot + 0.4F
If _Exit Then
DeleteTexture t 'if program end, first free texture from memory, then exit from GL and return to main loop
_glClear _GL_COLOR_BUFFER_BIT
ExitSignal = Not 0
Exit Sub
End If
End Sub
Sub GL_Init
If GL_InitInfo = 0 Then
' _glViewport 0, 0, _Width, _Height
GL_InitInfo = 1
End If
End Sub
Sub Init2
_glEnable _GL_TEXTURE_2D 'allow texture maping
_glShadeModel _GL_SMOOTH ' smooth
_glClearColor 0.1F, 0.1F, 0.1F, 0.5F 'Black background
_glClearDepth 1.0F ' depth buffer settings
_glEnable _GL_DEPTH_TEST ' Allow depth testing - try comment it and the run program. Object is then very deformed.
_glDepthFunc _GL_LEQUAL ' Depth testing type
_glHint _GL_PERSPECTIVE_CORRECTION_HINT, _GL_NICEST ' best perspective projection
End Sub
Sub DeleteTexture (nr As Long)
For P = LBound(GLL) To UBound(GLL)
If GLL(P).PointerGL = nr Then
Dim DEL As Long
DEL = GLL(P).PointerGL
_glDeleteTextures 1, _Offset(DEL)
Exit Sub
End If
Next
End Sub
Function LoadTexture (image As String)
If GL_InitInfo = 0 Then GL_Init
If _FileExists(image) Then
TT = 0
Do Until TT = UBound(GLL)
If GLL(TT).TextureName = image$ Then
LoadTexture = GLL(TT).PointerGL 'prevent memory leak loading next and next texture again and angain...
Exit Function
End If
TT = TT + 1
Loop
t = t + 1
tex& = _LoadImage(image$, 32)
texinv& = _NewImage(_Width(tex&), _Height(tex&), 32)
_PutImage (0, _Height(tex&) - 1)-(_Width(tex&) - 1, 0), tex&, texinv&
_FreeImage tex&
Dim Texture As Long
_glGenTextures 1, _Offset(Texture) 'generate our texture handle (reserve place in memory for new texture)
_glBindTexture _GL_TEXTURE_2D, Texture 'select our texture handle (set this texture for use)
Dim m As _MEM
m = _MemImage(texinv&)
_glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGB, _Width(texinv&), _Height(texinv&), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
'glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
U = UBound(GLL)
GLL(U).PointerGL = Texture
GLL(U).TextureName = image$
ReDim _Preserve GLL(U + 1) As GL_Loader
_MemFree m
'set our texture wrapping
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_WRAP_S, _GL_REPEAT
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_WRAP_T, _GL_REPEAT
'set out texture filtering
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR 'for scaling up
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR 'for scaling down
Else
Print "LoadTexture Error: "; image$; " - file not found."
End If
LoadTexture = Texture
End Function
Hi Petr,
where are the images for texture that you load?
are they these following?
tiger
bird
cat
horse
dog
eagle