Jump to content
  • Advertisement
Sign in to follow this  
c-mos

transforming nearplane

This topic is 3013 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

Hello, I need your help solving this problem. There is a color coded cube with minimum coordinate (0,0,0) and maximum coordinate (1,1,1). Each vertex of this cube has the respecitve color. That means, Vertex 0,0,0 is colored with 0,0,0, vertex 1,0,0 is colored with 1,0,0. Please have a look at the link http://img80.imageshack.us/i/nearplane.jpg/ The top image shows such a color coded cube. The problem is, when moving the camera into the cube, there are no colors where the nearplane clips the cube (bottom image). To solve this problem i calculate the near plane vectors and multiply them by the inverse modelview matrix. In eyespace the camera is at 0,0,0. The tangent is tan(fovY/2) = (height/2) / nearplane_distance). So the height/2 is tan(fovY/2) * nearplane_distance
float fovy_half = deg_to_rad * fovYgrad * 0.5f;
float near_height_half = tan(fovy_half) * nearplane_dist;
float near_width_half = near_height_half * (aspect_ratio);

Vector4<float> nbl(-near_width_half, -near_height_half, -nearplane_dist, 1.0f)
Vector4<float> nbr( near_width_half, -near_height_half, -nearplane_dist, 1.0f)
Vector4<float> ntl( near_width_half,  near_height_half, -nearplane_dist, 1.0f)
Vector4<float> ntr(-near_width_half,  near_height_half, -nearplane_dist, 1.0f)

Matrix4<float> modelview;
glGetFloatv(GL_MODELVIEW_MATRIX, modelview.get());

Matrix4<float> mvi = Matrix4<float>::transpose(Matrix4<float>::inverse(modelview));

nbl = mvi * nbl;
nbr = mvi * nbr;
ntl = mvi * ntl;
ntr = mvi * ntr;

The nearplane vector are now in objectspace coordinates. With these 4 vectors I draw a fullscreen quad. Because the cube is from 0,0,0 to 1,1,1 and the colors as well, the 4 vectors nbl,nbr,ntl,ntr are use to represent the colors of the near plane. The goal is to fill the clipped area with the colors. Well, there is a colored "nearplane", but the colors are not correct.
glMatrixMode(GL_MODELVIEW); 
glPushMatrix(); 
glLoadIdentity(); 
glMatrixMode(GL_PROJECTION);
glPushMatrix(); 
glLoadIdentity ();

glBegin(GL_QUADS); 
glColor3d(nbl.x, nbl.y, nbl.z); glVertex3f(-1.0f, -1.0f, -0.50f); 
glColor3d(nbr.x, nbr.y, nbr.z); glVertex3f( 1.0f, -1.0f, -0.50f); 
glColor3d(ntr.x, ntr.y, ntr.z); glVertex3f( 1.0f,  1.0f, -0.50f);
glColor3d(ntl.x, ntl.y, ntl.z); glVertex3f(-1.0f,  1.0f, -0.50f); 
glEnd ();

glPopMatrix(); 
glMatrixMode(GL_MODELVIEW); 
glPopMatrix ();	
I hope someone can help me, solving this problem regards, c-mos [Edited by - c-mos on April 23, 2010 7:02:13 AM]

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!