Jump to content


Need help using gluUnproject


1 reply to this topic

#1 Xanas

    New Member

  • Members
  • PipPip
  • 18 posts

Posted 19 September 2005 - 09:46 PM

I'm pretty sure this is the function I want, basically what I'm doing is I'm using the selection rendermode to select the object that I want, and then using gluUnproject to grab opengl world coordinates so I can move the object there. The problem is I'm in perspective viewmode gluPerspective(45.0, width/height, 0.1, 100.0); and so I basically need to figure out how to calculate values along the line (vector? ) that goes from 0.0, 0.0, 0.0 (presuming that's camera position in world coordinates anyway, but this may be something else i need to know.. it could be 0.1..).


Anyway, as you can see I don't know very much. I've just been doing opengl tutorials lately and some redbook/bluebook stuff as well trying to pickup how to use it. I'm progressing ok, but I've been out of school for awhile so I've forgotten a lot of the maths and it's really not that easy to get back into it without examples :) I was pretty good at math back then I should be able to relearn but it's been long enough that just hearing it outright sounds confusing.

So if someone is willing i could use some help in understanding exactly how I would calculate a specific point on the line. I'm sure it's easy to those still doing math constantly but a bit confusing to someone who hasn't done it in a few years.

If you are interested in the relavent code I have so far it'd be

void PositionGLObject(int mousex, mousey){
	GLfloat x, y, z; // allocate memory for the x, y, z position
	GLint viewport[4]; //stores viewport information 
	GLdouble modmatrix[16], projmatrix[16];
	glGetIntegerv(GL_VIEWPORT, viewport); //transfers viewport info into viewport array.
	glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
	glGetDoublev(GL_MODELVIEW_MATRIX, modmatrix);
	// invert mousey so that down lowers value
	mousey = viewport[3] - mousey;
	gluUnProject((GLdouble) mousex, (GLdouble) mousey, 1.0, projmatrix, modmatrix, &x, &y, &z);
	cout<<"X: "<<x<<"Y: "<<y<<"Z: "<<z<<endl; //print out values
}

Feel free to correct me if I'm doing anything wrong there! Basically I'm just going to set a global variable during the function that will move the object around. I'm doing a simple program on my own to teach myself, tutorials only get me so far so I do my own thing to truly understand what I'm actually doing.

In this scenario the object I'm selecting is translated in glTranslate(0.0, 0.0, -4.0); like that, but obviously the math should work for any distance upon the line so it shouldn't really matter.

#2 Xanas

    New Member

  • Members
  • PipPip
  • 18 posts

Posted 19 September 2005 - 10:38 PM

Ok, after racking my brain for a bit because for some reason it's strangely hard to find basic math answers :) (guess most people don't forget their math!) I actually started thinking about the rather.. easy.. solution.

Anyway...

what I realized is at any point on a straight line that you have all 3 coordinates, you can simply use division to get a ratio to convert one coordinate to another.

So, in this example I have

x = 22, y = - 9 z = -96

I simply have to divide x/z and multiply by it by whatever z value I happen to want a number for (in this case -4.0) and I have my x coordinate, then do y/z and multiply by -4.0 for the y coordinate.

I know that's easy, but somehow I kept thinking it was more complicated than that.. i need a really basic geometry book apparently :P (if anyone knows a site with some good info on basic/complex math would be great news, haven't seen as much as I'd like at least not in the same manner as I would learn it in school, and the text books are very expensive for me to get them instead).

Here's the updated code:

void PositionGLObject(int mousex, int mousey){
	GLdouble x, y, z; // allocate memory for the x, y, z position
	GLint viewport[4]; //stores viewport information 
	GLdouble modmatrix[16], projmatrix[16];
	glGetIntegerv(GL_VIEWPORT, viewport); //transfers viewport info into viewport array.
	glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
	glGetDoublev(GL_MODELVIEW_MATRIX, modmatrix);
	// invert mousey so that down lowers value
	mousey = viewport[3] - mousey;
	gluUnProject((GLdouble) mousex, (GLdouble) mousey, 1.0, modmatrix, projmatrix, viewport, &x, &y, &z);
	cout<<" X: "<<x<<" Y: "<<y<<" Z: "<<z<<endl; //print out values
	//gluUnProject((GLdouble) mousex, (GLdouble) mousey, 0.0, projmatrix, modmatrix, viewport, &x, &y, &z);
	//cout<<"X: "<<x<<"Y: "<<y<<"Z: "<<z<<endl; //print out values
	GLdouble xzr =  x/z;
	GLdouble yzr = y/z;
	z = -4.0;
	movex = xzr * -4.0;
	movey = yzr * -4.0;
	movez = z;
	cout<<"X: "<<movex<<" Y: "<<movey<< " Z: "<< movez<< endl;
	
}

For a bit I thought this wasn't working, but it was just my inability to put the parameters in the right spots! as you can see in the above code it wouldn't even compile because I forgot to put in the viewport param for gluUnproject, but I also mixed up the modelview matrix and projection matrix parameters, which caused some weird problems.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users