VS2012下基于Glut glRotatef glTranslatef示例程序:

Demo使用glRotatef ,glTranslatef来实现一个太阳、地球、月亮运动的3D图形。

1.glTranslatef() ——模型变换函数 移动

void glTranslatef(GLfloat  x,  GLfloat  y,  GLfloat  z);

这个函数表示模型是怎样移动的。举个例子:

glTranslatef(-1.0,0.0,-2.0);//表示物体沿x负方向移动1.0,沿z轴负方向移动2.0。所以就好像能看见侧面一样

2.glRotatef()——模型变换函数  旋转

void glRotatef(GLfloat  angle,  GLfloat  x,  GLfloat  y,  GLfloat  z);

angle表示旋转的角度(注意单位不是弧度),(x,y,z)表示转轴。举个例子:

glRotatef(45.0, 0.0, 0.0, 1.0);//表示模型沿着(0,0,1)这个轴旋转45°。

 

3.glPerspective() ——投影变换函数 透视投影

void gluPerspective(GLdouble  fovy,  GLdouble  aspect,  GLdouble  zNear,  GLdouble  zFar);

第一个参数表示在y方向上的视角度数。个人理解:比如45,表示眼睛和模型中心这个条线与y轴的夹角。

第二个参数表示纵横比。x/y。比如2表示x、y本来都是1个长度,现在x可以当两个长度用,这样,模型就好像被x方向压缩了一样。

后两个参数glFrustum()中后两个相近。要求都是正数。

平移、旋转、以及缩放效果:

正交投影以及透视投影:

正交投影下显示效果:

右击鼠标右键菜单选择是否显示坐标轴以及透视和正交投影模式:

透视投影:

源代码:

 

