📌  相关文章
📜  在C ++中使用OpenGL绘制小屋的程序(1)

📅  最后修改于: 2023-12-03 15:37:34.393000             🧑  作者: Mango

在C++中使用OpenGL绘制小屋的程序

在这个教程中,我们将使用OpenGL和C ++来绘制一个简单的小屋。

实现步骤
步骤一:OpenGL窗口初始化

首先,我们需要初始化OpenGL窗口。我们将使用GLUT库来实现这一点。下面是代码片段:

#include <GL/glut.h>

int main(int argc, char** argv) {
  // 初始化GLUT库
  glutInit(&argc, argv);

  // 创建窗口并设置窗口属性
  glutCreateWindow("小屋");
  glutInitWindowSize(640, 480);
  glutInitWindowPosition(50, 50);

  // 初始化OpenGL
  glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_COLOR_MATERIAL);

  // 进入主循环
  glutMainLoop();

  return 0;
}
步骤二:绘制小屋的框架

我们将使用OpenGL画图基元来绘制小屋的框架。下面是代码片段:

void drawHouse() {
  // 绘制屋顶
  glBegin(GL_TRIANGLES);
  glColor3f(0.9f, 0.1f, 0.1f);
  glVertex3f(0.0f, 2.0f, 0.0f);
  glVertex3f(-2.0f, 0.0f, 0.0f);
  glVertex3f(2.0f, 0.0f, 0.0f);
  glEnd();

  // 绘制墙体
  glBegin(GL_QUADS);
  glColor3f(0.6f, 0.2f, 0.2f);
  glVertex3f(-1.5f, 0.0f, 0.0f);
  glVertex3f(-1.5f, -1.0f, 0.0f);
  glVertex3f(1.5f, -1.0f, 0.0f);
  glVertex3f(1.5f, 0.0f, 0.0f);
  glEnd();

  // 绘制门
  glBegin(GL_QUADS);
  glColor3f(0.0f, 0.0f, 0.0f);
  glVertex3f(-0.4f, -1.0f, 0.01f);
  glVertex3f(-0.4f, -0.5f, 0.01f);
  glVertex3f(0.4f, -0.5f, 0.01f);
  glVertex3f(0.4f, -1.0f, 0.01f);
  glEnd();

  // 绘制窗户
  glBegin(GL_QUADS);
  glColor3f(0.8f, 0.8f, 0.8f);
  glVertex3f(-0.8f, -0.2f, 0.02f);
  glVertex3f(-0.8f, 0.2f, 0.02f);
  glVertex3f(-0.2f, 0.2f, 0.02f);
  glVertex3f(-0.2f, -0.2f, 0.02f);
  glEnd();

  glBegin(GL_QUADS);
  glColor3f(0.8f, 0.8f, 0.8f);
  glVertex3f(0.2f, -0.2f, 0.02f);
  glVertex3f(0.2f, 0.2f, 0.02f);
  glVertex3f(0.8f, 0.2f, 0.02f);
  glVertex3f(0.8f, -0.2f, 0.02f);
  glEnd();
}
步骤三:绘制地面

我们在小屋底部添加一些地面。下面是代码片段:

void drawGround() {
  glBegin(GL_QUADS);
  glColor3f(0.0f, 0.8f, 0.0f);
  glVertex3f(-50.0f, -1.0f, 50.0f);
  glVertex3f(-50.0f, -1.0f, -50.0f);
  glVertex3f(50.0f, -1.0f, -50.0f);
  glVertex3f(50.0f, -1.0f, 50.0f);
  glEnd();
}
步骤四:将小屋和地面渲染到屏幕上

现在,我们可以将小屋和地面的渲染逻辑结合起来,并将它们渲染到屏幕上。下面是完整的代码片段:

#include <GL/glut.h>

void drawHouse() {
  // 绘制屋顶
  glBegin(GL_TRIANGLES);
  glColor3f(0.9f, 0.1f, 0.1f);
  glVertex3f(0.0f, 2.0f, 0.0f);
  glVertex3f(-2.0f, 0.0f, 0.0f);
  glVertex3f(2.0f, 0.0f, 0.0f);
  glEnd();

  // 绘制墙体
  glBegin(GL_QUADS);
  glColor3f(0.6f, 0.2f, 0.2f);
  glVertex3f(-1.5f, 0.0f, 0.0f);
  glVertex3f(-1.5f, -1.0f, 0.0f);
  glVertex3f(1.5f, -1.0f, 0.0f);
  glVertex3f(1.5f, 0.0f, 0.0f);
  glEnd();

  // 绘制门
  glBegin(GL_QUADS);
  glColor3f(0.0f, 0.0f, 0.0f);
  glVertex3f(-0.4f, -1.0f, 0.01f);
  glVertex3f(-0.4f, -0.5f, 0.01f);
  glVertex3f(0.4f, -0.5f, 0.01f);
  glVertex3f(0.4f, -1.0f, 0.01f);
  glEnd();

  // 绘制窗户
  glBegin(GL_QUADS);
  glColor3f(0.8f, 0.8f, 0.8f);
  glVertex3f(-0.8f, -0.2f, 0.02f);
  glVertex3f(-0.8f, 0.2f, 0.02f);
  glVertex3f(-0.2f, 0.2f, 0.02f);
  glVertex3f(-0.2f, -0.2f, 0.02f);
  glEnd();

  glBegin(GL_QUADS);
  glColor3f(0.8f, 0.8f, 0.8f);
  glVertex3f(0.2f, -0.2f, 0.02f);
  glVertex3f(0.2f, 0.2f, 0.02f);
  glVertex3f(0.8f, 0.2f, 0.02f);
  glVertex3f(0.8f, -0.2f, 0.02f);
  glEnd();
}

void drawGround() {
  glBegin(GL_QUADS);
  glColor3f(0.0f, 0.8f, 0.0f);
  glVertex3f(-50.0f, -1.0f, 50.0f);
  glVertex3f(-50.0f, -1.0f, -50.0f);
  glVertex3f(50.0f, -1.0f, -50.0f);
  glVertex3f(50.0f, -1.0f, 50.0f);
  glEnd();
}

void display() {
  // 渲染场景
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // 设置摄像机位置
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(0.0f, 1.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

  // 绘制小屋和地面
  drawHouse();
  drawGround();

  // 刷新窗口
  glutSwapBuffers();
}

int main(int argc, char** argv) {
  // 初始化GLUT库
  glutInit(&argc, argv);

  // 创建窗口并设置窗口属性
  glutCreateWindow("小屋");
  glutInitWindowSize(640, 480);
  glutInitWindowPosition(50, 50);

  // 初始化OpenGL
  glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_COLOR_MATERIAL);

  // 注册回调函数
  glutDisplayFunc(display);

  // 进入主循环
  glutMainLoop();

  return 0;
}
总结

使用OpenGL和C ++,我们可以轻松地绘制复杂的3D场景,如此教程所示。现在,您已经熟悉如何在C ++中使用OpenGL进行绘图。欢迎您将这些技能应用到您自己的项目中。