flopoloco's picture

Taming Matrices

My 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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
flopoloco's picture

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.

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;
		Matrix4 matrixModelSelected;
		Matrix4 matrixModelFinal;
		bool flip = false;
 
		public override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			GL.ClearColor(Color.MidnightBlue);
			GL.Enable(EnableCap.DepthTest);
 
			// Setup custom matrices
			matrixStyle1 = Matrix4.RotateZ((float)(45.0 * (Math.PI/180.0)));
			matrixStyle2 = Matrix4.Mult(
										Matrix4.Translation(0f, 0f, -20f),
										Matrix4.RotateX((float)(-45.0 * (Math.PI/180.0)))
										);
			matrixModelSelected = matrixStyle1;
 
			Keyboard.KeyDown += new KeyDownEvent(Keyboard_KeyDown);
		}
 
		void Keyboard_KeyDown(KeyboardDevice sender, Key key)
		{
			switch (key)
			{
				case Key.Escape:
					Exit();
					break;
				case Key.Space:
					if (flip)
						matrixModelSelected = matrixStyle1;
					else
						matrixModelSelected = matrixStyle2;
 
					flip = !flip;
 
					break;
			}
		}
 
		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();
            matrixModelFinal = Matrix4.Mult(matrixModelSelected, modelview);
			GL.LoadMatrix(ref matrixModelFinal);
            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);
		}
 
		public static void Main()
		{
			using (MatrixExample me = new MatrixExample())
			{
				me.Run();
			}
		}
	}
}