Another sample with texture - this time in combination with the colors from the previous examples. Texture and color gradient can be combined. I made an adjustment in the LoadTexture sub, because writing IF t = 0 Then... seems silly to me. So for now, I solved the memory leak protection so that if the program tries to load the same texture again (that is, a file with the same name) that already has a texture assigned, then the LoadTexture function returns the original handle to the already created texture, without recreated this texture as new in memory. I am aware that this solution will be a significant drag in complex programs, so I will think about an improvement.
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(600, 600, 32)
_DisplayOrder _GLRender , _Software 'in background run OpenGL screen, in foreground run Software (QB64 standard) screen
Do
Locate 1
Print Time$
If ExitSignal Then System
_Limit 40
Loop
Sub _GL ()
t = LoadTexture("dum03.jpg") 'function load texture from valid file and return OpenGL Handle for this texture,
' if handle exists, return it and do not next.
_glEnable _GL_TEXTURE_2D 'enable texture mapping
_glClearColor 0, 0, 0, 1 'set color to solid black - here it is in range 0 to 1 - in QB64 it is in range 0 to 255, so 0.5 here is the same as 127 in QB64
_glClear _GL_COLOR_BUFFER_BIT
_glBindTexture _GL_TEXTURE_2D, t '<--------------- this handle is used here
_glBegin _GL_QUADS 'simply draw a quad
_glTexCoord2f 0, 1
_glColor3f 1.0F, 0.0F, 0.0F
_glVertex2f -0.5, 0.5 'top-left
_glTexCoord2f 1, 1
_glColor3f 0.0F, 0.5F, 0.0F
_glVertex2f 0.5, 0.5 'top-right
_glTexCoord2f 1, 0
_glColor3f 1.0F, 1.0F, 0.0F
_glVertex2f 0.5, -0.5 'bottom right
_glTexCoord2f 0, 0
_glColor3f 0.3F, 0.0F, 0.5F
_glVertex2f -0.5, -0.5 'bottom left
_glEnd
_glFlush
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 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
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
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_NICEST 'for scaling up
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST 'for scaling down
Else
Print "LoadTexture Error: "; image$; " - file not found."
End If
LoadTexture = Texture
End Function