VS2012下基于Glut OpenGL GL_POLYGON示例程序

很多书介绍 GL_POLYGON连续点生成多边形时这下点必须是凸多边形的,规则如下图所示。写个Demo验证了下似乎不是凸多边形的也可以。

Demo的正面与背面显示效果:

源代码如下所示:

 

[cpp] view plaincopy

  1. // GlutPolygonDemo.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include “stdafx.h”
  4. #include <gl/glut.h>
  5. #include <math.h>
  6. //圆周率宏
  7. #define GL_PI 3.1415f
  8. //获取屏幕的宽度
  9. GLint SCREEN_WIDTH=0;
  10. GLint SCREEN_HEIGHT=0;
  11. //设置程序的窗口大小
  12. GLint windowWidth=400;
  13. GLint windowHeight=300;
  14. //绕x轴旋转角度
  15. GLfloat xRotAngle=0.0f;
  16. //绕y轴旋转角度
  17. GLfloat yRotAngle=0.0f;
  18. //是否打开不显示背面功能
  19. GLint cullFace=0;
  20. //是否打开深度测试功能
  21. GLint depthTest=0;
  22. //背面是采用线条描外框还是使用单色填充
  23. GLint outLine=0;
  24. //菜单回调函数
  25. void processMenu(int value){
  26.     switch(value){
  27.         case 1:
  28.             depthTest=!depthTest;
  29.             break;
  30.         case 2:
  31.             cullFace=!cullFace;
  32.         case 3:
  33.             outLine=!outLine;
  34.             break;
  35.             break;
  36.         default:
  37.             break;
  38.     }
  39.     //重新绘制
  40.     glutPostRedisplay();
  41. }
  42. //显示回调函数
  43. void renderScreen(void){
  44.     GLfloat x,y,z,angle;
  45.     //把整个窗口清理为当前清理颜色:黑色
  46.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  47.     //将当前Matrix状态入栈
  48.     glPushMatrix();
  49.     //坐标系绕x轴旋转xRotAngle
  50.     glRotatef(xRotAngle,1.0f,0.0f,0.0f);
  51.     //坐标系绕y轴旋转yRotAngle
  52.     glRotatef(yRotAngle,0.0f,1.0f,0.0f);
  53.     if(depthTest)
  54.         glEnable(GL_DEPTH_TEST);//打开深度测试功能
  55.     else
  56.         glDisable(GL_DEPTH_TEST);//关闭深度测试功能
  57.     if(cullFace)
  58.         glEnable(GL_CULL_FACE);//打开不显示背面功能
  59.     else
  60.         glDisable(GL_CULL_FACE);//关闭不显示背面功能
  61.     if(outLine)
  62.         glPolygonMode(GL_BACK,GL_LINE);//多边形背部使用线条描框
  63.     else
  64.         glPolygonMode(GL_BACK,GL_FILL);//多边形背部使用单色填充
  65.     x=0.0f;
  66.     y=0.0f;
  67.     z=0.0f;
  68.     //进行平滑处理
  69.     glEnable(GL_LINE_SMOOTH);
  70.     glHint(GL_LINE_SMOOTH,GL_NICEST);
  71.     //绘制坐标系
  72.     glColor3f(1.0f,1.0f,1.0f);
  73.     glBegin(GL_LINES);
  74.         glVertex3f(-80.0f,0.0f,0.0f);
  75.         glVertex3f(80.0f,0.0f,0.0f);
  76.         glVertex3f(0.0f,-80.0f,0.0f);
  77.         glVertex3f(0.0f,80.0f,0.0f);
  78.         glVertex3f(0.0f,0.0f,-80.0f);
  79.         glVertex3f(0.0f,0.0f,80.0f);
  80.     glEnd();
  81.     glPushMatrix();
  82.     glTranslatef(80.0f,0.0f,0.0f);
  83.     glRotatef(90.0f,0.0f,1.0f,0.0f);
  84.     glutWireCone(3,6,10,10);
  85.     glPopMatrix();
  86.     glPushMatrix();
  87.     glTranslatef(0.0f,80.0f,0.0f);
  88.     glRotatef(-90.0f,1.0f,0.0f,0.0f);
  89.     glutWireCone(3,6,10,10);
  90.     glPopMatrix();
  91.     glPushMatrix();
  92.     glTranslatef(0.0f,0.0f,80.0f);
  93.     glRotatef(90.0f,0.0f,0.0f,1.0f);
  94.     glutWireCone(3,6,10,10);
  95.     glPopMatrix();
  96.     //开始绘制两个三角形
  97.     glBegin(GL_POLYGON);
  98.         //红色绘制第一个四边形
  99.         glColor3f(1.0f,0.0f,0.0f);
  100.         glVertex3f(0.0f,0.0f,0.0f);
  101.         glVertex3f(0.0f,50.0f,0.0f);
  102.         glVertex3f(25.0f,5.0f,0.0f);
  103.         glVertex3f(5.0f,25.0f,0.0f);
  104.         glVertex3f(50.0f,0.0f,0.0f);
  105.     //结束绘三角形
  106.     glEnd();
  107.     //恢复压入栈的Matrix
  108.     glPopMatrix();
  109.     //交换两个缓冲区的指针
  110.     glutSwapBuffers();
  111. }
  112. //设置Redering State
  113. void setupRederingState(void){
  114.     //设置清理颜色为黑色
  115.     glClearColor(0.0f,0.0,0.0,1.0f);
  116.     //设置绘画颜色为绿色
  117.     glColor3f(0.0f,1.0f,0.0f);
  118. }
  119. //窗口大小变化回调函数
  120. void changSize(GLint w,GLint h){
  121.     //横宽比率
  122.     GLfloat ratio;
  123.     //设置坐标系为x(-100.0f,100.0f)、y(-100.0f,100.0f)、z(-100.0f,100.0f)
  124.     GLfloat coordinatesize=100.0f;
  125.     //窗口宽高为零直接返回
  126.     if((w==0)||(h==0))
  127.         return;
  128.     //设置视口和窗口大小一致
  129.     glViewport(0,0,w,h);
  130.     //对投影矩阵应用随后的矩阵操作
  131.     glMatrixMode(GL_PROJECTION);
  132.     //重置当前指定的矩阵为单位矩阵
  133.     glLoadIdentity();
  134.     ratio=(GLfloat)w/(GLfloat)h;
  135.     //正交投影
  136.     if(w<h)
  137.         glOrtho(-coordinatesize,coordinatesize,-coordinatesize/ratio,coordinatesize/ratio,-coordinatesize,coordinatesize);
  138.     else
  139.         glOrtho(-coordinatesize*ratio,coordinatesize*ratio,-coordinatesize,coordinatesize,-coordinatesize,coordinatesize);
  140.     //对模型视图矩阵堆栈应用随后的矩阵操作
  141.     glMatrixMode(GL_MODELVIEW);
  142.     //重置当前指定的矩阵为单位矩阵
  143.     glLoadIdentity();
  144. }
  145. //按键输入处理回调函数
  146. void specialKey(int key,int x,int y){
  147.     if(key==GLUT_KEY_UP){
  148.         xRotAngle-=5.0f;
  149.     }
  150.     else if(key==GLUT_KEY_DOWN){
  151.         xRotAngle+=5.0f;
  152.     }
  153.     else if(key==GLUT_KEY_LEFT){
  154.         yRotAngle-=5.0f;
  155.     }
  156.     else if(key==GLUT_KEY_RIGHT){
  157.         yRotAngle+=5.0f;
  158.     }
  159.     //重新绘制
  160.     glutPostRedisplay();
  161. }
  162. int main(int argc, char* argv[])
  163. {
  164.     //初始化glut
  165.     glutInit(&argc,argv);
  166.     //使用双缓冲区模式
  167.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
  168.     //获取系统的宽像素
  169.     SCREEN_WIDTH=glutGet(GLUT_SCREEN_WIDTH);
  170.     //获取系统的高像素
  171.     SCREEN_HEIGHT=glutGet(GLUT_SCREEN_HEIGHT);
  172.     //创建窗口,窗口名字为OpenGL TriAngle Demo
  173.     glutCreateWindow(“OpenGL Polygon Demo”);
  174.     //菜单回调函数
  175.     glutCreateMenu(processMenu);
  176.     //添加菜单
  177.     glutAddMenuEntry(“Toggle depth test”,1);
  178.     glutAddMenuEntry(“Toggle cull face”,2);
  179.     glutAddMenuEntry(“Toggle back outline”,3);
  180.     //将菜单榜定到鼠标右键上
  181.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  182.     //设置窗口大小
  183.     glutReshapeWindow(windowWidth,windowHeight);
  184.     //窗口居中显示
  185.     glutPositionWindow((SCREEN_WIDTH-windowWidth)/2,(SCREEN_HEIGHT-windowHeight)/2);
  186.     //窗口大小变化时的处理函数
  187.     glutReshapeFunc(changSize);
  188.     //设置显示回调函数
  189.     glutDisplayFunc(renderScreen);
  190.     //设置按键输入处理回调函数
  191.     glutSpecialFunc(specialKey);
  192.     //设置全局渲染参数
  193.     setupRederingState();
  194.     glutMainLoop();
  195.     return 0;
  196. }

标签