flopoloco's picture

How to do camera rotations?

Hi there I had previously made a rotating camera with Matrix4 as you can see here:
http://www.opentk.com/node/1570

I heard that Matrix4 is very slow, are there any better ways?

using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
 
namespace OpenTKTest
{
	class Program : GameWindow
	{
		int vertexBuffer;
		Matrix4 matrixProjection, matrixModelview;
		float cameraRotation = 0f;
 
		void CreateVertexBuffer()
		{
			Vector3[] vertices = new Vector3[3];
			vertices[0] = new Vector3(-1f, -1f, 0f);
			vertices[1] = new Vector3( 1f, -1f, 0f);
			vertices[2] = new Vector3( 0f,  1f, 0f);
 
			GL.GenBuffers(1, out vertexBuffer);
			GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
			GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
			                       new IntPtr(vertices.Length * Vector3.SizeInBytes),
			                       vertices, BufferUsageHint.StaticDraw);
		}
 
		protected override void OnLoad(EventArgs e)
		{
			GL.ClearColor(Color.Brown);
			GL.Enable(EnableCap.DepthTest);
			CreateVertexBuffer();
		}
 
		protected override void OnRenderFrame(FrameEventArgs e)
		{
			GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
			// THIS IS THE PART THAT SHOULD WORK -------
			cameraRotation++;
			if (cameraRotation > 360f) cameraRotation = 0f;
			GL.MatrixMode(MatrixMode.Modelview);
			Matrix4.CreateRotationY(cameraRotation, out matrixModelview);
			matrixModelview *= Matrix4.LookAt(0f, 0f, -5f, 0f, 0f, 0f, 0f, 1f, 0f);
			GL.LoadMatrix(ref matrixModelview);
			// -----------------------------------------
 
			GL.EnableVertexAttribArray(0);
			GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
			GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
			GL.DrawArrays(BeginMode.Triangles, 0, 3);
			GL.DisableVertexAttribArray(0);
 
			SwapBuffers();
		}
 
		protected override void OnResize(EventArgs e)
		{
			base.OnResize(e);
			GL.Viewport(0, 0, Width, Height);
			matrixProjection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1f, 100f);
			GL.MatrixMode(MatrixMode.Projection);
			GL.LoadMatrix(ref matrixProjection);
		}
 
		[STAThread]
		private static void Main(string[] args)
		{
			using (Program p = new Program())
			{
				p.Run(60);
			}
		}
	}
}

Thanks.


Comments

Comment viewing options

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

Hi there I had previously made a rotating camera with Matrix4 as you can see here:
http://www.opentk.com/node/1570

I heard that Matrix4 is very slow, are there any better ways?

Where did you hear that? The Matrix4 struct is perfectly fine. You won't notice any CPU bottleneck due to matrix multiplications unless you do a few thousand of them per frame. Matrix4 should be as fast as it gets - however, it is a struct and things could maybe be speed up here and there when using ref Matrix4 matrix instead of just using Matrix4 matrix, as in .NET, structs as method parameters are copied on calls. But that's a really minor issue.

What is the question behind your post? Is the code you posted not working? Or are you looking for methods which do the exact same thing? GL.MatrixMode and GL.LoadMatrix are deprecated calls of the fixed function pipeline, you can still use them but it would be better to upload the matrix yourself to your shader (which requires you to work with shaders, of course). The matrix handling could be improved here although using the fixed function matrices won't decrease your application's performance.