Jump to content
  • Advertisement
Sign in to follow this  
zonozz

OpenGL About transform screen coords to OpenGL 3D World

This topic is 1520 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 everyone:

 

I tried to use the following code for translate screen coordinates to a fixed-z in OpenGL 3D world:

Point3D Get_3D_Pos(int x, int y)
{
	Point3D glpt;
	GLint viewport[4];   
	GLdouble modelview[16];   
	GLdouble projection[16];   
	GLfloat winX, winY, winZ;   
	GLdouble posX, posY, posZ;
	GLdouble last_posX, last_posY, last_posZ;

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glPushMatrix();  

	glGetIntegerv(GL_VIEWPORT, viewport);
	glGetDoublev(GL_MODELVIEW_MATRIX, modelview);   
	glGetDoublev(GL_PROJECTION_MATRIX, projection);   
	glPopMatrix();   

	winX = x;   
	winY = viewport[3] - (float)y;   
	glReadPixels((int)winX, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);   
	gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
	glpt.x = posX;
	glpt.y = posY;
	glpt.z = posZ;

	return glpt;
}

What I'm trying to implement is that I want users to click somewhere in the window and the position will show a explosion effects, just like a fireworks, but I need to fix the Z coordinates, and the effects only works on X & Y axis.

 

Now I think maybe the results of the function above is wrong. See picture below:

[attachment=24215:Wrong position.bmp]

 

The explosion's position is not where I click.

 

Could you guys give me an idea?

Share this post


Link to post
Share on other sites
Advertisement

tell me if that will not work:

 

 

 

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glPopMatrix();

 

you understand that you are loading identity and everytime you call that function it actually kills your current modelview (in example you were loking at left you call this function you are looking in wrong direction)

 

so there are two options

 

first

 

write this

glMatrixMode(GL_MODELVIEW);


glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);

or the second

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
set the view transform (like gltranslatef or glrotatef or glulookat or glmultmatrixf)

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glPopMatrix();

third thing may be that you are not getting correct x,y cursor positions i always did something like this:

		 TPoint mouse; //for vc++ its tTagPoint 
		 GetCursorPos(&mouse);                
ScreenToClient(hwnd, &mouse);
HIT_SPACE_POSITION = GetOGLPos(mouse.x,mouse.y);

my complete code look like this (works for me)

		 TPoint mouse;
		 GetCursorPos(&mouse);                   // Gets The Current Cursor Coordinates (Mouse Coordinates)
ScreenToClient(hwnd, &mouse);
//		 ZAPISZ_WIDOK_DO_PLIKU_BITMAPY("E:\\region_mouse_move.bmp",1024,512);
HIT_SPACE_POSITION = GetOGLPos(mouse.x,mouse.y);



=================




t3dpoint __fastcall T3DPlanet::GetOGLPos(int x, int y)
{
t3dpoint res;
glGetDoublev( GL_PROJECTION_MATRIX, taprojection );
glGetDoublev( GL_MODELVIEW_MATRIX, tamodelview );
glGetIntegerv( GL_VIEWPORT, taviewport );
				  int wy = taviewport[3] -y;
	  double nx,ny,nz;      //  double fx,fy,fz;
  double	winX = double(x);
   double	winY = double(taviewport[3]) - double(y);
double winZ;
			  // glReadBuffer(GL_DEPTH_BUFFER_BIT);
float * pdata = new float;
float pk;

    glReadBuffer(GL_DEPTH_BUFFER_BIT);
	glReadPixels(x,wy,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,pdata);
	pk = (*pdata);
winZ = double(pk);

delete pdata;


gluUnProject(winX, winY, winZ, tamodelview, taprojection, taviewport, &nx, &ny, &nz);

res.x = float(nx);
res.y = float(ny);
res.z = float(nz);

 return res;
}

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!