Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OpenGL Lighting Issue
#6
From what I understood, we need to assign normal vectors for each 4 vertices of the face (I am using GL_QUADS btw) and not just the face.
This is what I did:
GL Code:
Code: (Select All)
    _glEnable _GL_LIGHTING
    _glEnable _GL_LIGHT0

    _glLightfv _GL_LIGHT0, _GL_AMBIENT, glVec4(SkyColorRed, SkyColorGreen, SkyColorBlue, 0)
    _glLightfv _GL_LIGHT0, _GL_DIFFUSE, glVec4(SkyColorRed, SkyColorGreen, SkyColorBlue, 0)
    _glLightfv _GL_LIGHT0, _GL_SPECULAR, glVec4(SkyColorRed, SkyColorGreen, SkyColorBlue, 0)
    _glLightfv _GL_LIGHT0, _GL_POSITION, glVec4(Player_Position.X, Player_Position.Y, Player_Position.Z, 0)
    _glLightfv _GL_LIGHT0, _GL_SPOT_DIRECTION, glVec4(0, 1, 0, 0)
    _glLightfv _GL_LIGHT0, _GL_SPOT_EXPONENT, glVec4(1, 0, 0, 0)

    _glEnable _GL_COLOR_MATERIAL
    _glColorMaterial _GL_FRONT, _GL_DIFFUSE

    _glEnable _GL_TEXTURE_2D

    _glBindTexture _GL_TEXTURE_2D, TextureHandle

    _glEnableClientState _GL_VERTEX_ARRAY
    _glEnableClientState _GL_TEXTURE_COORD_ARRAY
    _glEnableClientState _GL_NORMAL_ARRAY

    For I = LBound(Chunks) - 1 To UBound(Chunks) - 1
        If Chunks(I + 1).ShowCount = 0 Or Chunks(I + 1).isRenderDataLoaded = 0 Then _Continue
        _glVertexPointer 3, _GL_SHORT, 0, _Offset(Vertices(I * ChunkSectionVerticesSize + 1))
        _glTexCoordPointer 2, _GL_FLOAT, 0, _Offset(TexCoords(I * ChunkSectionVerticesSize + 1))
        _glNormalPointer _GL_FLOAT, 0, _Offset(Normals(I * ChunkSectionNormalsSize + 1))
        _glDrawArrays _GL_QUADS, 0, Chunks(I + 1).ShowCount
    Next I

    _glDisableClientState _GL_VERTEX_ARRAY
    _glDisableClientState _GL_TEXTURE_COORD_ARRAY
    _glDisableClientState _GL_NORMAL_ARRAY

    _glDisable _GL_TEXTURE_2D
    _glDisable _GL_COLOR_MATERIAL
    _glDisable _GL_LIGHT0
    _glDisable _GL_LIGHTING
Code for assigning Vertices, Texture Coords, Normals
Take LV as the new index
Vertices are Vector3, TexCoords are Vector2, Normals are Vector3
(Take Visibility (Unsigned Byte) = -1
AssetsModelVertices(1 to 24), AssetsModelTexCoords(1 to 24) are the hardcoded vertices and texture coords for a cube
BlockFaces Array includes which Texture is used at each face
I am using Texture Atlas)
Code: (Select All)
For I = 0 To 23
    Face = _SHL(1, _SHR(I, 2))
    If (Face And Visibility) = 0 Then _Continue
    LV = LV + 1
    Vertices(LV).X = AssetsModelVertices(I + 1).X + CX16 + X
    Vertices(LV).Y = AssetsModelVertices(I + 1).Y + Y
    Vertices(LV).Z = AssetsModelVertices(I + 1).Z + CZ16 + Z
    TexCoords(LV).X = AssetsModelTexCoords(I + 1).X
    TexCoords(LV).Y = (AssetsModelTexCoords(I + 1).Y + BlockFaces(Block, _SHR(I, 2) + 1) - 1) / TotalImages
    Chunks(FoundI).Count = Chunks(FoundI).Count + 1
    Select Case _SHR(I, 2)
        Case 1: Normals(LV).X = 1: Normals(LV).Y = 0: Normals(LV).Z = 0
        Case 2: Normals(LV).X = -1: Normals(LV).Y = 0: Normals(LV).Z = 0
        Case 3: Normals(LV).X = 0: Normals(LV).Y = 1: Normals(LV).Z = 0
        Case 4: Normals(LV).X = 0: Normals(LV).Y = -1: Normals(LV).Z = 0
        Case 5: Normals(LV).X = 0: Normals(LV).Y = 0: Normals(LV).Z = 1
        Case 6: Normals(LV).X = 0: Normals(LV).Y = 0: Normals(LV).Z = -1
    End Select
Next I
It gave me this result:
[Image: 09242024152752.png]
Some blocks have a different light effect.
Reply


Messages In This Thread
OpenGL Lighting Issue - by aadityap0901 - 09-23-2024, 12:47 PM
RE: OpenGL Lighting Issue - by MasterGy - 09-23-2024, 01:22 PM
RE: OpenGL Lighting Issue - by aadityap0901 - 09-23-2024, 02:45 PM
RE: OpenGL Lighting Issue - by MasterGy - 09-23-2024, 02:58 PM
RE: OpenGL Lighting Issue - by MasterGy - 09-23-2024, 03:40 PM
RE: OpenGL Lighting Issue - by aadityap0901 - 09-24-2024, 10:01 AM
RE: OpenGL Lighting Issue - by Petr - 09-24-2024, 07:31 PM
RE: OpenGL Lighting Issue - by aadityap0901 - 09-25-2024, 12:00 PM
RE: OpenGL Lighting Issue - by aadityap0901 - 09-26-2024, 04:05 PM
RE: OpenGL Lighting Issue - by Petr - 09-27-2024, 08:14 AM
RE: OpenGL Lighting Issue - by aadityap0901 - 09-27-2024, 12:31 PM
RE: OpenGL Lighting Issue - by aadityap0901 - 10-02-2024, 12:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  OpenGL lesson 1 ! MasterGy 7 663 11-07-2025, 02:32 AM
Last Post: madscijr
  keyboard issue ? SHIFT + TAB not detected (linux) Herve 13 1,759 08-27-2025, 09:47 AM
Last Post: Herve
  Another Mouse Issue? NakedApe 17 2,816 05-02-2024, 03:16 AM
Last Post: NakedApe
  A Perplexing Issue NakedApe 5 1,443 08-04-2023, 06:21 PM
Last Post: OldMoses
  HELP! I find an issue using _DEVICES! TempodiBasic 10 1,851 05-02-2023, 09:57 AM
Last Post: TempodiBasic

Forum Jump:


Users browsing this thread: 1 Guest(s)