Hi BPlus, thanks for your comment. When I call the H file in the next program, I will modify its call according to your request. You are right, guys are very good at 3d, I'm still learning 3D.
So, now to today's program. Again, I am inspired by NeHe. This is another type of transparency - namely, using a mask. How would I explain it...
Imagine you have an image with a yellow background. In normal work in QB64 (not in OpenGL) you set _ClearColor &HFFFF0000, handle& and you're done. Then you can insert the image into the scene and the yellow background is not visible.
In OpenGL, one method - based on transparency - is demonstrated in the previous example on a tree texture in a 3D world. Its disadvantage is that it has to be done last and moreover from the depth towards the camera.
This program shows another way - using two textures. One texture is a normal color image and the other texture is a black and white mask based on that color texture.
The way it works in this program is that what is white will be fully transparent. What is black will be visible from the color texture. This method should be easier (if you remember in the middle of the program that you want to add something). Focus your attention on the parameters of the _glBlendFunc command, it just sets the transparency types and also to _glEnable _gl_Blend and _glDisable _gl_Blend - turns blending (which is transparency) on and off.
Press space for scene change, M for enable/disable masking. Need textures in attachement.
So, now to today's program. Again, I am inspired by NeHe. This is another type of transparency - namely, using a mask. How would I explain it...
Imagine you have an image with a yellow background. In normal work in QB64 (not in OpenGL) you set _ClearColor &HFFFF0000, handle& and you're done. Then you can insert the image into the scene and the yellow background is not visible.
In OpenGL, one method - based on transparency - is demonstrated in the previous example on a tree texture in a 3D world. Its disadvantage is that it has to be done last and moreover from the depth towards the camera.
This program shows another way - using two textures. One texture is a normal color image and the other texture is a black and white mask based on that color texture.
The way it works in this program is that what is white will be fully transparent. What is black will be visible from the color texture. This method should be easier (if you remember in the middle of the program that you want to add something). Focus your attention on the parameters of the _glBlendFunc command, it just sets the transparency types and also to _glEnable _gl_Blend and _glDisable _gl_Blend - turns blending (which is transparency) on and off.
Code: (Select All)
Declare CustomType Library
Sub gluBuild2DMipmaps (ByVal Target As _Unsigned Long, Byval iFormat As Long, Byval Wdth As Long, Byval Hght As Long, Byval format As _Unsigned Long, Byval typ As _Unsigned Long, Byval dat As _Offset)
End Declare
_Title "OpenGL Masking (hide image background when used as texture)"
Type GL_Loader
PointerGL As Long
TextureName As String
Filtering As _Unsigned _Byte
End Type
ReDim Shared GLL(0) As GL_Loader, t As Long
Dim Shared GL_InitInfo As _Byte, preINIT As _Byte
Dim Shared ExitSignal As _Byte, Blend As _Byte
'---------------------------------------------------
Dim Shared Masking, Scene, Roll
Dim Shared Texture(5) As Long
Screen _NewImage(1024, 768, 32)
Do
i$ = InKey$
Select Case UCase$(i$)
Case "M": Masking = Not Masking
Case " ": Scene = Not Scene
End Select
_Limit 20
Loop
Sub _GL
Static Roll
Init2
_glMatrixMode _GL_PROJECTION '// Set projection matrix
_gluPerspective 45.0F, _Width / _Height, 0.1F, 100.0F ' This is GLUT statement, this is directly supported by QB64. Set up perspective projection matrix. First is angle for perspective, then is aspct, next is Z Near and Z Far
_glMatrixMode _GL_MODELVIEW ' Set Modelview matrix
_glClear _GL_COLOR_BUFFER_BIT
_glClear _GL_DEPTH_BUFFER_BIT 'clear screen and depth buffer
_glLoadIdentity 'matrix reset
_glTranslatef 0.0F, 0.0F, -2.0F 'shit do depth on the srceen
_glBindTexture _GL_TEXTURE_2D, Texture(0) 'select logo texture
_glBegin _GL_QUADS 'start drawing rectangle
_glTexCoord2f 0.0F, -Roll + 0.0F: _glVertex3f -1.1F, -1.1F, 0.0F
_glTexCoord2f 3.0F, -Roll + 0.0F: _glVertex3f 1.1F, -1.1F, 0.0F
_glTexCoord2f 3.0F, -Roll + 3.0F: _glVertex3f 1.1F, 1.1F, 0.0F
_glTexCoord2f 0.0F, -Roll + 3.0F: _glVertex3f -1.1F, 1.1F, 0.0F
_glEnd 'end drawing
_glEnable _GL_BLEND 'enable blending (transparency)
_glDisable _GL_DEPTH_TEST 'disable depth testing
If Masking Then 'is masking allowed?
_glBlendFunc _GL_DST_COLOR, _GL_ZERO ' Blend color image using zero (all, what is one (white) is transparent, all what is ZERO (black), is visible on the screen
End If
If Scene Then ' drawing second scene?
' We don't want the objects to be too big, so we move deeper into the screen. We will perform a rotation on the z axis by 0 degrees to 360 degrees according to the roll variable.
_glTranslatef 0.0F, 0.0F, -1.0F 'shit up to 1 to depth
_glRotatef Roll * 360, 0.0F, 0.0F, 1.0F 'rotation on Z axis
'If masking is on, we render the mask first and then the object. When off, only the object.
If Masking Then ' is masking enabled?
_glBindTexture _GL_TEXTURE_2D, Texture(3) 'select second mask texture
_glBegin _GL_QUADS 'start quad draving
_glTexCoord2f 0.0F, 0.0F: _glVertex3f -1.1F, -1.1F, 0.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f 1.1F, -1.1F, 0.0F
_glTexCoord2f 1.0F, 1.0: _glVertex3f 1.1F, 1.1F, 0.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.1F, 1.1F, 0.0F
_glEnd '();// Konec kreslení
End If
_glBlendFunc _GL_ONE, _GL_ONE 'for second color texture
_glBindTexture _GL_TEXTURE_2D, Texture(4) 'select second color texture
_glBegin _GL_QUADS 'start quad drawing
_glTexCoord2f 0.0F, 0.0F: _glVertex3f -1.1F, -1.1F, 0.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f 1.1F, -1.1F, 0.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f 1.1F, 1.1F, 0.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.1F, 1.1F, 0.0F
_glEnd '();// Konec kreslení
Else
If Masking Then 'is masking enabled?
_glBindTexture _GL_TEXTURE_2D, Texture(1) 'select first mask texture
_glBegin _GL_QUADS 'start drawing quad
_glTexCoord2f Roll + 0.0F, 0.0F: _glVertex3f -1.1F, -1.1F, 0.0F
_glTexCoord2f Roll + 4.0F, 0.0F: _glVertex3f 1.1F, -1.1F, 0.0F
_glTexCoord2f Roll + 4.0F, 4.0F: _glVertex3f 1.1F, 1.1F, 0.0F
_glTexCoord2f Roll + 0.0F, 4.0F: _glVertex3f -1.1F, 1.1F, 0.0F
_glEnd 'end drawing
End If
'We will set the Blending the same as last time. We select the scene one texture and render it in the same place as the mask.
_glBlendFunc _GL_ONE, _GL_ONE 'for first color texture
_glBindTexture _GL_TEXTURE_2D, Texture(2) 'select first color texture
_glBegin _GL_QUADS 'start drawing quad
_glTexCoord2f Roll + 0.0F, 0.0F: _glVertex3f -1.1F, -1.1F, 0.0F
_glTexCoord2f Roll + 4.0F, 0.0F: _glVertex3f 1.1F, -1.1F, 0.0F
_glTexCoord2f Roll + 4.0F, 4.0F: _glVertex3f 1.1F, 1.1F, 0.0F
_glTexCoord2f Roll + 0.0F, 4.0F: _glVertex3f -1.1F, 1.1F, 0.0F
_glEnd 'end drawing quad
'In order for the scene to move dynamically, we need to increment the roll.
End If
Roll = Roll + 0.002 ' Inkrement roll
If Roll > 1.0 Then Roll = Roll - 1 'is bigger than 1?
_glEnable _GL_DEPTH_TEST 'enable depth testing
_glDisable _GL_BLEND 'disable blending (transparency)
End Sub
Sub Init2
If GL_InitInfo = 0 Then
Texture(0) = LoadTexture("logo.jpg", 1)
Texture(1) = LoadTexture("mask1.jpg", 1)
Texture(2) = LoadTexture("image1.jpg", 1)
Texture(3) = LoadTexture("mask2.jpg", 1)
Texture(4) = LoadTexture("image2.jpg", 1)
GL_InitInfo = 1
Exit Sub
End If
_glClearColor 0.0F, 0.0F, 0.0F, 0.0F 'black background
_glClearDepth 1.0 'allow deleting depth buffer
_glEnable _GL_DEPTH_TEST ' allow depth testing
_glShadeModel _GL_SMOOTH ' smooth shading
_glEnable _GL_TEXTURE_2D 'enable texture mapping
End Sub
Function LoadTexture (image As String, Filter As _Unsigned _Byte)
' If GL_InitInfo = 0 Then GL_Init
If _FileExists(image) Then
TT = 0
Do Until TT = UBound(GLL)
If GLL(TT).TextureName = image$ And GLL(TT).Filtering = Filter 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)
' _SetAlpha 0, _RGB32(255, 255, 0) To _RGB32(254, 255, 255), tex&
texinv& = _NewImage(_Width(tex&), _Height(tex&), 32)
_PutImage (0, _Height(tex&))-(_Width(tex&), 0), tex&, texinv&
Dim Texture As _Unsigned 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&)
Select Case Filter
Case 3
'set our texture wrapping
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_WRAP_S, _GL_REPEAT
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_WRAP_T, _GL_REPEAT
Case 0
'set out texture filtering
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_NEAREST 'for scaling up
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST 'for scaling down
Case 1
_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
Case 2 'works....not sure, if this output is correct
gluBuild2DMipmaps _GL_TEXTURE_2D, 3, 16, 16, _GL_RGB, _GL_UNSIGNED_BYTE, _Offset(Texture)
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR_MIPMAP_LINEAR 'for scaling down
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_NEAREST
' 'for scaling UP
End Select
_FreeImage tex&
_glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGBA, _Width(texinv&), _Height(texinv&), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
saveit:
U = UBound(GLL)
GLL(U).PointerGL = Texture
GLL(U).TextureName = image
GLL(U).Filtering = Filter
ReDim _Preserve GLL(U + 1) As GL_Loader
_MemFree m
Else
Print "LoadTexture Error: "; image$; " - file not found."
End If
LoadTexture = Texture
End Function
Press space for scene change, M for enable/disable masking. Need textures in attachement.