Sign in to follow this  
JARVIS_UK

calculate point from matrix

Recommended Posts

Hi, This is a simplified version of a post i made a little while ago. Recently i have needed to obtain a points new position from a matrix. The matrix i use is obtained from the current matrix where a gl transformation has been applied. The problem is that the resulatant point that i calculate keeps moving when the camera is simply moved? this has never happened before but since implementing it into my project it has delayed m progress. Does anyone know why this is happening. heres my display code: glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(vAng, asp, nearD, farD); glMatrixMode(GL_MODELVIEW); // Modeling transformation glLoadIdentity(); // Initialize the model matrix as identity gluLookAt(.....); cam.SetViewByMouse(screenWidth, screenHeight); // Move the camera by the mouse MATRIXSTRUCT myMatTest; MATRIXSTRUCT myMatTest1; glPushMatrix(); glTranslatef(10,0,0); glGetDoublev(GL_MODELVIEW_MATRIX,myMatTest.myRotMatArrray); DoubleArrayToMatrix(myMatTest.myRotMatix,myMatTest.myRotMatArrray); glPopMatrix(); Point3 myPoint;myPoint.set(0,0,0); Point3 myPointTemp;myPointTemp.set(setNexVertexPosPoint(myMatTest.myRotMatix,myPoint)); glPushMatrix(); glTranslatef(myPoint.x,myPoint.y,myPoint.z); glutWireSphere(10,5,5); glPopMatrix(); this is to simply draw two spheres one at 0,0,0 and the other at 10,0,0. the second sphere however moves :( just incase someone asks here is the MATRIXSTRUCT struct typedef struct matrixStruct { GLdouble myRotMatArrray[16]; matrix_4x4_type myRotMatix; //collision detection eye coords required }MATRIXSTRUCT; plz help me :) cheers

Share this post


Link to post
Share on other sites
yeah sorry missed that off :)
cheers for spotting

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(vAng, asp, nearD, farD);
glMatrixMode(GL_MODELVIEW); // Modeling transformation
glLoadIdentity(); // Initialize the model matrix as identity

gluLookAt(.....);

cam.SetViewByMouse(screenWidth, screenHeight); // Move the camera by the mouse

MATRIXSTRUCT myMatTest;
MATRIXSTRUCT myMatTest1;

glPushMatrix();

glTranslatef(10,0,0);
glGetDoublev(GL_MODELVIEW_MATRIX,myMatTest.myRotMatArrray);
DoubleArrayToMatrix(myMatTest.myRotMatix,myMatTest.myRotMatArrray);

glPopMatrix();

Point3 myPoint;myPoint.set(0,0,0);
Point3 myPointTemp;myPointTemp.set(setNexVertexPosPoint(myMatTest.myRotMatix,myPoint));


glPushMatrix();
glTranslatef(myPoint.x,myPoint.y,myPoint.z);
glutWireSphere(10,5,5);
glPopMatrix();

glPushMatrix();
glTranslatef(myPointTemp.x,myPointTemp.y,myPointTemp.z);
glutWireSphere(10,5,5);
glPopMatrix();

Share this post


Link to post
Share on other sites
What i'm attempting to do is to store the glTranslatef(10,0,0) as a named matrix. Then using the function setNexVertexPosPoint i pass a point and the named matrix to obtain the points new position i.e +10 on the x axis.
therefore when a translation is performed with the new points position it should move objects to that point.
The spheres are drawn to see if its working properly.

this is the function used to obtain the new point from the old point and the named matrix.

vertex_type setNexVertexPos(matrix_4x4_type MyMat, vertex_type verTemp){
vertex_type verFinal;
matrix_1x4_type resultant;
matrix_1x4_type equal;
equal[0] = verTemp.x;
equal[1] = verTemp.y;
equal[2] = verTemp.z;
equal[3] = 1;
MatrMul_1x4_4x4(equal,MyMat,resultant);

verFinal.x=resultant[0];
verFinal.y=resultant[1];
verFinal.z=resultant[2];

return verFinal;
}



/**********************************************************
*
* SUBROUTINE MatrMul_1x4_4x4 (matrix_1x4_type p_matrix1, matrix_4x4_type p_matrix2, matrix_1x4_type p_matrix_res)
*
* This subroutine multiply a vector by a 4x4 matrix to generate a new vector
*
* Input parameters: p_matrix1 = Vector1
* p_matrix2 = Matrix
* p_matrix_res = New Vector = Vector1 x matrix
*
*********************************************************/

void MatrMul_1x4_4x4 (matrix_1x4_type p_matrix1, matrix_4x4_type p_matrix2, matrix_1x4_type p_matrix_res)
{
int j,k;
float l_sum;

for (j=0;j<4;j++)
{
l_sum=0;
for(k=0;k<4;k++)
l_sum+=p_matrix1[k]*p_matrix2[k][j];
p_matrix_res[j]=l_sum;
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by JARVIS_UK
What i'm attempting to do is to store the glTranslatef(10,0,0) as a named matrix.


You aren't doing this though. glPushMatrix doesn't reset the current modelview matrix to identity - but keep what is now at the level below. This means, the matrix you're saving is actually the camera transformation + a translation of [10, 0, 0]. You need a glLoadIdentity() after you push your modelview matrix.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this