我制作了一個全屏大小的正方形以顯示在視窗上。
但可悲的是,我被困在改變視點(相機或視角?)以使視窗中心的正方形看起來很小。
正如許多人在網上建議的那樣,我遵循了設定矩陣和透視視野的指導,這不起作用......
我想知道我的代碼中缺少什么。
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();
}
uj5u.com熱心網友回復:
您在投影矩陣之后加載單位矩陣。這會覆寫投影矩陣。請執行下列操作:
// 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);
請注意,GL.MatrixMode
選擇當前矩陣。所有待處理的矩陣運算都會影響選定的矩陣。GL.LoadIdentity
“清除”矩陣。它加載身份矩陣。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/492180.html