Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. #include <windows.h>
  2. #include <GL/glew.h>
  3. #include <GL/gl.h>
  4. #include <math.h>
  5. #include <GLFW/glfw3.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <iostream>
  9.  
  10. #define X .525731112119133606
  11. #define Z .850650808352039932
  12. #define Y 0.0
  13. #define PI 3.1415926535898
  14. #define CIRCLE_STEP 5000
  15.  
  16. using namespace std;
  17.  
  18. // Open an OpenGL window
  19. GLFWwindow* window;
  20. int keyboard = 0, itr;
  21. bool big_Sphere = true, sphereWithNormalV = false, animation = false;
  22. GLdouble angle = 0;
  23.  
  24. /****Step 1: define vertices in (x, y, z) form****/
  25.  
  26. // Coordinates to draw a Icosahedron
  27. GLfloat icoVertices[12][3] = {
  28. {-X, Y, Z}, {X, Y, Z}, {-X, Y, -Z}, {X, Y, -Z},
  29. {Y, Z, X}, {Y, Z, -X}, {Y, -Z, X}, {Y, -Z, -X},
  30. {Z, X, Y}, {-Z, X, Y}, {Z, -X, Y}, {-Z, -X, Y}
  31. };
  32.  
  33. // Coordinates to draw a Pyramid
  34. GLfloat pyVertices[4][3] = {
  35. {0.0f, 1.0f, 0.0f},
  36. {1.0f, 0.0f, 0.0f},
  37. {-1.0f, 0.0f, 0.0f},
  38. {0.0f, 0.0f, -1.0f}
  39. };
  40.  
  41. static GLuint icoIndices[20][3] = {
  42. {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
  43. {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
  44. {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
  45. {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
  46.  
  47. static GLuint pyIndices[4][3] = {
  48. {0,1,2}, {0,1,3}, {0,2,3}, {1,2,3}
  49. };
  50. /************************/
  51.  
  52. void normalize3f(float v[3]) {
  53. GLfloat d = sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
  54. if (d == 0.0) {
  55. fprintf(stderr, "zero length vector");
  56. return;
  57. }
  58. v[0] /= d; v[1] /= d; v[2] /= d;
  59. }
  60.  
  61. void sphereNormalV(GLfloat p1[3], GLfloat p2[3], GLfloat p3[3])
  62. {
  63. glBegin(GL_LINES);
  64. glVertex3f(p1[0] *1.5,p1[1] *1.5, p1[2] *1.5);
  65. glVertex3fv(p1);
  66. glEnd();
  67.  
  68. glBegin(GL_LINES);
  69. glVertex3f(p2[0] *1.1,p2[1] *1.1, p2[2] *1.1);
  70. glVertex3fv(p2);
  71. glEnd();
  72.  
  73. glBegin(GL_LINES);
  74. glVertex3f(p3[0] *1.1,p3[1] *1.1, p3[2] *1.1);
  75. glVertex3fv(p3);
  76. glEnd();
  77. }
  78.  
  79. void drawtriangle(float *v1, float *v2, float *v3)
  80. {
  81. glBegin(GL_LINE_LOOP);
  82. //glNormal3fv(v1);
  83. glVertex3fv(v1);
  84. //glNormal3fv(v2);
  85. glVertex3fv(v2);
  86. //glNormal3fv(v3);
  87. glVertex3fv(v3);
  88. glEnd();
  89. }
  90.  
  91. void subdivide(GLfloat *v1, GLfloat *v2, GLfloat *v3, long depth)
  92. {
  93. GLfloat v12[3], v23[3], v31[3];
  94. GLint i;
  95.  
  96. if (depth == 0){
  97. drawtriangle(v1, v2, v3);
  98. if (sphereWithNormalV == true){
  99. sphereNormalV(v1, v2, v3);
  100. }
  101. return;
  102. }
  103. for (i = 0; i < 3; i++) {
  104. v12[i] = v1[i]+v2[i];
  105. v23[i] = v2[i]+v3[i];
  106. v31[i] = v3[i]+v1[i];
  107. }
  108.  
  109. normalize3f(v12);
  110. normalize3f(v23);
  111. normalize3f(v31);
  112. subdivide(v1, v12, v31, depth-1);
  113. subdivide(v2, v23, v12, depth-1);
  114. subdivide(v3, v31, v23, depth-1);
  115. subdivide(v12, v23, v31, depth-1);
  116. }
  117.  
  118. void drawSphere(GLfloat x, GLfloat y, GLfloat z){
  119. glLoadIdentity();
  120. glPushMatrix();
  121. if (big_Sphere == true){
  122. glScaled(0.4, 0.55, 0.4);
  123. }else{
  124. glScaled(0.13, 0.18, 0.13);
  125. }
  126.  
  127. if (animation){
  128. glTranslatef(x, y, z);
  129. }
  130.  
  131. glBegin(GL_LINE_LOOP);
  132. for (int i = 0; i < 20; i++) {
  133. subdivide(&icoVertices[icoIndices[i][0]][0], &icoVertices[icoIndices[i][1]][0], &icoVertices[icoIndices[i][2]][0], 3);
  134. }
  135. glEnd();
  136. glPopMatrix();
  137. }
  138.  
  139. void drawPyramid(){//(GLfloat x, GLfloat y, GLfloat z){
  140. glLoadIdentity();
  141. glPushMatrix();
  142. glScaled(0.13, 0.18, 0.13);
  143. glBegin(GL_LINE_LOOP);
  144. for (int i = 0; i < 4; i++){
  145. glColor3f(1.0f, 0.0f, 0.0f);
  146. glVertex3fv(pyVertices[pyIndices[i][0]]);
  147. glColor3f(0.0f, 1.0f, 0.0f);
  148. glVertex3fv(pyVertices[pyIndices[i][1]]);
  149. glColor3f(0.0f, 0.0f, 1.0f);
  150. glVertex3fv(pyVertices[pyIndices[i][2]]);
  151. }
  152. glEnd();
  153. glPopMatrix();
  154. }
  155.  
  156. int getKeyPressed(){
  157. if (glfwGetKey(window, GLFW_KEY_A)){
  158. keyboard = GLFW_KEY_A;
  159. }
  160.  
  161. if (glfwGetKey(window, GLFW_KEY_B)){
  162. keyboard = GLFW_KEY_B;
  163. }
  164.  
  165. if (glfwGetKey(window, GLFW_KEY_C)){
  166. keyboard = GLFW_KEY_A;
  167. }
  168.  
  169. if (glfwGetKey(window, GLFW_KEY_D)){
  170. keyboard = GLFW_KEY_D;
  171. }
  172.  
  173. if (glfwGetKey(window, GLFW_KEY_E)){
  174. keyboard = GLFW_KEY_E;
  175. }
  176.  
  177. return keyboard;
  178. }
  179.  
  180. void controlSphere(bool _big_Sphere, bool _sphereNormalV, bool _animation){
  181. big_Sphere = _big_Sphere;
  182. sphereWithNormalV = _sphereNormalV;
  183. animation = _animation;
  184. }
  185.  
  186. void gluPerspective(double fovy,double aspect, double zNear, double zFar)
  187. {
  188. // Start in projection mode.
  189. glMatrixMode(GL_PROJECTION);
  190. glLoadIdentity();
  191. double xmin, xmax, ymin, ymax;
  192. ymax = zNear * tan(fovy * PI / 360.0);
  193. ymin = -ymax;
  194. xmin = ymin * aspect;
  195. xmax = ymax * aspect;
  196. glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
  197. }
  198.  
  199. int main( void ) {
  200. if (!glfwInit()){
  201. fprintf(stderr, "Failed to initialize GLFW.n");
  202. return -1;
  203. }
  204.  
  205. // Create a windowed mode window and its OpenGL context
  206. window = glfwCreateWindow(1100, 800, "Hello World", NULL, NULL);
  207. if (window == NULL) {
  208. fprintf(stderr, "glfw failed to create window.n");
  209. //glfwTerminate();
  210. return -1;
  211. }
  212. // Make the window's context current
  213. glfwMakeContextCurrent(window);
  214.  
  215. glewInit();
  216. if (glewInit() != GLEW_OK){
  217. fprintf(stderr, "Failed to initialize GLEW: %s.n", glewGetErrorString(glewInit()));
  218. return -1;
  219. }
  220. // 4x anti aliasing
  221. glfwWindowHint(GLFW_SAMPLES, 4);
  222.  
  223. /**Step 3: Main loop for OpenGL draw the shape**
  224. /* Main loop */
  225. int i = 0;
  226. GLfloat pos_X, pos_Y;
  227. glRotatef(60, 0.0f, 0.3f, 0.4f);
  228.  
  229. do{
  230.  
  231. //glMatrixMode(GL_MODELVIEW);
  232. glClear(GL_COLOR_BUFFER_BIT);
  233.  
  234. switch(getKeyPressed()){
  235. case 65:
  236. controlSphere(true, false, false);
  237. drawSphere(0, 0, 0);
  238. break;
  239. case 66:
  240. controlSphere(true, true, false);
  241. drawSphere(0, 0, 0);
  242. break;
  243. case 67:
  244. // drawPyramid();
  245. break;
  246. case 68:
  247. // drawing a Sphere moving in a circular path
  248. controlSphere(false, false, true);
  249. angle = 2*PI*i/CIRCLE_STEP;
  250. pos_X = cos(angle) * 4.5;
  251. pos_Y = sin(angle) * 4.5;
  252. drawSphere(pos_X, pos_Y, 0);
  253. i += 1;
  254. angle += 1;
  255. if (angle >= 360){
  256. angle = 0;
  257. }
  258.  
  259. // drawing a Pyramid rotate around its y axis
  260. drawPyramid();
  261. break;
  262. default:
  263. controlSphere(true, false, false);
  264. drawSphere(0, 0, 0);
  265. break;
  266. }
  267.  
  268. Sleep(1);
  269. // Swap front and back rendering buffers
  270. glfwSwapBuffers(window);
  271.  
  272. //Poll for and process events
  273. glfwPollEvents();
  274. } // check if the ESC key was pressed or the window was closed
  275. while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);
  276. /***********************************************/
  277.  
  278.  
  279. // Close window and terminate GLFW
  280. glfwDestroyWindow(window);
  281. glfwTerminate();
  282. // Exit program
  283. exit( EXIT_SUCCESS );
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement