Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Computer Graphics
- Cone with Vertex Arrays.
- */
- #include <math.h>
- #include <stdio.h>
- #ifdef __APPLE__
- #include <OpenGL/gl.h>
- #include <OpenGL/glu.h>
- #include <GLUT/glut.h>
- #else
- #include "freeglut.h"
- #endif
- #include "cCylinder.h"
- #include "cCone.h"
- #include "cSphere.h"
- int width, height;
- float deltaX, deltaY;
- float xSpotDir, ySpotDir, zOffset, spotCutOff;
- Cylinder* base;
- Cylinder* base2;
- Cylinder* base3; //soporte inf derecho
- Cylinder* base4; //soporte inf izq
- Cylinder* base5; //soporte horizontal
- Cylinder* base6; //SOPORTE SUPERIOR
- Cone* base7; //Cono
- Sphere*base8; //Foco
- GLdouble fovy;
- void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glRotatef(deltaY, 1.0, 0.0, 0.0);
- glRotatef(deltaX, 0.0, 1.0, 0.0);
- base->draw(); // base principal
- glPushMatrix();
- {
- glTranslatef(0.0, 1.5, 0.0);
- base2->draw(); //base columna
- glTranslatef(0.6, 0.6, 0.0);
- base3->draw();//soporte gemelo derecho
- glTranslatef(-1.2, 0.0, 0.0);
- base4->draw();//soporte gemelo izquierdo
- //glRotatef(45, 1, 0, 0); // Esto es lo que se va a ANIMAR
- glPushMatrix();//grupo general
- {
- glTranslatef(0.6, 1.2, 0.0);
- glRotatef(90, 0, 0, 1);
- base5->draw();//soporte horizontal
- glPushMatrix();
- {
- glTranslatef(1.5, 0.0, 0.0);
- glRotatef(-90, 0, 0, 1);
- base6->draw();//soporte superior
- glTranslatef(0.0, 2.0, 0.0);
- glRotatef(-90, 1, 0, 0);
- base7->draw();//cono
- glRotatef(90, 1, 0, 0);
- glTranslatef(0.0, 0.0, 0.0);
- base8->draw();//esfera a.k.a Foco
- }
- glPopMatrix();
- }
- glPopMatrix();
- }
- glPopMatrix();
- glPushMatrix(); //Luz para la esfera a.k.a Foco
- {
- // glRotatef(-90, 1, 0 , 0);
- //glRotated(-40, 0, 1, 0);
- //set the light position
- //GLfloat qaLightPosition[] = { 0.0, 10.0, 0.0, 1.0 };
- //glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);
- }glPopMatrix();
- glutSwapBuffers();
- }
- void idle( void )
- {
- glutPostRedisplay();
- }
- void init( void )
- {
- fovy = 45.0;
- glEnable( GL_DEPTH_TEST );
- glShadeModel( GL_FLAT );
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- float pos0[] = { 0.0f, 0.0f, 0.0f };
- float size0[] = { 4.0f, 0.2f, 4.0f };
- float pos1[] = { 0.0f, 0.8f, 0.0f };
- float size1[] = { 1.0f, 3.0f, 1.0f };
- float pos2[] = { 0.5f, 2.3f, 0.0f };
- float size2[] = { 0.3f, 4.0f, 0.3f };
- float pos3[] = { -0.5f, 2.3f, 0.0f };
- float size3[] = { 0.3f, 4.0f, 0.3f };
- float pos4[] = { 3.5f, 0.0f, 0.0f };
- float size4[] = { 0.3f, 3.0f, 0.3f };
- float pos5[] = { 0.0f, 0.0f, 0.0f };
- float size5[] = { 0.3f, 5.0f, 0.3f };
- float pos6[] = { 0.0f, -0.5f, 1.0f };
- float size6[] = { 4.0f, 4.2f, 4.0f };
- float pos7[] = { 0.0f, 1.0f, 1.5f };
- float size7[] = { 1.5f, 1.5f, 1.5f };
- base = new Cylinder(pos0, size0);
- base2 = new Cylinder(pos1, size1);
- base3 = new Cylinder(pos2, size2);
- base4 = new Cylinder(pos3, size3);
- base5 = new Cylinder(pos4, size4);
- base6 = new Cylinder(pos5, size5);
- base7 = new Cone(pos6, size6);
- base8 = new Sphere(pos7, size7);
- /*/set for light
- glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_NORMALIZE);
- //color and intensity set for lighting
- GLfloat noAmbientLight[] = {0.0,0.0,0.2,1.0};
- GLfloat DiffuseLight[] = { 0.8,0.8,0.8,1.0 };
- GLfloat SpecularLight[] = { 1.0,1.0,1.0,1.0 };
- glLightfv(GL_LIGHT0, GL_AMBIENT, noAmbientLight);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, DiffuseLight);
- glLightfv(GL_LIGHT0, GL_SPECULAR, SpecularLight);
- /*spot properties
- updateSpot();
- //exponent propertie defines the concentration of the light
- glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 15.0f);
- //light attenuation (default values used here : no attenuation with the distance)
- glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 1.0f);
- glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.0f);
- glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.0f);*/
- }
- /*void updateSpot(void)
- {
- float direction[] = {xSpotDir, ySpotDir, zOffset};
- //spot direction
- glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction);
- //angle of the cone light emitted by the spot : value between 0 to 180
- glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, spotCutOff);
- }*/
- void reshape( int w, int h )
- {
- width = w;
- height = h;
- glViewport( 0, 0, w, h );
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( fovy, w / h * 1.0, 0.01, 20 );
- glTranslatef( 0, -5, -15 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- }
- void keys( unsigned char key, int x, int y )
- {
- switch( key )
- {
- case 'r': case 'R':
- glClearColor( 1.0, 0.0, 0.0, 1.0 );
- break;
- case 'b': case 'B':
- glClearColor( 0.0, 0.0, 1.0, 1.0 );
- break;
- case '0':
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- break;
- case 27:
- //delete cone0;
- //delete cone1;
- //delete cone2;
- //exit( 0 );
- default:
- break;
- }
- }
- void mouse( int button, int state, int x, int y )
- {
- }
- void mouseWheel( int button, int dir, int x, int y )
- {
- if( dir > 0 ) // ZOOM_IN
- {
- fovy -= 1.0;
- if( fovy < 10.0 )
- {
- fovy = 10.0;
- }
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( fovy, width / height * 1.0, 0.01, 20 );
- glTranslatef( 0, 0, -10 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- }
- else // ZOOM_OUT
- {
- fovy += 1.0;
- if( fovy > 70.0 )
- {
- fovy = 70.0;
- }
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( fovy, width / height * 1.0, 0.01, 20 );
- glTranslatef( 0, 0, -10 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- }
- }
- void move( int x, int y )
- {
- static int lastX = 0, lastY = 0;
- deltaX += lastX - x;
- deltaY += lastY - y;
- lastX = x;
- lastY = y;
- }
- int main( int argc, char** argv )
- {
- glutInit( &argc, argv );
- glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
- glutInitWindowSize( 512, 512 );
- glutInitWindowPosition( 600, 100 );
- glutCreateWindow( "Luxo" );
- glutReshapeFunc( reshape );
- glutKeyboardFunc( keys );
- glutMotionFunc( move );
- //glutMouseFunc( mouse );
- //glutMouseWheelFunc( mouseWheel );
- init();
- //updateSpot();
- glutDisplayFunc( display );
- glutIdleFunc( idle );
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement