Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OpenGL Lighting Issue
#1
While I was coding the new QB_Blocks rewrite, I decided to add GL Light.
Eventually it worked someway, but I can't figure out why did this happen:

[Image: 09232024180833.png]
It does not occur in flat worlds.
Can somebody explain? How can I fix it?
Reply
#2
(09-23-2024, 12:47 PM)aadityap0901 Wrote: While I was coding the new QB_Blocks rewrite, I decided to add GL Light.
Eventually it worked someway, but I can't figure out why did this happen:

[Image: 09232024180833.png]
It does not occur in flat worlds.
Can somebody explain? How can I fix it?
Hi !

I've been working with opengl for the past few months, and I'm still in it now. Based on the image, it seems that there are no normal vectors for the vertices, which determine the direction of the vertices. Since they all have the same (0), the graphic will have a fog effect rather than a light effect. The direction is important because strangely enough, opengl won't determine the direction of plane domes from their location. This is because a plane has 2 sides. when it comes to light effects, it is an important factor where the light source is and in which direction the plane is facing. Imagine that you are inside a cube. the light source comes from outside. What determines that you, who are inside the cube, can see the two opposite sides of the cube? This is the interesting part, because if the normal vectors of the two opposite sides are the same, and also parallel in position, then the two sheets will be equally bright, which makes no sense. That's why we need the normal vector.

In the case of a cube, the normal vectors belonging to the points must be directed relative to the center of the cube.
If you provide the source code, I can try to include it, and then you can set it up yourself and understand it.
Where you create the points, it must be added to create the normal vectors in addition to the vertices.
Reply
#3
I am currently assigning a normal vector to each VISIBLE face of the cube, or should I assign for each vertex? I can't understand how would I assign a normal vector for each vertex when there is nothing for cross product of vectors.
Reply
#4
No, you don't have to assign a normal vector to a plane. but to vertex. There must be the same number of normal vectors as there are vertices.

1 point vertex : xyz . this defines the position of the point
1 point normalvector : x'y'Z this determines the direction.

You don't have to worry about determining the direction of your skiing!!! opengl then calculates with linear interpolation when rendering, from the 3 normal vectors per pixel, where and how much light is needed on the surface.
Reply
#5
I'll try to explain it this way. I decided to include the skis. so I prepare everything before rendering. this ptv4 does that i invite this somewhere.
for example, I want a texture of any kind, in a given place, with a given normal vector calculation (2 types), with given texture coordinates, with a given fog type, and I parameterize whether this rectangle should be put in order (is it transparent and semi transparent part) This is of course called 6x for a cube. this is the insertion of a rectangle: not qb64, but basic.


Sub add_ptv4(lighttype As Int,nx As Float,ny As Float,nz As Float,SORT As Boolean,FOG As Byte,text As Int,t0 As Float,t1 As Float,t2 As Float,t3 As Float,t4 As Float,t5 As Float,t6 As Float,t7 As Float,d0 As Float,d1 As Float,d2 As Float,d3 As Float,d4 As Float,d5 As Float,d6 As Float,d7 As Float,d8 As Float,d9 As Float,d10 As Float,d11 As Float)

planf_vert(rend_dat(0)) = rend_dat(1)
' planf_txtc(rend_dat(0)) = rend_dat(2)
planf_poly(rend_dat(0)) = 4
planf_fog(rend_dat(0)) = FOG
planf_text(rend_dat(0)) = text
planf_sort(rend_dat(0)) = SORT
planf_lsun(rend_dat(0)) = False
If lighttype>0 Then planf_lsun(rend_dat(0)) = True

rend_dat(0) = rend_dat(0)+1

If lighttype = 1 Then
'planf normalvector FLAT -------------------------------------------------------------------------------------

Dim dis(4) As Float
dis(0) = (d0+d3+d6+d9)*.25-nx: dis(1) = (d1+d4+d7+d10)*.25-ny: dis(2) = (d2+d5+d8+d11)*.25-nz


For t = 0 To 3
rend_norm(rend_dat(1)+t*3+0) = dis(0): rend_norm(rend_dat(1)+t*3+2) = dis(1): rend_norm(rend_dat(1)+t*3+1) = dis(2)
Next
End If

