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

visor_g.C

Ir a la documentación de este archivo.
00001 //===========================================================================
00002 //= visor_g.h                                                 Julio de 1998 =
00003 //=-------------------------------------------------------------------------=
00004 //= Definicion de clase VISOR_GEOMETRIA                                     =
00005 //=-------------------------------------------------------------------------=
00006 //= ADVERTENCIA: ESTE SOFTWARE NO ESTA CONCEBIDO NI DISENNADO PARA EL USO   =
00007 //= EN EQUIPO DE CONTROL EN LINEA EN ENTORNOS PELIGROSOS QUE REQUIERAN UN   =
00008 //= DESEMPENNO LIBRE DE FALLAS, COMO LA OPERACION DE PLANTAS NUCLEARES,     = 
00009 //= SISTEMAS DE NAVEGACION O COMUNICACION EN AVIONES, TRAFICO AEREO,        =
00010 //= EQUIPO MEDICO DEL CUAL DEPENDAN VIDAS HUMANAS O SISTEMAS DE ARMAMENTO,  =
00011 //= EN LOS CUALES UNA FALLA EN EL SOFTWARE PUEDA IMPLICAR DIRECTAMENTE LA   =
00012 //= MUERTE, DANNOS PERSONALES O DANNOS FISICOS Y/O AMBIENTALES GRAVES       =
00013 //= ("ACTIVIDADES DE ALGO RIESGO").                                         =
00014 //=-------------------------------------------------------------------------=
00015 //= Autor original: Oscar J. Chavarro G.  A.K.A. JEDILINK. Copyright (c),   =
00016 //= 1997 - 2003, oscarchavarro@hotmail.com                                  =
00017 //= AQUYNZA es software libre, y se rige bajo los terminos de la licencia   =
00018 //= LGPL de GNU (http://www.gnu.org). Para mayor informacion respecto a la  =
00019 //= licencia de uso, consulte el archivo ./doc/LICENCIA en la distribucion. =
00020 //===========================================================================
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 
00026 #include "framework/visual/visor_g.h"
00027 #include "toolkits/geom/mesh.h"
00028 #include "toolkits/media/jed_img.h"
00029 #include "matriz4.h"
00030 #include "lista.cc"
00031 
00032 //===========================================================================
00033 //= Constantes y macros                                                     =
00034 //===========================================================================
00035 
00036 #define INICIO_EULER() \
00037     R.importar_quaternion(orientacion); \
00038     R.exportar_angulos_euler(&yaw, &pitch, &roll);
00039 
00040 #define FIN_EULER() \
00041     R.rotacion_angulos_euler(yaw, pitch, roll); \
00042     orientacion = R.exportar_quaternion();
00043 
00044 #define ESPERO(tipo, msg) \
00045     if ( tipo_token != (tipo) ) { \
00046         fprintf(stderr, "<VISOR_GEOMETRIA> " \
00047           "ERROR: Esperaba %s y recibi [%s].\n", \
00048           (msg), cad); fflush(stderr);  return FALSE; \
00049     }
00050 
00051 #define VERIFICAR(c) \
00052     if ( !(c) ) {\
00053         fprintf(stderr, \
00054             "<VISOR_GEOMETRIA> " \
00055             "ERROR: No se pudo crear un objeto dinamico!\n");\
00056         fflush(stderr);\
00057         exit(1);\
00058     }
00059 
00060 //===========================================================================
00061 //= CLASE VISOR_GEOMETRIA                                                   =
00062 //===========================================================================
00063 
00064 VISOR_GEOMETRIA::VISOR_GEOMETRIA() : VISOR()
00065 {
00066     controlando_camara = TRUE;
00067     Geometria = NULL;
00068     mostrar_ejes = FALSE;
00069     modo_stereo = STEREO_NULL;
00070     init();
00071 }
00072 
00073 VISOR_GEOMETRIA::VISOR_GEOMETRIA(GEOMETRIA *g) : VISOR()
00074 {
00075     controlando_camara = TRUE;
00076     Geometria = g;
00077     mostrar_ejes = FALSE;
00078     modo_stereo = STEREO_NULL;
00079     init();
00080 }
00081 
00082 VISOR_GEOMETRIA::~VISOR_GEOMETRIA()
00083 {
00084     if ( _nombre_cosa ) delete _nombre_cosa;
00085 
00086     // OJO: La `Geometria` es responsabilidad de quien usa este visor, y aqui
00087     //      NO se borra!
00088     delete Material;
00089 }
00090 
00091 void
00092 VISOR_GEOMETRIA::init(void)
00093 {
00094 
00095     _nombre_cosa = NULL;
00096     mostrar_grilla = FALSE;
00097 
00098     Material = new MATERIAL;
00099 
00100     // Normalmente se tiene una camara mirando hacia el origen...
00101     //VECTOR p(2.5, -2.5, 2);
00102     //VECTOR p(1, -2.5, 2);
00103     VECTOR p(1.5, -2, 4.5);
00104     MATRIZ_4x4 R;
00105 
00106     camara.set_posicion(p);
00107     //R.rotacion_angulos_euler(DEG2RAD(135), DEG2RAD(-30), 0);
00108     //R.rotacion_angulos_euler(DEG2RAD(90), DEG2RAD(-45), 0);
00109     R.rotacion_angulos_euler(DEG2RAD(120), DEG2RAD(-50), 0);
00110     camara.set_orientacion(R.exportar_quaternion());
00111 
00112     // y la geometria no esta rotada
00113     R.identidad();
00114     orientacion = R.exportar_quaternion();
00115 
00116     luz1.set_id(0);
00117     luz2.set_id(1);
00118     _x_tam = 320;
00119     _y_tam = 200;
00120 }
00121 
00122 CAMARA *
00123 VISOR_GEOMETRIA::get_camara(void)
00124 {
00125     return &camara;
00126 }
00127 
00128 void
00129 VISOR_GEOMETRIA::set_geometria(GEOMETRIA *g)
00130 {
00131     Geometria = g;
00132 }
00133 
00134 char *
00135 VISOR_GEOMETRIA::nombre_cosa(void)
00136 {
00137     return _nombre_cosa;
00138 }
00139 
00140 #ifdef GL_ENABLED
00141 void
00142 VISOR_GEOMETRIA::pintar_nucleo_gl(void)
00143 {
00144     //- Cuadrar modelo de iluminacion ---------------------------------------
00145     static VECTOR lp1(25, 25, 50);
00146     static VECTOR lp2(-25, -25, -50);
00147 
00148     glEnable(GL_LIGHTING);
00149     luz1.set_posicion(lp1);
00150     luz1.activar_gl();
00151     luz2.set_posicion(lp2);
00152     luz2.activar_gl();
00153 
00154     // El material por dentro (por si acaso) es rojo mate
00155     // OJO: Notese que el material por dentro esta especificado en MATERIAL
00156     static GLfloat back_mat_shininess[] = {1.0f}; // puede ir de 1 a 128
00157     static GLfloat back_mat_specular[] = {0.1f, 0.1f, 0.1f, 1.0f};
00158     static GLfloat back_mat_diffuse[] = {0.9f, 0, 0, 1.0f};
00159 
00160     glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
00161     glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
00162     glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
00163 
00164     //- Pintar geometria ----------------------------------------------------
00165     MATRIZ_4x4 R;
00166 
00167     glEnable(GL_DEPTH_TEST);
00168     glClear(GL_DEPTH_BUFFER_BIT);
00169     if ( Geometria ) {
00170         glLoadIdentity();
00171         R.importar_quaternion(orientacion);
00172         R.cargar_gl();        
00173         Geometria->pintar_gl(&calidad, Material, &camara);
00174     }
00175 
00176     //- Pintar ejes ---------------------------------------------------------
00177     MATERIAL m;
00178     COLOR c;
00179     VECTOR v_o(0, 0, 0), v_i(1, 0, 0), v_j(0, 1, 0), v_k(0, 0, 1);
00180     CALIDAD_VISUAL ql;
00181 
00182     ql.activar_caras_gl(c, &m);
00183 
00184     if ( mostrar_ejes ) {
00185         c.r = 1;        c.g = 0;        c.b = 0;
00186         m.set_difusa(c);        Material->activar_gl();
00187         PINTAR_VECTOR_GL(v_o, v_i, 0.025, 0.3, 0.05);
00188 
00189         c.r = 0;        c.g = 1;        c.b = 0;
00190         m.set_difusa(c);        Material->activar_gl();
00191         PINTAR_VECTOR_GL(v_o, v_j, 0.025, 0.3, 0.05);
00192 
00193         c.r = 0;        c.g = 0;        c.b = 1;
00194         m.set_difusa(c);        Material->activar_gl();
00195         PINTAR_VECTOR_GL(v_o, v_k, 0.025, 0.3, 0.05);
00196     }
00197 
00198     //- Pintar Grilla -------------------------------------------------------
00199     double x_tam = 1, y_tam = 1, z_tam = 1, delta = 0.1;
00200     VECTOR p;
00201 
00202     if ( mostrar_grilla ) { 
00203         glLoadIdentity();
00204         glEnable(GL_DEPTH_TEST);
00205         glDisable(GL_LIGHTING);
00206         glShadeModel(GL_FLAT);
00207         glDisable(GL_TEXTURE_2D);
00208         glColor3f(1, 1, 1);
00209 
00210         glBegin(GL_POINTS);
00211         for ( p.x = -x_tam; p.x < x_tam; p.x += delta ) {
00212             for ( p.y = -y_tam; p.y < y_tam; p.y += delta ) {
00213                 for ( p.z = -z_tam; p.z < z_tam; p.z += delta ) {
00214                     if ( Geometria->clasificar_punto(p) >= 0 ) {
00215                         glVertex3f((float)p.x, (float)p.y, (float)p.z);
00216                         //glLoadIdentity();
00217                         //glTranslatef(p.x, p.y, p.z);
00218                         //pintar_cubo(delta/2);
00219                     }
00220                 }
00221             }
00222         }
00223         glEnd();
00224     }
00225 }
00226 
00227 void
00228 VISOR_GEOMETRIA::pintar_gl(void)
00229 {
00230     IMAGEN_RGB izq, der, sum;
00231 
00232     //- Configurar un entorno virtual ---------------------------------------
00233     if ( modo_stereo == STEREO_NULL ) {
00234         glColor3f(0.5f, 0.5f, 0.7f);
00235         PINTAR_FONDO_VISOR();
00236         camara.activar_gl();
00237         pintar_nucleo_gl();
00238       }
00239       else {
00240         // Genere la primera imagen del par stereo (Ojo izquierdo)
00241         glColor3f(0.5f, 0.5f, 0.7f);    PINTAR_FONDO_VISOR();
00242         camara.activar_gl(1);
00243         pintar_nucleo_gl();
00244         izq.importar_gl(0, 0, _x_tam, _y_tam);
00245 
00246         // Genere la segunda imagen del par stereo (Ojo derecho)
00247         glColor3f(0.5f, 0.5f, 0.7f);    PINTAR_FONDO_VISOR();
00248         camara.activar_gl(2);
00249         pintar_nucleo_gl();
00250         der.importar_gl(0, 0, _x_tam, _y_tam);
00251 
00252         // Calcule la combinacion stereo
00253         sum.combinar_stereo(&izq, &der, modo_stereo);
00254         glColor3f(0.5f, 0.5f, 0.7f);    PINTAR_FONDO_VISOR();
00255         sum.pintar_gl();
00256     }
00257 
00258     //----------------------------------------------------------------------
00259     pintar_visores_subordinados_gl(&camara);
00260 }
00261 #endif // GL_ENABLED
00262 
00263 int
00264 VISOR_GEOMETRIA::procesar_teclado(EVENTO_GUI *e)
00265 {
00266     switch( e->key_code ) {
00267       case JK_9:
00268         if ( controlando_camara ) controlando_camara = FALSE;
00269         else controlando_camara = TRUE;
00270         break;
00271       case JK_e:
00272         mostrar_ejes = !mostrar_ejes;
00273         break;
00274       case JK_E:
00275         modo_stereo++;
00276         if ( modo_stereo == STEREO_END ) modo_stereo = STEREO_NULL;
00277         break;
00278       case JK_g:
00279         if ( mostrar_grilla ) mostrar_grilla = 0;
00280         else mostrar_grilla = 1;
00281         break;
00282       default:
00283         return calidad.procesar_teclado(e) || camara.procesar_teclado(e);
00284     }
00285     return 1;
00286 }
00287 
00288 int
00289 VISOR_GEOMETRIA::procesar_mouse(EVENTO_GUI *e)
00290 {
00291     //if ( e->mouse_button_mask ) printf("%d", e->mouse_button_mask);
00292     //else printf(".");
00293     //fflush(stdout);
00294 
00295     if ( controlando_camara ) return camara.procesar_mouse(e);
00296 
00297     MATRIZ_4x4 R;
00298     double yaw, pitch, roll;
00299 
00300     if ( e->mouse_button_mask & 0x01 ) {
00301         // Rotacion
00302         INICIO_EULER();
00303         yaw += DEG2RAD(e->mouse_delta_x) * 2;
00304         pitch -= DEG2RAD(e->mouse_delta_y) * 2;
00305         FIN_EULER();
00306         return 1;
00307     }
00308 
00309     if ( e->mouse_button_mask & 0x02 ) {
00310         // Rotacion
00311         INICIO_EULER();
00312         roll += DEG2RAD(e->mouse_delta_y) * 2;
00313         FIN_EULER();
00314         return 1;
00315     }
00316     return 0;
00317 }
00318 
00319 void
00320 VISOR_GEOMETRIA::procesar_resize(int /*x*/, int /*y*/,
00321                                  int x_tam, int y_tam)
00322 {
00323     camara.procesar_resize(x_tam, y_tam);
00324     _x_tam = x_tam;
00325     _y_tam = y_tam;
00326 }
00327 
00328 //= SERVICIOS DE PERSISTENCIA ===============================================
00329 
00330 BOOLEAN
00331 VISOR_GEOMETRIA::leer(TOKENIZADOR *Sabiondo)
00332 {
00333     char cad[1000];
00334     int tipo_token = TK_DESCONOCIDO, pos;
00335 
00336     //- Ejecute el parser especifico de la COSA_RIGIDA -----------------
00337     pos = 1;
00338     while ( tipo_token != TK_CERRAR) {
00339         tipo_token = Sabiondo->siguiente_token(cad);
00340         switch ( pos ) {
00341             case 1:
00342               ESPERO(TK_CADENA, "una cadena con un nombre de un VISOR");
00343               if ( strlen(cad) > MAX_CAD-1 ) cad[MAX_CAD-1] = '\0'; 
00344               des_comille(cad);
00345               //set_nombre(cad);
00346               pos++;
00347               break;
00348             case 2:  ESPERO(TK_ABRIR, "\"{\"");  pos++; break;
00349             default:
00350               if ( tipo_token == TK_CERRAR ) break;
00351               ESPERO(TK_IDENTIFICADOR, "un identificador (3)");
00352               if ( strcmp(cad, "objetivo") == 0 ) {
00353                   tipo_token = Sabiondo->siguiente_token(cad);
00354                   ESPERO(TK_CADENA, "una cadena con el nombre de una COSA");
00355                   des_comille(cad);
00356                   _nombre_cosa = new char [strlen(cad) + 1];
00357                   VERIFICAR(_nombre_cosa);
00358                   strcpy(_nombre_cosa, cad);
00359                 }
00360                 else if ( !leer_visor_subordinado(Sabiondo, cad) ) {
00361                     return FALSE;
00362                 }
00363               ;
00364               break;
00365         }
00366     }
00367 
00368     return TRUE;
00369 }
00370 
00371 //===========================================================================
00372 //= EOF                                                                     =
00373 //===========================================================================
00374 

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.