Lazarus

Programming => Graphics and Multimedia => OpenGL => Topic started by: LemonParty on January 29, 2025, 03:15:47 pm

Title: Rotaring cube
Post by: LemonParty on January 29, 2025, 03:15:47 pm
I am doing a tutorial with OpenGL. In tutorial created a rotating cube. But when I runing the code I getting this weird effect (look at video https://streamable.com/u6zlvk (https://streamable.com/u6zlvk)).
Rendering loop
Code: Pascal  [Select][+][-]
  1.   while glfwWindowShouldClose(window) = 0 do begin
  2.     ProcessInput(window);
  3.  
  4.     glClearColor(0.2, 0.3, 0.3, 1.0);
  5.     glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  6.  
  7.  
  8.     glActiveTexture(GL_TEXTURE0);
  9.     glBindTexture(GL_TEXTURE_2D, Texture1);
  10.     glActiveTexture(GL_TEXTURE1);
  11.     glBindTexture(GL_TEXTURE_2D, Texture2);
  12.  
  13.     Vec1.Create(0.5, 1, 0);
  14.     View.CreateTranslation(0, 0, -80);
  15.     Projection.CreatePerspective(DegToRad(45), 800.0 / 600.0, 0.1, 100);
  16.     Model.CreateRotate(glfwGetTime, Vec1);
  17.  
  18.     Sh.Use;
  19.     glUniformMatrix4fv(glGetUniformLocation(Sh.ID, 'model'), 1, GL_FALSE, @Model.m00);
  20.     glUniformMatrix4fv(glGetUniformLocation(Sh.ID, 'view'), 1, GL_FALSE, @View.m00);
  21.     glUniformMatrix4fv(glGetUniformLocation(Sh.ID, 'projection'), 1, GL_FALSE, @Projection.m00);
  22.     glBindVertexArray(VAO);
  23.     glDrawArrays(GL_TRIANGLES, 0, 36);
  24.  
  25.     glfwSwapBuffers(window);
  26.  
  27.     glfwPollEvents();
  28.   end;    
  29.  
Vertex shader:
Code: C  [Select][+][-]
  1. #version 330 core
  2. layout (location = 0) in vec3 aPos;
  3. layout (location = 1) in vec2 aTexCoord;
  4.  
  5. out vec2 TexCoord;
  6.  
  7. uniform mat4 model;
  8. uniform mat4 view;
  9. uniform mat4 projection;
  10.  
  11. void main()
  12. {
  13.         gl_Position = projection * view * model * vec4(aPos, 1.0);
  14.         TexCoord = vec2(aTexCoord.x, aTexCoord.y);
  15. }
I am using SupraEngine library as replacement of glm.
Have anybody an idea why this effect appear?
Title: Re: Rotaring cube
Post by: Khrys on January 29, 2025, 04:30:09 pm
Disclaimer: I'm not familiar with the exact library you're using, but one thing I've noticed that seems fishy to me is that your rotation axis isn't normalized (length is not 1):

Code: Pascal  [Select][+][-]
  1. Vec1.Create(0.5, 1, 0);
Title: Re: Rotaring cube
Post by: LemonParty on January 30, 2025, 12:17:18 pm
I made it normalized and still the same issue.
Title: Re: Rotaring cube
Post by: Mathias on January 30, 2025, 05:48:31 pm
That's difficult to say, you'd have to see more code. There's something wrong with your perspective matrix. Are you still doing any calculations with it?

I have another example that might help.

https://github.com/sechshelme/Lazarus-OpenGL-3.3-Tutorial/tree/master/05_-_3D/20_-_Fluchtpunktperspektive_(Frustum)
Title: Re: Rotaring cube
Post by: Gigatron on January 30, 2025, 07:01:43 pm
Hi,
I would like to help you but not know the library you user;

Vec1.Create(1, 1, 1);
 

Try only Y axis ;
Code: Pascal  [Select][+][-]
  1. Vec1.Create(0, 1, 0);

Or send your project ;

Regards
Title: Re: Rotaring cube
Post by: LemonParty on January 30, 2025, 08:14:10 pm
Here is the project.
Title: Re: Rotaring cube
Post by: Gigatron2 on January 30, 2025, 08:51:59 pm
Just looked this part , it's seems ok i hope ;

LookAt for camera pos added it's now ok i think;

Good luck

Code: Pascal  [Select][+][-]
  1. while glfwWindowShouldClose(window) = 0 do begin
  2.     ProcessInput(window);
  3.  
  4.     glClearColor(0.2, 0.3, 0.3, 1.0);
  5.     glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  6.  
  7.  
  8.     glActiveTexture(GL_TEXTURE0);
  9.     glBindTexture(GL_TEXTURE_2D, Texture1);
  10.     glActiveTexture(GL_TEXTURE1);
  11.     glBindTexture(GL_TEXTURE_2D, Texture2);
  12.  
  13.     Vec1.Create(1, 1, 1);
  14.     Vec1:= Vec1.Normalize;
  15.  
  16.      Model.CreateRotate(glfwGetTime, Vec1);
  17.      View.CreateTranslation(0, 0, -80);
  18.  
  19.       View.CreateLookAt(
  20.       Vec3.Create(0, 0, 140), // camera pos
  21.       Vec3.Create(0, 0, 0), // look camera points x,y,z
  22.       Vec3.Create(0, 2, 0)  // x,Y,z
  23.     );
  24.  
  25.      Projection.CreatePerspective(DegToRad(45), 800.0 / 600.0, 0.1, 300);      
  26.  
  27.     Sh.Use;
  28.     glUniformMatrix4fv(glGetUniformLocation(Sh.ID, 'model'), 1, GL_FALSE, @Model.m00);
  29.     glUniformMatrix4fv(glGetUniformLocation(Sh.ID, 'view'), 1, GL_FALSE, @View.m00);
  30.     glUniformMatrix4fv(glGetUniformLocation(Sh.ID, 'projection'), 1, GL_FALSE, @Projection.m00);
  31.     glBindVertexArray(VAO);
  32.     glDrawArrays(GL_TRIANGLES, 0, 36);
  33.  
  34.     {
  35.     glBegin(GL_TRIANGLES);
  36.       glColor3d(1,0,0);
  37.       glVertex2f(-1, -1);
  38.       glColor3d(1,1,0);
  39.       glVertex2f(1, -1);
  40.       glColor3d(1,0,1);
  41.       glVertex2f(0, 1);
  42.     glEnd;
  43.     }
  44.  
  45.     glfwSwapBuffers(window);
  46.  
  47.     glfwPollEvents();
  48.   end;              
Title: Re: Rotaring cube
Post by: LemonParty on January 31, 2025, 12:05:35 pm
Gigatron2, this solution looks fine:
Code: Pascal  [Select][+][-]
  1.     Vec1.Create(1, 1, 1);
  2.     Vec1:= Vec1.Normalize;
  3.     View.CreateTranslation(0, 0, -80);
  4.     Projection.CreatePerspective(DegToRad(45), cWindowWidth / cWindowHeight, 0.1, 100);
  5.     Model.CreateRotate(glfwGetTime, Vec1);
The version where LookAt used not showing anything in scene.
Title: Re: Rotaring cube
Post by: Gigatron on January 31, 2025, 04:44:11 pm
Gigatron2, this solution looks fine:
Code: Pascal  [Select][+][-]
  1.     Vec1.Create(1, 1, 1);
  2.     Vec1:= Vec1.Normalize;
  3.     View.CreateTranslation(0, 0, -80);
  4.     Projection.CreatePerspective(DegToRad(45), cWindowWidth / cWindowHeight, 0.1, 100);
  5.     Model.CreateRotate(glfwGetTime, Vec1);
The version where LookAt used not showing anything in scene.

Hi LookAt is not show someting but it's just for using to control camera position.
In GL  world you have Scene Camera and objets.
To be clear here an example based on your source.

Code: Pascal  [Select][+][-]
  1. window := glfwCreateWindow(cWindowWidth, cWindowHeight, 'Hello World', nil, nil);
  2.   if window = nil then
  3.   begin
  4.     glfwTerminate();
  5.     ExitCode := -1;
  6.   end;
  7.  
  8.   glfwMakeContextCurrent(window);
  9.   glfwSetFramebufferSizeCallback(window, @FramebufferSizeCallback);
  10.  
  11.   //must be called after glfwMakeContextCurrent
  12.   if Load_GL_version_3_3 then
  13.     else ;
  14.   InitScene;
  15.  
  16.   while glfwWindowShouldClose(window) = 0 do begin
  17.     ProcessInput(window);
  18.  
  19.     glClearColor(0.2, 0.3, 0.3, 1.0);
  20.     glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  21.  
  22.  
  23.     glActiveTexture(GL_TEXTURE0);
  24.     glBindTexture(GL_TEXTURE_2D, Texture1);
  25.     glActiveTexture(GL_TEXTURE1);
  26.     glBindTexture(GL_TEXTURE_2D, Texture2);
  27.  
  28.     Vec1.Create(1, 1, 1);
  29.     Vec1:= Vec1.Normalize;
  30.  
  31.  
  32.      Model.CreateRotate(glfwGetTime*2, Vec3.Create(Vec1.x,Vec1.y,Vec1.z));
  33.      View.CreateTranslation(0, 0, -80);
  34.  
  35.     // Cam control
  36.       View.CreateLookAt(
  37.       Vec3.Create(0, 0, 150), // camera pos
  38.       Vec3.Create(0.4*sin(glfwGetTime*2), 0.8*cos(glfwGetTime*2), 0), // look camera points x,y,z
  39.       Vec3.Create(0, 2, 0)  // x,Y,z
  40.     );
  41.  
  42.      Projection.CreatePerspective(DegToRad(45), 800.0 / 600.0, 0.1, 300);
  43.  
  44.     Sh.Use;
  45.     glUniformMatrix4fv(glGetUniformLocation(Sh.ID, 'model'), 1, GL_FALSE, @Model.m00);
  46.     glUniformMatrix4fv(glGetUniformLocation(Sh.ID, 'view'), 1, GL_FALSE, @View.m00);
  47.     glUniformMatrix4fv(glGetUniformLocation(Sh.ID, 'projection'), 1, GL_FALSE, @Projection.m00);
  48.     glBindVertexArray(VAO);
  49.     glDrawArrays(GL_TRIANGLES, 0, 36);
  50.  
  51.     {
  52.     glBegin(GL_TRIANGLES);
  53.       glColor3d(1,0,0);
  54.       glVertex2f(-1, -1);
  55.       glColor3d(1,1,0);
  56.       glVertex2f(1, -1);
  57.       glColor3d(1,0,1);
  58.       glVertex2f(0, 1);
  59.     glEnd;
  60.     }
  61.  
  62.     glfwSwapBuffers(window);
  63.  
  64.     glfwPollEvents();
  65.   end;                      
Title: Re: Rotaring cube
Post by: LemonParty on January 31, 2025, 05:07:03 pm
Gigatron, in my message I mean that nothing is visible on screen when I run code with LookAt.
Title: Re: Rotaring cube
Post by: Gigatron on January 31, 2025, 05:48:47 pm
Gigatron, in my message I mean that nothing is visible on screen when I run code with LookAt.

Ah ok sorry I didn't understand well;
Title: Re: Rotaring cube
Post by: Mathias on February 04, 2025, 09:06:27 am
Is there a guarantee that the SupraEngine is error-free?
Title: Re: Rotaring cube
Post by: LemonParty on February 04, 2025, 10:11:37 am
Mathias, SupraEngine may have different format of matrixes. Rows and columns may be swaped.
Title: Re: Rotaring cube
Post by: Khrys on February 04, 2025, 01:20:34 pm
Is there a guarantee that the SupraEngine is error-free?

I just checked  TMatrix4x4.CreateRotate  against code from my own OpenGL project (which worked), and did indeed find an error at  SupraEngine.Math:6729  (line 9 here):

Code: Pascal  [Select][+][-]
  1. constructor TMatrix4x4.CreateRotate(const Angle:TScalar;const Axis:TVector3);
  2. var SinusAngle,CosinusAngle:TScalar;
  3. begin
  4.  SinCos(Angle,SinusAngle,CosinusAngle);
  5.  RawComponents[0,0]:=CosinusAngle+((1.0-CosinusAngle)*sqr(Axis.x));
  6.  RawComponents[1,0]:=((1.0-CosinusAngle)*Axis.x*Axis.y)-(Axis.z*SinusAngle);
  7.  RawComponents[2,0]:=((1.0-CosinusAngle)*Axis.x*Axis.z)+(Axis.y*SinusAngle);
  8.  RawComponents[0,3]:=0.0;
  9.  RawComponents[0,1]:=((1.0-CosinusAngle)*Axis.x*Axis.z)+(Axis.z*SinusAngle);
  10.  RawComponents[1,1]:=CosinusAngle+((1.0-CosinusAngle)*sqr(Axis.y));
  11.  RawComponents[2,1]:=((1.0-CosinusAngle)*Axis.y*Axis.z)-(Axis.x*SinusAngle);
  12.  RawComponents[1,3]:=0.0;
  13.  RawComponents[0,2]:=((1.0-CosinusAngle)*Axis.x*Axis.z)-(Axis.y*SinusAngle);
  14.  RawComponents[1,2]:=((1.0-CosinusAngle)*Axis.y*Axis.z)+(Axis.x*SinusAngle);
  15.  RawComponents[2,2]:=CosinusAngle+((1.0-CosinusAngle)*sqr(Axis.z));
  16.  RawComponents[2,3]:=0.0;
  17.  RawComponents[3,0]:=0.0;
  18.  RawComponents[3,1]:=0.0;
  19.  RawComponents[3,2]:=0.0;
  20.  RawComponents[3,3]:=1.0;
  21. end;

Note how the other lines never use the same component of  Axis  more than once, but the marked line uses  Z  twice - most likely a copy/paste error.
To fix this, just replace the first  Z  with  Y:

Code: Pascal  [Select][+][-]
  1. RawComponents[0,1]:=((1.0-CosinusAngle)*Axis.x*Axis.y)+(Axis.z*SinusAngle);

Also don't forget to normalize  Vec1  like I mentioned in my first response; otherwise the cube will expand & deform depending on the angle.
Title: Re: Rotating cube
Post by: LemonParty on February 04, 2025, 02:57:56 pm
Thank you, Khrys. Problem solved.
Title: Re: Rotaring cube
Post by: Mathias on February 10, 2025, 05:16:05 pm
Quote
I just checked  TMatrix4x4.CreateRotate  against code from my own OpenGL project (which worked), and did indeed find an error at  SupraEngine.Math:6729  (line 9 here):

If you find the error, you should actually make a bug report.
Is this library something official?
Title: Re: Rotaring cube
Post by: TRon on February 23, 2025, 01:26:46 am
If you find the error, you should actually make a bug report.
That would be a PM then.

Quote
Is this library something official?
Last time I checked it is a closed source project from BeRo though afaik replaced by its successor PasVulkan.Math.
TinyPortal © 2005-2018