OpenGL examples
#9
I'm glad to hear that, your tutorial is worked out nicely and clearly, and a lot of details always come in handy. If you keep the format you started, it will be great.

Here is a sample - the first texture. I wrote a function for maximum simplification and I immediately solved the freeing of memory when the program was terminated, and I added an example with DisplayOrder to make it clear how the layers of the image are rendered one after the other.

Please use correct file name on row 34.

Code: (Select All)
Type GL_Loader
    PointerGL As Long
End Type
ReDim Shared GLL(0) As GL_Loader
Dim Shared GL_InitInfo As _Byte


_Title "Use texture!"
Screen _NewImage(600, 600, 32)

Dim Shared t 'bad solution, it is just for this use

_DisplayOrder _GLRender , _Software 'in background run OpenGL screen, in foreground run Software (QB64 standard) screen
Do

    Locate 1
    Print Time$
    _Limit 40
    If _Exit Then
        'if program end, this is step 2. Step 1 is in _GL Sub, in this place program just end.
        System
    End If
Loop








Sub _GL ()

   if t = 0 then t = LoadTexture("dum03.jpg") 'function load texture from valid file and return OpenGL Handle for this texture


    _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
    _glVertex2f -0.5, 0.5 'top-left
    _glTexCoord2f 1, 1
    _glVertex2f 0.5, 0.5 'top-right
    _glTexCoord2f 1, 0
    _glVertex2f 0.5, -0.5 'bottom right
    _glTexCoord2f 0, 0
    _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
        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

        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
        ReDim _Preserve GLL(U + 1) As GL_Loader

        _MemFree m

        _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


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: 13 Guest(s)