View Full Version : a math problem, how to find a point

beginner620824

03-13-2003, 01:02 AM

Hi all

I have to find this point.

My main loop for render looks like this:

glLoadIdentity();/*now we are in the world coord*/

glPushMatrix();

glRotate(A,0,1,0);

glRotate(B,1,0,0);

glBegin(GL_POINT);//draw a point XZ

glVertex(x,0,z);

glEnd;

glPopMatrix();

Now i want to find out the world coord of the point XZ. It seems so difficult to me. I believe there are a lot of guys much more intelligent than me, if you could help me with this i shall be very happy and very grateful.

Liuya

JustHanging

03-13-2003, 01:21 AM

Hi,

You could get the modelview matrix (with glGetFloatv) and multiply your point vector with it. That should do it.

-Ilkka

beginner620824

03-13-2003, 01:34 AM

Originally posted by JustHanging:

Hi,

You could get the modelview matrix (with glGetFloatv) and multiply your point vector with it. That should do it.

-Ilkka

HI Ilkka:

Thanks a lot. I understand your suggestion will bring it an easy solution instead of a lot of math. But as you know i am new at opengl I still have no idea of how to make your idea into the actual code. Could you show me the steps in more detail?

Many thanks again!

Liuya

JustHanging

03-13-2003, 01:47 AM

Ok, this is how it goes. This is in Delphi (my second native language), but it's easy to translate to c. First you get the modelview matrix by

glGetFloatv(GL_MODELVIEW_MATRIX, addr(m));

where m is array[0..3, 0..3] of glFloat,

then you set worldCoord:=transform4(pos, m). The transform4 function looks like following

function transform4(v : vector; mat : matrix4): vector;

var

temp : vector;

w : float;

begin

w:=v[0]*mat[0, 3]+v[1]*mat[1, 3]+v[2]*mat[2, 3]+mat[3, 3];

temp[0]:=(v[0]*mat[0, 0]+v[1]*mat[1, 0]+v[2]*mat[2, 0]+mat[3, 0])/w;

temp[1]:=(v[0]*mat[0, 1]+v[1]*mat[1, 1]+v[2]*mat[2, 1]+mat[3, 1])/w;

temp[2]:=(v[0]*mat[0, 2]+v[1]*mat[1, 2]+v[2]*mat[2, 2]+mat[3, 2])/w;

transform4:=temp;

end;

In c you will have to replace addr(m) with m, and let m be of type glFloat[15]. Then you replace m[i, j] by m[i*4+j]. I hope that was correct, c isn't one of my strong points.

-Ilkka

beginner620824

03-13-2003, 04:20 AM

Sorry, i made a mistake:

glLoadIdentity();/*now we are in the world coord*/

glPushMatrix();

glTranslate( to a point);//

glRotate(A,0,1,0);

glRotate(B,1,0,0);

glBegin(GL_POINT);//draw a point XY

glVertex(x,y,0);//this was X,Z********

glEnd;

glPopMatrix();

Now i want to find out the world coord of point XY.

Here i want to describer the background of my problem.

I need to draw a cylinder in any position (knowing coord of its base and top)in the space. There are some points(their coord are known if you call glCylinder without doing any transformation) on both planes of its base and top. I have to know the coord of these points after transformation for some other objects(e.g. lines) to reach to. This should be done during initialization to fill the data(this shouldn't be a problem, i can do a try render, right?).

Ilkka, I read your last post, but i don't understand it. Maybe the language is one obstacle, but i just couldn't see how MY vector shall be calculated.

I need more help! Ilkka you again or anyone else?

Thanks

JustHanging

03-13-2003, 04:45 AM

Ok, maybe I could write it open, assuming you have the modelview matrix (after the rotations) in a glFloat array,

w = x*m[3]+y*m[7]+m[15];

worldx = (x*m[0]+y*m[4]+m[12])/w;

worldy = (x*m[1]+y*m[5]+m[13])/w;

worldz = (x*m[2]+y*m[6]+m[14])/w;

Totally untested. Good luck!

-Ilkka

beginner620824

03-13-2003, 06:03 AM

Originally posted by JustHanging:

Ok, maybe I could write it open, assuming you have the modelview matrix (after the rotations) in a glFloat array,

w = x*m[3]+y*m[7]+m[15];

worldx = (x*m[0]+y*m[4]+m[12])/w;

worldy = (x*m[1]+y*m[5]+m[13])/w;

worldz = (x*m[2]+y*m[6]+m[14])/w;

Totally untested. Good luck!

-Ilkka

Hi Ilkka:

Thanks a lot.

this formula seems to be specific to my question since there is no Z. What shall i read if i want to fully understand and handle it myself? now i know there is a lot to learn about opengl. well i will try it first!

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.