如何在Opentk中设置正确的透视视图?
我做了一个全屏幕大小的正方形,以在窗户上显示。
但是可悲的是,我一直困扰着改变视点(相机还是透视图?),以使正方形在窗户的中心看起来很小。
正如许多人在网络上建议的那样,我遵循了建立矩阵和视角视野的GUID,这是行不通的……
我想知道我在代码上缺少什么。
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
您在投影矩阵之后加载身份矩阵。这覆盖了投影矩阵。执行以下操作:
请注意,
gl.matrixmode
选择当前矩阵。所有待处理的矩阵操作都会影响所选矩阵。gl.loadidentity
“清除”矩阵。它加载“身份”>“身份矩阵”。You load the identity matrix after the projection matrix. This overrides the projection matrix. Do the following:
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.