
Taming Matrices
Posted Wednesday, 22 July, 2009 - 22:16 by flopoloco inMy journey into the realm of thy unknown continues... I try to find more about how using matrices in OpenTK.
Here's an example code... The example base is from OpenTK 0.9.9 where it uses the elegant GL.LoadMatrix command. In the same spirit I want to pre-load 3 custom matrices and load one of them. For better clarification of my code-thoughts notice my comments.
using System; using System.Drawing; using OpenTK; using OpenTK.Graphics; using OpenTK.Input; using OpenTK.Platform; using OpenTK.Math; namespace OpenTKMatrixTest { public class MatrixExample : GameWindow { Matrix4 matrixStyle1, matrixStyle2, matrixStyle3; public override void OnLoad(EventArgs e) { base.OnLoad(e); GL.ClearColor(Color.MidnightBlue); GL.Enable(EnableCap.DepthTest); // Here we can setup our matrices matrixStyle1 = Matrix4.Identity; // I know that Identity is useful for Translations matrixStyle1 = Matrix4.RotateX(-35f); // Now matrixStyle1 should be same as GL.Translate(-35f, 1, 0, 0) } protected override void OnRenderFrame(FrameEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // Set up camera Matrix4 modelview = Matrix4.LookAt(new Vector3(10f, 10f, 10f), Vector3.UnitZ, Vector3.UnitY); GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref modelview); GL.PushMatrix(); // I think that pushing the Matrix stack is usefull for not messing the ModelView with other matrices... // Unfortunately this will not work // GL.MatrixMode(MatrixMode.Modelview); // GL.LoadMatrix(ref matrixStyle1); // If I load the modelview matrix declared above everything will be OK GL.Begin(BeginMode.Triangles); GL.Color3(Color.LightYellow); GL.Vertex3(-1.0f, -1.0f, 0.0f); GL.Color3(Color.LightYellow); GL.Vertex3(1.0f, -1.0f, 0.0f); GL.Color3(Color.LightSkyBlue); GL.Vertex3(0.0f, 1.0f, 0.0f); GL.End(); GL.PopMatrix(); SwapBuffers(); } protected override void OnResize(EventArgs e) { GL.Viewport(0, 0, Width, Height); Matrix4 projection = Matrix4.Perspective(45.0f, Width / (float)Height, 1.0f, 64.0f); GL.MatrixMode(MatrixMode.Projection); GL.LoadMatrix(ref projection); } protected override void OnUpdateFrame(FrameEventArgs e) { if (Keyboard[Key.Escape]) Exit(); } public static void Main() { using (MatrixExample me = new MatrixExample()) { me.Run(); } } } }
Thanks for your time, keep up the good work!


Comments
Re: Taming Matrices
One last try I did before going to sleep was enough to succeed my purpose. It is being said that after 1:00 human's brainpower is increased (but on the other hand the body is corrupted)... :P
Here's the code.