繁体   English   中英

OpenGL透视投影

[英]OpenGL perspective projection

我有以下代码:

#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;

}
}

每当我在其中启用深度测试时,屏幕都会变黑。 我需要从后面的视图中隐藏对象。

键盘类型1和2旋转相机视图

您的问题可能出在gluPerspective调用上。 永远不要将近平面设置为零,因为它会引起奇怪的矩阵(考虑类似的glFrustum调用会更明显)。 尝试将近平面更改为1甚至0.01,以查看它是否更好。

有关发生这种情况的线索,可以在gluPerspective文档中找到:

笔记:
深度缓冲区的精度受zNearzFar指定的值的影响。 zFar与zNear的比率越大,深度缓冲区在区分彼此靠近的表面时效果越差。 如果

r = zFar / zNear

深度缓冲区精度的大约log 2 (r)位丢失了。 因为当zNear接近0时r接近无穷大,所以zNear绝不能设置为0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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