Jump to content
  • Advertisement
Sign in to follow this  
Colossus_1

OpenGL Position the models in the scene behind and far each other

This topic is 5219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm developing a 3d scene assembler where the user can position the models in the scene dragging them with mouse, the problem: I want to position for example two models of an house. The second one should be position behind the first far from it. With gluUnproject I have the OpenGL coordinates of the mouse. The problem is that the Z value doesn't changhe when I move ahead in the scene to position the second house very far behind the first. The code I use to get the OpenGL coordinates is this:
        GLint viewport[4];
	GLdouble mvmatrix[16], projmatrix[16];
	GLint realy;									//  OpenGL y coordinate position  
	GLdouble wx, wy, wz;							//  returned world x, y, z coords  

	glGetIntegerv (GL_VIEWPORT, viewport);
	glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
	glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
	realy = viewport[3] - (GLint) event->pos().y() - 1;

	float depth;
	glReadPixels( event->pos().x() , realy , 1 , 1 , GL_DEPTH_COMPONENT , GL_FLOAT , &depth);

	gluUnProject ((GLdouble) event->pos().x(), (GLdouble) realy, depth, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);


Obviously I miss something, what ? [Edited by - Colossus_1 on August 26, 2004 2:58:52 AM]

Share this post


Link to post
Share on other sites
Advertisement
Make sure that the projection matrix contains the projection transformation used to render the scene, and that the modelview matrix contains the viewing transformation used.

Share this post


Link to post
Share on other sites
Thank you TomasH for your reply. To "go ahead and back in the scene" I use gluLookAt, how can I get the modelview matrix (the one used by gluLookAt) so to pass it to gluUnProject ?

Thanks,

Share this post


Link to post
Share on other sites
Just do as you're doing now, but make sure that gluLookAt is actually still on the modelview matrix.
One way you could get the problem you're having is if you do something like this:
glLoadIdentity();
glPushMatrix();
gluLookAt( ... );
// Render scene
glPopMatrix();
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);

I can't say if this really is the problem without seeing more of your code [smile]

Share this post


Link to post
Share on other sites
Ok, this is the part who initialises OpenGL:


void GLWidget::initializeGL()
{
glClearColor( 0.8, 0.8, 0.8, 1.0 );
glEnable(GL_RESCALE_NORMAL);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_SMOOTH);
glDisable(GL_TEXTURE_2D);
glDisable(GL_CULL_FACE);
PositionCamera ( 0.0 , 0.0 , 6 , 0.0 , 1.5 , 0.0 , 0.0 , 1.0 , 0.0 );
}



this is the part who resizes the viewport:


void GLWidget::resizeGL( int w, int h )
{
glViewport( 0, 0, (GLint)w, (GLint)h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h, 0.5f , 5000.0f);
}



this is the part who draws the scene with your kind help:


void GLWidget::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
CameraLook();
for ( std::map< unsigned int , string >::iterator i= GLWindow->DisplayList_Map.begin(); i != GLWindow->DisplayList_Map.end(); i++)
{
glPushMatrix();
glCallList ( i->first );
glPopMatrix();
}
}



and finally the part who reads the mouse coords:


void GLWidget::mouseMoveEvent ( QMouseEvent *event )
{
mouseX = event->pos().x();
mouseY = event->pos().y();
UpdateCamera();

GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy;
GLdouble wx, wy, wz;
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
realy = viewport[3] - (GLint) event->pos().y() - 1;

float depth;
glReadPixels( event->pos().x() , realy , 1 , 1 , GL_DEPTH_COMPONENT , GL_FLOAT , &depth);

gluUnProject ((GLdouble) event->pos().x(), (GLdouble) realy, depth, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
printf ("\r%f %f %f\n", wx , wy , wz );
}



Hope this helps !

Share this post


Link to post
Share on other sites
I assume CameraLook() just calls gluLookAt? In that case it looks correct, I think. What does UpdateCamera() do?

Share this post


Link to post
Share on other sites
Yes, CameraLook() calls gluLookAt while updateCamera just cross and normalize the vectors of the camera. I modified the code like this:


void GLWidget::resizeGL( int w, int h )
{
glViewport( 0, 0, (GLint)w, (GLint)h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h, 0.5f , 5000.0f);
glGetDoublev ( GL_PROJECTION_MATRIX, projmatrix ); // Added this
}

void GLWidget::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
CameraLook();
//Draw3DSGrid();
for ( std::map< unsigned int , string >::iterator i= GLWindow->DisplayList_Map.begin(); i != GLWindow->DisplayList_Map.end(); i++)
{
glPushMatrix();
glCallList ( i->first );
glPopMatrix();
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix); //Added this
}
}



Shall I get the viewport also in the same manner ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Colossus_1
Yes, CameraLook() calls gluLookAt while updateCamera just cross and normalize the vectors of the camera.

[oh] Why do you "cross and normalize" at that point?
Shouldn't you call PositionCamera after the unproject, by the way?

Quote:
I modified the code like this:

*** Source Snippet Removed ***

Shall I get the viewport also in the same manner ?

You could get the viewport and projection matrix like that, if you prefer. It should work even if you keep them in the mouseMoveEvent function, though.
For the modelview matrix, that definitely isn't right. If you want to get it outside the mouseMoveEvent function, you should put it right after CameraLook(), not inside the loop.

Share this post


Link to post
Share on other sites
Quote:
Original post by TomasH
Why do you "cross and normalize" at that point?
Shouldn't you call PositionCamera after the unproject, by the way?


PositionCamera inits the vectors that gluLookAt uses, I call it only twice, at the beginning to give a camera the initial position and when I want to reset it. Why should I call this function after gluUnProject ? I don't need to move the camera when getting the mouse coords or I need ??

Share this post


Link to post
Share on other sites
Sorry, I was thinking you wanted to move the camera there... [rolleyes]
Anyway, the I still want to know about updateCamera..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!