[cpp][/cpp] view plaincopy

  1. // GlutSolarDemo.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. GLfloat sizes[2];
  20. //受支持的点大小增量
  21. GLfloat step;
  22. //最大的投影矩阵堆栈深度
  23. GLint iMaxProjectionStackDepth;
  24. //最大的模型视图矩阵堆栈深度
  25. GLint iMaxModeviewStackDepth;
  26. //最大的纹理矩阵堆栈深度
  27. GLint iMaxTextureStackDepth;
  28. GLfloat  whiteLight[] = { 0.4f, 0.4f, 0.4f, 1.0f };
  29. GLfloat  sourceLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  30. GLfloat  lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  31. GLint iCoordinateaxis=2;
  32. GLint iProjectionMode=1;
  33. void changSize(GLint w,GLint h);
  34. //菜单回调函数
  35. void processMenu(int value){
  36.     switch(value){
  37.         case 1:
  38.             iCoordinateaxis=1;
  39.             break;
  40.         case 2:
  41.             iCoordinateaxis=2;
  42.             break;
  43.         case 3:
  44.             iProjectionMode=1;
  45.             //强制调用窗口大小变化回调函数,更改投影模式为正交投影
  46.             changSize(glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
  47.             break;
  48.         case 4:
  49.             iProjectionMode=2;
  50.             //强制调用窗口大小变化回调函数,更改投影模式为透视投影
  51.             changSize(glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
  52.             break;
  53.         default:
  54.             break;
  55.     }
  56.     //重新绘制
  57.     glutPostRedisplay();
  58. }
  59. //显示回调函数
  60. void renderScreen(void){
  61.     static float fMoonRot = 0.0f;
  62.     static float fEarthRot = 0.0f;
  63.     //将窗口颜色清理为黑色
  64.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  65.     //把整个窗口清理为当前清理颜色:黑色;清除深度缓冲区。
  66.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  67.     //将当前Matrix状态入栈
  68.     glPushMatrix();
  69.     if(2==iProjectionMode)
  70.         glTranslatef(0.0f, 0.0f, -250.0f);  //透视投影为便于观察整个坐标系往内移动250个单位
  71.     //坐标系绕x轴旋转xRotAngle
  72.     glRotatef(xRotAngle,1.0f,0.0f,0.0f);
  73.     //坐标系绕y轴旋转yRotAngle
  74.     glRotatef(yRotAngle,0.0f,1.0f,0.0f);
  75.     //进行平滑处理
  76.     glEnable(GL_POINT_SMOOTH);
  77.     glHint(GL_POINT_SMOOTH,GL_NICEST);
  78.     glEnable(GL_LINE_SMOOTH);
  79.     glHint(GL_LINE_SMOOTH,GL_NICEST);
  80.     glEnable(GL_BLEND);
  81.     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  82.     //白色绘制坐标系
  83.     if(1==iCoordinateaxis){
  84.         glColor3f(1.0f,1.0f,1.0f);
  85.         glBegin(GL_LINES);
  86.             glVertex3f(-90.0f,00.0f,0.0f);
  87.             glVertex3f(90.0f,0.0f,0.0f);
  88.             glVertex3f(0.0f,-90.0f,0.0f);
  89.             glVertex3f(0.0f,90.0f,0.0f);
  90.             glVertex3f(0.0f,0.0f,-90.0f);
  91.             glVertex3f(0.0f,0.0f,90.0f);
  92.         glEnd();
  93.         glPushMatrix();
  94.         glTranslatef(90.0f,0.0f,0.0f);
  95.         glRotatef(90.0f,0.0f,1.0f,0.0f);
  96.         glutSolidCone(3,6,10,10);
  97.         glPopMatrix();
  98.         glPushMatrix();
  99.         glTranslatef(0.0f,90.0f,0.0f);
  100.         glRotatef(-90.0f,1.0f,0.0f,0.0f);
  101.         glutSolidCone(3,6,10,10);
  102.         glPopMatrix();
  103.         glPushMatrix();
  104.         glTranslatef(0.0f,0.0f,90.0f);
  105.         glRotatef(70.0f,0.0f,0.0f,1.0f);
  106.         glutSolidCone(3,6,10,10);
  107.         glPopMatrix();
  108.     }
  109.     glPushMatrix();
  110.     glDisable(GL_LIGHTING);
  111.     //Draw sun at(0.0f,0.0f,0.0f)
  112.     glColor3ub(255, 255, 0);
  113.     glutSolidSphere(15.0f, 30, 17);
  114.     glEnable(GL_LIGHTING);
  115.     //Move the light after we draw the sun!
  116.     glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  117.     //Rotate coordinate system
  118.     glColor3ub(0,0,255);
  119.     glRotatef(fEarthRot, 0.0f, 1.0f, 0.0f);
  120.     fEarthRot += 5.0f;
  121.     if(fEarthRot > 360.0f)
  122.         fEarthRot = 0.0f;
  123.     glTranslatef(105.0f,0.0f,0.0f);
  124.     //Draw the Earth
  125.     glutSolidSphere(15.0f, 30, 17);
  126.     //Rotate from Earth based coordinates and draw Moon
  127.     glColor3ub(200,200,200);
  128.     glRotatef(fMoonRot,0.0f, 1.0f, 0.0f);
  129.     fMoonRot+= 15.0f;
  130.     if(fMoonRot > 360.0f)
  131.         fMoonRot = 0.0f;
  132.     glTranslatef(30.0f, 0.0f, 0.0f);
  133.     glutSolidSphere(6.0f, 30, 17);
  134.     glPopMatrix();
  135.     //恢复压入栈的Matrix
  136.     glPopMatrix();
  137.     //交换两个缓冲区的指针
  138.     glutSwapBuffers();
  139. }
  140. //设置Redering State
  141. void setupRederingState(void){
  142.     // Enable lighting
  143.     glEnable(GL_LIGHTING);
  144.     // Setup and enable light 0
  145.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
  146.     glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
  147.     glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  148.     glEnable(GL_LIGHT0);
  149.     // Enable color tracking
  150.     glEnable(GL_COLOR_MATERIAL);
  151.     // Set Material properties to follow glColor values
  152.     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  153.     glEnable(GL_DEPTH_TEST);    //使能深度测试
  154.     glFrontFace(GL_CCW);        //多边形正面为逆时针方向
  155.     glEnable(GL_CULL_FACE);     //不显示背面
  156.     //设置清理颜色为黑色
  157.     glClearColor(0.0f,0.0,0.0,1.0f);
  158.     //设置绘画颜色为绿色
  159.     glColor3f(1.0f,1.0f,0.0f);
  160.     //使能深度测试
  161.     glEnable(GL_DEPTH_TEST);
  162.     //获取受支持的点大小范围
  163.     glGetFloatv(GL_POINT_SIZE_RANGE,sizes);
  164.     //获取受支持的点大小增量
  165.     glGetFloatv(GL_POINT_SIZE_GRANULARITY,&step);
  166.     //获取最大的投影矩阵堆栈深度
  167.     glGetIntegerv( GL_MAX_PROJECTION_STACK_DEPTH,&iMaxProjectionStackDepth);
  168.     //获取最大的模型视图矩阵堆栈深度
  169.     glGetIntegerv( GL_MAX_MODELVIEW_STACK_DEPTH,&iMaxModeviewStackDepth);
  170.     //获取最大的纹理矩阵堆栈深度
  171.     glGetIntegerv( GL_MAX_TEXTURE_STACK_DEPTH,&iMaxTextureStackDepth);
  172.     printf(“point size range:%f-%f\n”,sizes[0],sizes[1]);
  173.     printf(“point step:%f\n”,step);
  174.     printf(“iMaxProjectionStackDepth=%d\n”,iMaxProjectionStackDepth);
  175.     printf(“iMaxModeviewStackDepth=%d\n”,iMaxModeviewStackDepth);
  176.     printf(“iMaxTextureStackDepth=%d\n”,iMaxTextureStackDepth);
  177. }
  178. //窗口大小变化回调函数
  179. void changSize(GLint w,GLint h){
  180.     //横宽比率
  181.     GLfloat ratio;
  182.     //设置坐标系为x(-100.0f,100.0f)、y(-100.0f,100.0f)、z(-100.0f,100.0f)
  183.     GLfloat coordinatesize=120.0f;
  184.     //窗口宽高为零直接返回
  185.     if((w==0)||(h==0))
  186.         return;
  187.     //设置视口和窗口大小一致
  188.     glViewport(0,0,w,h);
  189.     //对投影矩阵应用随后的矩阵操作
  190.     glMatrixMode(GL_PROJECTION);
  191.     //重置当前指定的矩阵为单位矩阵
  192.     glLoadIdentity();
  193.     ratio=(GLfloat)w/(GLfloat)h;
  194.     //正交投影
  195.     if(1==iProjectionMode){
  196.         printf(“glOrtho\n”);
  197.         if(w<h)
  198.             glOrtho(-coordinatesize,coordinatesize,-coordinatesize/ratio,coordinatesize/ratio,-coordinatesize*2.0f,coordinatesize*2.0f);
  199.         else
  200.             glOrtho(-coordinatesize*ratio,coordinatesize*ratio,-coordinatesize,coordinatesize,-coordinatesize*2.0f,coordinatesize*2.0f);
  201.         //当前矩阵设置为模型视图矩阵
  202.         glMatrixMode(GL_MODELVIEW);
  203.         //重置当前指定的矩阵为单位矩阵
  204.         glLoadIdentity();
  205.     }
  206.     else{
  207.         printf(“gluPerspective\n”);
  208.         gluPerspective(45,ratio,10.0f,500.0f);
  209.         //当前矩阵设置为模型视图矩阵
  210.         glMatrixMode(GL_MODELVIEW);
  211.         //重置当前指定的矩阵为单位矩阵
  212.         glLoadIdentity();
  213.     }
  214. }
  215. //按键输入处理回调函数
  216. void specialKey(int key,int x,int y){
  217.     if(key==GLUT_KEY_UP){
  218.         xRotAngle-=5.0f;
  219.     }
  220.     else if(key==GLUT_KEY_DOWN){
  221.         xRotAngle+=5.0f;
  222.     }
  223.     else if(key==GLUT_KEY_LEFT){
  224.         yRotAngle-=5.0f;
  225.     }
  226.     else if(key==GLUT_KEY_RIGHT){
  227.         yRotAngle+=5.0f;
  228.     }
  229.     //重新绘制
  230.     glutPostRedisplay();
  231. }
  232. void timerFunc(int value)
  233. {
  234.     glutPostRedisplay();
  235.     glutTimerFunc(100, timerFunc, 1);
  236. }
  237. int main(int argc, char* argv[])
  238. {
  239.     //菜单
  240.     GLint iMainMenu;
  241.     GLint iCoordinateaxisMenu;
  242.     GLint iOrthoOrPerspectMenu;
  243.     //初始化glut
  244.     glutInit(&argc,argv);
  245.     //使用双缓冲区、深度缓冲区。
  246.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
  247.     //获取系统的宽像素
  248.     SCREEN_WIDTH=glutGet(GLUT_SCREEN_WIDTH);
  249.     //获取系统的高像素
  250.     SCREEN_HEIGHT=glutGet(GLUT_SCREEN_HEIGHT);
  251.     //创建窗口,窗口名字为OpenGL Solar Demo
  252.     glutCreateWindow(“OpenGL Solar Demo”);
  253.     //设置窗口大小
  254.     glutReshapeWindow(windowWidth,windowHeight);
  255.     //窗口居中显示
  256.     glutPositionWindow((SCREEN_WIDTH-windowWidth)/2,(SCREEN_HEIGHT-windowHeight)/2);
  257.     //窗口大小变化时的处理函数
  258.     glutReshapeFunc(changSize);
  259.     //设置显示回调函数
  260.     glutDisplayFunc(renderScreen);
  261.     //设置按键输入处理回调函数
  262.     glutSpecialFunc(specialKey);
  263.     //菜单回调函数
  264.     iCoordinateaxisMenu=glutCreateMenu(processMenu);
  265.     //添加菜单
  266.     glutAddMenuEntry(“Display coordinate axis”,1);
  267.     glutAddMenuEntry(“Don’t dispaly coordinate axis”,2);
  268.     iOrthoOrPerspectMenu=glutCreateMenu(processMenu);
  269.     glutAddMenuEntry(“Ortho”,3);
  270.     glutAddMenuEntry(“Perspect”,4);
  271.     iMainMenu=glutCreateMenu(processMenu);
  272.     glutAddSubMenu(“Display or hide coordinate axis”,iCoordinateaxisMenu);
  273.     glutAddSubMenu(“Ortho Or Perspect”,iOrthoOrPerspectMenu);
  274.     //将菜单榜定到鼠标右键上
  275.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  276.     glutTimerFunc(250,timerFunc, 1);
  277.     //设置全局渲染参数
  278.     setupRederingState();
  279.     glutMainLoop();
  280.     return 0;
  281. }

标签