# OpenGL 绘制长方体 计算机图形学

上图通过glPolygonMode(GL_FRONT, GL_LINE);函数的调用实现了显示前面面上的边线而下面的面显示为默认的面填充显示效果为如上图。

【注】算法代码如下，其中void quad(GLint n1,GLint n2,GLint n3,GLint n4,GLint index)为画立方体的算法：

#include<windows.h>

#include<math.h>

#ifdef __APPLE__

#include <GLUT/glut.h>

#else

#include <GL/glut.h>

#endif

#include <stdlib.h>

void init(void)

{

glClearColor(1.0,1.0,1.0,0.0);

glMatrixMode(GL_MODELVIEW);//设置当前矩阵模式为视图模型矩阵

//设置视点的位置，视线的方向和相机的向上方向

gluLookAt(0,0,0, 0,0,-1, 0,1,0); //该设置为默认设置

glScalef(0.3,1.0,0.5); //产生比例变换，分别设置物体在xyz方向上缩放因子，根据需要自己可以设置

glRotatef(45,1.0,1.0,1.0);//产生旋转变换，让物体绕（111）轴旋转45

glMatrixMode(GL_PROJECTION);//设置当前矩阵模式为投影矩阵

glFrustum(-4,4,-4,4,0,20);设置场景投影时的可视范围

}

float points[8][3] = {{0,0,0},{0,0.5,0},{0.5,0,0},{0.5,0.5,0},{0,0,0.5},{0,0.5,0.5},{0.5,0,0.5},{0.5,0.5,0.5}};

float colors[6][3] = {{0.25,0,0},{0.25,0.25,0.25},{0,0,0.25},{0.5,0.25,0},{0,0.25,0.25},{0.25,0,0.25}};

void vertexArray(void)

{

glClear(GL_COLOR_BUFFER_BIT);

// glColor 3f(1.0,1.0,0.0);

//glPolygonMode(GL_FRONT, GL_LINE);// 设置多边形的显示模式为面填充模式，该设置也是默认设置

glFrontFace(GL_CCW); //设置前面的面的模式为逆时针模式， 该设置也是默认设置

void quad(GLint n1,GLint n2,GLint n3,GLint n4,GLint index);

glFlush();

}

void quad(GLint n1,GLint n2,GLint n3,GLint n4,GLint index)

{

glColor3f(0.0,1.0,0.0);

glEnable(GL_POLYGON_OFFSET_FILL);

glPolygonOffset(1.0,1.0);

glDisable(GL_POLYGON_OFFSET_FILL);

glColor3f(colors[index][0],colors[index][1],colors[index][2]);

//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

//glPolygonMode(GL_FRONT, GL_LINE);

glPolygonMode(GL_BACK, GL_LINE);

//glColor3f(1.0,1.0,0);

glVertex3fv(points[n1]);

glVertex3fv(points[n2]);

glVertex3fv(points[n3]);

glVertex3fv(points[n4]);

glEnd();

}

int main (int argc, char** argv)

{

glutInit (&argc, argv);                         // Initialize GLUT.

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);   // Set display mode.

glutInitWindowPosition (50, 100);   // Set top-left display-window position.

glutInitWindowSize (600, 500);      // Set display-window width and height.

glutCreateWindow ("An Example OpenGL Program"); // Create display window.

init();                           // Execute initialization procedure.

glutDisplayFunc (vertexArray);       // Send graphics to display window.

glutMainLoop ( );// Send graphics to display window.                  // Display everything and wait.

return 0;

}

12-04

04-26

02-14

09-19 720

08-23 1359

#### OpenGL&nbsp;绘制字母 计算机图形学

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。