📄 raytracer.java
字号:
background, material, inQualitySelection); result.r += kr*rcolor.r; result.g += kr*rcolor.g; result.b += kr*rcolor.b; } else { result.r += kr*backgroundColor.r; result.g += kr*backgroundColor.g; result.b += kr*backgroundColor.b; } } } // Add code for refraction here // <TODO> // Clamp result to MAX 1.0 intensity. result.r = (result.r > 1) ? 1 : result.r; result.g = (result.g > 1) ? 1 : result.g; result.b = (result.b > 1) ? 1 : result.b; return result; } /** This method intersect the `inOut_Ray` with all of the geometries contained in `inSimpleBodyArray`. If none of the geometries is intersected `null` is returned, otherwise a reference to the containing SimpleBody is returned. Warning: This method includes the use of the ray transformation technique that permits the representation of geometries centered in its origin, and its combination with geometric transformations. */ private SimpleBody selectNearestThingInRayDirection(Ray inOut_Ray, ArrayList <SimpleBody> inSimpleBodyArray) { int i; SimpleBody gi; SimpleBody nearestObject; double nearestDistance; nearestDistance = Double.MAX_VALUE; nearestObject = null; for ( i = 0; i < inSimpleBodyArray.size(); i++ ) { inOut_Ray.t = Double.MAX_VALUE; gi = inSimpleBodyArray.get(i); if ( gi.doIntersection(inOut_Ray) && inOut_Ray.t < nearestDistance && inOut_Ray.t > VSDK.EPSILON ) { nearestDistance = inOut_Ray.t; nearestObject = gi; } } inOut_Ray.t = nearestDistance; return nearestObject; } /** Warning: This method includes the use of the ray transformation technique that permits the representation of geometries centered in its origin, and its combination with geometric transformations. Note that this method can return null, that means a transparent pixel should be used. */ private ColorRgb followRayPath(Ray inRay, ArrayList <SimpleBody> in_objetos, ArrayList <Light> in_luces, Background in_background, RendererConfiguration inQualitySelection) { ColorRgb c; Vector3D v = new Vector3D(); SimpleBody nearestObject; Ray myRay; GeometryIntersectionInformation info = new GeometryIntersectionInformation(); nearestObject = selectNearestThingInRayDirection(inRay, in_objetos); if ( nearestObject != null ) { //------------------------------------------------------------ nearestObject.doExtraInformation(inRay, inRay.t, info); //----- if ( !inQualitySelection.isTextureSet() ) { info.texture = null; } else { if ( info.texture == null ) { info.texture = nearestObject.getTexture(); } } //----- if ( !inQualitySelection.isBumpMapSet() ) { info.normalMap = nearestObject.getNormalMap(); } //------------------------------------------------------------ v.x = -inRay.direction.x; v.y = -inRay.direction.y; v.z = -inRay.direction.z; Material material; if ( info.material != null ) { material = info.material; } else { material = nearestObject.getMaterial(); } c = evaluateIlluminationModel( info, v, in_luces, in_objetos, in_background, material, inQualitySelection); } else { c = in_background.colorInDireccion(inRay.direction); } return c; } public void execute(RGBImage inoutViewport, RendererConfiguration inQualitySelection, ArrayList <SimpleBody> inSimpleBodyArray, ArrayList <Light> in_arr_luces, Background in_background, Camera inCamera, ProgressMonitor report) { execute(inoutViewport, inQualitySelection, inSimpleBodyArray, in_arr_luces, in_background, inCamera, report, null, 0, 0, inoutViewport.getXSize(), inoutViewport.getYSize()); } public void execute(RGBImage inoutViewport, RendererConfiguration inQualitySelection, ArrayList <SimpleBody> inSimpleBodyArray, ArrayList <Light> in_arr_luces, Background in_background, Camera inCamera, ProgressMonitor report, ZBuffer depthmap) { execute(inoutViewport, inQualitySelection, inSimpleBodyArray, in_arr_luces, in_background, inCamera, report, depthmap, 0, 0, inoutViewport.getXSize(), inoutViewport.getYSize()); } /** Macroalgoritmo de control para raytracing. Este método recibe el modelo de una escena 3D previamente construida en memoria y una imagen, y modifica la imagen de tal forma que contiene una visualizacion de la escena, result de aplicar la técnica de raytracing. PARAMETERS - `inout_viewport`: imagen RGB en donde el algoritmo calculará su result. - `in_objetos`: arreglo dinámico de SimpleBodys que constituyen los objetos visibles de la escena. - `in_luces`: arreglo dinámico de Light'es (luces puntuales) - `in_background`: especificación de un color de fondo para la escena (i.e. el color que se ve si no se ve ningún objeto!) - `inCamera`: especificación de la transformación de proyección 3D a 2D que se lleva a cabo en el proceso de visualización. - `depthmap`: can be null or a reference to a ZBuffer. If it is null, nothing is done with this parameter. If it is not null, the associated ZBuffer is filled with depth values corresponding to distances calculated in world space coordinates from ray intersections. Note that depth values are not scaled neither clamped to any specific range, so post-processing should be done if wanting to combine that with other depth maps, as those generated from OpenGL's ZBuffer. - `liveReport` can be null. In that case no report is updated. PRE: - Todas las referencias estan creadas, asi sea que apunten a estructuras vacías. - La imagen `inout_viewport` esta creada, y es de el tamaño que el usuario desea para su visualización. - In the case the ZBuffer depthmap is not null, the ZBuffer must be initialized to the same size of the image inoutViewport. POST: - `inout_viewport` contiene una representación visual de la escena 3D (`in_objetos`, `in_luces`, `in_background`), tal que corresponde a una proyección 3D a 2D controlada por la cámara virtual `inCamera`. NOTA: Este algoritmo se inició como una modificación del raytracer del curso 6.837 (computación gráfica) de MIT, original de Leonard McMillan y Tomas Lozano Perez, pero puede considerarse que es una re-escritura y re-estructuración completa de Oscar Chavarro. */ public void execute(RGBImage inoutViewport, RendererConfiguration inQualitySelection, ArrayList <SimpleBody> inSimpleBodyArray, ArrayList <Light> inLightsArray, Background inBackground, Camera inCamera, ProgressMonitor liveReport, ZBuffer outDepthmap, int limx1, int limy1, int limx2, int limy2) { int x, y; int relativeX; int relativeY; Ray rayo; ColorRgb color; inCamera.updateVectors(); if ( liveReport != null ) { liveReport.begin(); } for ( y = limy1, relativeY = 0; y < limy2; y++, relativeY++ ) { if ( liveReport != null ) { liveReport.update(0, inoutViewport.getYSize(), y); } for ( x = limx1, relativeX = 0; x < limx2; x++, relativeX++ ) { //- Trazado individual de un rayo -------------------------- // Es importante que la operacion generateRay sea inline // (i.e. "final") rayo = inCamera.generateRay(x, y); color = followRayPath(rayo, inSimpleBodyArray, inLightsArray, inBackground, inQualitySelection); if ( outDepthmap != null ) { outDepthmap.setZ(x, y, (float)rayo.t); } //- Exporto el result de color del pixel ---------------- if ( color != null ) { inoutViewport.putPixel(relativeX, relativeY, (byte)(255 * color.r), (byte)(255 * color.g), (byte)(255 * color.b)); } } } if ( liveReport != null ) { liveReport.end(); } }}//===========================================================================//= EOF =//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -