OpenGL 繪製長方體 計算機圖形學
不同面填充模式顯示出不同的效果,如下面三幅截圖:
上圖通過glPolygonMode(GL_FRONT, GL_LINE);函式的呼叫,實現了顯示前面面上的邊線,而下面的面顯示為預設的面填充,顯示效果為如上圖。
上圖通過glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);函式的呼叫,實現了顯示前後面面上的邊線,顯示效果就是圖中的的只顯示邊線,而沒有面填充效果。
上圖通過glPolygonMode(GL_BACK, GL_LINE);函式的呼叫,實現了顯示後面面上的邊線,而下面的面顯示為預設的面填充,顯示效果為如上圖。
【注】演算法程式碼如下,其中
#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);//
glLoadIdentity();
//設定視點的位置,視線的方向和相機的向上方向
gluLookAt(0,0,0, 0,0,-1, 0,1,0); //該設定為預設設定
glScalef(0.3,1.0,0.5); //產生比例變換,分別設定物體在x,y,z方向上縮放因子,根據需要自己可以設定
glRotatef(45,1.0,1.0,1.0);//產生旋轉變換,讓物體繞(1,1,1)軸旋轉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); //設定前面的面的模式為逆時針模式, 該設定也是預設設定
//glShadeModel(GL_SHADE_MODEL);// 預設就是此引數,可忽略,為了明確說明特意指定
void quad(GLint n1,GLint n2,GLint n3,GLint n4,GLint index);
quad(6,2,3,7,1);
quad(5,1,0,4,2);
quad(7,3,1,5,3);
quad(4,0,2,6,4);
quad(2,0,1,3,5);
quad(7,5,4,6,6);
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);
glBegin(GL_QUADS);
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;
}