OpenGL examples
#27
The example and demonstration of the fog created by the _glFog command follows on from an earlier example of a rotating cube with a single texture over the entire surface (answer 11 in this thread)

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


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
Dim Shared ExitSignal As _Byte, Blend As _Byte
'--------------------------------------------------

Dim Shared LightAmbient(3) As Single
LightAmbient(0) = 0.5
LightAmbient(1) = 0.5
LightAmbient(2) = 0.5
LightAmbient(3) = .1


Dim Shared LightDifuse(3) As Single
LightDifuse(0) = 1
LightDifuse(1) = 1
LightDifuse(2) = 1
LightDifuse(3) = 1

Dim Shared LightPosition(3) As Single
LightPosition(0) = 0
LightPosition(1) = 0
LightPosition(2) = 2
LightPosition(3) = 1

Dim Shared Textures(2) As Long, SetTexture, Light, Xrot, Yrot, Zdepth
Dim Shared Fog, FogLevel, FogQuality
FogLevel = 0.30
Dim Shared FogColor(3)
FogColor(0) = 0.5: FogColor(1) = 0.5: FogColor(2) = 0.5: FogColor(3) = 1

_Title "Fog"
Screen _NewImage(1024, 768, 32)

Fog = 1
SetTexture = 0
Light = -1
Xrot = .2
Yrot = .3
Zdepth = -5
_DisplayOrder _GLRender , _Software 'so PRINT comments is visible on the OpenGL screen

Do
    i$ = InKey$
    Select Case UCase$(i$)
        Case "L"
            If LightTimer < Timer Then
                Light = Light * -1
                LightTimer = Timer + 1
            End If
        Case "F"
            SetTexture = SetTexture + 1
            If SetTexture > 2 Then SetTexture = 0
        Case "S" 'rotation in X axis
            Xrot = Xrot + .3
        Case "X"
            Xrot = Xrot - .3
        Case "C"
            Yrot = Yrot - .3
        Case "D"
            Yrot = Yrot + .3
        Case "G"
            Zdepth = Zdepth + .1
        Case "B"
            Zdepth = Zdepth - .1
        Case "K" 'fog level +
            FogLevel = FogLevel + .01
        Case "M" ' fog level -
            FogLevel = FogLevel - .01
        Case "P"
            Fog = Fog + 1: If Fog > 3 Then Fog = 0
        Case "I"
            FogQuality = FogQuality + 1
            If FogQuality > 2 Then FogQuality = 0

    End Select

    If ExitSignal Then System
    _Limit 40
Loop

Sub _GL ()

    Init2

    Static xr, yr, zrot
    Locate 2
    '   _glClearColor 0.5, 0.5, 0.5, 1
    Color , _RGB32(127, 127, 127)
    Print "P for fog type "
    Print "K increase fog, M decrease fog"
    Print "Try keys L: Light on/off"
    Print "Set texture filtering: F"
    Print "Rotation speed in X axis: S, X"
    Print "Rotation speed in Y axis: C, D"
    Print "Set Depth: G, B"


    'navic
    _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)
    _gluPerspective 90.0F, _Width / _Height, 0.1F, 100.0F ' Perspective calculation
    _glMatrixMode _GL_MODELVIEW '                           set modelview matrix
    '-----


    _glClear _GL_COLOR_BUFFER_BIT And _GL_DEPTH_BUFFER_BIT 'Clear screen and depth buffer
    _glLoadIdentity 'matrix reset

    _glTranslatef 0.0F, 0.0F, Zdepth 'Shift to depth - without projection matrix settings if Z is -5 just black screen occur!
    _glRotatef xr, 1.0F, 0.0F, 0.0F 'rotation in axis X
    _glRotatef yr, 0.0F, 1.0F, 0.0F '                 Y
    _glRotatef zrot, 0.0F, 0.0F, 1.0F '               Z


    _glBindTexture _GL_TEXTURE_2D, Textures(SetTexture) 'set texture - in this case 1 texture for whole cube, 1 texture with 3 types of filtering


    _glBegin _GL_QUADS

    'Front Wall
    _glNormal3f 0.0F, 0.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.0F
    _glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.0F, 1.0F, 1.0F

    ' Rear Wall
    _glNormal3f 0.0F, 0.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
    _glTexCoord2f 0.0F, 0.0F: _glVertex3f 1.0F, -1.0F, -1.0F

    ' Upper Wall
    _glNormal3f 0.0F, 1.0F, 0.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
    _glTexCoord2f 1.0F, 1.0F: _glVertex3f 1.0F, 1.0F, -1.0

    ' Bottom Wall
    _glNormal3f 0.0F, -1.0F, 0.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
    _glTexCoord2f 1.0F, 0.0F: _glVertex3f -1.0F, -1.0F, 1.0F

    ' Right Wall
    _glNormal3f 1.0F, 0.0F, 0.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
    _glTexCoord2f 0.0F, 0.0F: _glVertex3f 1.0F, -1.0F, 1.0F

    ' Left Wall
    _glNormal3f -1.0F, 0.0F, 0.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.0F
    _glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.0F, 1.0F, -1.0F
    _glEnd



    zrot = zrot + 0.4F
    xr = xr + Xrot
    yr = yr + Yrot




    If _Exit Then
        For t = 0 To 2
            DeleteTexture t 'if program end, first free texture from memory, then exit from GL and return to main loop
        Next t
        _glClear _GL_COLOR_BUFFER_BIT
        ExitSignal = Not 0
        Exit Sub
    End If

End Sub

Sub GL_Init
    If GL_InitInfo = 0 Then


        GL_InitInfo = 1
    End If
End Sub

