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

不同面填充模式显示出不同的效果,如下面三幅截图:

OpenGL <wbr>绘制长方体 <wbr>计算机图形学

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

OpenGL <wbr>绘制长方体 <wbr>计算机图形学

 

上图通过glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);函数的调用实现了显示前后面面上的边线显示效果就是图中的的只显示边线而没有面填充效果。

OpenGL <wbr>绘制长方体 <wbr>计算机图形学

 

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

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

#include

#include

 

#ifdef __APPLE__

#include

#else

#include

#endif

 

 

#include

 

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); //产生比例变换,分别设置物体在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); //设置前面的面的模式为逆时针模式, 该设置也是默认设置

 //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;

}

你可能感兴趣的:(计算机图形学OpenGL)