Página principal | Jerarquía de la clase | Lista alfabética | Lista de componentes | Lista de archivos | Miembros de las clases | Archivos de los miembros | Páginas relacionadas

mesh_gl.C

Ir a la documentación de este archivo.
00001 //===========================================================================
00002 //= mesh_gl.cc                                              Octubre de 1999 =
00003 //=-------------------------------------------------------------------------=
00004 //= Modulo de representacion y despliegue de geometrias 3D en mallas de     =
00005 //= triangulos. Este modulo contiene la implementacion de la clase MESH y   =
00006 //= esta dividido en las siguientes secciones:                              =
00007 //=   - Operaciones de visualizacion                                        =
00008 //=       crear_trozos_gl, crear_tiras, pintar_*_gl, nucleo_pintar_gl y     =
00009 //=       compilar_lista_gl                                                 =
00010 //= Notese que este es un modulo extraido de mesh.cc                        =
00011 //=-------------------------------------------------------------------------=
00012 //= Basado en el proyecto "3D Studio to OpenGL File Converter" de David     =
00013 //= Farrell, fdavid@cyberramp.net                                           =
00014 //= Modificado y extendido por Oscar Chavarro     oscarchavarro@hotmail.com =
00015 //===========================================================================
00016 
00017 #include <math.h>
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include <ctype.h>
00022 
00023 #include "toolkits/geom/mesh.h"
00024 
00025 #ifdef GL_VERSION_1_1  // OJO: Toca revisar si esta vaina funciona bien!
00026     #define GL_COMPLETO
00027 #endif
00028 
00029 #if (PLATAFORMA == SGI) || (PLATAFORMA == i386_WIN32_VC)
00030     #undef GL_COMPLETO  // OJO: Esto es un machete!
00031 #endif
00032 
00033 #define DEBUG_MESH
00034 
00035 //===========================================================================
00036 //= Constantes y macros                                                     =
00037 //===========================================================================
00038 
00039 #define PINTAR_NORMALES()                                               \
00040     double x, y, z, nx, ny, nz;                                          \
00041   if ( pintar_normales ) {                                              \
00042     glColor3f(1, 1, 0);                                                 \
00043     glDisable(GL_LIGHTING);                                             \
00044     glDisable(GL_TEXTURE_2D); \
00045     glDisable(GL_BLEND);      \
00046     glShadeModel(GL_FLAT);    \
00047     for ( i = 0; i < arr_trozos_gl.tam(); i++ ) {                             \
00048       if ( !Arr_control || (Arr_control && Arr_control[i]) ) {          \
00049         glBegin(GL_LINES);                                              \
00050         for ( j = 0; j < arr_trozos_gl[i].arr_triangulos.tam(); j++ ) {       \
00051            nx = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].nx;  \
00052            ny = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].ny;  \
00053            nz = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].nz;  \
00054            x = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].x;    \
00055            y = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].y;    \
00056            z = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].z;    \
00057            glVertex3d(x, y, z);                                         \
00058            glVertex3d(x + nx/escala/10, y+ny/escala/10, z+nz/escala/10);\
00059            nx = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].nx;  \
00060            ny = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].ny;  \
00061            nz = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].nz;  \
00062            x = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].x;    \
00063            y = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].y;    \
00064            z = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].z;    \
00065            glVertex3d(x, y, z);                                         \
00066            glVertex3d(x + nx/escala/10, y+ny/escala/10, z+nz/escala/10);\
00067            nx = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].nx;  \
00068            ny = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].ny;  \
00069            nz = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].nz;  \
00070            x = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].x;    \
00071            y = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].y;    \
00072            z = arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].z;    \
00073            glVertex3d(x, y, z);                                         \
00074            glVertex3d(x + nx/escala/10, y+ny/escala/10, z+nz/escala/10);\
00075         }                                                               \
00076         glEnd();                                                        \
00077       }                                                                 \
00078     }                                                                   \
00079   }
00080 
00081 #define ACTIVAR_TEXTURA_TROZO()                         \
00082     if ( !(material_global && imagen) ) {  \
00083             if ( Calidad->con_textura ) {                       \
00084                 arr_trozos_gl[i].init(_nombre_de_archivo);      \
00085                 if ( arr_trozos_gl[i].Imagen ) {                \
00086                     arr_trozos_gl[i].Imagen->activar_gl();      \
00087                     glEnable(GL_TEXTURE_2D);                    \
00088                   }                                             \
00089                   else {                                        \
00090                     glDisable(GL_TEXTURE_2D);                   \
00091                 }                                               \
00092               }                                                 \
00093               else {                                            \
00094                 glDisable(GL_TEXTURE_2D);                       \
00095             } \
00096 }
00097 
00098 #define PINTAR_NORMALES_T()                                                   \
00099     double x, y, z, nx, ny, nz;                                               \
00100   if ( pintar_normales ) {                                                    \
00101     glColor3f(1, 1, 0);                                                       \
00102     glDisable(GL_LIGHTING);                                                   \
00103     for ( i = 0; i < arr_trozos_gl.tam(); i++ ) {                             \
00104         if ( !Arr_control || (Arr_control && Arr_control[i]) ) {              \
00105             for ( j = 0; j < arr_trozos_gl[i].indices_vertices_tiras.tam(); j++ ) { \
00106                 glBegin(GL_LINES);                                            \
00107                  nx = arr_vertices[arr_trozos_gl[i].indices_vertices_tiras[j]].nx;  \
00108                  ny = arr_vertices[arr_trozos_gl[i].indices_vertices_tiras[j]].ny;  \
00109                  nz = arr_vertices[arr_trozos_gl[i].indices_vertices_tiras[j]].nz;  \
00110                  x = arr_vertices[arr_trozos_gl[i].indices_vertices_tiras[j]].x;    \
00111                  y = arr_vertices[arr_trozos_gl[i].indices_vertices_tiras[j]].y;    \
00112                  z = arr_vertices[arr_trozos_gl[i].indices_vertices_tiras[j]].z;    \
00113                  glVertex3d(x, y, z);                                         \
00114                  glVertex3d(x + nx/escala/10, y+ny/escala/10, z+nz/escala/10);\
00115                 glEnd();                                                      \
00116             }                                                                 \
00117         }                                                                     \
00118     }                                                                         \
00119   }
00120 
00121 // OJO: Si, parece loco copiar todo al material global y usarlo para activar_gl
00122 //      pero arr_trozos_gl[i].material.activar_gl NO funciona (misterioso no?)
00123 
00124 COLOR AA, DD, EE, AAb, DDb, EEb; // OJO!
00125 
00126 #define CONFIGURAR_MATERIAL()                                                 \
00127     if ( !material_global &&                                                  \
00128          !(arr_trozos_gl[i].Imagen && Calidad->con_textura) ) {               \
00129         DD = arr_trozos_gl[i].material.difusa();                              \
00130         AA = arr_trozos_gl[i].material.ambiente();                            \
00131         EE = arr_trozos_gl[i].material.especular();                           \
00132         DDb = MATERIAL_Global->difusa();                                      \
00133         AAb = MATERIAL_Global->ambiente();                                    \
00134         EEb = MATERIAL_Global->especular();                                   \
00135         MATERIAL_Global->set_difusa(DD);                                      \
00136         MATERIAL_Global->set_ambiente(AA);                                    \
00137         MATERIAL_Global->set_especular(EE);                                   \
00138     }                                                                         \
00139     MATERIAL_Global->set_doble_cara(arr_trozos_gl[i].material.doble_cara());  \
00140     MATERIAL_Global->activar_gl();                                            \
00141     if ( !material_global &&                                                  \
00142          !(arr_trozos_gl[i].Imagen && Calidad->con_textura) ) {               \
00143         MATERIAL_Global->set_difusa(DDb);                                     \
00144         MATERIAL_Global->set_ambiente(AAb);                                   \
00145         MATERIAL_Global->set_especular(EEb);                                  \
00146     }
00147 
00148 //===========================================================================
00149 //= Variables globales                                                      =
00150 //===========================================================================
00151 
00152 #ifdef GL_ENABLED
00153 static MATERIAL *MATERIAL_Global;
00154 #define NCT 12
00155 static COLOR COLORES_tiras[NCT];
00156 static BOOLEAN CT_i = FALSE;
00157 
00158 void
00159 inicializar_ct(void)
00160 {
00161     if ( CT_i ) return;
00162     COLORES_tiras[0].r = 1;  COLORES_tiras[0].g = 0;  COLORES_tiras[0].b = 0;
00163     COLORES_tiras[1].r = 0;  COLORES_tiras[1].g = 1;  COLORES_tiras[1].b = 0;
00164     COLORES_tiras[2].r = 0;  COLORES_tiras[2].g = 0;  COLORES_tiras[2].b = 1;
00165     COLORES_tiras[3].r = 1;  COLORES_tiras[3].g = 1;  COLORES_tiras[3].b = 0;
00166     COLORES_tiras[4].r = 1;  COLORES_tiras[4].g = 0;  COLORES_tiras[4].b = 1;
00167     COLORES_tiras[5].r = 0;  COLORES_tiras[5].g = 1;  COLORES_tiras[5].b = 1;
00168     COLORES_tiras[6].r = 0.9f;  COLORES_tiras[6].g = 0.5f;  COLORES_tiras[6].b = 0.5f;
00169     COLORES_tiras[7].r = 0.5f;  COLORES_tiras[7].g = 0.9f;  COLORES_tiras[7].b = 0.5f;
00170     COLORES_tiras[8].r = 0.5f;  COLORES_tiras[8].g = 0.5f;  COLORES_tiras[8].b = 0.9f;
00171     COLORES_tiras[9].r = 0.9f;  COLORES_tiras[9].g = 0.9f;  COLORES_tiras[9].b = 0.5f;
00172     COLORES_tiras[10].r = 0.9f;  COLORES_tiras[10].g = 0.5f;  COLORES_tiras[10].b = 0.9f;
00173     COLORES_tiras[11].r = 0.5f;  COLORES_tiras[11].g = 0.9f;  COLORES_tiras[11].b = 0.9f;
00174     CT_i = TRUE;
00175 }
00176 
00177 #endif
00178 
00179 //===========================================================================
00180 
00181 TROZO_GL::TROZO_GL() : 
00182         arr_triangulos(1), indices_vertices_tiras(10), inicios_tiras(10), 
00183         longitudes_tiras(10) 
00184 { 
00185     Imagen = NULL;
00186     inicializado = FALSE;
00187 }
00188 
00189 void
00190 TROZO_GL::init(char *patron)
00191 {  
00192     if ( inicializado ) return;
00193     Imagen = NULL;
00194     if ( !texturename || !strlen(texturename) ) {
00195         inicializado = TRUE;
00196         return;
00197     }
00198 
00199     //- Determine el path de donde se lee el archivo ------------------------
00200     char path[2048];
00201     int i;
00202 
00203     if ( !patron || !strlen(patron) ) {
00204         strcpy(path, "./");
00205     }
00206 
00207     strcpy(path, patron);
00208     for ( i = strlen(path)-1; i >= 0; i-- ) {
00209         if ( path[i] == '/' || path[i] == '\\'  ) {
00210             path[i+1] = '\0';
00211             break;
00212         }
00213     }
00214     if ( !strchr(patron, '/') && !strchr(patron, '\\') ) {
00215         strcpy(path, "./");
00216     }
00217 
00218     //- Intente abrir un archivo con el nombre o uno similar ----------------
00219     FILE *fd;
00220     char *ptr;
00221     char archivo[2048];
00222 
00223     printf("<MESH> - Busco la textura %12s:\t", texturename); 
00224     sprintf(archivo, "%s", texturename);
00225     fd = fopen(archivo, "rb");
00226     if ( !fd ) {
00227         sprintf(archivo, "%s%s", path, texturename);
00228         fd = fopen(archivo, "rb");
00229         if ( !fd ) {
00230             ptr = strchr(texturename, '.');
00231             if ( ptr ) {
00232                 ptr[1] = tolower(ptr[1]);
00233                 ptr[2] = tolower(ptr[2]);
00234                 ptr[3] = tolower(ptr[3]);
00235             }
00236             sprintf(archivo, "%s%s", path, texturename);
00237             fd = fopen(archivo, "rb");
00238             if ( !fd ) {
00239                 for ( i = 0; texturename[i]; i++ ) {
00240                     texturename[i] = tolower(texturename[i]);
00241                 }
00242                 sprintf(archivo, "%s%s", path, texturename);
00243                 fd = fopen(archivo, "rb");
00244             }
00245         }
00246     }
00247 
00248     if ( fd ) {
00249         for ( i = 0; texturename[i]; i++ ) {
00250             texturename[i] = tolower(texturename[i]);
00251         }
00252         if ( strstr(texturename, "jpg") ) {
00253 #ifdef JPEG_ENABLED
00254             Imagen = new IMAGEN_RGB();
00255             if ( Imagen ) {
00256                 if ( !Imagen->importar_jpeg(fd) ) {
00257                     printf("No pude cargar un jpg\n");
00258                     delete Imagen;
00259                     Imagen = NULL;
00260                   }
00261                   else {
00262                     printf("JPEG OK!\n");
00263                 }
00264 #define MACHETE_VOODOO 1
00265 #ifdef MACHETE_VOODOO
00266                 if ( Imagen->xtam() > 256 || Imagen->ytam() > 256 ) {
00267                     printf("<MESH> Pilas, reescalando textura...\n"); 
00268                     fflush(stdout);
00269                     fclose(fd);
00270                     sprintf(path, 
00271                             "convert -geometry 128x128 \"%s\" /tmp/imgtmp.jpg",
00272                             archivo);
00273                     system(path);
00274                     fd = fopen("/tmp/imgtmp.jpg", "rb");
00275                     Imagen->importar_jpeg(fd);
00276                 }
00277 #endif
00278             }
00279             ;
00280 #endif
00281           }
00282           else {
00283             printf("Formato no soportado.\n");
00284         }
00285         fclose(fd);
00286       }
00287       else {
00288         printf("No encontre ningun archivo!\n"); fflush(stdout);
00289     }
00290 
00291     //-----------------------------------------------------------------------
00292 
00293     inicializado = TRUE;
00294 }
00295 
00296 //===========================================================================
00297 //= Operaciones de visualizacion OpenGL de la clase MESH                    =
00298 //===========================================================================
00299 
00300 #ifdef GL_ENABLED
00301 void
00302 MESH::pintar_triangulos_gl(CALIDAD_VISUAL *Calidad)
00309 {
00310     int i, j;
00311     double r, g, b;
00312 
00313 #define COLOR_AR(p)                                                         \
00314  r = (double)(arr_colores[arr_trozos_gl[i].triangulos_gl[j].p*3]) / 255;    \
00315  g = (double)(arr_colores[arr_trozos_gl[i].triangulos_gl[j].p*3+1]) / 255;  \
00316  b = (double)(arr_colores[arr_trozos_gl[i].triangulos_gl[j].p*3+2]) / 255;  \
00317  glColor3d(r, g, b);
00318 
00319   if ( arr_colores.tam() >= arr_vertices.tam() ) {
00320     glShadeModel(GL_SMOOTH);
00321 
00322     for ( i = 0; i < arr_trozos_gl.tam(); i++ ) {
00323         CONFIGURAR_MATERIAL();
00324         if ( !Arr_control || (Arr_control && Arr_control[i]) ) {
00325             glBegin(GL_TRIANGLES);
00326         for ( j = 0; j < arr_trozos_gl[i].arr_triangulos.tam(); j++ ) {
00327           ACTIVAR_TEXTURA_TROZO();
00328           COLOR_AR(p0);
00329           glTexCoord2fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].u);
00330           glNormal3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].nx);
00331           glVertex3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].x);
00332 
00333           COLOR_AR(p1);
00334           glTexCoord2fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].u);
00335           glNormal3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].nx);
00336           glVertex3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].x);
00337 
00338           COLOR_AR(p2);
00339           glTexCoord2fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].u);
00340           glNormal3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].nx);
00341           glVertex3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].x);
00342         }
00343             glEnd();
00344         }
00345     }
00346   }
00347   else {
00348     for ( i = 0; i < arr_trozos_gl.tam(); i++ ) {
00349         CONFIGURAR_MATERIAL();
00350         if ( !Arr_control || (Arr_control && Arr_control[i]) ) {
00351             glBegin(GL_TRIANGLES);
00352         for ( j = 0; j < arr_trozos_gl[i].arr_triangulos.tam(); j++ ) {
00353           glTexCoord2fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].u);
00354           glNormal3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].nx);
00355           glVertex3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p0].x);
00356 
00357           glTexCoord2fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].u);
00358           glNormal3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].nx);
00359           glVertex3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p1].x);
00360 
00361           glTexCoord2fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].u);
00362           glNormal3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].nx);
00363           glVertex3fv(&arr_vertices[arr_trozos_gl[i].triangulos_gl[j].p2].x);
00364         }
00365             glEnd();
00366         }
00367     }
00368   }
00369 
00370     PINTAR_NORMALES();
00371 }
00372 
00373 void
00374 MESH::pintar_debug_vertice(long int i)
00375 {
00376     TRIANGLENODE *ptr;
00377     int acum;
00378     double x, y, z;
00379 
00380     for( acum = 0,ptr = listas_de_referencias[i]; ptr; ptr = ptr->sig,acum++ );
00381 
00382     //clrscr();
00383     printf("<MESH> Pinto el vertice %ld de %ld (%ld):\n", 
00384         i+1, arr_vertices.tam(), listas_de_referencias.tam());
00385     printf("  - Tiene %d triangulos incidentes.\n", acum);
00386     fflush(stdout);
00387 
00388 //    if ( i < 0 || i >= num_vertices() ) return;
00389 
00390     //- Pinto los triangulos incidentes en el vertice -----------------------
00391     TRIANGULITO *triangle;
00392 
00393     MATERIAL m;
00394     COLOR c(1, 0, 0);
00395     m.set_difusa(c);
00396     m.set_transparencia(0.2);
00397     m.activar_gl();
00398 
00399     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00400     glEnable(GL_LIGHTING);
00401     glDisable(GL_DEPTH_TEST);
00402     glBegin(GL_TRIANGLES);
00403     for( ptr = listas_de_referencias[i]; ptr; ptr = ptr->sig ) { 
00404         triangle = ptr->triangle;
00405         x = arr_vertices[triangle->p0].x;
00406         y = arr_vertices[triangle->p0].y;
00407         z = arr_vertices[triangle->p0].z;
00408         glVertex3d(x, y, z);
00409         x = arr_vertices[triangle->p1].x;
00410         y = arr_vertices[triangle->p1].y;
00411         z = arr_vertices[triangle->p1].z;
00412         glVertex3d(x, y, z);
00413         x = arr_vertices[triangle->p2].x;
00414         y = arr_vertices[triangle->p2].y;
00415         z = arr_vertices[triangle->p2].z;
00416         glVertex3d(x, y, z);
00417     }
00418     glEnd();
00419     glEnable(GL_DEPTH_TEST);
00420 
00421     x = arr_vertices[i].x;
00422     y = arr_vertices[i].y;
00423     z = arr_vertices[i].z;
00424 
00425     //- Pinto el vertice como una cruz --------------------------------------
00426     double dx, dy, dz;
00427 
00428     dx = dy = dz = 1;
00429 
00430     glDisable(GL_LIGHTING);
00431     glDisable(GL_TEXTURE_2D);
00432     glDisable(GL_BLEND);
00433     glShadeModel(GL_FLAT);
00434     
00435     glColor3d(0, 1, 0);
00436     glBegin(GL_LINES);
00437         glVertex3d(x-dx, y, z); glVertex3d(x+dx, y, z);
00438         glVertex3d(x, y-dy, z); glVertex3d(x, y+dy, z);
00439         glVertex3d(x, y, z-dz); glVertex3d(x, y, z+dz);
00440     glEnd();
00441 
00442     //- Pinto el vertice como una esferita ----------------------------------
00443     CALIDAD_VISUAL v;
00444     ESFERA e(0.2);
00445 
00446     m.set_transparencia(0.4);
00447     glPushMatrix();
00448     glTranslated(x, y, z);
00449     e.pintar_gl(&v, &m, NULL);
00450     glPopMatrix();
00451 }
00452 
00453 void
00454 MESH::pintar_debug_triangulo(long int i)
00455 {
00456     if ( i < 0 || i >= num_triangulos() ) return;
00457     printf("<MESH> Pintar_debug_triangulo no implementado\n");
00458     fflush(stdout);
00459 }
00460 
00461 #ifdef GL_COMPLETO
00462 
00463 void
00464 MESH::pintar_triangulosv_gl(CALIDAD_VISUAL *Calidad)
00470 {
00471     int i, j;
00472 
00473     glInterleavedArrays(GL_T2F_N3F_V3F, 0, arr_vertices.data());
00474     if ( arr_colores.tam() >= arr_vertices.tam() ) {
00475         glEnableClientState(GL_COLOR_ARRAY);
00476         glDisable(GL_CULL_FACE);
00477         glShadeModel(GL_SMOOTH);
00478         glColorPointer(3, GL_UNSIGNED_BYTE, 0, arr_colores.data());
00479     }
00480 
00481     for ( i = 0; i < arr_trozos_gl.tam(); i++ ) {
00482         if ( !Arr_control || (Arr_control && Arr_control[i]) ) {
00483             ACTIVAR_TEXTURA_TROZO();
00484             if ( arr_colores.tam() < arr_vertices.tam() ) {
00485                 CONFIGURAR_MATERIAL();
00486             }
00487             glDrawElements(GL_TRIANGLES,
00488                 arr_trozos_gl[i].arr_triangulos.tam()*3, GL_UNSIGNED_INT, 
00489                 &arr_trozos_gl[i].triangulos_gl[0]);
00490         }
00491     }
00492     if ( arr_colores.tam() >= arr_vertices.tam() ) {
00493         glDisable(GL_COLOR_ARRAY);
00494     }
00495     PINTAR_NORMALES();
00496 }
00497 
00498 #endif
00499 
00500 void
00501 MESH::pintar_triangulos_tira_gl(CALIDAD_VISUAL *Calidad)
00507 {
00508     int i, j, k, current;
00509 
00510     if ( mostrar_tiras ) inicializar_ct();
00511 
00512     for ( i = 0; i < arr_trozos_gl.tam(); i++ ) {
00513         CONFIGURAR_MATERIAL();
00514         current = 0;
00515         if ( !Arr_control || (Arr_control && Arr_control[i]) ) {
00516             ACTIVAR_TEXTURA_TROZO();
00517             for ( j = 0; j < arr_trozos_gl[i].longitudes_tiras.tam(); j++ ) {
00518                 if ( mostrar_tiras ) {
00519                     MATERIAL_Global->set_difusa(COLORES_tiras[j%NCT]);
00520                     MATERIAL_Global->activar_gl();
00521                 }
00522                 glBegin(GL_TRIANGLE_STRIP);
00523                 for ( k=0; (GLuint)k < arr_trozos_gl[i].longitudes_tiras[j]; k++ ) {
00524                     glTexCoord2fv(&arr_vertices[
00525                             arr_trozos_gl[i].indices_vertices_tiras[current+k]].u);
00526                     glNormal3fv(&arr_vertices[
00527                             arr_trozos_gl[i].indices_vertices_tiras[current+k]].nx);
00528                     glVertex3fv(&arr_vertices[
00529                             arr_trozos_gl[i].indices_vertices_tiras[current+k]].x);
00530                 }
00531                 glEnd();
00532                 current += arr_trozos_gl[i].longitudes_tiras[j];
00533             }
00534         }
00535     }
00536 
00537 /* OJO: Este bloque de compilacion condicional no se ha probado en 
00538         plataformas donde GL_COMPLETO no esta completo... */
00539 #ifdef GL_COMPLETO
00540     if ( arr_colores.tam() >= arr_vertices.tam() ) {
00541         glDisable(GL_COLOR_ARRAY);
00542     }
00543 #endif
00544 
00545     PINTAR_NORMALES_T();
00546 }
00547 
00548 #ifdef GL_COMPLETO
00549 
00550 void
00551 MESH::pintar_triangulosv_tira_gl(CALIDAD_VISUAL *Calidad)
00552 /* Draw triangle strips */
00553 {
00554     int i, j, current;
00555 
00556     if ( mostrar_tiras ) inicializar_ct();
00557     glInterleavedArrays(GL_T2F_N3F_V3F, 0, arr_vertices.data());
00558     if ( arr_colores.tam() >= arr_vertices.tam() ) {
00559         glEnableClientState(GL_COLOR_ARRAY);
00560         glDisable(GL_CULL_FACE);
00561         glShadeModel(GL_SMOOTH);
00562         glColorPointer(3, GL_UNSIGNED_BYTE, 0, arr_colores.data());
00563     }
00564     for ( i = 0; i < arr_trozos_gl.tam(); i++ ) {
00565         if ( arr_colores.tam() < arr_vertices.tam() ) {
00566             CONFIGURAR_MATERIAL();
00567         }
00568         current = 0;
00569 
00570         if ( !Arr_control || (Arr_control && Arr_control[i]) ) {
00571             ACTIVAR_TEXTURA_TROZO();
00572             for ( j = 0; j < arr_trozos_gl[i].longitudes_tiras.tam(); j++ ) {
00573                 if ( mostrar_tiras ) {
00574                     MATERIAL_Global->set_difusa(COLORES_tiras[j%NCT]);
00575                     MATERIAL_Global->activar_gl();
00576                 }
00577                 glDrawElements(GL_TRIANGLE_STRIP,
00578                     arr_trozos_gl[i].longitudes_tiras[j], GL_UNSIGNED_INT,
00579                     &arr_trozos_gl[i].indices_vertices_tiras.data()[current]);
00580                 current += arr_trozos_gl[i].longitudes_tiras[j];
00581             }
00582         }
00583     }
00584 
00585     PINTAR_NORMALES_T();
00586 }
00587 
00588 #endif
00589 
00590 void
00591 MESH::compilar_lista_gl(int lista_gl, CALIDAD_VISUAL *Calidad)
00599 {
00600     if ( !con_lista_gl ) return;
00601 
00602     //printf("Compilo %s...\n", _nombre_de_archivo);
00603 
00604     glNewList(lista_gl, GL_COMPILE);
00605 
00606     if ( !modo_tiras_de_triangulos ) {
00607       #ifdef GL_COMPLETO
00608         pintar_triangulosv_gl(Calidad);
00609       #else
00610         pintar_triangulos_gl(Calidad);
00611       #endif
00612       }
00613       else {
00614       #ifdef GL_COMPLETO
00615         pintar_triangulosv_tira_gl(Calidad);
00616       #else
00617         pintar_triangulos_tira_gl(Calidad);
00618       #endif
00619     }
00620 
00621     glEndList();
00622 
00623     if ( glGetError() != GL_NO_ERROR ) {
00624         fprintf(stderr, "<MESH> - ERROR: Compilando una secuencia OpenGL!\n");
00625         fflush(stderr);
00626         exit(-1);
00627     }
00628 }
00629 
00630 void
00631 MESH::nucleo_pintar_gl(CALIDAD_VISUAL *Calidad)
00638 {
00639     if ( con_lista_gl ) {                       
00640         glCallList(id_lista_opengl);
00641       }                                         
00642       else if ( !modo_tiras_de_triangulos ) {   
00643        #ifdef GL_COMPLETO                       
00644         pintar_triangulosv_gl(Calidad);
00645        #else                                    
00646         pintar_triangulos_gl(Calidad);
00647        #endif                                   
00648       }                                         
00649       else {                                    
00650        #ifdef GL_COMPLETO                       
00651         pintar_triangulosv_tira_gl(Calidad);
00652        #else                                    
00653         pintar_triangulos_tira_gl(Calidad);
00654        #endif                                   
00655       }
00656 }
00657 
00658 void
00659 MESH::pintar_gl(CALIDAD_VISUAL *Calidad, MATERIAL* Material,
00660                 CAMARA * /*Camara*/)
00668 {
00669 //
00670 #ifdef NONONO
00671     long int i;
00672     double x, y, z;
00673 
00674     glDisable(GL_LIGHTING);
00675     glColor3d(1, 1, 1);
00676     glBegin(GL_POINTS);
00677     for ( i = 0; i < arr_vertices.tam(); i++ ) {
00678         x = arr_vertices[i].x;
00679         y = arr_vertices[i].y;
00680         z = arr_vertices[i].z;
00681         glVertex3d(x, y, z);;
00682     }
00683     glEnd();
00684     return;
00685 #endif
00686 //
00687 
00688     MATERIAL_Global = Material;
00689 
00690     glDisable(GL_TEXTURE_2D);
00691     glPushMatrix();
00692 
00693     if ( Calidad->con_cajas ) PINTAR_MINMAX_GL();
00694 
00695     //- Asegurese de que la estructura de datos esta Ok. -------------------
00696     glScalef((float)escala, (float)escala, (float)escala);
00697     if ( !preprocesada ) init();
00698 
00699     static int vistas = 1;
00700     if ( !preprocesada_gl ) {
00701         id_lista_opengl = glGenLists(1);
00702         compilar_lista_gl(id_lista_opengl, Calidad);
00703         // OJO: Esta molestando cuando hay varias vistas!
00704         vistas--;
00705         if ( vistas <= 0 ) preprocesada_gl = TRUE;
00706     }
00707 
00708     //- Pintado de bordes --------------------------------------------------
00709     COLOR cb(1, 0, 0);
00710 
00711     if ( Calidad->con_bordes ) {
00712         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00713         if ( !Calidad->con_caras ) glDisable(GL_CULL_FACE);
00714         Calidad->activar_bordes_gl(cb, Material);
00715         nucleo_pintar_gl(Calidad);
00716     }
00717 
00718     //- Pintado de caras ---------------------------------------------------
00719     COLOR c(1, 1, 1);
00720 
00721     if ( Calidad->con_caras ) {
00722         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00723         Calidad->activar_caras_gl(c, Material);
00724 
00725         if ( imagen && Calidad->con_textura ) {
00726             if ( material_global ) { 
00727                 imagen->activar_gl(); 
00728                 glEnable(GL_TEXTURE_2D);
00729             }
00730             Calidad->activar_textura_gl();
00731           }
00732           else if ( Calidad->con_caustics ) {
00733             Calidad->activar_textura_gl();
00734           }
00735           else {
00736             glDisable(GL_TEXTURE_2D);
00737         }
00738 
00739         ACTIVAR_POLYGON_OFFSET();
00740         nucleo_pintar_gl(Calidad);
00741         DESACTIVAR_POLYGON_OFFSET();
00742     }
00743     Material->set_doble_cara(FALSE);  // No quitar!
00744 
00745     //-----------------------------------------------------------------------
00746     if ( pintar_debug_vertices ) {
00747         pintar_debug_vertice(index_debug_vertice);
00748     }
00749     if ( pintar_debug_triangulos ) {
00750         pintar_debug_triangulo(index_debug_triangulo);
00751     }
00752     glPopMatrix();
00753 }
00754 #endif // GL_ENABLED
00755 
00756 //===========================================================================
00757 //= EOF                                                                     =
00758 //===========================================================================
00759 

Este archivo HTML ha sido generado automáticamente a partir del código fuente AQUYNZA. NO LO EDITE. Para mayor información contacte al autor.