giawa / opengl4csharp

OpenGL 4 Bindings (partially based on OpenTK) for C#
Other
232 stars 61 forks source link

animating a prism #67

Closed nyendwa closed 2 years ago

nyendwa commented 2 years ago

I intend to animate my rectangle I noticed you were using Visual Studio 2015 and I am using VS2022 and the interface and layout is different but using the following matrices as an example Matrix4 model = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(-55.0f)); Matrix4 view = Matrix4.CreateTranslation(0.0f, 0.0f, -3.0f); Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45.0f), Width / Height, 0.1f, 100.0f);

I have a rectangle and I want to transform it into a 3D, for a start I want to animate it as you did in your tutorial 4 and then change it to 3D as a cube or cuboid. I want to know where I need to which class store the view, model, and projection matrices and how to bind them and send them to the shader. Let me know if I can share my code with you.

giawa commented 2 years ago

Hi there, thanks for trying out this library! Typically you will bind each matrix one at a time to your shader - this allows you to independently update the model, view and projection matrices. For example, here's a typical vertex shader with those 3 matrices:

#version 130

in vec3 vertexPosition;
in vec3 vertexColor;

out vec3 color;

uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;

void main(void)
{
    color = vertexColor;
    gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1);
}

And then for the projection and view matrices I only set them the single time, since they don't update per frame (unless the camera is moving):

            program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f));
            program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, new Vector3(0, 1, 0)));

Lastly, the model matrix can be updated per frame if you are animating it:

program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle) * Matrix4.CreateTranslation(new Vector3(-1.5f, 0, 0)));

Hopefully this helps!

nyendwa commented 2 years ago

Does this function program["projection_matrix"] Require initialisation? Something like a method or class?

On Tue, Jul 19, 2022, 03:12 Charles Ambrye @.***> wrote:

Hi there, thanks for trying out this library! Typically you will bind each matrix one at a time to your shader - this allows you to independently update the model, view and projection matrices. For example, here's a typical vertex shader with those 3 matrices:

in vec3 vertexPosition; in vec3 vertexColor;

out vec3 color;

uniform mat4 projection_matrix; uniform mat4 view_matrix; uniform mat4 model_matrix;

void main(void) { color = vertexColor; gl_Position = projection_matrix view_matrix model_matrix * vec4(vertexPosition, 1); }```

And then for the projection and view matrices I only set them the single time, since they don't update per frame (unless the camera is moving):


            program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, new Vector3(0, 1, 0)));```

Lastly, the model matrix can be updated per frame if you are animating it:

```program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle) * Matrix4.CreateTranslation(new Vector3(-1.5f, 0, 0)));```

Hopefully this helps!

—
Reply to this email directly, view it on GitHub
<https://github.com/giawa/opengl4csharp/issues/67#issuecomment-1188487858>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKSGPII3UYI75B3BE42WFELVUX6JFANCNFSM53WJ4RTQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
giawa commented 2 years ago

program in this case is the ShaderProgram that contains your vertex and fragment shader code. ["projection_matrix"] then performs a lookup and assignment to a uniform in that shader. In this case, it would be a uniform mat4 projection_matrix as given in the vertex shader example I provided.

nyendwa commented 2 years ago
Here is a link to my program that renders a rectangle, I would love to work with you to have it rendered in 3D and then animate it, if you have time you can create a video or make comments on the changes and commit it on your GitHub for others to learn from. https://github.com/nyendwa/OpenTKRectangle From: Charles AmbryeSent: Wednesday, July 20, 2022 6:16 PMTo: giawa/opengl4csharpCc: Janny Nyendwa; AuthorSubject: Re: [giawa/opengl4csharp] animating a prism (Issue #67) program in this case is the ShaderProgram that contains your vertex and fragment shader code. ["projection_matrix"] then performs a lookup and assignment to a uniform in that shader. In this case, it would be a uniform mat4 projection_matrix as given in the vertex shader example I provided.—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***> 
nyendwa commented 2 years ago
I have attached a screenshot of my work below. I want to know how to initialize “program” in my OnRenderFrame, I have submitted my works on my GitHub kindly take a look at it and see if we can make this work. From: Charles AmbryeSent: Tuesday, July 19, 2022 3:12 AMTo: giawa/opengl4csharpCc: Janny Nyendwa; AuthorSubject: Re: [giawa/opengl4csharp] animating a prism (Issue #67) Hi there, thanks for trying out this library! Typically you will bind each matrix one at a time to your shader - this allows you to independently update the model, view and projection matrices. For example, here's a typical vertex shader with those 3 matrices: in vec3 vertexPosition;in vec3 vertexColor; out vec3 color; uniform mat4 projection_matrix;uniform mat4 view_matrix;uniform mat4 model_matrix; void main(void){    color = vertexColor;    gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1);}``` And then for the projection and view matrices I only set them the single time, since they don't update per frame (unless the camera is moving): ```            program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f));            program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, new Vector3(0, 1, 0)));``` Lastly, the model matrix can be updated per frame if you are animating it: ```program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle) * Matrix4.CreateTranslation(new Vector3(-1.5f, 0, 0)));``` Hopefully this helps!—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***> 
giawa commented 2 years ago

Unfortunately, it looks like that github repo is empty. I'd suggest taking a look at the code here and going from there if you would like to use the opengl4csharp library. https://github.com/giawa/opengl4tutorials

giawa commented 2 years ago

That being said, this looks like you're asking for customer support and this isn't really an issue with the library itself, so I am going to close this issue for now. Hopefully the link above helps!

nyendwa commented 2 years ago
I followed your tutorials and they were giving me errors, in my view, the difference could be as a result of my version of my .Net framework. From: Charles AmbryeSent: Wednesday, July 20, 2022 7:54 PMTo: giawa/opengl4csharpCc: Janny Nyendwa; AuthorSubject: Re: [giawa/opengl4csharp] animating a prism (Issue #67) Unfortunately, it looks like that github repo is empty. I'd suggest taking a look at the code here and going from there if you would like to use the opengl4csharp library. https://github.com/giawa/opengl4tutorials—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>