我有以下代码:
#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 旋转相机视图