简体   繁体   中英

OpenGL perspective projection

I have the following code:

#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
void display(void);
void reshape(int, int);
void keyboard_input(unsigned char key, int x, int y);
int d =0;
float angle = 0;
float x = 2;
bool add_flag = false,flag =true,flag1 = false,flag2 = false;
int main(int argc, char** argv)
{
glutInit(&argc, argv);

glutInitWindowSize(512, 512);

glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

glutCreateWindow("Lab 6");

glutDisplayFunc(display);

glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard_input);

glClearColor(0,0,0,1);

glutIdleFunc(display);

glutMainLoop();

return 0;
}
void drawSnowMan() {

glRotatef(angle,0,1,0);
 glColor3f(1.0f, 1.0f, 1.0f);

// Draw Body 
 glTranslatef(0.0f ,0.75f, 0.0f);
 glutSolidSphere(0.75f,20,20);


// Draw Head
 glTranslatef(0.0f, 1.0f, 0.0f);
 glutSolidSphere(0.25f,20,20);

// Draw Eyes
 glPushMatrix();
 glColor3f(0.0f,0.0f,0.0f);
 glTranslatef(0.05f, 0.10f, 0.18f);
 glutSolidSphere(0.05f,10,10);
 glTranslatef(-0.1f, 0.0f, 0.0f);
 glutSolidSphere(0.05f,10,10);
 glPopMatrix();

// Draw Nose
 //
 glColor3f(1.0f, 0.5f , 0.5f);
 glRotatef(0.0f,1.0f, 0.0f, 0.0f);
 glTranslatef(0, 0.0f, 0.25f);
 glutSolidCone(0.04f,0.2f,10,2);
}

void drawSnow()
{   
{
    glRotatef(angle,0,1,0);
    glColor3f(1.0, 1 , 1);
    glutSolidSphere(0.05f,10,10);

}
}

void display()
{
if(flag1 == true)
{
    angle = angle + 2;
    flag1=false;
}
else if(flag2 == true)
{
    angle = angle - 2;
    flag2 = false;
}
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

for(float i = -2; i <= 2 ; i=i+0.5)
{
    if(d%3==0)
glTranslatef(0,i,0);
glTranslatef(i,x,0.0);
drawSnow();
if(x >= 2)
{
    x = 2;
    add_flag = true;
}
else if(x <= -2)
{
    x = 2;
    add_flag = false;
}

if(add_flag == false)
    x = x + 0.0002;
else if(add_flag == true )
    x = x - 0.0002;
d++;
glLoadIdentity();
}
glTranslatef(0.0f, -1.0f, 0.0f);
drawSnowMan();
glLoadIdentity();
glRotatef(angle,0,1,0);
glTranslatef(0.0f, -1, 1.5f);

glColor3f(0, 1,1);
glRotatef(90,-1,0,0);
 glutSolidCone(0.08f,0.5f,10,2);
 glTranslatef(0.1, 0, 0);
  glutSolidCone(0.08f,0.2f,10,2);
 glTranslatef(-0.2, 0, 0);
  glutSolidCone(0.08f,0.2f,10,2);
  glTranslatef(-0.1, 0, 0);
  glutSolidCone(0.08f,0.5f,10,2);
  glRotatef(-90,-1,0,0);
glLoadIdentity();
glRotatef(angle,0,1,0);
  glTranslatef(1.0f, -1, -3.5f);

glColor3f(0, 1,1);
glRotatef(90,-1,0,0);
 glutSolidCone(0.08f,0.5f,10,2);
 glTranslatef(0.1, 0, 0);
  glutSolidCone(0.08f,0.2f,10,2);
 glTranslatef(-0.2, 0, 0);
  glutSolidCone(0.08f,0.2f,10,2);
  glTranslatef(-0.1, 0, 0);
  glutSolidCone(0.08f,0.5f,10,2);
  glRotatef(-90,-1,0,0);
glLoadIdentity();
    glutSwapBuffers();

}
void reshape(int width, int height)
{
 //glEnable(GL_DEPTH_TEST);
glViewport(0,0,width,height);

/* switch to the projection matrix */
glMatrixMode(GL_PROJECTION);

/* clear the projection matrix */
glLoadIdentity();
  gluPerspective(45,1,0,20);
  glTranslatef(0,0,-5);
/* at centre but resizing won't disturb drawing */



/* switch back to the model view matrix */
glMatrixMode(GL_MODELVIEW);
}

void keyboard_input(unsigned char key, int x, int y)
{
switch(key)
{
case '2':
    flag1 = true;
    break;
case '1' :
    flag2 = true;
    break;

}
}

Whenever I enable depth test in this the screen goes blank. I need to hide the objects from view that are at the back.

keyboard type 1 and 2 to rotate the camera view

Your problem may lie in the gluPerspective call. You should never set your near plane to zero because it causes weird matrices (this is more obvious when you think about what a similar glFrustum call would be). Try changing the near plane to 1 or even 0.01 to see if it looks better.

A clue as to why this is happening can be found in the gluPerspective documentation :

Notes:
Depth buffer precision is affected by the values specified for zNear and zFar . The greater the ratio of zFar to zNear is, the less effective the depth buffer will be at distinguishing between surfaces that are near each other. If

r = zFar / zNear

roughly log 2 (r) bits of depth buffer precision are lost. Because r approaches infinity as zNear approaches 0, zNear must never be set to 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM