32 #include "SDL_opengl.h"
34 #pragma warning(disable:4244 4305) //for VC++, no precision loss complaints
37 #include <yarp/os/LogStream.h>
39 using namespace yarp::os;
65 glShadeModel( GL_SMOOTH );
68 glCullFace( GL_BACK );
69 glFrontFace( GL_CCW );
70 glEnable( GL_CULL_FACE );
71 glEnable(GL_DEPTH_TEST);
73 glEnable(GL_LIGHTING);
75 glEnable(GL_NORMALIZE);
76 glEnable(GL_COLOR_MATERIAL);
83 GLfloat light_color[] = {0.0,0.0,0.0,1.0};
84 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, light_color);
86 glClearColor(0.0,0.0,0.0,0);
88 string floor = finder.findFile(
"floor");
91 string body1 = finder.findFile(
"body1");
94 string body2 = finder.findFile(
"body2");
97 string skybox_ft = finder.findFile(
"skybox_ft");
100 string skybox_bk = finder.findFile(
"skybox_bk");
103 string skybox_lt = finder.findFile(
"skybox_lt");
106 string skybox_rt = finder.findFile(
"skybox_rt");
109 string skybox_up = finder.findFile(
"skybox_up");
112 string face = finder.findFile(
"face");
116 yError(
"No texture loaded\n");
123 glEnable(GL_TEXTURE_2D);
127 for(
float i = -500; i <= 500; i += 5)
130 glVertex3f(-500, 0, i);
131 glVertex3f(500, 0, i);
132 glVertex3f(i, 0,-500);
133 glVertex3f(i, 0, 500);
140 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
141 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
142 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
143 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
144 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, 0 ? GL_MODULATE : GL_DECAL);
148 glTexCoord2f (-50.0
f * 0.5
f + 0.5
f,-50.0
f * 0.5
f + 0.5
f);
149 glVertex3f (-50.0
f, -50.0
f, 0.0
f);
150 glTexCoord2f (50.0
f * 0.5
f + 0.5
f, -50.0
f * 0.5
f + 0.5
f);
151 glVertex3f (50.0
f, -50.0
f, 0.0
f);
152 glTexCoord2f (50.0
f * 0.5
f + 0.5
f, 50.0
f * 0.5
f + 0.5
f);
153 glVertex3f (50.0
f, 50.0
f, 0.0
f);
154 glTexCoord2f (-50.0
f * 0.5
f + 0.5
f, 50.0
f * 0.5
f + 0.5
f);
155 glVertex3f (-50.0
f, 50.0
f, 0.0
f);
165 float skyboxMaterial[] = {1.0f,1.0f,1.0f,1.0f};
166 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
167 glRotatef(180,0,0,1);
169 glBindTexture(GL_TEXTURE_2D,
Texture[6]);
171 glMaterialfv(GL_FRONT, GL_AMBIENT, skyboxMaterial);
172 glTexCoord2f(1.0
f, 0.0
f); glVertex3f(
x,
y,
z+length);
173 glTexCoord2f(1.0
f, 1.0
f); glVertex3f(
x,
y+
height,
z+length);
175 glTexCoord2f(0.0
f, 0.0
f); glVertex3f(
x+
width,
y,
z+length);
179 glBindTexture(GL_TEXTURE_2D,
Texture[5]);
181 glTexCoord2f(1.0
f, 0.0
f); glVertex3f(
x+
width,
y,
z);
183 glTexCoord2f(0.0
f, 1.0
f); glVertex3f(
x,
y+
height,
z);
184 glTexCoord2f(0.0
f, 0.0
f); glVertex3f(
x,
y,
z);
188 glBindTexture(GL_TEXTURE_2D,
Texture[4]);
190 glTexCoord2f(1.0
f, 1.0
f); glVertex3f(
x,
y+
height,
z);
191 glTexCoord2f(0.0
f, 1.0
f); glVertex3f(
x,
y+
height,
z+length);
192 glTexCoord2f(0.0
f, 0.0
f); glVertex3f(
x,
y,
z+length);
193 glTexCoord2f(1.0
f, 0.0
f); glVertex3f(
x,
y,
z);
197 glBindTexture(GL_TEXTURE_2D,
Texture[3]);
199 glTexCoord2f(0.0
f, 0.0
f); glVertex3f(
x+
width,
y,
z);
200 glTexCoord2f(1.0
f, 0.0
f); glVertex3f(
x+
width,
y,
z+length);
206 glBindTexture(GL_TEXTURE_2D,
Texture[7]);
210 glTexCoord2f(1.0
f, 1.0
f); glVertex3f(
x,
y+
height,
z+length);
211 glTexCoord2f(0.0
f, 1.0
f); glVertex3f(
x,
y+
height,
z);
215 glBindTexture(GL_TEXTURE_2D,
Texture[7]);
217 glTexCoord2f(0.0
f, 0.0
f); glVertex3f(
x,
y,
z);
218 glTexCoord2f(1.0
f, 0.0
f); glVertex3f(
x,
y,
z+length);
219 glTexCoord2f(1.0
f, 1.0
f); glVertex3f(
x+
width,
y,
z+length);
220 glTexCoord2f(0.0
f, 1.0
f); glVertex3f(
x+
width,
y,
z);
223 glDisable(GL_TEXTURE_2D);
226 void DrawBox(
float width,
float height,
float length,
bool wireframe,
bool texture,
int whichtexture){
228 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
232 else mode = GL_QUADS;
237 glEnable(GL_TEXTURE_2D);
238 glBindTexture(GL_TEXTURE_2D,
Texture[whichtexture]);
241 glDisable(GL_TEXTURE_2D);
245 glNormal3i(-1, 1,-1);
247 glVertex3i(-1, 1,-1);
248 glNormal3i( 1, 1,-1);
250 glVertex3i( 1, 1,-1);
251 glNormal3i( 1,-1,-1);
253 glVertex3i( 1,-1,-1);
254 glNormal3i(-1,-1,-1);
256 glVertex3i(-1,-1,-1);
258 glNormal3i(-1,-1,-1);
260 glVertex3i(-1,-1,-1);
261 glNormal3i( 1,-1,-1);
263 glVertex3i( 1,-1,-1);
264 glNormal3i( 1,-1, 1);
266 glVertex3i( 1,-1, 1);
267 glNormal3i(-1,-1, 1);
269 glVertex3i(-1,-1, 1);
271 glNormal3i( 1,-1, 1);
273 glVertex3i( 1,-1, 1);
274 glNormal3i( 1,-1,-1);
276 glVertex3i( 1,-1,-1);
277 glNormal3i( 1, 1,-1);
279 glVertex3i( 1, 1,-1);
280 glNormal3i( 1, 1, 1);
282 glVertex3i( 1, 1, 1);
284 glNormal3i( 1, 1,-1);
286 glVertex3i( 1, 1,-1);
287 glNormal3i(-1, 1,-1);
289 glVertex3i(-1, 1,-1);
290 glNormal3i(-1, 1, 1);
292 glVertex3i(-1, 1, 1);
293 glNormal3i( 1, 1, 1);
295 glVertex3i( 1, 1, 1);
297 glNormal3i(-1, 1, 1);
299 glVertex3i(-1, 1, 1);
300 glNormal3i(-1, 1,-1);
302 glVertex3i(-1, 1,-1);
303 glNormal3i(-1,-1,-1);
305 glVertex3i(-1,-1,-1);
306 glNormal3i(-1,-1, 1);
308 glVertex3i(-1,-1, 1);
310 glNormal3i( 1,-1, 1);
312 glVertex3i( 1,-1, 1);
313 glNormal3i( 1, 1, 1);
315 glVertex3i( 1, 1, 1);
316 glNormal3i(-1, 1, 1);
318 glVertex3i(-1, 1, 1);
319 glNormal3i(-1,-1, 1);
321 glVertex3i(-1,-1, 1);
325 glDisable(GL_TEXTURE_2D);
329 void DrawSphere(
float radius,
bool wireframe,
bool texture,
int whichtexture){
330 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
334 glEnable(GL_TEXTURE_2D);
335 gluQuadricTexture(
sphere,
true);
336 glBindTexture(GL_TEXTURE_2D,
Texture[whichtexture]);
339 glDisable(GL_TEXTURE_2D);
343 gluQuadricDrawStyle(
sphere, GLU_LINE);
346 gluQuadricDrawStyle(
sphere, GLU_FILL);
348 gluSphere(
sphere, radius, 20,20);
350 glDisable(GL_TEXTURE_2D);
353 void DrawCylinder(
float radius,
float length,
bool wireframe,
bool texture,
int whichtexture){
356 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
359 cap = gluNewQuadric();
363 glEnable(GL_TEXTURE_2D);
365 gluQuadricTexture(
cap,
true);
366 glBindTexture(GL_TEXTURE_2D,
Texture[whichtexture]);
369 glDisable(GL_TEXTURE_2D);
373 glTranslatef(0,0,-length/2);
374 gluQuadricDrawStyle(
cylinder, GLU_LINE);
377 glTranslatef(0,0,-length/2);
379 gluQuadricDrawStyle(
cylinder, GLU_FILL);
380 gluCylinder(
cylinder,radius,radius,length,20,3);
383 glRotatef(180.0,1,0,0);
384 gluDisk(
cap,0,radius,20,10);
388 glTranslatef(0,0,length);
389 gluDisk(
cap,0,radius,20,10);
394 glTranslatef(0,0,length/2);
396 glDisable(GL_TEXTURE_2D);
398 gluDeleteQuadric(
cap);
401 float local_matrix[16];
402 local_matrix[0] = R[0];
403 local_matrix[1] = R[4];
404 local_matrix[2] = R[8];
406 local_matrix[4] = R[1];
407 local_matrix[5] = R[5];
408 local_matrix[6] = R[9];
410 local_matrix[8] = R[2];
411 local_matrix[9] = R[6];
412 local_matrix[10] = R[10];
413 local_matrix[11] = 0;
414 local_matrix[12] =
pos[0];
415 local_matrix[13] =
pos[1];
416 local_matrix[14] =
pos[2];
417 local_matrix[15] = 1;
418 glMultMatrixf(local_matrix);
423 glEnable( GL_TEXTURE_2D );
426 unsigned char *
data;
435 file = fopen( filename,
"rb" );
436 if (
file == NULL )
return 0;
443 glGenTextures(1, &texture);
444 glBindTexture(GL_TEXTURE_2D, texture);
445 gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
width,
height, GL_RGB, GL_UNSIGNED_BYTE,
data);
446 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
447 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
451 glDisable( GL_TEXTURE_2D );
463 glEnable(GL_TEXTURE_2D);
464 glDisable( GL_CULL_FACE );
466 float color[] = { 1.0f, 1.0f, 1.0f, 1.0f };
467 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
469 glBindTexture(GL_TEXTURE_2D,
Texture[whichtexture]);
471 glBegin(GL_TRIANGLES);
500 glEnable( GL_CULL_FACE );
501 glDisable(GL_TEXTURE_2D);
518 glEnable(GL_TEXTURE_2D);
526 yDebug() <<
"Loading texture " <<
" '" << filename <<
"' ";
528 if( (file1 = fopen(filename,
"rb"))==NULL){ yError() <<
"Cannot load/find texture file ";
return (0); }
529 yDebug() <<
"......... OK! ";
530 fseek(file1, 18, SEEK_CUR);
533 ret=fread(&infoheader1.
biWidth,
sizeof(
int), 1, file1);
538 ret=fread(&infoheader1.
biHeight,
sizeof(
int), 1, file1);
542 ret=fread(&infoheader1.
biPlanes,
sizeof(
short int), 1, file1);
547 yDebug() <<
"Texture Size " << infoheader1.
biHeight <<
" " << infoheader1.
biWidth;
549 yDebug(
"Planes from %s is not 1: %u\n", filename, infoheader1.
biPlanes);
553 ret=fread(&infoheader1.
biBitCount,
sizeof(
unsigned short int), 1, file1);
559 yDebug(
"Bpp from %s is not 24: %d\n", filename, infoheader1.
biBitCount);
562 fseek(file1, 24, SEEK_CUR);
566 if (infoheader1.
data == NULL) {
567 yError(
"Error allocating memory for color-corrected image data\n");
571 if ((i = fread(infoheader1.
data, infoheader1.
biWidth * infoheader1.
biHeight * 3, 1, file1)) != 1) {
572 yError(
"Error reading image data from %s.\n", filename);
577 temp1 = infoheader1.
data[i];
578 infoheader1.
data[i] = infoheader1.
data[i+2];
579 infoheader1.
data[i+2] = temp1;
584 glBindTexture(GL_TEXTURE_2D, whichtexture);
586 yInfo() <<
"Finished Binding texture ";
587 yInfo() <<
"Finished Setting parameters ";
588 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
589 yInfo() <<
"Finished Setting glTexEnvf ";
591 glTexImage2D(GL_TEXTURE_2D, 0, 3, infoheader1.
biWidth, infoheader1.
biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, infoheader1.
data);
592 yInfo() <<
"Finished Setting glTexImage2D ";
594 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, infoheader1.
biWidth, infoheader1.
biHeight, GL_RGB, GL_UNSIGNED_BYTE, infoheader1.
data);
596 yInfo() <<
"Finished Setting gluBuild2DMipmaps ";
597 free(infoheader1.
data);
599 yInfo() <<
"Finished creating 3D Model................\n";
601 return ( whichtexture );