OpenGL Lighting Issue - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: OpenGL Lighting Issue (/showthread.php?tid=3067) Pages:
1
2
|
OpenGL Lighting Issue - aadityap0901 - 09-23-2024 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: It does not occur in flat worlds. Can somebody explain? How can I fix it? RE: OpenGL Lighting Issue - MasterGy - 09-23-2024 (09-23-2024, 12:47 PM)aadityap0901 Wrote: While I was coding the new QB_Blocks rewrite, I decided to add GL Light.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. RE: OpenGL Lighting Issue - aadityap0901 - 09-23-2024 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. RE: OpenGL Lighting Issue - MasterGy - 09-23-2024 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. RE: OpenGL Lighting Issue - MasterGy - 09-23-2024 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 RE: OpenGL Lighting Issue - aadityap0901 - 09-24-2024 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)
Code for assigning Vertices, Texture Coords, NormalsTake 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)
It gave me this result:Some blocks have a different light effect. RE: OpenGL Lighting Issue - Petr - 09-24-2024 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 here _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... RE: OpenGL Lighting Issue - aadityap0901 - 09-25-2024 I used this: Code: (Select All)
and then thisCode: (Select All)
and I added normals for each vertex: but this shadow showed up in some parts of the world, and it changes as I move RE: OpenGL Lighting Issue - aadityap0901 - 09-26-2024 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: QB_Blocks_4_Alpha_26092024.7z (Size: 50.42 KB / Downloads: 14) RE: OpenGL Lighting Issue - Petr - 09-27-2024 The attachment cannot be downloaded and saved because Windows defender finds a virus in it and blocks the saving. |