opengl画一个三维视角的长方体线框,每一面为粉色
时间: 2024-03-17 17:40:13 浏览: 232
好的,以下是使用OpenGL画一个三维视角的长方体线框,每一面为粉色的代码:
```c++
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 设置视角
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
// 绘制长方体线框
glColor3f(1.0f, 0.75f, 0.8f); // 设置颜色为粉色
glBegin(GL_LINES);
// 绘制前面
glVertex3f(-1, -1, 1);
glVertex3f(-1, 1, 1);
glVertex3f(-1, 1, 1);
glVertex3f(1, 1, 1);
glVertex3f(1, 1, 1);
glVertex3f(1, -1, 1);
glVertex3f(1, -1, 1);
glVertex3f(-1, -1, 1);
// 绘制后面
glVertex3f(-1, -1, -1);
glVertex3f(-1, 1, -1);
glVertex3f(-1, 1, -1);
glVertex3f(1, 1, -1);
glVertex3f(1, 1, -1);
glVertex3f(1, -1, -1);
glVertex3f(1, -1, -1);
glVertex3f(-1, -1, -1);
// 绘制连接前后面的线
glVertex3f(-1, -1, 1);
glVertex3f(-1, -1, -1);
glVertex3f(-1, 1, 1);
glVertex3f(-1, 1, -1);
glVertex3f(1, 1, 1);
glVertex3f(1, 1, -1);
glVertex3f(1, -1, 1);
glVertex3f(1, -1, -1);
glEnd();
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutCreateWindow("OpenGL");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
```
这段代码使用了OpenGL库,需要在编译时链接OpenGL库,例如:
```
g++ main.cpp -o main -lGL -lGLU -lglut
```
阅读全文