📄 codigo_imprimible.txt
字号:
camara.posicion = new VECTOR(leerNumero(st), leerNumero(st), leerNumero(st)); } else if (st.sval.equals("lookat")) { imprimirMensaje("lookat"); camara.puntoObservado = new VECTOR(leerNumero(st), leerNumero(st), leerNumero(st)); } else if (st.sval.equals("up")) { imprimirMensaje("up"); camara.up = new VECTOR(leerNumero(st), leerNumero(st), leerNumero(st)); } else if (st.sval.equals("fov")) { imprimirMensaje("fov"); camara.fov = leerNumero(st); } else if (st.sval.equals("background")) { imprimirMensaje("background"); color_de_fondo = new COLOR(); color_de_fondo.r = (float)leerNumero(st); color_de_fondo.g = (float)leerNumero(st); color_de_fondo.b = (float)leerNumero(st); } else if (st.sval.equals("light")) { imprimirMensaje("light"); float r = leerNumero(st); float g = leerNumero(st); float b = leerNumero(st); if ( st.nextToken() != StreamTokenizer.TT_WORD ) { System.err.println("ERROR: in line "+st.lineno() + " at "+st.sval); throw new IOException(st.toString()); } if ( st.sval.equals("ambient") ) { imprimirMensaje("ambient"); arr_luces.addElement(new LUZ(LUZ.AMBIENTE, null, r,g,b)); } else if ( st.sval.equals("directional") ) { imprimirMensaje("directional"); VECTOR v = new VECTOR(leerNumero(st), leerNumero(st), leerNumero(st)); arr_luces.addElement(new LUZ(LUZ.DIRECCIONAL, v, r,g,b)); } else if ( st.sval.equals("point") ) { imprimirMensaje("point"); VECTOR v = new VECTOR(leerNumero(st), leerNumero(st), leerNumero(st)); arr_luces.addElement(new LUZ(LUZ.PUNTUAL, v, r, g, b)); } else { System.err.println("ERROR: in line " + st.lineno()+" at "+st.sval); throw new IOException(st.toString()); } ; } else if ( st.sval.equals("surface") ) { imprimirMensaje("surface"); float r = leerNumero(st); float g = leerNumero(st); float b = leerNumero(st); float ka = leerNumero(st); float kd = leerNumero(st); float ks = leerNumero(st); float ns = leerNumero(st); float kr = leerNumero(st); float kt = leerNumero(st); float index = leerNumero(st); material_actual = new MATERIAL(r, g, b, ka, kd, ks, ns, kr, kt, index); } ; break; default: fin_de_lectura = true; break; } // switch } // while is.close(); if ( st.ttype != StreamTokenizer.TT_EOF ) { System.err.println("ERROR: in line "+st.lineno()+" at "+st.sval); throw new IOException(st.toString()); } }}//===========================================================================//= EOF =//===========================================================================//===========================================================================package vitral.toolkits.common;public class VECTOR { public float x, y, z; public VECTOR() { x = 0; y = 0; z = 0; } public VECTOR(float x, float y, float z) { this.x = x; this.y = y; this.z = z; } public final VECTOR producto_cruz(VECTOR B) { return new VECTOR(y*B.z - z*B.y, z*B.x - x*B.z, x*B.y - y*B.x); } public final float producto_punto(VECTOR B) { return (x*B.x + y*B.y + z*B.z); } public final void normalizar( ) { float t = x*x + y*y + z*z; if (t != 0 && t != 1) t = (float) (1 / Math.sqrt(t)); x *= t; // Falta implementar VECTOR::escalar(float m); y *= t; z *= t; } public final float norma() { return (float)Math.sqrt(x*x + y*y + z*z); }}//===========================================================================//= EOF =//===========================================================================//===========================================================================package vitral.toolkits.common;public class COLOR { public float r; public float g; public float b;}//===========================================================================//= EOF =//===========================================================================//===========================================================================package vitral.toolkits.entorno;import vitral.toolkits.common.VECTOR;public class RAYO { public VECTOR origin; public VECTOR direction; public float t; public RAYO(VECTOR eye, VECTOR dir) { origin = new VECTOR(eye.x, eye.y, eye.z); direction = new VECTOR(dir.x, dir.y, dir.z); direction.normalizar(); }}//===========================================================================//= EOF =//===========================================================================//===========================================================================package vitral.toolkits.entorno;public class MATERIAL { public float ir, ig, ib; // Color intrinseco de la superficie public float ka, kd, ks, ns; // Constantes para el modelo phong public float kt, kr, nt; public MATERIAL(float rval, float gval, float bval, float a, float d, float s, float n, float r, float t, float index) { ir = rval; ig = gval; ib = bval; ka = a; kd = d; ks = s; ns = n; kr = r; kt = t; nt = index; }}//===========================================================================//= EOF =//===========================================================================//===========================================================================package vitral.toolkits.entorno;import vitral.toolkits.common.VECTOR;// All the public variables here are ugly, but I// wanted Lights and Surfaces to be "friends"public class LUZ { public static final int AMBIENTE = 0; public static final int DIRECCIONAL = 1; public static final int PUNTUAL = 2; public int tipo_de_luz; public VECTOR lvec; // the position of a point light or // the direction to a directional light public float ir, ig, ib; // color of the light source public LUZ(int type, VECTOR v, float r, float g, float b) { tipo_de_luz = type; ir = r; ig = g; ib = b; if ( type != AMBIENTE ) { lvec = v; if ( type == DIRECCIONAL ) { lvec.normalizar(); } } }}//===========================================================================//= EOF =//===========================================================================//===========================================================================package vitral.toolkits.entorno;import vitral.toolkits.common.VECTOR;public class CAMARA { // Modelo basico de la camara public VECTOR posicion; public VECTOR puntoObservado; public VECTOR up; public float fov; // Vectores privados que se preprocesan para agilizar los calculos private VECTOR dx, dy, _dir; /** Una `CAMARA` debe saber de qué tamaño es el viewport para el cual está generando una proyección, para poder modificar sus parámetros internos en función del ángulo de vision (`fov`) y de la actual proporción de ancho/alto del viewport. Las variables internas `ventana_xtam` y `ventana_ytam` representan el tamańo en pixels para el viewport, y son valores que solo pueden ser cambiados por el método `CAMARA::procesar_resize`. Estos dos valores son para uso interno de la clase cámara y no pueden ser consultados (i.e. son una copia de la configuración del viewport, que debe ser administrado por la aplicación que use `CAMARA`s). */ private float ventana_xtam; /// Ver la documentación de `ventana_xtam` private float ventana_ytam; public CAMARA() { // Modelo de camara por defecto propuesto por el raytracer original // de MIT posicion = new VECTOR(0,0,10); puntoObservado = new VECTOR(0,0,0); up = new VECTOR(0,1,0); fov = 30; ventana_xtam = 320; ventana_ytam = 320; // OJO: dx, dy y _dir no estan inicializados! } public void procesar_resize(int dx, int dy) { ventana_xtam = dx; ventana_ytam = dy; } /** PRE: - puntoObservado y posicion tienen que ser diferentes! POST: - dx queda normalizado - dy queda normalizado - _dir ... no se ha entendido bien! */ public void preprocesar_vista() { // Compute mapping from screen coordinate to a ray direction VECTOR _dir_no_normalizado = new VECTOR(puntoObservado.x - posicion.x, puntoObservado.y - posicion.y, puntoObservado.z - posicion.z); float fl = (float)(ventana_xtam / (2*Math.tan((0.5*fov)*Math.PI/180))); dx = _dir_no_normalizado.producto_cruz(up); dx.normalizar(); dy = _dir_no_normalizado.producto_cruz(dx); dy.normalizar(); _dir = _dir_no_normalizado; _dir.normalizar(); _dir.x = _dir.x * fl - 0.5f * (ventana_xtam*dx.x + ventana_ytam*dy.x); _dir.y = _dir.y * fl - 0.5f * (ventana_xtam*dx.y + ventana_ytam*dy.y); _dir.z = _dir.z * fl - 0.5f * (ventana_xtam*dx.z + ventana_ytam*dy.z); } public final RAYO generar_rayo(int x, int y) { // OJO: Es posible que esto este siendo lento por asignar a memoria // dinamica estas dos variables. Notese que podrian ser estaticos // y reutilizarse... // Notese como se utiliza un vector preprocesado `dy` para calcular // las coordenadas en la pantalla que aumentan hacia abajo VECTOR dir = new VECTOR( x*dx.x + y*dy.x + _dir.x, x*dx.y + y*dy.y + _dir.y, x*dx.z + y*dy.z + _dir.z); // La direccion de este rayo es un vector unitario, dado que en la // version actual de RAYO::RAYO se normaliza... RAYO ray = new RAYO(posicion, dir); return ray; }}//===========================================================================//= EOF =//===========================================================================//===========================================================================package vitral.toolkits.geom;import vitral.toolkits.common.COLOR;import vitral.toolkits.common.VECTOR;import vitral.toolkits.entorno.RAYO;import vitral.toolkits.entorno.MATERIAL;// An object must implement a GEOMETRIA interface in order to// be ray traced. Using this interface it is straight forward// to add new objectspublic abstract class GEOMETRIA { public MATERIAL material = null; public abstract boolean interseccion(RAYO r); public abstract void informacion_extra(RAYO ray, float t, VECTOR p, VECTOR n);}//===========================================================================//= EOF =//===========================================================================//===========================================================================package vitral.toolkits.geom;import vitral.toolkits.common.COLOR;import vitral.toolkits.common.VECTOR;import vitral.toolkits.entorno.RAYO;import vitral.toolkits.entorno.MATERIAL;import vitral.toolkits.geom.GEOMETRIA;public class ESFERA extends GEOMETRIA { private VECTOR _centro; private float _radio; private float _radio_al_cuadrado; private VECTOR _static_delta; public ESFERA(MATERIAL s, VECTOR c, float r) { material = s; _centro = c; _radio = r; _radio_al_cuadrado = _radio*_radio; _static_delta = new VECTOR(); } /** Dado un RAYO `inout_rayo`, esta operación determina si el rayo se intersecta con la superficie de este objeto o no. Si el rayo no intersecta al objeto se retorna 0, y de lo contrario se retorna la distancia desde el origen del rayo hasta el punto de interseccion. En caso de intersección, se modifica `inout_rayo.t` para que contenga la distancia entre el punto de intersección y el origen del `inout_rayo`. */ public boolean interseccion(RAYO inout_rayo) { /* OJO: Como en Java, a diferencia de C no hay sino objetos por referencia, no se puede hacer una declaración estática de un objeto, y poder hacerla es importante porque la constructora VECTOR::VECTOR se ejecuta muchas veces,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -