OpenGL在Windows和Ubuntu下


感觉OpenGL开窍啦~

移动、缩放、旋转效果
代码如下,略有不同: Windows:
  1. #include "StdAfx.h"   
  2. #include "windows.h"   
  3. #include "GL\gl.h"   
  4. #include "GL\glu.h"   
  5. #include "GL\glut.h"   
  6. #pragma comment( lib, "glu32.lib" )    
  7. #pragma comment( lib, "OpenGL32.lib" )    
  8. void myinit(void);   
  9. void myReshape(int w,int h);   
  10. void display();   
  11. static int color = 0;   
  12. static float size = 1.0;   
  13. //set current color according to "color"   
  14. void setColor()   
  15. {   
  16.     //Change current color   
  17.     switch(color){   
  18.     case 0:   
  19.         glColor3f(1.0,1.0,0);   
  20.         break;   
  21.     case 1:   
  22.         glColor3f(0,1.0,1.0);   
  23.         break;   
  24.     case 2:   
  25.         glColor3f(1.0,0,1.0);   
  26.         break;   
  27.     default:   
  28.         glColor3f(1.0,1.0,1.0);   
  29.     }   
  30. }   
  31. //set current color according to next "color"   
  32. void nextColor()   
  33. {   
  34.     if (color < 2)   
  35.         color += 1;   
  36.     else  
  37.         color = 0;   
  38.     setColor();   
  39. }   
  40. void init(void)   
  41. {   
  42.     glClearColor(0.0, 0.0 ,0.0, 0.0);   
  43.     glClear(GL_COLOR_BUFFER_BIT);   
  44. }   
  45. void display()   
  46. {   
  47.     init();   
  48.     //show coordinate lines   
  49.     glColor3f(1.0,1.0,1.0);   
  50.     glBegin(GL_LINES);   
  51.         //x   
  52.         glColor3f(1.0f,0,0);   
  53.         glVertex3f(0.9f,0,0);   
  54.         glVertex3f(-0.9f,0,0);   
  55.         //y   
  56.         glColor3f(0,1.0f,0);   
  57.         glVertex3f(0,0.9f,0);   
  58.         glVertex3f(0,-0.9f,0);   
  59.         //z   
  60.         glColor3f(0,0,1.0f);   
  61.         glVertex3f(0,0,0.9f);   
  62.         glVertex3f(0,0,-0.9f);   
  63.     glEnd();   
  64.     //show image   
  65.     setColor();   
  66.     glutWireCube(size);   
  67.     glFlush();   
  68. }   
  69. //Call when resize   
  70. void reshape(int w,int h)   
  71. {   
  72.      glViewport(0,0,w,h);   
  73. }   
  74. //Use Space to change color   
  75. void CALLBACK changeColor()   
  76. {   
  77.     nextColor();   
  78.     display();   
  79. }   
  80. //Use Right to move right   
  81. void CALLBACK moveRight()   
  82. {           
  83.     glTranslatef(0.05f, 0.0, 0.0);    
  84.     display();   
  85. }   
  86. //Use Left to move left   
  87. void CALLBACK moveLeft()   
  88. {       
  89.     glTranslatef(-0.05f, 0.0, 0.0);    
  90.     display();   
  91. }   
  92. //Use Up to move up   
  93. void CALLBACK moveUp()   
  94. {           
  95.     glTranslatef(0.0, 0.05f, 0.0);    
  96.     display();   
  97. }   
  98. //Use Down to move down   
  99. void CALLBACK moveDown()   
  100. {       
  101.     glTranslatef(0.0, -0.05f, 0.0);    
  102.     display();   
  103. }   
  104. //Use s to make bigger   
  105. void CALLBACK bigger()   
  106. {       
  107.     size /= 0.9f;   
  108.     display();   
  109. }   
  110. //Use a to make smaller   
  111. void CALLBACK smaller()   
  112. {       
  113.     size *= 0.9f;   
  114.     display();   
  115. }   
  116. //Use q to rotate by x   
  117. void CALLBACK rotateX()   
  118. {   
  119.     glRotatef(5.0, 1.0, 0, 0);   
  120.     display();   
  121. }   
  122. //Use w to rotate by y   
  123. void CALLBACK rotateY()   
  124. {   
  125.     glRotatef(5.0, 0, 1.0, 0);   
  126.     display();   
  127. }   
  128. //Use e to rotate by z   
  129. void CALLBACK rotateZ()   
  130. {   
  131.     glRotatef(5.0, 0, 0, 1.0);   
  132.     display();   
  133. }   
  134. void keyboard(unsigned char key, int x, int y)   
  135. {   
  136.     switch (key)   
  137.     {   
  138.     case 27: // ESCAPE key   
  139.         exit (0);   
  140.         break;   
  141.     case 's':   
  142.         bigger();   
  143.         break;   
  144.     case 'a':   
  145.         smaller();   
  146.         break;   
  147.     case 'q':   
  148.         rotateX();   
  149.         break;   
  150.     case 'w':   
  151.         rotateY();   
  152.         break;   
  153.     case 'e':   
  154.         rotateZ();   
  155.         break;   
  156.     case ' ':   
  157.         changeColor();   
  158.         break;   
  159.     case 'j':   
  160.         moveLeft();   
  161.         break;   
  162.     case 'k':   
  163.         moveDown();   
  164.         break;   
  165.     case 'l':   
  166.          moveRight();   
  167.          break;   
  168.     }   
  169. }   
  170. void special(int key, int x, int y)   
  171. {   
  172.     switch (key)   
  173.     {   
  174.     case GLUT_KEY_LEFT:   
  175.         moveLeft();   
  176.         break;   
  177.     case GLUT_KEY_RIGHT:   
  178.         moveRight();   
  179.         break;   
  180.     case GLUT_KEY_UP:   
  181.         moveUp();   
  182.         break;   
  183.     case GLUT_KEY_DOWN:   
  184.         moveDown();   
  185.         break;   
  186.     }   
  187. }   
  188. void main(int argc, char** argv)   
  189. {   
  190.     //Init a window   
  191.     glutInit(&argc, argv);   
  192.     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);   
  193.     glutInitWindowSize (500, 500);    
  194.     glutInitWindowPosition (100, 100);   
  195.     glutCreateWindow("Test");   
  196.     //clear window and set init color   
  197.     setColor();   
  198.     //listener of keyboard   
  199.     glutKeyboardFunc(keyboard);   
  200.     glutSpecialFunc(special);   
  201.     init();   
  202.     glutDisplayFunc(display);    
  203.     glutReshapeFunc(reshape);   
  204.     glutMainLoop();   
  205. }  
