PDA

View Full Version : zFar clipping issue



shreddhead
01-12-2010, 06:48 PM
SOLUTION: lol about 10 minutes after I posted this, I found a solution to my problem. I added the following lines of code to my setup.



glViewport( 0, 500, 0, 500 );
glMatrixMode( GL_PROJECTION );
glEnable( GL_DEPTH_TEST );
gluPerspective( 45, (float)500.0/500.0f, .1, 100 );
glMatrixMode( GL_MODELVIEW );


I pretty much had to setup my viewport and perspective. But I'm still open to advice and comments about my problem. Always open to constructive criticism. Thanks!


Hello Everyone, I'm new to OpenGL and I'm having a clipping issue. This problem shouldn't be too difficult, but I've spent about 6 hours on it. I'm renderinga 2D rectangular surface and I added some code from an example program to spin and zoom in and out of the surface using the mouse. Spinning works fine, but for some reason when I zoom in or out, the surface disappears instead of growing and shrinking, even though my code is almost identical to the example program.

I believe this has something to do with zFar clipping. I've been looking all over online and haven't had any luck so far. Any help is much appreciative. :D

Note: I'm using cuda to do some computations , just ignore the cuda calls. I think I've added all the relevant funtions... If you need anymore information please let me know.

Thanks!

Edit:

To help specify, the variable cameraDistance is calculated in mouseMotionCB(int x, int y) function. Then is used to move the camera (or scene?) back and forth with glTranslatef(0, 0, cameraDistance); in the display function.

Initialization Code:



void initGL( int argc, char**argv )
{
glutInit(&argc, argv);
printf("Initializing GLUT...\n");

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(500, 500);//imageW, imageH);
glutInitWindowPosition(100, 100);
glutCreateWindow("HeatGL");

printf("OpenGL window created.\n");

glutIdleFunc(display);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouseCB);
glutMotionFunc(mouseMotionCB);
glewInit();

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_FLAT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);//glDepthFunc(GL_ALWAYS);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // 4-byte pixel alignment
// glHint(GL_CLIP_VOLUME_CLIPPING_HINT_EXT,GL_FASTEST );

glDisable(GL_LIGHTING);
//glEnable(GL_CULL_FACE); //Eliminates Rendering behind objects
//glClearStencil(0);

//track material ambient and diffuse from surface color, call it before glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
//setCamera(0, 0, 3, 0, 0, 0);
}




Motion Code:



void reshape( int w, int h )
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h); // set viewport to be the entire window
glMatrixMode(GL_PROJECTION); // set perspective viewing frustum
glLoadIdentity();
//glFrustum(-aspectRatio, aspectRatio, -1, 1, 1, 100);
gluPerspective(45.0f, (float)(w)/h, 0.1f, 100.0f); // FOV, AspectRatio, NearClip, FarClip
glMatrixMode(GL_MODELVIEW); // switch to modelview matrix in order to set scene
}

void mouseMotionCB(int x, int y)
{
if(mouseLeftDown)
{
cameraAngleY += (x - mouseX);
cameraAngleX += (y - mouseY);
mouseX = x;
mouseY = y;
}
if(mouseRightDown)
{
cameraDistance += (y - mouseY) * 0.2f;
mouseY = y;
}
}


void mouseCB(int button, int state, int x, int y)
{
mouseX = x;
mouseY = y;

if(button == GLUT_LEFT_BUTTON)
{
if(state == GLUT_DOWN)
{
mouseLeftDown = true;
}
else if(state == GLUT_UP)
mouseLeftDown = false;
}

else if(button == GLUT_RIGHT_BUTTON)
{
if(state == GLUT_DOWN)
{glPushMatrix();
mouseRightDown = true;
}
else if(state == GLUT_UP)
mouseRightDown = false;
}
}




Display Function:


void display()
{
frameCount++;

if(frameCount < TIMESTEPS)
{
TColor *d_dst = NULL;
cudaGLMapBufferObject((void**)&amp;d_dst, gl_PBO);
cuda_bindTexture();

dim3 dimBlock( BLOCK_SIZE_X, BLOCK_SIZE_Y );
dim3 dimGrid( NX/ dimBlock.x, NY/ dimBlock.y);

cudaStart(&amp;start);

compBoundary<<<dimGrid, dimBlock>>>( U_d );
cudaThreadSynchronize();
computeTimeMarch<<< dimGrid, dimBlock >>>(d_dst, U_d, Unew_d, dt);

cudaStop(&amp;stop);
computeTime = cudaGetTimeMilliSeconds(&amp;start, &amp;stop);

temp_d = U_d;
U_d = Unew_d;
Unew_d = temp_d;

cuda_unbindTexture();
cudaGLUnmapBufferObject(gl_PBO);
}
else printf("finished!\n");

t1.start();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glPushMatrix(); //Save Current ModelView
glTranslatef(0, 0, cameraDistance);
glRotatef(cameraAngleX, 1, 0, 0); // pitch
glRotatef(cameraAngleY, 0, 1, 0); // heading
glBindTexture(GL_TEXTURE_2D, gl_Tex[0]);

//Draw Heat Surface
{
//glTranslatef(0.6, 0.0, -6.0);
// copy pixels from PBO to texture object. Use offset instead of ponter.
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, imageW, imageH, GL_RGBA, GL_UNSIGNED_BYTE, BUFFER_DATA(0) );
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, -0.5f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, 0.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, 0.0f);
glEnd();
}

glBindTexture(GL_TEXTURE_2D, gl_Tex[1]);
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, imageW, imageH2, GL_RGBA, GL_UNSIGNED_BYTE, BUFFER_DATA(1) );

//Draw Color Bar
{
glTranslatef(0.6, 0.0, 0.0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.05f, -0.5f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.05f, -0.5f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.05f, 0.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.05f, 0.5f, 0.0f);
glEnd();
}

glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindTexture(GL_TEXTURE_2D, 0);

t1.stop();
renderTime = t1.getElapsedTimeInMilliSec();
fps = 1.0f/((renderTime+computeTime)/1000.0f);

glPopMatrix();
glutSwapBuffers();
}