21xrx.com
2024-12-22 21:27:34 Sunday
登录
文章检索 我的文章 写文章
C++中使用OpenGL绘制小屋的实现算法
2023-07-04 19:41:23 深夜i     --     --
C++ OpenGL 绘制 小屋 实现算法

OpenGL是一种用于绘制三维图形的API,它是跨平台的,可以在多个操作系统上使用。在学习OpenGL的过程中,绘制一个小屋是一个很常见的练习。

实现算法:

1. 初始化OpenGL环境。

2. 绘制地面,这个可以用一个平面来实现。

3. 使用三角形绘制房顶,三角形的三个点坐标为(0,1,0)、(-1,0,0)和(1,0,0)。

4. 绘制四面墙体。分别使用四个矩形来绘制。

5. 绘制门和窗户。门和窗户可以通过使用两个梯形来实现。

6. 绘制屋顶的两个烟囱,可以通过使用两个立方体来实现,然后对其中一面进行旋转。

具体实现流程:

1. 引入需要的OpenGL和GLUT库。


#include <stdlib.h>

#include <GL/glut.h>

2. 设置视角和显示模式。


void reshape(int w,int h){

  glViewport(0,0,w,h);

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  gluPerspective(45.0,(double)w/(double)h,1.0,200.0); 

}

3. 绘制地面。


void drawGround()

{

  glBegin(GL_QUADS);

    glVertex3f(-20.0f, 0.0f, -20.0f);

    glVertex3f(-20.0f, 0.0f, 20.0f);

    glVertex3f(20.0f, 0.0f, 20.0f);

    glVertex3f(20.0f, 0.0f, -20.0f);

  glEnd();

}

4. 绘制三角形的房顶和四面墙体。


void drawRoof()

{

  glBegin(GL_TRIANGLES);

    glVertex3f(0.0f, 1.0f, 0.0f);

    glVertex3f(-1.0f, 0.0f, 0.0f);

    glVertex3f(1.0f, 0.0f, 0.0f);

  glEnd();

}

void drawWall()

{

  glBegin(GL_QUADS);

    //front wall

    glVertex3f(-1.0f, 0.0f, 0.0f);

    glVertex3f(-1.0f, 0.0f, -1.0f);

    glVertex3f(-1.0f, 1.0f, -1.0f);

    glVertex3f(-1.0f, 1.0f, 0.0f);

    

    //back wall

    glVertex3f(-1.0f,0.0f,-1.0f);

    glVertex3f(-1.0f,1.0f,-1.0f);

    glVertex3f(1.0f,1.0f,-1.0f);

    glVertex3f(1.0f,0.0f,-1.0f);

    //right wall

    glVertex3f(1.0f,0.0f,-1.0f);

    glVertex3f(1.0f,1.0f,-1.0f);

    glVertex3f(1.0f,1.0f,0.0f);

    glVertex3f(1.0f,0.0f,0.0f);

    //left wall

    glVertex3f(1.0f,0.0f,0.0f);

    glVertex3f(1.0f,1.0f,0.0f);

    glVertex3f(-1.0f,1.0f,0.0f);

    glVertex3f(-1.0f,0.0f,0.0f);

  glEnd();

}

5. 绘制门和窗户。


void drawDoor()

{

  glBegin(GL_QUADS);

    glVertex3f(-0.3f, 0.0f, 0.02f);

    glVertex3f(-0.3f, 0.6f, 0.02f);

    glVertex3f(0.3f, 0.6f, 0.02f);

    glVertex3f(0.3f, 0.0f, 0.02f);

  glEnd();

}

void drawWindow()

{

  glBegin(GL_QUADS);

    glVertex3f(-0.8f, 0.4f, 0.0f);

    glVertex3f(-0.8f, 0.8f, 0.0f);

    glVertex3f(-0.4f, 0.8f, 0.0f);

    glVertex3f(-0.4f, 0.4f, 0.0f);

  glEnd();

}

6. 绘制烟囱。


void drawChimney()

{

  glBegin(GL_QUADS);

    glVertex3f(-0.2f,0.8f,0.2f);

    glVertex3f(-0.2f,0.6f,0.2f);

    glVertex3f(-0.2f,0.6f,-0.2f);

    glVertex3f(-0.2f,0.8f,-0.2f);

    glVertex3f(0.2f,0.8f,0.2f);

    glVertex3f(0.2f,0.6f,0.2f);

    glVertex3f(0.2f,0.6f,-0.2f);

    glVertex3f(0.2f,0.8f,-0.2f);

    glVertex3f(-0.2f,0.8f,-0.2f);

    glVertex3f(-0.2f,0.6f,-0.2f);

    glVertex3f(0.2f,0.6f,-0.2f);

    glVertex3f(0.2f,0.8f,-0.2f);

    glVertex3f(-0.2f,0.8f,0.2f);

    glVertex3f(-0.2f,0.6f,0.2f);

    glVertex3f(0.2f,0.6f,0.2f);

    glVertex3f(0.2f,0.8f,0.2f);

    glVertex3f(-0.2f,0.8f,-0.2f);

    glVertex3f(-0.2f,0.8f,0.2f);

    glVertex3f(0.2f,0.8f,0.2f);

    glVertex3f(0.2f,0.8f,-0.2f);

    glVertex3f(-0.2f,0.6f,-0.2f);

    glVertex3f(-0.2f,0.6f,0.2f);

    glVertex3f(0.2f,0.6f,0.2f);

    glVertex3f(0.2f,0.6f,-0.2f);

  glEnd();

}

7. 显示绘制结果。