Linux:
  1. #include "GL/gl.h"   
  2. #include "GL/glu.h"   
  3. #include "GL/glut.h"   
  4. #include <iostream>   
  5. using namespace std;   
  6. void myinit(void);   
  7. void myReshape(int w,int h);   
  8. void display();   
  9. static int color = 0;   
  10. static float size = 1.0;   
  11. //set current color according to "color"   
  12. void setColor()   
  13. {   
  14.     //Change current color   
  15.     switch(color){   
  16.     case 0:   
  17.         glColor3f(1.0,1.0,0);   
  18.         break;   
  19.     case 1:   
  20.         glColor3f(0,1.0,1.0);   
  21.         break;   
  22.     case 2:   
  23.         glColor3f(1.0,0,1.0);   
  24.         break;   
  25.     default:   
  26.         glColor3f(1.0,1.0,1.0);   
  27.     }   
  28. }   
  29. //set current color according to next "color"   
  30. void nextColor()   
  31. {   
  32.     if (color < 2)   
  33.         color += 1;   
  34.     else  
  35.         color = 0;   
  36.     setColor();   
  37. }   
  38. void init(void)   
  39. {   
  40.     glClearColor(0.0, 0.0 ,0.0, 0.0);   
  41.     glClear(GL_COLOR_BUFFER_BIT);   
  42. }   
  43. void display()   
  44. {   
  45.     init();   
  46.     //show coordinate lines   
  47.     glColor3f(1.0,1.0,1.0);   
  48.     glBegin(GL_LINES);   
  49.         //x   
  50.         glColor3f(1.0f,0,0);   
  51.         glVertex3f(0.9f,0,0);   
  52.         glVertex3f(-0.9f,0,0);   
  53.         //y   
  54.         glColor3f(0,1.0f,0);   
  55.         glVertex3f(0,0.9f,0);   
  56.         glVertex3f(0,-0.9f,0);   
  57.         //z   
  58.         glColor3f(0,0,1.0f);   
  59.         glVertex3f(0,0,0.9f);   
  60.         glVertex3f(0,0,-0.9f);   
  61.     glEnd();   
  62.     //show image   
  63.     setColor();   
  64.     glutWireCube(size);   
  65.     glFlush();   
  66. }   
  67. //Call when resize   
  68. void reshape(int w,int h)   
  69. {   
  70.      glViewport(0,0,w,h);   
  71. }   
  72. //Use Space to change color   
  73. void changeColor()   
  74. {   
  75.     nextColor();   
  76.     display();   
  77. }   
  78. //Use Right to move right   
  79. void moveRight()   
  80. {           
  81.     glTranslatef(0.05f, 0.0, 0.0);    
  82.     display();   
  83. }   
  84. //Use Left to move left   
  85. void moveLeft()   
  86. {       
  87.     glTranslatef(-0.05f, 0.0, 0.0);    
  88.     display();   
  89. }   
  90. //Use Up to move up   
  91. void moveUp()   
  92. {           
  93.     glTranslatef(0.0, 0.05f, 0.0);    
  94.     display();   
  95. }   
  96. //Use Down to move down   
  97. void moveDown()   
  98. {       
  99.     glTranslatef(0.0, -0.05f, 0.0);    
  100.     display();   
  101. }   
  102. //Use s to make bigger   
  103. void bigger()   
  104. {       
  105.     size /= 0.9f;   
  106.     display();   
  107. }   
  108. //Use a to make smaller   
  109. void smaller()   
  110. {       
  111.     size *= 0.9f;   
  112.     display();   
  113. }   
  114. //Use q to rotate by x   
  115. void rotateX()   
  116. {   
  117.     glRotatef(5.0, 1.0, 0, 0);   
  118.     display();   
  119. }   
  120. //Use w to rotate by y   
  121. void rotateY()   
  122. {   
  123.     glRotatef(5.0, 0, 1.0, 0);   
  124.     display();   
  125. }   
  126. //Use e to rotate by z   
  127. void rotateZ()   
  128. {   
  129.     glRotatef(5.0, 0, 0, 1.0);   
  130.     display();   
  131. }   
  132. void keyboard(unsigned char key, int x, int y)   
  133. {   
  134.     switch (key)   
  135.     {   
  136.     case 27: // ESCAPE key   
  137.      exit (0);   
  138.      break;   
  139.     case 's':   
  140.      bigger();   
  141.      break;   
  142.     case 'a':   
  143.      smaller();   
  144.      break;   
  145.     case 'q':   
  146.      rotateX();   
  147.      break;   
  148.     case 'w':   
  149.      rotateY();   
  150.      break;   
  151.     case 'e':   
  152.      rotateZ();   
  153.      break;   
  154.     case ' ':   
  155.      changeColor();   
  156.      break;   
  157.     case 'j':   
  158.      moveLeft();   
  159.      break;   
  160.     case 'k':   
  161.      moveDown();   
  162.      break;   
  163.     case 'l':   
  164.      moveRight();   
  165.      break;   
  166.     case 'i':   
  167.      moveUp();   
  168.      break;   
  169.     }   
  170. }   
  171. void special(int key, int x, int y)   
  172. {   
  173.     switch (key)   
  174.     {   
  175.     case GLUT_KEY_LEFT:   
  176.         moveLeft();   
  177.         break;   
  178.     case GLUT_KEY_RIGHT:   
  179.         moveRight();   
  180.         break;   
  181.     case GLUT_KEY_UP:   
  182.         moveUp();   
  183.         break;   
  184.     case GLUT_KEY_DOWN:   
  185.         moveDown();   
  186.         break;   
  187.     }   
  188. }   
  189. int main(int argc, char** argv)   
  190. {   
  191.     //Init a window   
  192.     glutInit(&argc, argv);   
  193.     glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);   
  194.     glutInitWindowSize (600, 600);    
  195.     glutInitWindowPosition (100, 100);   
  196.     glutCreateWindow("Test");   
  197.     //clear window and set init color   
  198.     setColor();   
  199.     //listener of keyboard   
  200.     glutKeyboardFunc(keyboard);   
  201.     glutSpecialFunc(special);   
  202.     init();   
  203.     glutDisplayFunc(display);    
  204.     glutReshapeFunc(reshape);   
  205.     glutMainLoop();   
  206.     return 0;   
  207. }  
编译: $ g++ -IGL -lglut test2.cpp -o test.exe 就生成了test.exe $  ./test.exe 显示: OpenGL Warning: XGetVisualInfo returned 0 visuals for 0xa082b78 OpenGL Warning: Retry with 0x8002 returned 0 visuals Segmentation fault 网上搜了一下,得到解决方案 $ LIBGL_ALWAYS_INDIRECT=1 ./test.exe 运行结果: 按qwe进行xyz轴旋转,as放大缩小,上下左右移动,space换色,得到以下效果——

相关内容