PDA

View Full Version : Please help me! i can't find the point i have put in the world !!!!



beginner620824
03-14-2003, 04:26 AM
Hi all and Ilkka:
I am having a big trouble to find out the world coord of a point which has been done by certain transformations.
I think i am hopeless if i can't get any help from you.
Please and thanks, liuya

below is somwhat of my main loop for render
int DrawGLScene(GLvoid)
{
glClearColor(0.0,0.0,0.0,0.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //
glLoadIdentity();
//below some stuff for the viewer walking around the world, don' care it much*/ float xtrans = -xpos;
float ztrans = -zpos;
float ytrans = -0.25f-ypos;
float sceneroty =360.0f-yrot;

glRotatef(lookupdown,1.0f,0,0);
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(xtrans, ytrans, ztrans);

/*!!!!!!!!!!!!here i call a functoin to draw a cylinder*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/

drawcylinder(0.1,1,1,1,2,2,2);

/*/!!!!!now i want to know the location, in world coord,of those points on the base/top of the cylinder*/

}
//************************************************** ********************************************
void drawcylinder(float r,float x1,float y1,float z1,float x2,float y2,float z2)
{
//r=radius
//x1,y1,z1, the center of the base
//x2,y2,z2, the center of the top
//DTOR=3.1415/180.0f
float len, angleA,angleB;
len=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
angleA=atan((x2-x1)/(z2-z1));
angleB=asin((y2-y1)/len);
glPushMatrix();
glTranslatef(x1,y1,z1);
glRotatef(angleA/DTOR,0.0f,1.0f,0.0f);
glRotatef(-angleB/DTOR,1,0,0);
glColor3f(0.3,0.0,0.9);
gluCylinder(obj,r,r,len,8,1);
glColor3f(1,1,1);
glBegin(GL_LINES);
//below i make some points on the plane of both base and top and draw a line between them(the base and top)*/

for(int i=0;i<10;i++)
{
float x=0.005*sin(DTOR*36*i);
float y=0.005*cos(DTOR*36*i);
glVertex3d(x,y,0);
glVertex3d(x,y,len);
}
glEnd();
glPopMatrix();
}
//**********************************


There has been some help from Ilkka which can be found in my post "a math probelm, how to find a point". you can see it also as below. I just can't figure out where i should put glGetFloatv(i tried putting it everywhere, the ouptput never made any sense)
and what the XYZ is, are they the world coord of the point (which i want to find) before transformatoin?

quote:
--------------------------------------------------------------------------------
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!

JustHanging
03-14-2003, 05:18 AM
Hello again.

This is where the glGetFloatv should go:

-------
void drawcylinder(float r,float x1,float y1,float z1,float x2,float y2,float z2)
{
//r=radius
//x1,y1,z1, the center of the base
//x2,y2,z2, the center of the top
//DTOR=3.1415/180.0f
float len, angleA,angleB;
len=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
angleA=atan((x2-x1)/(z2-z1));
angleB=asin((y2-y1)/len);
glPushMatrix();
glTranslatef(x1,y1,z1);
glRotatef(angleA/DTOR,0.0f,1.0f,0.0f);
glRotatef(-angleB/DTOR,1,0,0);
-------insert here: glGetFloatv(GL_MODELVIEW_MATRIX, m); --------

----

To have the formulas work for x, y and z coordinates change them to:
w = x*m[3]+y*m[7]+z*[11]+m[15];

worldx = (x*m[0]+y*m[4]+z*[8]+m[12])/w;
worldy = (x*m[1]+y*m[5]+z*[9]+m[13])/w;
worldz = (x*m[2]+y*m[6]+z*[10]+m[14])/w;

These formulas mean multiplying a column vector (x, y, z, 1) with 4*4 matrix. The division by w is because of the homogenous coordinates used in OpenGL. The x, y and z coordinates you use in it should be the x, y and len coordinates you use to draw lines after the cylinder. The worldx, -y and -z coordinates are the world coordinates of these points, so they should appear in place if drawn after the glPopMatrix.

I think the reason this goes wrong is, that you have the viewer transform in the same matrix. I missed that before, sorry about that. To get it right, you should first call glLoadIdentity, then apply the cylinder's transformations, and then get the matrix.

-Ilkka

beginner620824
03-14-2003, 04:20 PM
HI Ilkka:

Thanks a lot, it works in a simple test. I believe this is the way to do it. i am so happy with it and so grateful to your great help. Internet is great and people like you in it are great!
But my actual code still has a long way to go, no doubt there will be more troubles which need help again and again.
Best regards, Liuya