• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to set up correct perspective view in OpenTK?

Resolved: How to set up correct perspective view in OpenTK?

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I made a full-screen size square to show on window.
But sadly, I am stuck at changing viewpoint (camera or perspective?) to make square look small at the center of window.
As many people suggested on web, I followed guid of setting up Matrix and perspective field of view which does not work…
I am wondering what I am missing on my code.

private void ImageControl_OnRender(TimeSpan delta)
{
//Create perspective camera matrix
//ImageControl is the name of window
GL.Viewport(0, 0, (int)ImageControl.Width, (int)ImageControl.Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();

Matrix4 perspectiveMatrix;
Matrix4.CreatePerspectiveFieldOfView(45.0f * (float)Math.PI / 180, (float)(ImageControl.Width / ImageControl.Height), 0.1f, 100.0f, out perspectiveMatrix);

//Set perspective camera
//GL.MatrixMode(MatrixMode.Projection);
//GL.LoadIdentity();

GL.LoadMatrix(ref perspectiveMatrix);
GL.LoadIdentity();

GL.MatrixMode(MatrixMode.Modelview);

//GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();

//Now starting to draw objects

//Set the background colour
GL.ClearColor(Color4.SkyBlue);

//Clear the colour and depth buffer for next matrix.
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

//Set the scale of object first hand
//GL.Scale(0.5f, 0.5f, 0.5f);

//GL.Translate() <<< Set the translation of object first hand GL.Translate(0.0f, 0.0f, -2.0f); //Set the colour of object first hand GL.Color3(0.3f, 0.2f, 0.5f); //Tells that we are going to draw a sqare consisting of vertices. Can be Triangle too! GL.Begin(PrimitiveType.Quads); GL.Vertex3(-1.0f, -1.0f, 0.0f); GL.Vertex3(1.0f, -1.0f, 0.0f); GL.Vertex3(1.0f, 1.0f, 0.0f); GL.Vertex3(-1.0f, 1.0f, 0.0f); //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.End(); GL.Finish(); } [/code]

Answer:

You load the identity matrix after the projection matrix. This overrides the projection matrix. Do the following:

// 1. Select projection matrix mode
GL.MatrixMode(MatrixMode.Projection); // <--- INSERT // 2. Load projection matrix GL.LoadMatrix(ref perspectiveMatrix); // GL.LoadIdentity(); <--- DELETE // 3. Select model view matrix mode GL.MatrixMode(MatrixMode.Modelview); // 4. Clear model view matrix (load the identity matrix) GL.LoadIdentity(); // 5. Multiply model view matrix with the translation matrix GL.Translate(0.0f, 0.0f, -2.0f); [/code]

Note that GL.MatrixMode selects the current matrix. All pending matrix operations affect the selected matrix. GL.LoadIdentity “clears” the matrix. It loads the Identity matrix.

If you have better answer, please add a comment about this, thank you!

c# opengl opentk wpf
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: I am facing ERR_HTTP2_PROTOCOL_ERROR on my website

27/03/2023

Resolved: TypeScript does not recognize properties when instantiating interface array

27/03/2023

Resolved: How to make statement case insensitive when uploading a dta file with specified columns?

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.