Está en la página 1de 11

#include <GL/glut.h> #include <stdlib.h> #include <math.

h>

#define PI 3.1415926 double phi1=0,phi2,phi3,l1=25,l2=80,H=40;

void Draw() { double J; J=l1*cos(phi1)/(l1*sin(phi1)+H);

phi3=PI/2-atan(J); phi2=phi3-phi1; glPushMatrix();

GLfloat x = 0.0,y = 0.0,R = 1.5;

glColor3f(0.2,0.4,0.6); glBegin(GL_LINE_LOOP); for(int i = 0; i < 360;i++) glVertex3f(R*cos(2*PI*i/360) + x,R*sin(2*PI*i/360) +y ,0.0);

glEnd();

glColor3f(0.0, 1.0, 1.0); glBegin(GL_LINE_LOOP ); glVertex2d(0,0); glVertex2d(4,-4); glVertex2d(-4,-4); glEnd(); glRotated(phi1*180/PI,0,0,1);

glColor3f(1.0, 0.0, 1.0); glBegin(GL_LINES); glVertex2d(0,0); glVertex2d(l1,0); glEnd();

glEnd();

glTranslated(l1,0,0); glRotated(phi2*180/PI,0,0,1);

GLfloat x2 = 0.0,y2 = 0.0,R2 = 1.5; glColor3f(0.2,0.8,0.6); glBegin(GL_LINE_LOOP); for(int k = 0; k < 360;k++) glVertex3f(R2*cos(2*PI*k/360) + x2,R*sin(2*PI*k/360) +y2 ,0.0); glEnd();

glColor3f(1.0, 1.0, 0.0); glBegin(GL_LINE_LOOP ); glVertex2d(5,3); glVertex2d(5,-3); glVertex2d(-5,-3); glVertex2d(-5,3); glEnd();

glPopMatrix();

glPushMatrix();

glTranslated(0,-H,0); glColor3f(1.0, 0.0, 0.0);

GLfloat x1 = 0.0,y1 = 0.0,R1 = 1.5; glColor3f(0.2,0.4,0.6); glBegin(GL_LINE_LOOP); for(int j = 0; j < 360;j++) glVertex3f(R1*cos(2*PI*j/360) + x1,R*sin(2*PI*j/360) +y1 ,0.0); glEnd();

glColor3f(0.0, 1.0, 1.0); glBegin(GL_LINE_LOOP ); glVertex2d(0,0); glVertex2d(4,-4); glVertex2d(-4,-4);

glEnd(); glRotated(phi3*180/PI,0,0,1); glColor3f(0.0, 1.0, 0.0); glBegin(GL_LINES); glVertex2d(0,0); glVertex2d(l2,0); glEnd();

glPopMatrix();

void display(void) { glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glColor3f(1.0, 1.0, 1.0); Draw(); glPopMatrix(); glutSwapBuffers(); }

void spinDisplay(void) { phi1 = phi1 + 0.001; if (phi1 > 2*PI) phi1 = phi1 - 2*PI; glutPostRedisplay(); glPopMatrix(); }

void init(void) { glClearColor (.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); }

void reshape(int w, int h) {

glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }

void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(spinDisplay); break; case GLUT_MIDDLE_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(NULL); break; default: break; } }

void keyboard (unsigned char key, int x, int y) { switch (key) { case 'R': case 'r': glutIdleFunc(spinDisplay); break; case 'S': case 's': glutIdleFunc(NULL); break; case 27: exit(0); break; default: break; } }

void menu_option(int value) {

switch(value) { case 1: glutIdleFunc(spinDisplay); break; case 2: glutIdleFunc(NULL); break; default: break; } }

void null_action(int value)//OR: void null_action() { }

void create_menu() { int menu_title[2];

menu_title[0] = glutCreateMenu(menu_option); glutAddMenuEntry("Rotate ", 1);

glutAddMenuEntry("-------", 0); glutAddMenuEntry("Stop ", 2);

menu_title[1] = glutCreateMenu(null_action); glutAddMenuEntry("Rotate ", 1); glutAddMenuEntry("Stop ", 2);

glutCreateMenu(menu_option); glutAddMenuEntry("Rotate Rectangle", 1); glutAddMenuEntry("Stop Rotate Rectangle", 2); glutAddMenuEntry("---------------------", 0);

glutAddSubMenu("Enable Rotate And Stop", menu_title[0]); glutAddSubMenu("Disable Rotate And Stop", menu_title[1]);

glutAttachMenu(GLUT_RIGHT_BUTTON); }

int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); create_menu(); glutMainLoop(); return 0; }

También podría gustarte