OpenGL examples
#49
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.



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.

   


Attached Files
.zip   masking.zip (Size: 39.96 KB / Downloads: 12)


Reply


Messages In This Thread
OpenGL examples - by Petr - 03-25-2023, 04:01 PM
RE: OpenGL examples - by AshishKingdom - 03-25-2023, 09:46 PM
RE: OpenGL examples - by mnrvovrfc - 03-25-2023, 10:15 PM
RE: OpenGL examples - by Petr - 03-26-2023, 12:26 PM
RE: OpenGL examples - by Petr - 03-29-2023, 01:58 PM
RE: OpenGL examples - by dcromley - 03-29-2023, 03:35 PM
RE: OpenGL examples - by TerryRitchie - 03-29-2023, 04:19 PM
RE: OpenGL examples - by AshishKingdom - 03-31-2023, 11:25 AM
RE: OpenGL examples - by Petr - 03-31-2023, 02:31 PM
RE: OpenGL examples - by Petr - 03-31-2023, 03:12 PM
RE: OpenGL examples - by Petr - 03-31-2023, 06:24 PM
RE: OpenGL examples - by Petr - 03-31-2023, 06:59 PM
RE: OpenGL examples - by TempodiBasic - 04-13-2023, 10:53 PM
RE: OpenGL examples - by Petr - 04-01-2023, 12:56 PM
RE: OpenGL examples - by Petr - 04-01-2023, 01:19 PM
RE: OpenGL examples - by MasterGy - 04-01-2023, 05:20 PM
RE: OpenGL examples - by Petr - 04-01-2023, 06:56 PM
RE: OpenGL examples - by Petr - 04-01-2023, 07:25 PM
RE: OpenGL examples - by Petr - 04-02-2023, 06:36 PM
RE: OpenGL examples - by mnrvovrfc - 04-02-2023, 09:48 PM
RE: OpenGL examples - by Petr - 04-02-2023, 09:03 PM
RE: OpenGL examples - by MasterGy - 04-03-2023, 12:21 PM
RE: OpenGL examples - by Petr - 04-03-2023, 02:12 PM
RE: OpenGL examples - by mnrvovrfc - 04-03-2023, 03:59 PM
RE: OpenGL examples - by Petr - 04-03-2023, 02:23 PM
RE: OpenGL examples - by Petr - 04-03-2023, 07:44 PM
RE: OpenGL examples - by Petr - 04-03-2023, 09:02 PM
RE: OpenGL examples - by Petr - 04-08-2023, 08:40 PM
RE: OpenGL examples - by Petr - 04-09-2023, 07:12 PM
RE: OpenGL examples - by mnrvovrfc - 04-10-2023, 12:42 PM
RE: OpenGL examples - by Petr - 04-12-2023, 08:05 PM
RE: OpenGL examples - by Petr - 04-13-2023, 09:11 PM
RE: OpenGL examples - by mnrvovrfc - 04-14-2023, 01:46 AM
RE: OpenGL examples - by TerryRitchie - 04-14-2023, 03:10 AM
RE: OpenGL examples - by TempodiBasic - 04-14-2023, 08:52 AM
RE: OpenGL examples - by bplus - 04-14-2023, 09:17 AM
RE: OpenGL examples - by MasterGy - 04-14-2023, 10:19 AM
RE: OpenGL examples - by Petr - 04-14-2023, 02:00 PM
RE: OpenGL examples - by Petr - 04-14-2023, 04:23 PM
RE: OpenGL examples - by Petr - 04-14-2023, 08:04 PM
RE: OpenGL examples - by Petr - 04-14-2023, 09:22 PM
RE: OpenGL examples - by Petr - 04-15-2023, 03:08 PM
RE: OpenGL examples - by bplus - 04-15-2023, 05:09 PM
RE: OpenGL examples - by Petr - 04-15-2023, 09:05 PM
RE: OpenGL examples - by Petr - 04-15-2023, 09:27 PM
RE: OpenGL examples - by bplus - 04-15-2023, 10:01 PM
RE: OpenGL examples - by Petr - 04-16-2023, 05:40 AM
RE: OpenGL examples - by bplus - 04-16-2023, 02:22 PM
RE: OpenGL examples - by Petr - 04-17-2023, 06:46 PM
RE: OpenGL examples - by MasterGy - 04-19-2023, 02:43 PM
RE: OpenGL examples - by Petr - 04-19-2023, 02:49 PM
RE: OpenGL examples - by MasterGy - 04-26-2023, 10:12 AM
RE: OpenGL examples - by Petr - 04-26-2023, 02:11 PM



Users browsing this thread: 19 Guest(s)