Advertisement
Guest User

22333

a guest
Apr 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <glut.h>
  3.  
  4. int flag=0;
  5. //GLfloat vertices[][3]={{-1.0,-1.0,1.0},{-1.0,1.0,1.0},{1.0,1.0,1.0},{1.0,-1.0,1.0},{-1.0,-1.0,-1.0},
  6. //{-1.0,1.0,-1.0},{1.0,1.0,-1.0},{1.0,-1.0,-1.0}};
  7. GLfloat vertices[][3]={{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,-1.0},{-1.0,1.0,-1.0},{-1.0,-1.0,1.0},{1.0,-1.0,1.0},{1.0,1.0,1.0},{-1.0,1.0,1.0}};
  8. GLfloat colors[][3]={{1.0,0.0,0.0},{0.0,1.0,1.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}, {1.0,0.0,1.0},{0.0,1.0,1.0},{1.0,1.0,1.0}};
  9.  
  10. static GLfloat theta[]={0.0,0.0,0.0};
  11. static GLint axis=2;
  12.  
  13. void reshape(int w,int h);
  14. void init();
  15. void display();
  16. void mouse(int btn,int state,int x,int y);
  17. void polygon(int a,int b,int c,int d);
  18. void colorcube(void);
  19. void spinCube();
  20.  
  21. int APIENTRY
  22. _tWinMain(HINSTANCE hInstance,
  23. HINSTANCE hPrevInstance,
  24. LPTSTR lpCmdLine,
  25. int nCmdShow)
  26. {
  27. UNREFERENCED_PARAMETER(hPrevInstance);
  28. UNREFERENCED_PARAMETER(lpCmdLine);
  29. char *argv[] = {"hello ", " "};
  30. int argc = 2; // must/should match the number of strings in argv
  31. glutInit(&argc, argv); //初始化GLUT库;
  32. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB|GLUT_DEPTH);
  33. glutInitWindowSize(800, 800);
  34. glutCreateWindow("colorcube"); //创建窗口,标题为“…”;
  35. init();
  36. glutReshapeFunc(reshape);
  37. glutDisplayFunc(display); //用于绘制当前窗口;
  38. glutIdleFunc(spinCube);
  39. glutMouseFunc(mouse);
  40. glutMainLoop(); //表示开始运行程序,用于程序的结尾;
  41. return 0;
  42. }
  43. void display()
  44. {
  45. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  46. glLoadIdentity();
  47. gluLookAt(3,3,3,0,0,0,0,1,0);
  48. glRotatef(theta[0],1.0,0.0,0.0);
  49. glRotatef(theta[1],0.0,1.0,0.0);
  50. glRotatef(theta[2],0.0,0.0,1.0);
  51. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  52. flag=0;
  53. colorcube(); //绘制彩色立方体
  54. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  55. flag=1;
  56. colorcube(); //绘制彩色立方体
  57. glutSwapBuffers();
  58. }
  59. void init()
  60. {
  61. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  62. glEnable(GL_DEPTH_TEST);
  63. glLineWidth(3);
  64. }
  65. void polygon(int a,int b,int c,int d)
  66. { /* draw a polygon via list of vertices */
  67.  
  68. if (flag==0) {
  69. glBegin(GL_POLYGON);
  70. glColor3fv(colors[a]);
  71. glVertex3fv(vertices[a]);
  72. glColor3fv(colors[b]);
  73. glVertex3fv(vertices[b]);
  74. glColor3fv(colors[c]);
  75. glVertex3fv(vertices[c]);
  76. glColor3fv(colors[d]);
  77. glVertex3fv(vertices[d]);
  78. glEnd();
  79. }
  80. else
  81. {
  82. glColor3f(0,0,0);
  83. glBegin(GL_POLYGON);
  84. glVertex3fv(vertices[a]);
  85. glVertex3fv(vertices[b]);
  86. glVertex3fv(vertices[c]);
  87. glVertex3fv(vertices[d]);
  88. glEnd();
  89. }
  90. }
  91. void colorcube(void)
  92. { /* map vertices to faces */
  93. polygon(0,3,2,1);
  94. polygon(2,3,7,6);
  95. polygon(0,4,7,3);
  96. polygon(1,2,6,5);
  97. polygon(4,5,6,7);
  98. polygon(0,1,5,4);
  99. }
  100. void mouse(int btn, int state,int x, int y)
  101. {
  102. if (btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) axis=0;
  103. if (btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) axis=1;
  104. if (btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) axis=2;
  105. }
  106. void reshape(int w,int h)
  107. {
  108. glViewport(0,0,w,h);
  109. glMatrixMode(GL_PROJECTION);
  110. glLoadIdentity();
  111. //定义正交投影观察体
  112. if (w<=h)
  113. glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,1.0,20.0);
  114. else
  115. glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,1.0,20.0);
  116. // gluPerspective(120,w/h,1,60);//定义透视投影投影观察体
  117. glMatrixMode(GL_MODELVIEW);
  118. glLoadIdentity();
  119. }
  120. void spinCube()
  121. {
  122. theta[axis]+=0.1;
  123. if (theta[axis]>360.0) theta[axis]-=360.0;
  124. glutPostRedisplay();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement