Getting tranlated coordinates of an object

Mar 6, 2012 at 1:41 AM

Hi there,

I am currently working on an application, where I would require to load an object from file translate / rotate object in 3d space and then save it back to file. The problem is that I would like to get new translated / rotated points of an object and how can I do that using SharpGL. Ideas and/or suggestions ?

Coordinator
Mar 6, 2012 at 1:57 PM

Create an OpenGL instance, set the matrix mode to GL_MODELVIEW. Then:

gl.Translate(...); // your translation
gl.Rotate(...); // Your rotation
var matrix = gl.ModelViewMatrix;

foreach(var point in points)
   var transformedPoint = matrix * point;

This pseduo code should do the trick - use OGL to build a modelview matrix for you, then transform every vertex by that matrix. 

Mar 10, 2012 at 6:47 AM

Thanks Dave,

I will try this.

Mar 23, 2012 at 7:39 PM

Hi Dave,

As per your suggestion I tried to get translated values but I am not getting proper results. Here is code snippet

Vertex translationVertex = new Vertex(1,1,1);
Vertex rotationVertex = new Vertex(0,0,0);


SharpGL.OpenGL ogl = new SharpGL.OpenGL();
ogl.MatrixMode(SharpGL.OpenGL.GL_MODELVIEW);
ogl.LoadIdentity();


ogl.Translate(translationVertex.X, translationVertex.Y, translationVertex.Z);
ogl.Rotate(rotationVertex.X, rotationVertex.Y, rotationVertex.Z);

Matrix matrix = ogl.GetModelViewMatrix();
Vertex vertex = new Vertex(0,0,0);
Vertex resultantVertex = vertex * martix;

resultantVertex should be equal to translationVertex but it retains its values.

Please guide.

Coordinator
Mar 26, 2012 at 9:38 AM

OK this is an oddity of 3D programs in generally. You've got the 4x4 modelview, that's fine - then you multiple a 3 tuple through it. This means it's using:

A B C d
E F G h
I J K l
m n o p

 the bold parts of the 4x4 matrices in the multiplication. The rotations will make up these parts, but the translation actually ends up in M/N/O.

You can't really do a translation easily via matrix multiplication, so multiply your vertex by your matrix - this does the rotate and scale, then ADD m to the vertex.x, n to the vertex.y and o to the vertex.z - this is what happens under the hood in OGL. It's weird, so it's a bit of a fudge, but generally how most CGI systems work it :) If you look at the matrix you get back with the debugger you should see that it's something like:

1 0 0 0
0 1 0 0
0 0 1 0
x y z 0

where (x,y,z) is your translation- and regardless of it's values it doesn't affect the vertex when you multiply it!! So just add it on afterwards :)