iCub-main
rendering.cpp
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4 * Copyright (C) 2010 RobotCub Consortium, European Commission FP6 Project IST-004370
5 * Author: Vadim Tikhanoff
6 * email: vadim.tikhanoff@iit.it
7 * website: www.robotcub.org
8 * Permission is granted to copy, distribute, and/or modify this program
9 * under the terms of the GNU General Public License, version 2 or any
10 * later version published by the Free Software Foundation.
11 *
12 * A copy of the license can be found at
13 * http://www.robotcub.org/icub/license/gpl.txt
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18 * Public License for more details
19 */
28 #ifdef WIN32
29 #include <windows.h>
30 #endif
31 #include "SDL.h"
32 #include "SDL_opengl.h"
33 #include "rendering.h"
34 #pragma warning(disable:4244 4305) //for VC++, no precision loss complaints
35 #include "VideoTexture.h"
36 #include <cstring>
37 #include <yarp/os/LogStream.h>
38 
39 using namespace yarp::os;
40 
41 using namespace std;
42 
43 const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
44 const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
45 const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
46 const GLfloat light_position[] = { 0.0f, 50.0f, 5.0f, 1.0f };
47 
48 GLenum mode;
49 GLuint nicetexture;
50 int num_texture1=15;
51 GLuint num_texture = 19;
52 
53 unsigned int Texture[200];
54 
55 GLUquadricObj *sphere;
56 GLUquadricObj *cylinder;
57 GLUquadricObj *cap;
58 
59 static GLuint FindTextureRAW(const char *str, bool flag) {
60  return LoadTextureRAW(str,flag);
61 }
62 
63 //Function to setup OpenGl
64 bool setup_opengl(ResourceFinder& finder){
65  glShadeModel( GL_SMOOTH );
66 
67  // Culling
68  glCullFace( GL_BACK );
69  glFrontFace( GL_CCW );
70  glEnable( GL_CULL_FACE );
71  glEnable(GL_DEPTH_TEST);
72  glDepthFunc(GL_LESS);
73  glEnable(GL_LIGHTING);
74  glEnable(GL_LIGHT0); // OpenGL light
75  glEnable(GL_NORMALIZE);
76  glEnable(GL_COLOR_MATERIAL);
77 
78  glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
79  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
80  glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
81  //glLightfv(GL_LIGHT1, GL_POSITION, light_position );
83  GLfloat light_color[] = {0.0,0.0,0.0,1.0}; // colour
84  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, light_color);
85  //glMaterialfv(GL_FRONT,GL_DIFFUSE,light_color); // colour
86  glClearColor(0.0,0.0,0.0,0); // background color
87 
88  string floor = finder.findFile("floor");
89  Texture[0] = LoadTextureRAW( floor.c_str(), false );
90 
91  string body1 = finder.findFile("body1");
92  Texture[1] = LoadTextureRAW( body1.c_str(), false );
93 
94  string body2 = finder.findFile("body2");
95  Texture[2] = LoadTextureRAW( body2.c_str(), false );
96 
97  string skybox_ft = finder.findFile("skybox_ft");
98  Texture[3] = LoadTextureRAW( skybox_ft.c_str(), false );
99 
100  string skybox_bk = finder.findFile("skybox_bk");
101  Texture[4] = LoadTextureRAW( skybox_bk.c_str(), false );
102 
103  string skybox_lt = finder.findFile("skybox_lt");
104  Texture[5] = LoadTextureRAW( skybox_lt.c_str(), false );
105 
106  string skybox_rt = finder.findFile("skybox_rt");
107  Texture[6] = LoadTextureRAW( skybox_rt.c_str(), false );
108 
109  string skybox_up = finder.findFile("skybox_up");
110  Texture[7] = LoadTextureRAW( skybox_up.c_str(), false );
111 
112  string face = finder.findFile("face");
113  Texture[8] = LoadTextureRAW( face.c_str(), false );
114 
115  if (!Texture[1]){
116  yError("No texture loaded\n");
117  return false;
118  }
119  return true;
120 }
121 
122 void DrawGround(bool wireframe){
123  glEnable(GL_TEXTURE_2D);
124  if (wireframe)
125  {
126 
127  for(float i = -500; i <= 500; i += 5)
128  {
129  glBegin(GL_LINES);
130  glVertex3f(-500, 0, i);
131  glVertex3f(500, 0, i);
132  glVertex3f(i, 0,-500);
133  glVertex3f(i, 0, 500);
134  glEnd();
135  }
136 }
137  else
138  {
139  glBindTexture(GL_TEXTURE_2D, Texture[FLOOR]);
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);
145 
146  glBegin (GL_QUADS);
147  glNormal3f (0,0,1);
148  glTexCoord2f (-50.0f * 0.5f + 0.5f,-50.0f * 0.5f + 0.5f);//replaced from 50
149  glVertex3f (-50.0f, -50.0f, 0.0f);
150  glTexCoord2f (50.0f * 0.5f + 0.5f, -50.0f * 0.5f + 0.5f);
151  glVertex3f (50.0f, -50.0f, 0.0f);
152  glTexCoord2f (50.0f * 0.5f + 0.5f, 50.0f * 0.5f + 0.5f);
153  glVertex3f (50.0f, 50.0f, 0.0f);
154  glTexCoord2f (-50.0f * 0.5f + 0.5f, 50.0f * 0.5f + 0.5f);
155  glVertex3f (-50.0f, 50.0f, 0.0f);
156  glEnd();
157  }
158 }
159 void drawSkyDome(float x, float y, float z, float width, float height, float length)
160 {
161  // Center the Skybox around the given x,y,z position
162  x = x - width / 2;
163  y = y - height / 2;
164  z = z - length / 2;
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);
168  // Draw Front side
169  glBindTexture(GL_TEXTURE_2D, Texture[6]);
170  glBegin(GL_QUADS);
171  glMaterialfv(GL_FRONT, GL_AMBIENT, skyboxMaterial);
172  glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z+length);
173  glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y+height, z+length);
174  glTexCoord2f(0.0f, 1.0f); glVertex3f(x+width, y+height, z+length);
175  glTexCoord2f(0.0f, 0.0f); glVertex3f(x+width, y, z+length);
176  glEnd();
177 
178  // Draw Back side
179  glBindTexture(GL_TEXTURE_2D, Texture[5]);
180  glBegin(GL_QUADS);
181  glTexCoord2f(1.0f, 0.0f); glVertex3f(x+width, y, z);
182  glTexCoord2f(1.0f, 1.0f); glVertex3f(x+width, y+height, z);
183  glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y+height, z);
184  glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z);
185  glEnd();
186 
187  // Draw Left side
188  glBindTexture(GL_TEXTURE_2D, Texture[4]);
189  glBegin(GL_QUADS);
190  glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y+height, z);
191  glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y+height, z+length);
192  glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z+length);
193  glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z);
194  glEnd();
195 
196  // Draw Right side
197  glBindTexture(GL_TEXTURE_2D, Texture[3]);
198  glBegin(GL_QUADS);
199  glTexCoord2f(0.0f, 0.0f); glVertex3f(x+width, y, z);
200  glTexCoord2f(1.0f, 0.0f); glVertex3f(x+width, y, z+length);
201  glTexCoord2f(1.0f, 1.0f); glVertex3f(x+width, y+height, z+length);
202  glTexCoord2f(0.0f, 1.0f); glVertex3f(x+width, y+height, z);
203  glEnd();
204 
205  // Draw Up side
206  glBindTexture(GL_TEXTURE_2D, Texture[7]);
207  glBegin(GL_QUADS);
208  glTexCoord2f(0.0f, 0.0f); glVertex3f(x+width, y+height, z);
209  glTexCoord2f(1.0f, 0.0f); glVertex3f(x+width, y+height, z+length);
210  glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y+height, z+length);
211  glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y+height, z);
212  glEnd();
213 
214  // Draw Down side
215  glBindTexture(GL_TEXTURE_2D, Texture[7]);
216  glBegin(GL_QUADS);
217  glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z);
218  glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z+length);
219  glTexCoord2f(1.0f, 1.0f); glVertex3f(x+width, y, z+length);
220  glTexCoord2f(0.0f, 1.0f); glVertex3f(x+width, y, z);
221  glEnd();
222 
223  glDisable(GL_TEXTURE_2D);
224 }
225 
226 void DrawBox(float width, float height, float length, bool wireframe, bool texture, int whichtexture){
227 
228  glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);//blends the texture with the color of the object
229 
230  if (wireframe)
231  mode = GL_LINE_LOOP;
232  else mode = GL_QUADS;
233 
234  glPushMatrix();
235  glScalef(width/2,height/2,length/2);
236  if (texture){
237  glEnable(GL_TEXTURE_2D);
238  glBindTexture(GL_TEXTURE_2D,Texture[whichtexture]);
239  }
240  else{
241  glDisable(GL_TEXTURE_2D);
242  }
243  glBegin(mode);
244  //face 1
245  glNormal3i(-1, 1,-1);
246  glTexCoord2i(1,1);
247  glVertex3i(-1, 1,-1);
248  glNormal3i( 1, 1,-1);
249  glTexCoord2i(0,1);
250  glVertex3i( 1, 1,-1);
251  glNormal3i( 1,-1,-1);
252  glTexCoord2i(0,0);
253  glVertex3i( 1,-1,-1);
254  glNormal3i(-1,-1,-1);
255  glTexCoord2i(1,0);
256  glVertex3i(-1,-1,-1);
257  //face 2
258  glNormal3i(-1,-1,-1);
259  glTexCoord2i(0,1);
260  glVertex3i(-1,-1,-1);
261  glNormal3i( 1,-1,-1);
262  glTexCoord2i(1,1);
263  glVertex3i( 1,-1,-1);
264  glNormal3i( 1,-1, 1);
265  glTexCoord2i(1,0);
266  glVertex3i( 1,-1, 1);
267  glNormal3i(-1,-1, 1);
268  glTexCoord2i(0,0);
269  glVertex3i(-1,-1, 1);
270  // face 3
271  glNormal3i( 1,-1, 1);
272  glTexCoord2i(0,1);
273  glVertex3i( 1,-1, 1);
274  glNormal3i( 1,-1,-1);
275  glTexCoord2i(1,1);
276  glVertex3i( 1,-1,-1);
277  glNormal3i( 1, 1,-1);
278  glTexCoord2i(1,0);
279  glVertex3i( 1, 1,-1);
280  glNormal3i( 1, 1, 1);
281  glTexCoord2i(0,0);
282  glVertex3i( 1, 1, 1);
283  //face 4
284  glNormal3i( 1, 1,-1);
285  glTexCoord2i(0,1);
286  glVertex3i( 1, 1,-1);
287  glNormal3i(-1, 1,-1);
288  glTexCoord2i(1,1);
289  glVertex3i(-1, 1,-1);
290  glNormal3i(-1, 1, 1);
291  glTexCoord2i(1,0);
292  glVertex3i(-1, 1, 1);
293  glNormal3i( 1, 1, 1);
294  glTexCoord2i(0,0);
295  glVertex3i( 1, 1, 1);
296  //face 5
297  glNormal3i(-1, 1, 1);
298  glTexCoord2i(0,1);
299  glVertex3i(-1, 1, 1);
300  glNormal3i(-1, 1,-1);
301  glTexCoord2i(1,1);
302  glVertex3i(-1, 1,-1);
303  glNormal3i(-1,-1,-1);
304  glTexCoord2i(1,0);
305  glVertex3i(-1,-1,-1);
306  glNormal3i(-1,-1, 1);
307  glTexCoord2i(0,0);
308  glVertex3i(-1,-1, 1);
309  //face 6
310  glNormal3i( 1,-1, 1);
311  glTexCoord2i(0,1);
312  glVertex3i( 1,-1, 1);
313  glNormal3i( 1, 1, 1);
314  glTexCoord2i(1,1);
315  glVertex3i( 1, 1, 1);
316  glNormal3i(-1, 1, 1);
317  glTexCoord2i(1,0);
318  glVertex3i(-1, 1, 1);
319  glNormal3i(-1,-1, 1);
320  glTexCoord2i(0,0);
321  glVertex3i(-1,-1, 1);
322  glEnd();
323  glPopMatrix();
324 
325  glDisable(GL_TEXTURE_2D);
326  //glDisable( GL_BLEND );
327 }
328 
329 void DrawSphere(float radius, bool wireframe, bool texture, int whichtexture){
330  glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);//makes the texture
331  sphere = gluNewQuadric();
332  if (texture)
333  {
334  glEnable(GL_TEXTURE_2D);
335  gluQuadricTexture(sphere, true);
336  glBindTexture(GL_TEXTURE_2D, Texture[whichtexture]);
337  }
338  else
339  glDisable(GL_TEXTURE_2D);
340 
341  if (wireframe)
342  {
343  gluQuadricDrawStyle(sphere, GLU_LINE);
344  }
345  else
346  gluQuadricDrawStyle(sphere, GLU_FILL);
347 
348  gluSphere(sphere, radius, 20,20);
349 
350  glDisable(GL_TEXTURE_2D);
351  gluDeleteQuadric(sphere);
352 }
353 void DrawCylinder(float radius, float length, bool wireframe, bool texture, int whichtexture){
354  //glEnable( GL_BLEND );
355  //glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
356  glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);//blends the texture with the color of the object
357 
358  cylinder = gluNewQuadric();
359  cap = gluNewQuadric();
360 
361  if (texture)
362  {
363  glEnable(GL_TEXTURE_2D);
364  gluQuadricTexture(cylinder, true);
365  gluQuadricTexture(cap, true);
366  glBindTexture(GL_TEXTURE_2D, Texture[whichtexture]);
367  }
368  else
369  glDisable(GL_TEXTURE_2D);
370 
371  if (wireframe)
372  {
373  glTranslatef(0,0,-length/2);
374  gluQuadricDrawStyle(cylinder, GLU_LINE);
375  }
376  else
377  glTranslatef(0,0,-length/2);
378 
379  gluQuadricDrawStyle(cylinder, GLU_FILL);
380  gluCylinder(cylinder,radius,radius,length,20,3);
381 
382  glPushMatrix();
383  glRotatef(180.0,1,0,0);
384  gluDisk(cap,0,radius,20,10);
385  glPopMatrix();
386 
387  glPushMatrix();
388  glTranslatef(0,0,length);
389  gluDisk(cap,0,radius,20,10);
390  glPopMatrix();
391 
392  //glPushMatrix();
393  //glLoadIdentity();
394  glTranslatef(0,0,length/2);
395  //glPopMatrix();
396  glDisable(GL_TEXTURE_2D);
397  gluDeleteQuadric(cylinder);
398  gluDeleteQuadric(cap);
399 }
400 void LDEsetM(const dReal *pos,const dReal *R){
401  float local_matrix[16];
402  local_matrix[0] = R[0];
403  local_matrix[1] = R[4];
404  local_matrix[2] = R[8];
405  local_matrix[3] = 0;
406  local_matrix[4] = R[1];
407  local_matrix[5] = R[5];
408  local_matrix[6] = R[9];
409  local_matrix[7] = 0;
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);
419 }
420 
421 GLuint LoadTextureRAW( const char * filename, int wrap )
422 {
423  glEnable( GL_TEXTURE_2D );
424  GLuint texture;
425  int width, height;
426  unsigned char * data;
427  FILE * file;
428 
429  // allocate buffer
430  width = 512;
431  height = 512;
432  data = (unsigned char*)malloc( width * height * 3 );
433 
434  // open texture data
435  file = fopen( filename, "rb" );
436  if ( file == NULL ) return 0;
437 
438  // read texture data
439  int ret;
440  ret = fread( data, width * height * 3, 1, file );
441  fclose( file );
442 
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);
448 
449  // free buffer
450  free( data );
451  glDisable( GL_TEXTURE_2D );
452  return texture;
453  //return 0;
454 }
455 
457  // give opportunity to replace textures with video
458  video->apply( Texture );
459 }
460 
461 void DrawX (dTriMeshX trim, int whichtexture){
462 
463  glEnable(GL_TEXTURE_2D);
464  glDisable( GL_CULL_FACE );
465 
466  float color[] = { 1.0f, 1.0f, 1.0f, 1.0f };
467  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
468 
469  glBindTexture(GL_TEXTURE_2D, Texture[whichtexture]);
470 
471  glBegin(GL_TRIANGLES);
472  for (int i=0; i<(int) (trim->IndexCount); )
473  {
474  // vertices: 3d vertices (no_vertices * 3)
475  // indices: no_polygons * 3
476  // meshcoord: no_polygons * 3 * 2
477 
478  // Coordinates of the first u,v texture
479  glNormal3f( trim->NormCoord[trim->Indices[i] * 3 + 0], trim->NormCoord[trim->Indices[i] * 3 + 1], trim->NormCoord[trim->Indices[i] * 3 + 2 ]);//Normals
480  glTexCoord2f( trim->MeshCoord[trim->Indices[i] * 2 + 0 ] , trim->MeshCoord[ trim->Indices[i] * 2 + 1] );
481  // Coordinates of the first vertex
482  glVertex3f( trim->Vertices[trim->Indices[i] * 3 + 0], trim->Vertices[trim->Indices[i] * 3 + 1], trim->Vertices[trim->Indices[i] * 3 + 2] );//Vertex definition
483  i++;
484 
485  // Coordinates of the first u,v texture
486  glNormal3f( trim->NormCoord[trim->Indices[i] * 3 + 0], trim->NormCoord[trim->Indices[i] * 3 + 1], trim->NormCoord[trim->Indices[i] * 3 + 2 ]);
487  glTexCoord2f( trim->MeshCoord[trim->Indices[i] * 2 + 0 ] , trim->MeshCoord[ trim->Indices[i] * 2 + 1] );
488  // Coordinates of the second vertex
489  glVertex3f( trim->Vertices[trim->Indices[i] * 3 + 0], trim->Vertices[trim->Indices[i] * 3 + 1], trim->Vertices[trim->Indices[i] * 3 + 2] );//Vertex definition
490  i++;
491 
492  // Coordinates of the first u,v texture
493  glNormal3f( trim->NormCoord[trim->Indices[i] * 3 + 0], trim->NormCoord[trim->Indices[i] * 3 + 1], trim->NormCoord[trim->Indices[i] * 3 + 2 ]);
494  glTexCoord2f( trim->MeshCoord[trim->Indices[i] * 2 + 0 ] , trim->MeshCoord[ trim->Indices[i] * 2 + 1] );
495  // Coordinates of the Third vertex
496  glVertex3f( trim->Vertices[trim->Indices[i] * 3 + 0], trim->Vertices[trim->Indices[i] * 3 + 1], trim->Vertices[trim->Indices[i] * 3 + 2] );//Vertex definition
497  i++;
498  }
499  glEnd();
500  glEnable( GL_CULL_FACE );
501  glDisable(GL_TEXTURE_2D);
502 }
503 
504 void setupTexture(char *filename, int whichtexture){
505 
506  Texture[whichtexture] = LoadBitmapTERMINAL(filename, whichtexture);
507 }
508 
509 int LoadBitmapTERMINAL(char *filename, int whichtexture)
510 {
511  //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
512  //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
513  // The next commands sets the texture parameters
514  //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
515  //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
516  //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
517  //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); //The minifying function
518  glEnable(GL_TEXTURE_2D);
519 
520  //GLuint texture;
521  FILE * file1;
522  char temp1;
523  long i;
524  SIMBITMAPINFOHEADER infoheader1;
525  //yDebug() << " RENDERING NUMBER TEXTURE " << whichtexture;
526  yDebug() << "Loading texture " << " '" << filename << "' ";
527  num_texture++; // The counter of the current texture is increased
528  if( (file1 = fopen(filename, "rb"))==NULL){ yError() << "Cannot load/find texture file "; return (0); }// Open the file for reading
529  yDebug() << "......... OK! ";
530  fseek(file1, 18, SEEK_CUR); // start reading width & height
531 
532  size_t ret=0;
533  ret=fread(&infoheader1.biWidth, sizeof(int), 1, file1);
534 
535  if (ret!=1)
536  return 0;
537 
538  ret=fread(&infoheader1.biHeight, sizeof(int), 1, file1);
539  if (ret!=1)
540  return 0;
541 
542  ret=fread(&infoheader1.biPlanes, sizeof(short int), 1, file1);
543 
544  if (ret!=1)
545  return 0;
546 
547  yDebug() << "Texture Size " << infoheader1.biHeight << " " << infoheader1.biWidth;
548  if (infoheader1.biPlanes != 1) {
549  yDebug("Planes from %s is not 1: %u\n", filename, infoheader1.biPlanes);
550  return 0;
551  }
552  // read the bpp
553  ret=fread(&infoheader1.biBitCount, sizeof(unsigned short int), 1, file1);
554 
555  if (ret!=1)
556  return 0;
557 
558  if (infoheader1.biBitCount != 24) {
559  yDebug("Bpp from %s is not 24: %d\n", filename, infoheader1.biBitCount);
560  return 0;
561  }
562  fseek(file1, 24, SEEK_CUR);
563 
564  // read the data.
565  infoheader1.data = (char *) malloc(infoheader1.biWidth * infoheader1.biHeight * 6);
566  if (infoheader1.data == NULL) {
567  yError("Error allocating memory for color-corrected image data\n");
568  return 0;
569  }
570 
571  if ((i = fread(infoheader1.data, infoheader1.biWidth * infoheader1.biHeight * 3, 1, file1)) != 1) {
572  yError("Error reading image data from %s.\n", filename);
573  return 0;
574  }
575 
576  for (i=0; i<(infoheader1.biWidth * infoheader1.biHeight * 3); i+=3) { // reverse all of the colors. (bgr -> rgb)
577  temp1 = infoheader1.data[i];
578  infoheader1.data[i] = infoheader1.data[i+2];
579  infoheader1.data[i+2] = temp1;
580  }
581  fclose(file1); // Closes the file stream
582 
583  //glGenTextures(1, &texture);
584  glBindTexture(GL_TEXTURE_2D, whichtexture);// Bind the ID texture specified by the 2nd parameter
585 
586  yInfo() << "Finished Binding texture ";
587  yInfo() << "Finished Setting parameters ";
588  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // We don't combine the color with the original surface color, use only the texture map.
589  yInfo() << "Finished Setting glTexEnvf ";
590  // Finally we define the 2d texture
591  glTexImage2D(GL_TEXTURE_2D, 0, 3, infoheader1.biWidth, infoheader1.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, infoheader1.data);
592  yInfo() << "Finished Setting glTexImage2D ";
593 
594  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, infoheader1.biWidth, infoheader1.biHeight, GL_RGB, GL_UNSIGNED_BYTE, infoheader1.data);
595 
596  yInfo() << "Finished Setting gluBuild2DMipmaps ";
597  free(infoheader1.data); // Free the memory we used to load the texture
598 
599  yInfo() << "Finished creating 3D Model................\n";
600  //glDisable(GL_TEXTURE_2D);
601  return ( whichtexture ); // Returns the current texture OpenGL ID
602 }
void DrawVideo(VideoTexture *video)
Definition: rendering.cpp:456
void DrawGround(bool wireframe)
Definition: rendering.cpp:122
static VideoTexture * video
Definition: iCub_Sim.cpp:82
void DrawSphere(float radius, bool wireframe, bool texture, int whichtexture)
Definition: rendering.cpp:329
const dReal * pos
Definition: iCub_Sim.cpp:62
Header file for the video stream texture.
#define FLOOR
Definition: rendering.h:42
void DrawCylinder(float radius, float length, bool wireframe, bool texture, int whichtexture)
Definition: rendering.cpp:353
GLUquadricObj * cap
Definition: rendering.cpp:57
STL namespace.
float * MeshCoord
Definition: xloader.h:49
static int width
Definition: iCub_Sim.cpp:53
int IndexCount
Definition: xloader.h:48
void DrawBox(float width, float height, float length, bool wireframe, bool texture, int whichtexture)
Definition: rendering.cpp:226
GLuint nicetexture
Definition: rendering.cpp:49
int * Indices
Definition: xloader.h:47
FILE * file
Definition: main.cpp:81
const GLfloat light_position[]
Definition: rendering.cpp:46
void LDEsetM(const dReal *pos, const dReal *R)
Definition: rendering.cpp:400
int num_texture1
Definition: rendering.cpp:50
const GLfloat light_diffuse[]
Definition: rendering.cpp:44
GLuint LoadTextureRAW(const char *filename, int wrap)
Definition: rendering.cpp:421
static int height
Definition: iCub_Sim.cpp:54
unsigned short biBitCount
Definition: rendering.h:50
int LoadBitmapTERMINAL(char *filename, int whichtexture)
Definition: rendering.cpp:509
unsigned int Texture[200]
Definition: rendering.cpp:53
Header file for the rendering.
const GLfloat light_ambient[]
Definition: rendering.cpp:43
void drawSkyDome(float x, float y, float z, float width, float height, float length)
Definition: rendering.cpp:159
void setupTexture(char *filename, int whichtexture)
Definition: rendering.cpp:504
const GLfloat light_specular[]
Definition: rendering.cpp:45
GLuint num_texture
Definition: rendering.cpp:51
float * Vertices
Definition: xloader.h:45
fclose(fileID)
GLUquadricObj * cylinder
Definition: rendering.cpp:56
void DrawX(dTriMeshX trim, int whichtexture)
Definition: rendering.cpp:461
static GLuint FindTextureRAW(const char *str, bool flag)
Definition: rendering.cpp:59
unsigned short biPlanes
Definition: rendering.h:49
GLUquadricObj * sphere
Definition: rendering.cpp:55
bool setup_opengl(ResourceFinder &finder)
Definition: rendering.cpp:64
float * NormCoord
Definition: xloader.h:51
GLenum mode
Definition: rendering.cpp:48
void apply(unsigned int *textures)