Home > database >  How to set up correct perspective view in OpenTK?
How to set up correct perspective view in OpenTK?

Time:06-18

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();
}

CodePudding user response:

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);

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.

  • Related