Open GL - pez en 3D

 
Vista:

pez en 3D

Publicado por Paco Martinez (1 intervención) el 26/05/2011 20:05:36
Hola a todos, necesito a partir de un pez hecho en 2D con transformaciones geométricas, con su zoom, movimientos de translación..etc.. a partir de eso hacerlo en 3D... y no se como hacerlo ni que funciones utilizar... alguien me puede echar una mano????? Gracias..
Os dejo el codigo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
 
int alto=800;
int ancho=800;
float zoom=1.0, tx=0.0, ty=0.0;
 
static void Init( void )
{
	glClearIndex( 0.0 );
   	glShadeModel( GL_FLAT );
}
 
static void Reshape( int width, int height )
{
	ancho=width;
	alto=height;
 
 
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glViewport(0.1, 0.1, (GLint)width/2, (GLint)height/2);
   	glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
	glScalef(zoom,zoom,1.0);
        glTranslatef(tx,ty,0.0);
 
	glClearColor(0.0,0,0.7,0.0);
   	glClear( GL_COLOR_BUFFER_BIT );
   	glColor3f(0.0,0.0,0.0);
        glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glCallList(2);
        		//primera figura izquierda abajo
 
 
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glViewport((GLint)width/2, 0.0, (GLint)width/2, (GLint)height/2);
   	glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
	glScalef(zoom,zoom,1.0);
	glTranslatef(tx,ty,0.0);
 
        glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glCallList(2);
			//segunda figura derecha abajo
 
 
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glViewport(0, (GLint)width/2, (GLint)width/2, (GLint)height/2);
   	glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
	glScalef(zoom,zoom,1.0);
	glTranslatef(tx,ty,0.0);
 
        glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glCallList(2);
			// tercera figura izquierda arriba
 
 
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glViewport((GLint)width/2, (GLint)width/2, (GLint)width/2, (GLint)height/2);
   	glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
	glScalef(zoom,zoom,1.0);
	glTranslatef(tx,ty,0.0);
 
        glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glCallList(2);
			//cuarta figura derecha arriba
 
 
	glFlush();
 
}
 
 
 
static void Key_Esc(unsigned char key, int x, int y )
{
  	if (key==27)
      		exit(0);
 
  	if (key ==43){
		zoom*=1.1;
		Reshape(ancho,alto);
	}
 	if (key==45){
		zoom*=0.9;
		Reshape(ancho,alto);
	}
}
 
 
static void Special(int key, int x, int y )
{
  	if (key==GLUT_KEY_LEFT){
               	tx=tx-0.2;
               	Reshape(ancho,alto);
	}
      	if (key==GLUT_KEY_RIGHT){
		tx=tx+0.2;
		Reshape(ancho,alto);
	}
	if (key==GLUT_KEY_UP){
		ty=ty+0.2;
		Reshape(ancho,alto);
	}
	if (key==GLUT_KEY_DOWN){
		ty=ty-0.2;
		Reshape(ancho,alto);
	}
}
 
 
static void triangulo ()
{
	glNewList(1, GL_COMPILE);
 
		glBegin(GL_TRIANGLES);
		// Dibujamos un triángulo
		glColor3f (1.0,0.6, 0.0);
		// Color
		glVertex3f(0.0,0.0,0.0);
		// Coordenadas del primer vértice
		glVertex3f(0.0,0.4,0.0);
		// Coordenadas del segundo vértice
		glVertex3f(0.4,0.0,0.0);
		// Coordenadas del tercer vértice
                glEnd();
 
	glEndList();	// Terminamos de dibujar
 
}
 
static void cuadrado()
{
	glBegin(GL_QUADS);
	glColor3f (1.0,0.6, 0.0);
		//color
	glVertex3f(0.0,0.0,0.0);
	glVertex3f(0.0,0.2,0.0);
	glVertex3f(0.2,0.2,0.0);
	glVertex3f(0.2,0.0,0.0);
 
	glEnd();
}
 
void pez(){
 
   	glNewList(2, GL_COMPILE);
 
	glPushMatrix();
	glTranslatef(0.0,0.0,0.0);
	glRotatef(0.0,0.0,0.0,0.0);
	glCallList(1);
        glPopMatrix();
		// cuerpo de pez 1
 
	glPushMatrix();
	glRotatef(180.0,1.0,0.0,0.0);
	glCallList(1);
	glPopMatrix();
		// cuerpo de pez 2
 
	glPushMatrix();
	glTranslatef(0.0,0.2,0.0);
	glRotatef(180.0,0.0,1.0,0.0);
	glScalef(0.5,0.5,0.0);
	glCallList(1);
	glPopMatrix();
		// ala de arriba 1
 
	glPushMatrix();
	glTranslatef(-0.2,0.2,0.0);
	glRotatef(180.0,1.0,0.0,0.0);
	glScalef(0.5,0.5,0.0);
	glCallList(1);
	glPopMatrix();
		// ala de arriba 2
 
	glPushMatrix();
	glTranslatef(-0.2,-0.2,0.0);
	glRotatef(0.0,0.0,0.0,0.0);
	glScalef(0.5,0.5,0.0);
	glCallList(1);
	glPopMatrix();
		// ala de abajo 1
 
	glPushMatrix();
	glTranslatef(0.0,-0.2,0.0);
	glRotatef(180.0,0.0,0.0,1.0);
	glScalef(0.5,0.5,0.0);
	glCallList(1);
	glPopMatrix();
		// ala de abajo 2
 
	glPushMatrix();
	glTranslatef(-0.4,0.0,0.0);
 
	glRotatef(0.0,0.0,0.0,0.0);
	glScalef(0.5,0.5,0.0);
	glCallList(1);
	glPopMatrix();
		// cola 1
 
	glPushMatrix();
	glTranslatef(-0.4,0.0,0.0);
	glRotatef(180.0,1.0,0.0,0.0);
	glScalef(0.5,0.5,0.0);
	glCallList(1);
	glPopMatrix();
		// cola 2
 
	glPushMatrix();
	glTranslatef(0.5,0.1,0.0);
	glRotatef(45.0,0.0,0.0,1.0);
	glScalef(0.5,0.5,0.0);
	glCallList(1);
	glPopMatrix();
		// gota 1
 
	glPushMatrix();
	glTranslatef(0.5,0.38,0.0);
	glRotatef(225.0,0.0,0.0,1.0);
	glScalef(0.5,0.5,0.0);
	glCallList(1);
	glPopMatrix();
   		// gota 2
 
	glEndList();
}
 
static void Display( void )
{
	Reshape(ancho,alto);
}
 
 
int main( int argc, char **argv )
{
 
   glutInit(&argc, argv);
   int a;
   glutInitDisplayMode( GLUT_RGB );
 
   glutInitWindowPosition(100,100 );
   glutInitWindowSize(800, 800 );
   glutCreateWindow("Pez con transformaciones geometricas");
 
   Init();
   triangulo();
   pez();
 
   glutReshapeFunc(Reshape);
   glutDisplayFunc(Display);
   glutKeyboardFunc(Key_Esc);
   glutSpecialFunc(Special);
 
   glutMainLoop( );
 
   return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder