我正在开发一个练习OpenGL程序。我基本上试图展示金字塔。我粘贴了迄今为止的代码。我遇到的问题是,当我打电话给gluLook时,我的屏幕变得完全黑了。这对我来说没有意义。我的金字塔有三角形,尺寸限制在一个大小(1,1,1)的盒子里,它位于原点。因此,如果我将眼睛放在远处的东西(10,10,10)并且看(0,0,0),为什么我看不到金字塔。当我注释掉gluLookAt时,我可以清楚地看到它。
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
struct
{
GLuint trifan;
GLuint tribase;
GLuint colors;
} b;
void display();
void init_buffer();
void check_errors();
void reshape(int w, int h);
void keyboard(unsigned char k, int x, int y);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutCreateWindow("Pyramid");
init_buffer();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
void init_buffer()
{
glGenBuffers(1, &(b.trifan));
glBindBuffer(GL_ARRAY_BUFFER, b.trifan);
GLfloat trifan[6][3] =
{
{0.0, 1.0, 0.0},
{1.0, 0.0, 1.0},
{-1.0, 0.0, 1.0},
{-1.0, 0.0, -1.0},
{1.0, 0.0, -1.0},
{1.0, 0.0, 1.0}
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, trifan, GL_STATIC_COPY);
glGenBuffers(1, &b.colors);
glBindBuffer(GL_ARRAY_BUFFER, b.colors);
GLfloat colors[6][3] =
{
{1.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{1.0, 0.0, 1.0},
{0.5, 0.0, 1.0},
{1.0, 0.0, 0.5},
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, colors, GL_STATIC_COPY);
check_errors();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float)w / (float)h, 1.0, 100.0);
}
float angle = 0.0;
float zoom = 1.0;
void keyboard(unsigned char k, int x, int y)
{
if(k == 'x')
{
angle += 10;
}
else if(k == 'y')
{
angle -= 10;
}
else if(k == 'w')
{
zoom += 0.1;
}
else if(k == 's')
{
zoom -= 0.1;
}
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotated(angle, 1.0, 0.0, 0.0);
gluLookAt(10.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
{
glBindBuffer(GL_ARRAY_BUFFER, b.trifan);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, b.colors);
glColorPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
}
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
check_errors();
glFlush();
}
void check_errors()
{
GLint err = glGetError();
if(err != GL_NO_ERROR)
{
fputs((char*)gluErrorString(err), stderr);
exit(1);
}
}