Está en la página 1de 7

siete.

c 11/6/2020 11:19

1
2
3 void tecladoEspecial(int tecla, int x, int y) {
4 switch(tecla) {
5 case GLUT_KEY_UP : posy++;break;
6 case GLUT_KEY_DOWN : posy--;break;
7 case GLUT_KEY_RIGHT : posx++;break;
8 case GLUT_KEY_LEFT : posx--;break;
9 case GLUT_KEY_PAGE_UP : esc=esc*1.01;break;
10 case GLUT_KEY_PAGE_DOWN : esc=esc*0.99;break;
11 case GLUT_KEY_F1 : figura=1;break;
12 case GLUT_KEY_F2 : figura=2;break;
13 case GLUT_KEY_F3 : figura=3;break;
14 case GLUT_KEY_F4 : figura=4;break;
15 case GLUT_KEY_F5 : alterna_movimiento();break;
16 case GLUT_KEY_F6 : alterna_pantalla();break;
17 case GLUT_KEY_F9 : luz=0;glPolygonMode(GL_FRONT_AND_BACK,G
18 case GLUT_KEY_F10 : luz=0;glPolygonMode(GL_FRONT_AND_BACK,
19 case GLUT_KEY_F11 : luz=1;glPolygonMode(GL_FRONT_AND_BACK,
20 }
21 glutPostRedisplay();
22 }
23
24 void texto(int x, int y, char *palabra) {
25 int i;
26 glColor3f(0,0,0);
27 glRasterPos2f(x, y);
28 for (i = 0; palabra[i]; i++)
29 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, palabra[i]);
30 }
31
32 void desocupado() {
33 if (movimiento==1) {
34 angulo=angulo+0.3;
35 if (angulo>=360)
36 angulo=0;
37 glutPostRedisplay();

Page 1 of 7
siete.c 11/6/2020 11:19

38 }
39 }
40 #include <GL/glut.h>
41 #include <stdio.h>
42 #include <string.h>
43 #define ancho 640
44 #define alto 480
45 #define profundidad 500
46
47 void dibuja();
48 void ejes();
49 void tecladoNormal(unsigned char tecla, int x, int y);
50 void tecladoEspecial(int tecla, int x, int y);
51 void texto(int x, int y, char *palabra);
52 void desocupado();
53 void redimensionado(int width, int height);
54 void luces();
55 void crear_menu();
56 void menu(int opcion);
57 void menupoligono(int opcion);
58 void menutipo(int opcion);
59 void alterna_movimiento();
60 void alterna_pantalla();
61 void mouse(int bouton,int estado,int x,int y);
62 void movimiento_mouse(int x,int y);
63 void texto_terminal(char *cmd);
64
65 int posx=0, posy=0;
66 float angulo=0;
67 float esc=1;
68 unsigned int figura=1;
69 GLfloat material_ambient[] = {0.05, 0.05, 0.05, 1.0f};
70 GLfloat material_diffuse[] = { 0.8, 0.0, 0.0, 1.0f};
71 GLfloat material_specular[] = {0.9, 0.8, 0.8, 1.0f};
72 GLfloat luz_ambient[] = { 0.75, 0.75, 0.75, 0.0 };
73 GLfloat luz_diffuse[] = { 1.0, 1.0, 1.0, 0.0 };
74 GLfloat luz_specular[] = { 1.0, 1.0, 1.0, 0.0 };

Page 2 of 7
siete.c 11/6/2020 11:19

75 GLfloat luz_position[] = { 1.0, 1.0, 1.0, 0.0 };


76 unsigned int luz=1;
77 unsigned int movimiento=0;
78 unsigned int pantalla=0;
79 char presionado;
80 int angulox=0,anguloy=0,x,y,xold,yold;
81
82 int main(int argc, char** argv) {
83 int i;
84 for (i=1;i<argc;i++) {
85 if (strstr(argv[i],"-h") != NULL)
86 texto_terminal(argv[0]);
87 if (strstr(argv[i],"-m") != NULL)
88 movimiento=1;
89 if (strstr(argv[i],"-f") != NULL) {
90 if (i+1 >= argc)
91 texto_terminal(argv[0]);
92 switch(atoi(argv[i+1])) {
93 case 1: figura=1;break;
94 case 2: figura=2;break;
95 case 3: figura=3;break;
96 case 4: figura=4;break;
97 default: figura=5;break;
98 }
99 }
100 }
101 glutInitDisplayMode(GLUT_STENCIL | GLUT_DOUBLE | GLUT_DEPTH);
102 glutInitWindowPosition(100, 0);
103 glutInitWindowSize(ancho, alto);
104 glutCreateWindow("Mouse");
105 glOrtho(-(ancho/2), (ancho/2), -(alto/2), (alto/2), -profundi
106 glClearColor(1, 1, 1, 0);
107 crear_menu();
108 glutDisplayFunc(dibuja);
109 glutKeyboardFunc(tecladoNormal);
110 glutSpecialFunc(tecladoEspecial);
111 glutIdleFunc(desocupado);

Page 3 of 7
siete.c 11/6/2020 11:19

112 glutReshapeFunc(redimensionado);
113 glutMouseFunc(mouse);
114 glutMotionFunc(movimiento_mouse);
115 glutMainLoop();
116 return 0;
117 }
118
119 void dibuja() {
120 glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_DEPT
121 ejes();
122 luces();
123 glPushMatrix();
124 glTranslatef(posx, posy, 0);
125 glRotatef(anguloy,1.0,0.0,0.0);
126 glRotatef(angulox,0.0,1.0,0.0);
127 glRotatef(angulo, 1, 0, 0);
128 glRotatef(angulo, 0, 1, 0);
129 glRotatef(angulo, 0, 0, 1);
130 glScalef(esc, esc, esc);
131 glColor3f(1, 0, 0);
132 if(luz==1)
133 glEnable(GL_LIGHTING);
134 switch(figura) {
135 case 1: glutSolidTorus(20, 80, 18, 18);break;
136 case 2: glutSolidSphere(100, 18, 18);break;
137 case 3: glutSolidCone(50, 200, 18, 18);break;
138 case 4: glutSolidCube(100); break;
139 case 5: glutSolidTeapot(120);break;
140 }
141 glDisable(GL_LIGHTING);
142 glPopMatrix();
143 glutSwapBuffers();
144 }
145
146 void ejes() {
147 glColor3f(0.9, 0.9, 0.9);
148 glBegin(GL_LINES);

Page 4 of 7
siete.c 11/6/2020 11:19

149 glVertex3f(-ancho/2, 0, 0);


150 glVertex3f(ancho/2, 0, 0);
151 glVertex3f(0, alto/2, 0);
152 glVertex3f(0, -alto/2, 0);
153 glEnd();
154 }
155
156 void tecladoNormal(unsigned char tecla, int x, int y) {
157 switch(tecla) {
158 case 27: exit(0);break;
159 }
160 }
161 void redimensionado(int anchop, int altop) {
162 glViewport(0,0,anchop,altop);
163 glMatrixMode(GL_PROJECTION);
164 glLoadIdentity();
165 gluPerspective(60.0f,(GLfloat)ancho/(GLfloat)alto,0.1,profund
166 gluLookAt(0,0,profundidad/1.2,0,0,0,0,1,0);
167 glMatrixMode(GL_MODELVIEW);
168 glLoadIdentity();
169 }
170
171
172 void luces() {
173 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient)
174 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse)
175 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specula
176 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
177 glLightfv(GL_LIGHT0, GL_AMBIENT, luz_ambient);
178 glLightfv(GL_LIGHT0, GL_DIFFUSE, luz_diffuse);
179 glLightfv(GL_LIGHT0, GL_SPECULAR, luz_specular);
180 glLightfv(GL_LIGHT0, GL_POSITION, luz_position);
181 glEnable(GL_DEPTH_TEST);
182 glEnable(GL_LIGHT0);
183 }
184
185 void crear_menu() {

Page 5 of 7
siete.c 11/6/2020 11:19

186 int submenupoligono, submenutipo;


187 submenupoligono = glutCreateMenu(menupoligono);
188 glutAddMenuEntry("Toroide", 0);
189 glutAddMenuEntry("Esfera", 1);
190 glutAddMenuEntry("Cono", 2);
191 glutAddMenuEntry("Cubo", 3);
192 submenutipo = glutCreateMenu(menutipo);
193 glutAddMenuEntry("Puntos", 0);
194 glutAddMenuEntry("Lineas", 1);
195 glutAddMenuEntry("Relleno", 2);
196 glutCreateMenu(menu);
197 glutAddSubMenu("Poligono", submenupoligono);
198 glutAddSubMenu("Tipo", submenutipo);
199 glutAddMenuEntry("Alternar Movimiento", 1);
200 glutAddMenuEntry("Alternar Pantalla", 2);
201 glutAddMenuEntry("------------------", 999);
202 glutAddMenuEntry("Salir", 3);
203 glutAttachMenu(GLUT_RIGHT_BUTTON);
204 }
205
206 void menu(int opcion) {
207 switch (opcion) {
208 case 1: alterna_movimiento();break;
209 case 2: alterna_pantalla();break;
210 case 3: exit(0);break;
211 default: break;
212 }
213 }
214
215 void menupoligono(int opcion) {
216 switch (opcion) {
217 case 0 : figura=1;break;
218 case 1 : figura=2;break;
219 case 2 : figura=3;break;
220 case 3 : figura=4;break;
221 default: break;
222 }

Page 6 of 7
siete.c 11/6/2020 11:19

223 }
224
225

Page 7 of 7

También podría gustarte