void display(void){

  glEnable(GL_DEPTH_TEST);

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity();

  glTranslatef(0.0f, 0.0f, -15.0f);

  drawGround();

  drawRoof();

  drawWall();

  drawDoor();

  drawWindow();

  drawChimney();

  glutSwapBuffers(); 

}

完整代码如下:


#include <stdlib.h>

#include <GL/glut.h>

void reshape(int w,int h){

  glViewport(0,0,w,h);

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  gluPerspective(45.0,(double)w/(double)h,1.0,200.0); 

}

void drawGround()

{

  glBegin(GL_QUADS);

    glVertex3f(-20.0f, 0.0f, -20.0f);

    glVertex3f(-20.0f, 0.0f, 20.0f);

    glVertex3f(20.0f, 0.0f, 20.0f);

    glVertex3f(20.0f, 0.0f, -20.0f);

  glEnd();

}

void drawRoof()

{

  glBegin(GL_TRIANGLES);

    glVertex3f(0.0f, 1.0f, 0.0f);

    glVertex3f(-1.0f, 0.0f, 0.0f);

    glVertex3f(1.0f, 0.0f, 0.0f);

  glEnd();

}

void drawWall()

{

  glBegin(GL_QUADS);

    //front wall

    glVertex3f(-1.0f, 0.0f, 0.0f);

    glVertex3f(-1.0f, 0.0f, -1.0f);

    glVertex3f(-1.0f, 1.0f, -1.0f);

    glVertex3f(-1.0f, 1.0f, 0.0f);

    

    //back wall

    glVertex3f(-1.0f,0.0f,-1.0f);

    glVertex3f(-1.0f,1.0f,-1.0f);

    glVertex3f(1.0f,1.0f,-1.0f);

    glVertex3f(1.0f,0.0f,-1.0f);

    //right wall

    glVertex3f(1.0f,0.0f,-1.0f);

    glVertex3f(1.0f,1.0f,-1.0f);

    glVertex3f(1.0f,1.0f,0.0f);

    glVertex3f(1.0f,0.0f,0.0f);

    //left wall

    glVertex3f(1.0f,0.0f,0.0f);

    glVertex3f(1.0f,1.0f,0.0f);

    glVertex3f(-1.0f,1.0f,0.0f);

    glVertex3f(-1.0f,0.0f,0.0f);

  glEnd();

}

void drawDoor()

{

  glBegin(GL_QUADS);

    glVertex3f(-0.3f, 0.0f, 0.02f);

    glVertex3f(-0.3f, 0.6f, 0.02f);

    glVertex3f(0.3f, 0.6f, 0.02f);

    glVertex3f(0.3f, 0.0f, 0.02f);

  glEnd();

}

void drawWindow()

{

  glBegin(GL_QUADS);

    glVertex3f(-0.8f, 0.4f, 0.0f);

    glVertex3f(-0.8f, 0.8f, 0.0f);

    glVertex3f(-0.4f, 0.8f, 0.0f);

    glVertex3f(-0.4f, 0.4f, 0.0f);

  glEnd();

}

void drawChimney()

{

  glBegin(GL_QUADS);

    glVertex3f(-0.2f,0.8f,0.2f);

    glVertex3f(-0.2f,0.6f,0.2f);

    glVertex3f(-0.2f,0.6f,-0.2f);

    glVertex3f(-0.2f,0.8f,-0.2f);

    glVertex3f(0.2f,0.8f,0.2f);

    glVertex3f(0.2f,0.6f,0.2f);

    glVertex3f(0.2f,0.6f,-0.2f);

    glVertex3f(0.2f,0.8f,-0.2f);

    glVertex3f(-0.2f,0.8f,-0.2f);

    glVertex3f(-0.2f,0.6f,-0.2f);

    glVertex3f(0.2f,0.6f,-0.2f);

    glVertex3f(0.2f,0.8f,-0.2f);

    glVertex3f(-0.2f,0.8f,0.2f);

    glVertex3f(-0.2f,0.6f,0.2f);

    glVertex3f(0.2f,0.6f,0.2f);

    glVertex3f(0.2f,0.8f,0.2f);

    glVertex3f(-0.2f,0.8f,-0.2f);

    glVertex3f(-0.2f,0.8f,0.2f);

    glVertex3f(0.2f,0.8f,0.2f);

    glVertex3f(0.2f,0.8f,-0.2f);

    glVertex3f(-0.2f,0.6f,-0.2f);

    glVertex3f(-0.2f,0.6f,0.2f);

    glVertex3f(0.2f,0.6f,0.2f);

    glVertex3f(0.2f,0.6f,-0.2f);

  glEnd();

}

void display(void){

  glEnable(GL_DEPTH_TEST);

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity();

  glTranslatef(0.0f, 0.0f, -15.0f);

  drawGround();

  drawRoof();

  drawWall();

  drawDoor();

  drawWindow();

  drawChimney();

  glutSwapBuffers(); 

}

int main(int argc,char** argv){

  glutInit(&argc,argv); //初始化

  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

  glutInitWindowSize(640,480); //窗口大小:640*480

  glutInitWindowPosition(100,100);//窗口位置:(100,100)

  glutCreateWindow("House");//创建名为‘House’的窗口

  glutDisplayFunc(display);//用于绘制窗口

  glutReshapeFunc(reshape);//用于更改窗口大小

  glutMainLoop();

  return 0;

}

运行代码后,可以在窗口中看到绘制出来的小屋。

绘制小屋的实现算法就介绍到这里。要绘制复杂的三维物体需要更多的OpenGL知识,可以继续学习OpenGL相关的知识。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复