00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "jed_defs.h"
00023 #include "toolkits/entorno/rayo.h"
00024 #include "toolkits/util/parsero.h"
00025 #include "matriz4.h"
00026
00027 #include <stdlib.h>
00028 #include <string.h>
00029
00030
00031
00032
00033
00034 #define ESPERO(tipo, msg) \
00035 if ( tipo_token != (tipo) ) { \
00036 fprintf(stderr, "<RAYO> ERROR: Esperaba %s y recibi [%s].\n", \
00037 (msg), cad); fflush(stderr); return FALSE; \
00038 }
00039
00040
00041
00042
00043
00044 #ifdef GL_ENABLED
00045 void
00046 RAYO::pintar_gl(void)
00047 {
00048 glDisable(GL_LIGHTING);
00049
00050 glColor3f(1, 0, 0);
00051 glBegin(GL_POINTS);
00052 glVertex3d(origen.x, origen.y, origen.z);
00053 glEnd();
00054
00055 glColor3d(0, 1, 1);
00056 glBegin(GL_LINES);
00057 glVertex3d(origen.x, origen.y, origen.z);
00058 glVertex3d(origen.x + direccion.x,
00059 origen.y + direccion.y,
00060 origen.z + direccion.z);
00061 glEnd();
00062 }
00063 #endif
00064
00065 BOOLEAN
00066 RAYO::procesar_teclado(EVENTO_GUI *e)
00067 {
00068 double theta, phi;
00069
00070 #define INICIO_ANGULAR() \
00071 phi = asin(direccion.z); \
00072 if ( fabs(direccion.x) < EPSILON && \
00073 fabs(direccion.y) < EPSILON ) direccion.x = 1; \
00074 direccion.z = 0; \
00075 direccion.normalizar(); \
00076 if ( direccion.y > 0 ) theta=acos(direccion.x); \
00077 else theta = -acos(direccion.x);
00078
00079 #define FIN_ANGULAR() \
00080 direccion.x = cos(phi) * cos(theta); \
00081 direccion.y = cos(phi) * sin(theta); \
00082 direccion.z = sin(phi); \
00083 direccion.normalizar(); \
00084 break;
00085
00086 switch( e->key_code ) {
00087 case JK_k: INICIO_ANGULAR();
00088 theta -= DEG2RAD(5);
00089 while ( theta < DEG2RAD(0) ) theta += DEG2RAD(360);
00090 FIN_ANGULAR();
00091 case JK_h: INICIO_ANGULAR();
00092 theta += DEG2RAD(5);
00093 while ( theta >= DEG2RAD(360) ) theta -= DEG2RAD(360);
00094 FIN_ANGULAR();
00095 case JK_j: INICIO_ANGULAR();
00096 phi -= DEG2RAD(5);
00097 if ( phi < DEG2RAD(-90) ) phi = DEG2RAD(-90);
00098 FIN_ANGULAR();
00099 case JK_u: INICIO_ANGULAR();
00100 phi += DEG2RAD(5);
00101 if ( phi > DEG2RAD(90) ) phi = DEG2RAD(90);
00102 FIN_ANGULAR();
00103 case JK_q: origen.x -= 0.1; break;
00104 case JK_Q: origen.x += 0.1; break;
00105 case JK_w: origen.y -= 0.1; break;
00106 case JK_W: origen.y += 0.1; break;
00107 case JK_e: origen.z -= 0.1; break;
00108 case JK_E: origen.z += 0.1; break;
00109 default: return FALSE;
00110 }
00111 return TRUE;
00112 }
00113
00114 BOOLEAN
00115 RAYO::leer(TOKENIZADOR *Sabiondo)
00116 {
00117 char cad[1000];
00118 int tipo_token = TK_DESCONOCIDO, pos;
00119 MATRIZ_4x4 R;
00120 double yaw, pitch;
00121 VECTOR eje_x(1, 0, 0);
00122
00123
00124 pos = 2;
00125 while ( tipo_token != TK_CERRAR) {
00126 tipo_token = Sabiondo->siguiente_token(cad);
00127 switch ( pos ) {
00128
00129
00130
00131
00132
00133
00134
00135
00136 case 2: ESPERO(TK_ABRIR, "\"{\""); pos++; break;
00137 default:
00138 if ( tipo_token == TK_CERRAR ) break;
00139 ESPERO(TK_IDENTIFICADOR, "un identificador (3)");
00140 if ( strcmp(cad, "origen") == 0 ) {
00141 tipo_token = Sabiondo->siguiente_token(cad);
00142 ESPERO(TK_VECTOR_INICIO, "el inicio de un VECTOR");
00143 origen.x = atof(&cad[1]);
00144 tipo_token = Sabiondo->siguiente_token(cad);
00145 ESPERO(TK_NUMERO, "un numero (dato 2 de un VECTOR)");
00146 origen.y = atof(cad);
00147 tipo_token = Sabiondo->siguiente_token(cad);
00148 ESPERO(TK_VECTOR_FIN, "el final de un VECTOR");
00149 cad[strlen(cad) - 1] = '\0';
00150 origen.z = atof(cad);
00151 }
00152 else if ( strcmp(cad, "direccion") == 0 ) {
00153 tipo_token = Sabiondo->siguiente_token(cad);
00154 ESPERO(TK_VECTOR_INICIO, "el inicio de un VECTOR");
00155 direccion.x = atof(&(cad[1]));
00156 tipo_token = Sabiondo->siguiente_token(cad);
00157 ESPERO(TK_NUMERO, "un numero (dato 2 de un VECTOR)");
00158 direccion.y = atof(cad);
00159 tipo_token = Sabiondo->siguiente_token(cad);
00160 ESPERO(TK_VECTOR_FIN, "el final de un VECTOR");
00161 cad[strlen(cad) - 1] = '\0';
00162 direccion.z = atof(cad);
00163 direccion.normalizar();
00164 }
00165 else if ( strcmp(cad, "direccion_angular") == 0 ) {
00166 tipo_token = Sabiondo->siguiente_token(cad);
00167 ESPERO(TK_VECTOR_INICIO, "el inicio de un VECTOR");
00168 yaw = DEG2RAD(atof(&(cad[1])));
00169 tipo_token = Sabiondo->siguiente_token(cad);
00170 ESPERO(TK_VECTOR_FIN, "el final de un VECTOR");
00171 cad[strlen(cad) - 1] = '\0';
00172 pitch = DEG2RAD(atof(cad));
00173 R.rotacion_angulos_euler(yaw, pitch, 0);
00174 direccion = R * eje_x;
00175 }
00176 ;
00177 break;
00178 }
00179 }
00180
00181 return TRUE;
00182 }
00183
00184
00185
00186
00187
00188
00189