If lighttype = 2 Then
'planf normalvector SMOOTH  ------------------------------------------------------------------------------------------
rend_norm(rend_dat(1)+0) = d0-nx: rend_norm(rend_dat(1)+2) = d1-ny: rend_norm(rend_dat(1)+1) = d2-nz
rend_norm(rend_dat(1)+3) = d3-nx: rend_norm(rend_dat(1)+5) = d4-ny: rend_norm(rend_dat(1)+4) = d5-nz
rend_norm(rend_dat(1)+6) = d9-nx: rend_norm(rend_dat(1)+8) = d10-ny: rend_norm(rend_dat(1)+7) = d11-nz
rend_norm(rend_dat(1)+9) = d6-nx: rend_norm(rend_dat(1)+11) = d7-ny: rend_norm(rend_dat(1)+10) = d8-nz
End If

rend_vert(rend_dat(1)+0) = d0:  rend_vert(rend_dat(1)+2) = d1: rend_vert(rend_dat(1)+1) = d2
rend_vert(rend_dat(1)+3) = d3: rend_vert(rend_dat(1)+5) = d4: rend_vert(rend_dat(1)+4) = d5
rend_vert(rend_dat(1)+6) = d9: rend_vert(rend_dat(1)+8) = d10: rend_vert(rend_dat(1)+7) = d11
rend_vert(rend_dat(1)+9) = d6: rend_vert(rend_dat(1)+11) = d7: rend_vert(rend_dat(1)+10) = d8
rend_dat(1) = rend_dat(1)+12

rend_txtc(rend_dat(2)+0) = t0: rend_txtc(rend_dat(2)+1) = t1: rend_txtc(rend_dat(2)+2) = t2: rend_txtc(rend_dat(2)+3) = t3
rend_txtc(rend_dat(2)+4) = t6: rend_txtc(rend_dat(2)+5) = t7: rend_txtc(rend_dat(2)+6) = t4: rend_txtc(rend_dat(2)+7) = t5
rend_dat(2) = rend_dat(2)+8

End Sub




The different parameters can be defined by planf vert/textc/poly (how many points define the planar 3/4 triangle, rectangle) /fog, text, sort

The relevant part will be important for you to understand!

you can see that it stores the vertices in the 'rend_vert' array, sequentially. for a rectangle, you can see, it increases the size by 12 coordinates. 12 coordinate points = 4 vertices = position of 4 points
rend_dat(1) is the counter of how many vertices are in the array.

The rend_norm array records the direction vectors belonging to the points. it also has rend_dat(1) as its counter, since it will have the same number of normal vectors as vertices.

you can see that sub gets d0,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11 . these are the 12 coordinate points mentioned above. x0y0z0, x1y1z1 ,x2y2z2 ,x3y3z3

these vertices !!!!

and the norm vector is based on these, and from the center of the object itself (the center of a cube, the center of a sphere, the center of whatever, from which the direction vectors start outward)
The center of the object is nx,ny,nz.

this sub must be called 6 times to insert the cube. all 6 times nx,ny,nz are unchanged! he is the center of the cube or sphere.

Knowing d0,d1,d2....,d11 and nx,ny,nz, there are 2 ways to create a normal vector (called lighttype in the code)

In one case, all points of the entire plane will point in 1 direction. (lighttype1) in the case of this cube, the rectangles falling in line should not have a staggered light.
In the other case, it calculates the normal vector directed exclusively from the center (lighttype2), which is nicer in the case of a sphere.

Both calculation methods can be used, there will be a small difference in the view.

there is one more very important thing !!!!!

The direction in which planar ridges are drawn.

all 6 sides must be approached in the correct order to the vertices. if one of them is not in sync with the others, there will be a negative light effect.

I also include the 'cube-insertion' sub, which contains the correct order of drawing the cube! this drawcube_direct inserts a cube into the render with the help of add_ptv4.
This may seem chaotic at first, but if you dig into it, you will get a feel for how to create the normal vector.









Sub drawcube_direct(lighttype As Int ,SORT As Boolean,FOG As Byte,x As Float, y As Float, z As Float, a1 As Float, a2 As Float, size As Float,texture As Int)

size = size/2

Dim p(4, 3), pc(8, 3) As Float 'Float
Dim side As Int
For t = 0 To 7
For t2 = 0 To 2
pc(t, t2) = size * math.cube_polars(t,t2) '+ cubes(makecube,0)

Next
math.rotate_2d (pc(t,0),pc(t,1),a1)
pc(t,0) = math.roti(0): pc(t,1) = math.roti(1)