Sub Init2
    If GL_InitInfo = 0 Then
        Textures(0) = LoadTexture("container.jpg", 2) 'function load texture from valid file and return OpenGL Handle for this texture,
        Textures(1) = LoadTexture("container.jpg", 1)
        Textures(2) = LoadTexture("container.jpg", 0)
        GL_InitInfo = 1
        _glClearColor 0.5, 0.5, 0.5, 1
        _glFogfv _GL_FOG_COLOR, _Offset(FogColor()) 'fog color
        _glFogf _GL_FOG_DENSITY, FogLevel 'fog density
        _glHint _GL_FOG_HINT, _GL_DONT_CARE 'middle fog quality
        '  End If


        _glViewport 0, 0, _DesktopWidth, _DesktopHeight '                   visible area is fullscreen
        _glMatrixMode _GL_PROJECTION '
        _glLoadIdentity '
        _gluPerspective 45.0F, _Width / _Height, 0.1F, 100.0F 'set camera, other statement for full 3D is _gluLookAt
        _glMatrixMode _GL_MODELVIEW '
        _glLoadIdentity '                                                    reset all axis to basic settings (0,0,0 = X, Y, Z in middle)
    End If


    _glEnable _GL_TEXTURE_2D ' enable texture mapping
    _glShadeModel _GL_SMOOTH '
    _glClearColor 0.5, 0.5F, 0.5F, 1.0F ' background color is the same as fog color
    _glClearDepth 1.0F '                   depth buffer settings
    _glEnable _GL_DEPTH_TEST '             enable depth buffer testing
    _glDepthFunc _GL_LEQUAL '              depth buffer testing type
    _glHint _GL_PERSPECTIVE_CORRECTION_HINT, _GL_NICEST

    Select Case FogQuality
        Case 0
            _glHint _GL_FOG_HINT, _GL_FASTEST
        Case 1
            _glHint _GL_FOG_HINT, _GL_DONT_CARE
        Case 2
            _glHint _GL_FOG_HINT, _GL_NICEST
    End Select


    Select Case Fog '                                               select fog filtering type selected with "g" from keyboard     (fog modes)

        Case 1
            _glFogi _GL_FOG_MODE, _GL_EXP '             fog mode          basic fog level
            _glFogfv _GL_FOG_COLOR, _Offset(FogColor()) ' fog color

        Case 2
            _glFogi _GL_FOG_MODE, _GL_EXP2 '            fog mode          middle fog level
            _glFogfv _GL_FOG_COLOR, _Offset(FogColor()) ' fog color

        Case 3
            _glFogi _GL_FOG_MODE, _GL_LINEAR '                            best fog level
            _glFogfv _GL_FOG_COLOR, _Offset(FogColor()) '

    End Select

    _glFogf _GL_FOG_DENSITY, FogLevel '            fog density

    If Fog > 0 Then
        _glHint _GL_FOG_HINT, _GL_DONT_CARE '       fog Quality
        _glFogf _GL_FOG_START, 1.0F '               fog begin in depth - axis z
        _glFogf _GL_FOG_END, 5.0F '                 fog end in depth - axis z
        _glEnable _GL_FOG '                         enable fog
    Else
        _glDisable _GL_FOG '                        if g = 0 then is none fog, this disable it
    End If


    'dodatek pro lighting

    _glLightfv _GL_LIGHT1, _GL_AMBIENT, _Offset(LightAmbient!()) 'ambient light
    _glLightfv _GL_LIGHT1, _GL_DIFFUSE, _Offset(LightDiffuse!()) 'diffuse light
    _glLightfv _GL_LIGHT1, _GL_POSITION, _Offset(LightPosition!()) 'light position settings
    _glEnable _GL_LIGHT1 '       enable light
    ' Else
    If Light = -1 Then
        _glDisable _GL_LIGHTING 'disable light
    Else
        _glEnable _GL_LIGHTING
    End If

    _glClear _GL_COLOR_BUFFER_BIT
    _glClear _GL_DEPTH_BUFFER_BIT 'clear screen ad depth buffer

    _glLoadIdentity '              matrix reset


    '//////////////////////////////////////////////////////////////////
    _glColor4f 0.5F, 0.5F, 0.5F, 1.0F '                                   set full brightness and 50% alpha
    _glBlendFunc _GL_SRC_ALPHA, _GL_ONE '                                 Blending =  this two are need for alphablending after pressing "a"
    '#################################


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, Filter As _Unsigned _Byte)
    D = _Dest
    S = _Source
    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)
        texinv& = _NewImage(_Width(tex&), _Height(tex&), 32)

        _PutImage (0, _Height(tex&))-(_Width(tex&), 0), tex&, texinv&

        ni& = _CopyImage(texinv&, 32)

        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&)

        Dim n As _MEM
        n = _MemImage(ni&)

        Select Case Filter
            Case -1
                '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, pic->bpp/8, pic->width, pic->height, textureType, GL_UNSIGNED_BYTE, pic->data);


                gluBuild2DMipmaps _GL_TEXTURE_2D, 4, 16, 16, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, _Offset(Texture) 'need own memory block (n - MEM) in combination with m - MEM program crash... why?   ? ? ? ? ?
                _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR_MIPMAP_NEAREST 'for scaling up
                _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR '  IF IS USED _GL_LINEAR_MIMAP_NEAREST here, texture is white. Is it correct?    -?-     just God know...
                '


                _glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGB, _Width(ni&), _Height(ni&), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, n.OFFSET

                _FreeImage tex&
                _MemFree n
                _FreeImage ni&
                GoTo saveit

                'gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

        End Select

        _FreeImage tex&
        _glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGB, _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
    _Dest D
    _Source S
End Function


Program need texture:

[Image: container.jpg]


Program output:


[Image: OGL16.png]


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