Archived

This topic is now archived and is closed to further replies.

clayasaurus

gluUnproject placing object problems

Recommended Posts

hi, i'm having trouble with placing an object # units away from player with mouse and gluUnproject() my code is this:

glMatrixMode(GL_PROJECTION); // load identities

glLoadIdentity();
gluPerspective(mFov,(GLfloat)mWidth/(GLfloat)mHeight,mZnear,mZfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(x,y,z,etc); // manipulate 3d scene using gluLookAt and view/position vectors


if (a key is hit)
{
	GLdouble proj[16]; // vars

	GLdouble model[16];
	GLint view[4];
	GLdouble nearx,neary,nearz;
	GLdouble farx,fary,farz;

	glGetDoublev(GL_PROJECTION_MATRIX,proj); // get projection, model, and view matrixes

	glGetDoublev(GL_MODELVIEW_MATRIX,model);
	glGetIntegerv(GL_VIEWPORT,view);

	// glunproject

	gluUnProject(mousex,mousey,0,model,proj,view,&nearx,&neary,&nearz);
	gluUnProject(mousex,mousey,1,model,proj,view,&farx,&fary,&farz);

	cVector3 vNear(nearx, neary, nearz);
	cVector3 vFar(farx, fary, farz);

	cVector3 vRay = vFar - vNear;
	vRay.Normalize();

	vRay = vRay * 50;

	create_object_at(vRay.x(),vRay.y(),vRay.z());
}

 
my problem is glUnproject seems to work fine if i don't look up or down, if i inverse the vRay.y(), and stay around gluLookAt(0, 1.5, 6, 0, 1.5, 0, 0, 1, 0). if i don't inverse the vRay.y(), the y() axis appears to be inversed. when i move around the screen or look up/down glUnproject doesn't seem to work correctly at all. what can i be doing wrong? thx. edit: the reason i want to place an object x distance away from the camera is so i can create a ray from camera to X to check against my world boxes for picking collision [edited by - clayasaurus on March 19, 2004 5:39:25 PM]

Share this post


Link to post
Share on other sites
ugh, i figured out what i was doing wrong, i needed


line[0] = vStart.x();
line[1] = vStart.y();
line[2] = vStart.z();

line[3] = vStart.x()+(vDir.x()*50.f);
line[4] = vStart.y()+(-vDir.y()*50.f);
line[5] = vStart.z()+(vDir.z()*50.f);


boxes.resize(boxes.size()+1);
boxes.at(boxes.size()-1).position(line[3], line[4], line[5]);
boxes.at(boxes.size()-1).size(1,1,1);


[edited by - clayasaurus on March 19, 2004 8:08:48 PM]

Share this post


Link to post
Share on other sites
hrm, ok i thought everything was fine, but it is not

i've narrowed the problem down even more though, the gluUnproject()
works perfectly as long as the camera isn't looking up or down,
and i inverse the y axis.

has anyone else had similar problems ?

edit: my current code is this


GLdouble proj[16]; // view matrix

GLdouble model[16];
GLint view[4];

GLdouble nearx,neary,nearz; // glu coords

GLdouble farx,fary,farz;

glGetDoublev(GL_PROJECTION_MATRIX,proj); // get matrix

glGetDoublev(GL_MODELVIEW_MATRIX,model);
glGetIntegerv(GL_VIEWPORT,view);

gluUnProject(x,y,0,model,proj,view,&nearx,&neary,&nearz); // get coords

gluUnProject(x,y,1,model,proj,view,&farx,&fary,&farz);

cVector3 vNear(nearx, -neary, nearz); // put near plane coords in vector

cVector3 vFar(farx, -fary, farz); // put far plane coords in vector


m_vDirection = vFar - vNear; // calculate the direction vector

m_vDirection.Normalize(); // set length to 1


m_vEnd = m_vStart + (m_vDirection*m_length); // calculate end of ray based on direction, length, and start




for m_vStart i use the camera position vector, and to draw my ray
i use this


glBegin(GL_LINE_LOOP);
glColor3ub(r,g,b);
glVertex3f(m_vStart.x(),m_vStart.y(),m_vStart.z());
glVertex3f(m_vEnd.x(),m_vEnd.y(),m_vEnd.z());
glEnd();




[edited by - clayasaurus on March 19, 2004 8:15:29 PM]

Share this post


Link to post
Share on other sites
does anyone know how to calculate the look up/down vector into the ray correctly ?

guess not.

[edited by - clayasaurus on March 20, 2004 12:48:22 AM]

Share this post


Link to post
Share on other sites