math.rotate_2d (pc(t,0),pc(t,2),a2)
pc(t,0) = math.roti(0): pc(t,2) = math.roti(1)


pc(t,0) = pc(t,0)-x
pc(t,1) = pc(t,1)-z
pc(t,2) = pc(t,2)-y

Next

For t = 0 To 5

For t2 = 0 To 3

side = Asc("5476 0123 2367 1054 3175 0246".CharAt(t*5+t2))-48
For t3 = 0 To 2: p(t2, t3) = pc(side, t3)
Next
Next

add_ptv4 (lighttype,-x,-z,-y,SORT,FOG,texture,0,1,1,1,0,0,1,0,p(0,0),p(0,1),p(0,2),p(1,0),p(1,1),p(1,2),p(2,0),p(2,1),p(2,2),p(3,0),p(3,1),p(3,2))




Next








End Sub
Reply
#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
#7
try (row 8, 9)

_glLightfv _GL_LIGHT0, _GL_SPOT_DIRECTION,  _offset(glVec4(0, 1, 0, 0))
_glLightfv _GL_LIGHT0, _GL_SPOT_EXPONENT, _offset (glVec4(1, 0, 0, 0))

I can't see the perspective settings in the program either:
example:


Declare Library
    Sub gluLookAt (ByVal eyeX As Double, Byval eyeY As Double, Byval eyeZ As Double, Byval centerX As Double, Byval centerY As Double, Byval CenterZ As Double, Byval upX As Double, Byval upY As Double, Byval upZ As Double)
End Declare

  ' try first call _glLightfv and as second after all _glenable (part of my program hereSmile

    _glLightfv _GL_LIGHT0, _GL_DIFFUSE, _Offset(Light_Diffuse())
    _glLightfv _GL_LIGHT0, GL_POSITION, _Offset(Light_Position())
    _glEnable _GL_LIGHT0
    _glEnable _GL_LIGHTING
    _glEnable _GL_DEPTH_TEST
    _glMatrixMode _GL_PROJECTION
    _gluPerspective 40.0, 1.0, 1.0, 10.0
    '  glMatrixMode(GL_MODELVIEW);
    _glMatrixMode _GL_MODELVIEW
    '  gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
    '    0.0, 0.0, 0.0,      /* center is at (0,0,0) */
    '    0.0, 1.0, 0.);      /* up is in positive Y direction */
    gluLookAt 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0

    '  /* Adjust cube position to be asthetic angle. */
    '  glTranslatef(0.0, 0.0, -1.0);
    '  glRotatef(60, 1.0, 0.0, 0.0);
    '  glRotatef(-20, 0.0, 0.0, 1.0);

    _glTranslatef 0.0, 0.0, -1.0
    _glRotatef 60, 1.0, 0.0, 0.0
    _glRotatef -20, 0.0, 0.0, 1.0

maybe this help, maybe not, OpenGL is magic garden...


Reply
#8
I used this:
Code: (Select All)
_glTranslatef 0, 0, -0.25
_glRotatef -Player_Angle.Y, 1, 0, 0
_glRotatef Player_Angle.X, 0, 1, 0
_glTranslatef -Player_Position.X, -Player_Position.Y, -Player_Position.Z
and then this
Code: (Select All)
_glMatrixMode _GL_PROJECTION
_glLoadIdentity
_gluPerspective FOV, _Width / _Height, 0.1, Max(ChunkHeight, RenderDistance * 16)

and I added normals for each vertex:
but this shadow showed up in some parts of the world, and it changes as I move

[Image: 09242024223125.png]
Reply
#9
One big issue I am facing - it freezes when I try to use timers (to calculate fps)
So when I uncomment line 203 -> Timer(FPSCounterTimer) On
After a second (the timer is set to 1 second), the game crashes with $Let GL = -1 (on line 76) but continues if we do $Let GL = 0 (line 76)
So, I concluded that it the Sub _GL that causes the issue with timers, but I can't figure out the problem.
On my old versions (QB Blocks 3), this isn't an issue.
Please help me, I am trying to fix it from days.
Here are all the files:
.7z   QB_Blocks_4_Alpha_26092024.7z (Size: 50.42 KB / Downloads: 15)
Reply
#10
The attachment cannot be downloaded and saved because Windows defender finds a virus in it and blocks the saving.


Reply




Users browsing this thread: 1 Guest(s)