Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. /*
  2. Computer Graphics
  3.  
  4. Cone with Vertex Arrays.
  5. */
  6.  
  7. #include <math.h>
  8. #include <stdio.h>
  9.  
  10. #ifdef __APPLE__
  11. #include <OpenGL/gl.h>
  12. #include <OpenGL/glu.h>
  13. #include <GLUT/glut.h>
  14. #else
  15. #include "freeglut.h"
  16. #endif
  17.  
  18. #include "cCylinder.h"
  19. #include "cCone.h"
  20. #include "cSphere.h"
  21.  
  22.  
  23. int width, height;
  24. float deltaX, deltaY;
  25. float xSpotDir, ySpotDir, zOffset, spotCutOff;
  26.  
  27. Cylinder* base;
  28. Cylinder* base2;
  29. Cylinder* base3; //soporte inf derecho
  30. Cylinder* base4; //soporte inf izq
  31. Cylinder* base5; //soporte horizontal
  32. Cylinder* base6; //SOPORTE SUPERIOR
  33. Cone* base7; //Cono
  34. Sphere*base8; //Foco
  35.  
  36.  
  37.  
  38.  
  39. GLdouble fovy;
  40.  
  41.  
  42. void display(void)
  43. {
  44.  
  45.  
  46. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  47. glLoadIdentity();
  48.  
  49. glRotatef(deltaY, 1.0, 0.0, 0.0);
  50. glRotatef(deltaX, 0.0, 1.0, 0.0);
  51.  
  52. base->draw(); // base principal
  53. glPushMatrix();
  54. {
  55.  
  56. glTranslatef(0.0, 1.5, 0.0);
  57. base2->draw(); //base columna
  58.  
  59.  
  60. glTranslatef(0.6, 0.6, 0.0);
  61. base3->draw();//soporte gemelo derecho
  62.  
  63.  
  64. glTranslatef(-1.2, 0.0, 0.0);
  65. base4->draw();//soporte gemelo izquierdo
  66.  
  67. //glRotatef(45, 1, 0, 0); // Esto es lo que se va a ANIMAR
  68. glPushMatrix();//grupo general
  69. {
  70. glTranslatef(0.6, 1.2, 0.0);
  71. glRotatef(90, 0, 0, 1);
  72. base5->draw();//soporte horizontal
  73.  
  74. glPushMatrix();
  75. {
  76. glTranslatef(1.5, 0.0, 0.0);
  77. glRotatef(-90, 0, 0, 1);
  78. base6->draw();//soporte superior
  79.  
  80.  
  81. glTranslatef(0.0, 2.0, 0.0);
  82. glRotatef(-90, 1, 0, 0);
  83. base7->draw();//cono
  84. glRotatef(90, 1, 0, 0);
  85.  
  86. glTranslatef(0.0, 0.0, 0.0);
  87. base8->draw();//esfera a.k.a Foco
  88. }
  89. glPopMatrix();
  90. }
  91. glPopMatrix();
  92.  
  93. }
  94. glPopMatrix();
  95.  
  96.  
  97.  
  98.  
  99. glPushMatrix(); //Luz para la esfera a.k.a Foco
  100. {
  101. // glRotatef(-90, 1, 0 , 0);
  102. //glRotated(-40, 0, 1, 0);
  103. //set the light position
  104. //GLfloat qaLightPosition[] = { 0.0, 10.0, 0.0, 1.0 };
  105. //glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);
  106.  
  107. }glPopMatrix();
  108.  
  109. glutSwapBuffers();
  110. }
  111.  
  112.  
  113. void idle( void )
  114. {
  115. glutPostRedisplay();
  116. }
  117.  
  118. void init( void )
  119. {
  120. fovy = 45.0;
  121.  
  122. glEnable( GL_DEPTH_TEST );
  123. glShadeModel( GL_FLAT );
  124. glClearColor( 0.0, 0.0, 0.0, 1.0 );
  125.  
  126. float pos0[] = { 0.0f, 0.0f, 0.0f };
  127. float size0[] = { 4.0f, 0.2f, 4.0f };
  128.  
  129. float pos1[] = { 0.0f, 0.8f, 0.0f };
  130. float size1[] = { 1.0f, 3.0f, 1.0f };
  131.  
  132. float pos2[] = { 0.5f, 2.3f, 0.0f };
  133. float size2[] = { 0.3f, 4.0f, 0.3f };
  134.  
  135. float pos3[] = { -0.5f, 2.3f, 0.0f };
  136. float size3[] = { 0.3f, 4.0f, 0.3f };
  137.  
  138. float pos4[] = { 3.5f, 0.0f, 0.0f };
  139. float size4[] = { 0.3f, 3.0f, 0.3f };
  140.  
  141. float pos5[] = { 0.0f, 0.0f, 0.0f };
  142. float size5[] = { 0.3f, 5.0f, 0.3f };
  143.  
  144. float pos6[] = { 0.0f, -0.5f, 1.0f };
  145. float size6[] = { 4.0f, 4.2f, 4.0f };
  146.  
  147. float pos7[] = { 0.0f, 1.0f, 1.5f };
  148. float size7[] = { 1.5f, 1.5f, 1.5f };
  149.  
  150. base = new Cylinder(pos0, size0);
  151. base2 = new Cylinder(pos1, size1);
  152. base3 = new Cylinder(pos2, size2);
  153. base4 = new Cylinder(pos3, size3);
  154. base5 = new Cylinder(pos4, size4);
  155. base6 = new Cylinder(pos5, size5);
  156. base7 = new Cone(pos6, size6);
  157. base8 = new Sphere(pos7, size7);
  158.  
  159. /*/set for light
  160. glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
  161. glEnable(GL_LIGHTING);
  162. glEnable(GL_LIGHT0);
  163. glEnable(GL_NORMALIZE);
  164.  
  165. //color and intensity set for lighting
  166. GLfloat noAmbientLight[] = {0.0,0.0,0.2,1.0};
  167. GLfloat DiffuseLight[] = { 0.8,0.8,0.8,1.0 };
  168. GLfloat SpecularLight[] = { 1.0,1.0,1.0,1.0 };
  169. glLightfv(GL_LIGHT0, GL_AMBIENT, noAmbientLight);
  170. glLightfv(GL_LIGHT0, GL_DIFFUSE, DiffuseLight);
  171. glLightfv(GL_LIGHT0, GL_SPECULAR, SpecularLight);
  172.  
  173. /*spot properties
  174. updateSpot();
  175.  
  176. //exponent propertie defines the concentration of the light
  177. glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 15.0f);
  178.  
  179. //light attenuation (default values used here : no attenuation with the distance)
  180. glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 1.0f);
  181. glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.0f);
  182. glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.0f);*/
  183. }
  184.  
  185.  
  186.  
  187.  
  188. /*void updateSpot(void)
  189. {
  190. float direction[] = {xSpotDir, ySpotDir, zOffset};
  191. //spot direction
  192. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction);
  193. //angle of the cone light emitted by the spot : value between 0 to 180
  194. glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, spotCutOff);
  195. }*/
  196.  
  197. void reshape( int w, int h )
  198. {
  199. width = w;
  200. height = h;
  201. glViewport( 0, 0, w, h );
  202. glMatrixMode( GL_PROJECTION );
  203. glLoadIdentity();
  204.  
  205. gluPerspective( fovy, w / h * 1.0, 0.01, 20 );
  206. glTranslatef( 0, -5, -15 );
  207. glMatrixMode( GL_MODELVIEW );
  208. glLoadIdentity();
  209. }
  210.  
  211. void keys( unsigned char key, int x, int y )
  212. {
  213. switch( key )
  214. {
  215. case 'r': case 'R':
  216. glClearColor( 1.0, 0.0, 0.0, 1.0 );
  217. break;
  218. case 'b': case 'B':
  219. glClearColor( 0.0, 0.0, 1.0, 1.0 );
  220. break;
  221. case '0':
  222. glClearColor( 0.0, 0.0, 0.0, 1.0 );
  223. break;
  224. case 27:
  225. //delete cone0;
  226. //delete cone1;
  227. //delete cone2;
  228. //exit( 0 );
  229. default:
  230. break;
  231. }
  232. }
  233.  
  234. void mouse( int button, int state, int x, int y )
  235. {
  236.  
  237. }
  238.  
  239. void mouseWheel( int button, int dir, int x, int y )
  240. {
  241. if( dir > 0 ) // ZOOM_IN
  242. {
  243. fovy -= 1.0;
  244. if( fovy < 10.0 )
  245. {
  246. fovy = 10.0;
  247. }
  248. glMatrixMode( GL_PROJECTION );
  249. glLoadIdentity();
  250. gluPerspective( fovy, width / height * 1.0, 0.01, 20 );
  251. glTranslatef( 0, 0, -10 );
  252. glMatrixMode( GL_MODELVIEW );
  253. glLoadIdentity();
  254. }
  255. else // ZOOM_OUT
  256. {
  257. fovy += 1.0;
  258. if( fovy > 70.0 )
  259. {
  260. fovy = 70.0;
  261. }
  262. glMatrixMode( GL_PROJECTION );
  263. glLoadIdentity();
  264. gluPerspective( fovy, width / height * 1.0, 0.01, 20 );
  265. glTranslatef( 0, 0, -10 );
  266. glMatrixMode( GL_MODELVIEW );
  267. glLoadIdentity();
  268. }
  269. }
  270.  
  271. void move( int x, int y )
  272. {
  273. static int lastX = 0, lastY = 0;
  274. deltaX += lastX - x;
  275. deltaY += lastY - y;
  276. lastX = x;
  277. lastY = y;
  278. }
  279.  
  280. int main( int argc, char** argv )
  281. {
  282. glutInit( &argc, argv );
  283. glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
  284. glutInitWindowSize( 512, 512 );
  285. glutInitWindowPosition( 600, 100 );
  286. glutCreateWindow( "Luxo" );
  287.  
  288. glutReshapeFunc( reshape );
  289. glutKeyboardFunc( keys );
  290. glutMotionFunc( move );
  291. //glutMouseFunc( mouse );
  292. //glutMouseWheelFunc( mouseWheel );
  293.  
  294. init();
  295. //updateSpot();
  296. glutDisplayFunc( display );
  297. glutIdleFunc( idle );
  298. glutMainLoop();
  299.  
  300. return 0;
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement