View Full Version : Skip to the matrix at a point

Pierre

02-07-2010, 01:21 PM

Hello to all the community

Since 3 days I am trying to determine the position x, y, z of a point.

To achieve this I have a table in the matrix.

I would like to pass the matrix to a point x, y, z corresponding to the original matrix.

Matrix [16] -> x, y, z

Could you help me thank you very much!

marshats

02-07-2010, 01:50 PM

The matrix (M) multiplies a point (P) to generate a scaled/rotate/translate/etc new point (P').

P' = M * P

Are you saying that you have only a matrix M and want to know the point P before the matrix transformation? The answer to that is

P = inverse(M)*P'

Pierre

02-07-2010, 02:02 PM

Ok I say the second thing:

P = inverse(M)*P'

But I don't have P'.

P' = glLoadIdentity();

But I do not understand too because for me M is equal to 16 values.

If I do so with your formula P '= 0 but p' is different from 0.

Could you give me a specific example or formula.

marshats

02-07-2010, 02:33 PM

P and P' are vectors containing x,y,z,w coordinates (4 elements in C array). And M is a 4x4 matrix (16 elements in C array).

M does not store any information about the point. M only transforms a point P to P'.

glLoadIdentity specifies the Matrix M, not the point P or P'.

So Say you have the code

glMatrixMode(GL_MODELVIEW);

glLoadIdentity(); // MV = Identity 4x4 matrix

glTranslate(...); // MV = I*T (T is 4x4 matrix)

glRotate(...); // MV = I*T*R (R is 4x4 MAtrix)

glScale(...); // MV = I*T*R*S (S is 4x4 matrix)

glMultMatrixf(Q); // MV = I*T*R*S*Q (Q is genral 4x4 matrix)

// Notice nowhere has a point been transformed yet

// to do that openGL must draw a set of vertices ie

glVertex4f(x,y,z,w); // P'=MV*P where P=(x,y,z,w)

// and MV has not changed by calling glVertex

// hence MV does not store any info about either P or P'

In other words you can only get the transformed point P'=(x',y',z',w') knowing both the MOVDELVIEW matrix (MV) AND the original vector/point P.

The inverse operation also requires two pieces of information the inverse(MV) matrix AND the final transformed vector/point P' to determine the original point P. The problem is ill-defined if you only know just the matrix and not P' ie it is impossible to know the original point if you only know the matrix M alone.

Pierre

02-07-2010, 02:44 PM

Ok for begin thank for your answer.

However I know that you tell:

I just want a formula as:

Objet.X = Matrix[4]+matrix[11];

Objet.Y = Matrix[5]+matrix[12];

Objet.Z = Matrix[6]+matrix[13];

It's a exemple.

I would know a formula for calculate.

Not a mathematical formula as P '= P * M.

Again Thank.

marshats

02-07-2010, 02:54 PM

see here (http://www.gamedev.net/reference/articles/article877.asp) to do matrix-vector multiplication (last equation before code sample at end of document). The P'=M*P would map into:

| 1 2 3 4 | | X | | 1X+ 2Y+ 3Z+ 4W |

| 5 6 7 8 |x| Y |=| 5X+ 6Y+ 7Z+ 8W |

| 9 10 11 12 | | Z | | 9X+10Y+11Z+12W |

| 13 14 15 16 | | W | | 13X+14Y+15Z+16W |

so

| X' | | 1X+ 2Y+ 3Z+ 4W |

| Y' |=| 5X+ 6Y+ 7Z+ 8W |

| Z' | | 9X+10Y+11Z+12W |

| W' | | 13X+14Y+15Z+16W |

see 4x4 matrix inverse (http://www.cvl.iis.u-tokyo.ac.jp/~miyazaki/tech/teche23.html). Inverse(A) = A^-1.

Pierre

02-07-2010, 03:22 PM

Ok good.

I know how inverse matrix.

I know how multiplication matrix.

But I don't know how find the position of my objet who is place by my matrix MatrixObjet.

It's my question ?

Ps: I have test and read your link and it's don't work.

Thank for your search and your knowledge.

marshats

02-07-2010, 03:40 PM

Concerning "Ps: I have test and read your link and it's don't work." Which link doesn't work? The difference may be due to the layout of the matrix in C -- There is a concept of Matrix layout -- column major or row major. The link on matrix multiplication above was correct but assumed a different layout than openGL/C uses -- take a look at this link instead here. (http://www.sjbaker.org/steve/omniv/matrices_can_be_your_friends.html)

SO in openGL array layout

| 0 4 8 12 | | X | | 0X+ 4Y+ 8Z+ 12W |

| 1 5 9 13 |x| Y |=| 1X+ 5Y+ 9Z+ 13W |

| 2 6 10 14 | | Z | | 2X+ 6Y+10Z+14W |

| 3 7 11 15 | | W | | 3X+ 7Y+11Z+15W |

so

| X' | | 0X+ 4Y+ 8Z+12W |

| Y' |=| 1X+ 5Y+ 9Z+13W |

| Z' | | 2X+ 6Y+10Z+14W |

| W' | | 3X+ 7Y+11Z+15W |

A little background can be found at "9.005 Are OpenGL matrices column-major or row-major?" (http://www.opengl.org/resources/faq/technical/transformations.htm).

Pierre

02-07-2010, 04:00 PM

I like the first link with the background green.

However when I write for my camera to follow the objet at position 0,0,0 as your site thus x -> matrix[12] y -> matrix[13] and z-> matrix[14].

Citation of website:

(0,0,0) ---> ( 0, 0, 0 ) + ( m[12], m[13], m[14] )

And this don't work.

Have you a idea ?

ps: I have rotation left and right but it's regist in matrix.

marshats

02-07-2010, 04:07 PM

Is the object you are following with your camera defined with its center at the origin ie when the MV matrix is identity will you see the object at the center (0,0,0)?

Are you using gluLookAt?

Pierre

02-07-2010, 04:23 PM

Yes I have dev a loader 3ds in Opengl/c#.

My objet is place at 0,0,0 at begin and after is load at the place of matrix:

Gl.glPushMatrix();

Gl.glLoadMatrixd(objectTransformationMatrix);

Gl.glBegin(Gl.GL_TRIANGLES);

for (int face = 0; face < nbFaces; face++)

{

for (int i = 0; i < 3; i++)

Gl.glVertex3f(

faces[face].point[i].x,

faces[face].point[i].z,

faces[face].point[i].y);

}

Gl.glEnd();

Gl.glPopMatrix();

I use glLookat, I don't want use rotated and translated for my camera because I think its heavy.

Thus my object is placed at the location of the matrix.

Founding x,y,z with my matrix i will found the objet in my 3d world.

marshats

02-07-2010, 04:31 PM

Can you show your exact gluLookAT line? I would think that you could simply set the center cooridinates (4th,5th,6th parameters of gluLookAt) to m[12],m[13],m[14].

Pierre

02-07-2010, 04:37 PM

i have test with:

Glu.gluLookAt(0, //eye X

0, //eye Y

0, //eye Z

-(Mesh.objectTransformationMatrix[12]), //center X

0, //center Y

-(Mesh.objectTransformationMatrix[14]), //center Z

0.0f, // upvect X

1, // upvect Y

0.0f); // upvect Z

And:

Glu.gluLookAt(0, //eye X

0, //eye Y

0, //eye Z

-(Mesh.objectTransformationMatrix[0] * Mesh.objectTransformationMatrix[12] +Mesh.objectTransformationMatrix[12]), //center X

0, //center Y

-(Mesh.objectTransformationMatrix[10] * Mesh.objectTransformationMatrix[14] +Mesh.objectTransformationMatrix[14]), //center Z

0.0f, // upvect X

1, // upvect Y

0.0f); // upvect Z

It's don't work.

I have not translated y thus I leave y =0;

Thank for your support.

marshats

02-07-2010, 04:44 PM

Why the minus signs? Try instead

Glu.gluLookAt(0, //eye X

0, //eye Y

0, //eye Z

Mesh.objectTransformationMatrix[12], //center X

Mesh.objectTransformationMatrix[13], //center Y

Mesh.objectTransformationMatrix[14], //center Z

0.0f, // upvect X

1, // upvect Y

0.0f); // upvect Z

Note this will fail if your eye (0,0,0) is the same as center (Mesh.objectTransformationMatrix[12],Mesh.objectTransformationMatrix[13],Mesh.objectTransformationMatrix[14]). In general you want to place the eye at some offset of center ie eye = offset + center to avoid the problem.

Pierre

02-07-2010, 04:51 PM

yes i have the minus signs because without the camera go in the opposite direction to the object after a rotation.

It's don't work.

I seeking a formula to follow the object for begining.

But I don't found.

marshats

02-07-2010, 05:13 PM

Are you applying any other matrix operations beside gluLookAt and Mesh.objectTransformationMatrix?

Since you are using glLoadMatrixf you are overwriting anything you do with gluLookAt before -- I think you effectively are not using gluLookAt. Or does the Mesh.objectTransformationMatrix contain the gluLookAt transformation itself? If that is the case then you cannot interpret the m[12 to 14] values as the location of the mesh origin anynore.

You may want to study the source code at "Points of View" (http://users.encs.concordia.ca/~grogono/Graphics/examples.html). I think if you see this it may help you out in the long run working with the gluLookAt camera.

Pierre

02-07-2010, 05:19 PM

No any other matrix operations.

I think that matrix[12] is not the only operations for find the position x.

But i don't know what other ?

Have you ever develop a game 3d adventure where the camera follows the character?

If yes how did you do?

marshats

02-07-2010, 05:21 PM

Yes, but a better example is the "points of View" link given above.

Pierre

02-07-2010, 05:37 PM

Your link is a exemple of that I can't do.

Because when i have a translation, rotation and translation, the camera will do translation, rotation but the second translation will go on the first direction.

And not the direction of the rotation.

I know there a formula that would allow me to find the position of the object from the arrays, but I think not.

Exemple of my matrix after transformation with Debug:

0.9

0

-0.42

0

0

1

0

0

0.422

0

0.9

0

-8.43

0

-21.53

1

If it's can give help.

marshats

02-07-2010, 06:38 PM

That doesn't really help. I am concerned how you get that matrix in the first place. If you leave the object at fixed location and orientation but change the parameters to gluLookAt only what are the matrix debug values before and after that change? If they change by simply changing gluLookAt parameters then that would be the cause of the confusion in my opinion. The whole idea of using m[12],m[13],m[14] directly assumes that the transformation matrix does not include the gluLookAt transformation.

Can you draw something that is always located at m[12],m[13],m[14] -- If what I think is going on I suspect that you will always see it in the center of your view no matter how you change gluLookAt.

I am thinking that the fix may come from using glMultMatrixf instead of glLoadMatrixf. But that depends somewhat on the answers to the above questions.

marshats

02-07-2010, 07:45 PM

I am a C++ coder but heres a rough C# code that does what I think you are trying to do. Try toggling the gluLookAt lines to see what;s going on. Also note that I use "Gl.glMultMatrixf(M);" not "Gl.glLoadMatrixf(M);" because I do not want to replace the matrix but rather multiply it by the modelview matrix which is effectifely just the gluLookAt transform.

#region License

/*

MIT License

Copyright ©2003-2005 Tao Framework Team

http://www.taoframework.com

All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal

in the Software without restriction, including without limitation the rights

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

copies of the Software, and to permit persons to whom the Software is

furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all

copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

SOFTWARE.

*/

#endregion License

#region Original Credits / License

/*

* Copyright (c) 1993-1997, Silicon Graphics, Inc.

* ALL RIGHTS RESERVED

* Permission to use, copy, modify, and distribute this software for

* any purpose and without fee is hereby granted, provided that the above

* copyright notice appear in all copies and that both the copyright notice

* and this permission notice appear in supporting documentation, and that

* the name of Silicon Graphics, Inc. not be used in advertising

* or publicity pertaining to distribution of the software without specific,

* written prior permission.

*

* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"

* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,

* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR

* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON

* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,

* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY

* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,

* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF

* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN

* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON

* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE

* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.

*

* US Government Users Restricted Rights

* Use, duplication, or disclosure by the Government is subject to

* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph

* (c)(1)(ii) of the Rights in Technical Data and Computer Software

* clause at DFARS 252.227-7013 and/or in similar or successor

* clauses in the FAR or the DOD or NASA FAR Supplement.

* Unpublished-- rights reserved under the copyright laws of the

* United States. Contractor/manufacturer is Silicon Graphics,

* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.

*

* OpenGL(R) is a registered trademark of Silicon Graphics, Inc.

*/

#endregion Original Credits / License

using System;

using Tao.FreeGlut;

using Tao.OpenGl;

public static class GlobalVar

{

/// <summary>

/// Static value protected by access routine.

/// </summary>

static float _globalValue;

/// <summary>

/// Access routine for global variable.

/// </summary>

public static float GlobalValue

{

get

{

return _globalValue;

}

set

{

_globalValue = value;

}

}

}

namespace Redbook {

#region Class Documentation

/// <summary>

/// This program demonstrates a single modeling transformation, glScalef() and a

/// single viewing transformation, gluLookAt(). A wireframe cube is rendered.

/// </summary>

/// <remarks>

/// <para>

/// Original Author: Silicon Graphics, Inc.

/// http://www.opengl.org/developers/code/examples/redbook/cube.c

/// </para>

/// <para>

/// C# Implementation: Randy Ridge

/// http://www.taoframework.com

/// </para>

/// </remarks>

#endregion Class Documentation

public sealed class Cube {

// --- Entry Point ---

#region Main()

[STAThread]

public static void Main() {

Glut.glutInit();

Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_RGB);

Glut.glutInitWindowSize(500, 500);

Glut.glutInitWindowPosition(100, 100);

Glut.glutCreateWindow("Cube");

Init();

Glut.glutIdleFunc(new Glut.IdleCallback(Display));

Glut.glutKeyboardFunc(new Glut.KeyboardCallback(Keyboard));

Glut.glutReshapeFunc(new Glut.ReshapeCallback(Reshape));

Glut.glutMainLoop();

}

#endregion Run()

// --- Application Methods ---

#region Init()

private static void Init() {

Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

Gl.glShadeModel(Gl.GL_FLAT);

GlobalVar.GlobalValue = 0.0f;

}

#endregion Init()

// --- Callbacks ---

#region Display()

private static void Display() {

Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);

Gl.glColor3f(1.0f, 1.0f, 1.0f);

// cheat and build object transformation matrix

Gl.glLoadIdentity();

GlobalVar.GlobalValue+=0.0001f;

Gl.glTranslatef((float)Math.Cos(GlobalVar.GlobalVa lue),(float)Math.Sin(GlobalVar.GlobalValue),0.0f);

float[] M=new float[16];

Gl.glGetFloatv(Gl.GL_MODELVIEW_MATRIX,M);

// Clear the matrix

Gl.glLoadIdentity();

// Viewing transformation

//Glu.gluLookAt(0.0,0.0,5.0, 0.0,0.0,0.0, 0.0,1.0,0.0);

Glu.gluLookAt(0.0,0.0,5.0, M[12],M[13],M[14], 0.0,1.0,0.0);

Gl.glColor3f(1.0f,1.0f,1.0f);

// Modeling transformation

Gl.glPushMatrix();

Gl.glTranslatef(1.0f, 1.0f, 0.0f);

Glut.glutWireCube(0.5);

Gl.glPopMatrix();

Gl.glPushMatrix();

Gl.glTranslatef(-1.0f, 1.0f, 0.0f);

Glut.glutWireCube(0.5);

Gl.glPopMatrix();

Gl.glPushMatrix();

Gl.glTranslatef(-1.0f, -1.0f, 0.0f);

Glut.glutWireCube(0.5);

Gl.glPopMatrix();

Gl.glPushMatrix();

Gl.glTranslatef(1.0f, -1.0f, 0.0f);

Glut.glutWireCube(0.5);

Gl.glPopMatrix();

//show rotating cube

Gl.glColor3f(1.0f,0.0f,0.0f);

Gl.glPushMatrix();

Gl.glMultMatrixf(M);

Glut.glutWireCube(0.5);

Gl.glPopMatrix();

Glut.glutSwapBuffers();

}

#endregion Display()

#region Keyboard(byte key, int x, int y)

private static void Keyboard(byte key, int x, int y) {

switch(key) {

case (byte) 27:

Environment.Exit(0);

break;

}

}

#endregion Keyboard(byte key, int x, int y)

#region Reshape(int w, int h)

private static void Reshape(int w, int h) {

Gl.glViewport(0, 0, w, h);

Gl.glMatrixMode(Gl.GL_PROJECTION);

Gl.glLoadIdentity();

Gl.glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);

Gl.glMatrixMode(Gl.GL_MODELVIEW);

}

#endregion Reshape(int w, int h)

}

}

Pierre

02-08-2010, 04:24 AM

Yes you are a genious!

It's work.

To begin I draw a cube at position 12,13,14 Matrix.

And its working!

So after I remlace LoadMatrixd by MultMatrixd!

And the panda was well placed!

Thank for your help, very very thank!

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.