Recursive reflection: Base on the recursive algorithm in unit 10 section 2 objec
ID: 3862253 • Letter: R
Question
Recursive reflection:
Base on the recursive algorithm in unit 10 section 2 objective 3 (shown below), implement a recursive routine in the program of unit 11 section 2 objective 1 (shown below) to produce a similar reflective effect. The minimum requirement is to rewrite the method render() of the program to implement the recursive ray tracing algorithm.
The implemented recursive method should cast a ray from the ray source to the scene. The ray may either hit the wall (a non-reflective surface here) or hit the floor (a reflective surface here). If it hits the wall, the method can terminate the recursion and return the local color of the wall. If the ray hits the floor, the method should (recursively) invoke itself with the reflection ray, and calculate and return the mixed color as demonstrated in the original render() routine. You may hard-code the inclusion test or you may implement your own method to support the inclusion. Also you may need to hard-code the normal vector of the wall and the floor. Feel free to change the program to suit your needs.
Recursive algorithm in unit 10 section 2 objective 3 (pseudocode):
Program of unit 11 section 2 objective 1 (the one needed to be modified to implement recursive ray tracing):
Please post a different answer than the following, as it is incorrect:
// create stencil mask
glClear(GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glColorMask(0, 0, 0, 0);
glBegin(GL_QUADS);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(a.x, a.y, a.z);
glVertex3f(b.x, b.y, b.z);
glVertex3f(c.x, c.y, c.z);
glVertex3f(d.x, d.y, d.z);
glEnd();
// draw mirrored scene
glColorMask(1, 1, 1, 1);
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glEnable(GL_CLIP_PLANE0);
glClipPlane(GL_CLIP_PLANE0, p);
dmFlipFaces();
glPushMatrix();
glMultMatrixf(m);
glClear(GL_DEPTH_BUFFER_BIT);
Scene->Render();
glPopMatrix();
dmFlipFaces();
glDisable(GL_CLIP_PLANE0);
glDisable(GL_STENCIL_TEST);
MirrorRecursion--;
Explanation / Answer
#include #include #include "gl/glut.h" #include "plane.h" using namespace std; // viewer vector3 viewer(-40.0, 40.0, 120.0); // floor vector3 floorNormal(0.0, 1.0, 0.0); plane floorDef(0.0, 4.0, 0.0, 0.0); vector3 floorLimit1(-75.0, 0, -75.0); vector3 floorLimit2(-75.0, 0, 75.0); vector3 floorLimit3(75.0, 0, 75.0); vector3 floorLimit4(75.0, 0, -75.0); // wall vector3 wallNormal(0.0, 0.0, 1.0); plane wall(0.0, 0.0, 4.0, 0.0); vector3 wallLimit1(-25.0, 0.0, 0.0); vector3 wallLimit2(-25.0, 50.0, 0.0); vector3 wallLimit3(25.0, 50.0, 0.0); vector3 wallLimit4(25.0, 0.0, 0.0); // colors vector3 grey(0.75, 0.75, 0.75); vector3 lightRed(0.75, 0.1, 0.1); // lighting position vector3 lightPosition(25, 25, 100.0); // lighting elements float ambient_coef = 0.3; float diffuse_coef = 0.7; float reflect_coef = 0.4; float local_coef = 0.6; // initialize void initialize() { // set background color glClearColor(0.5, 0.7, 0.5, 0.0); } // calculate local color // local color is intensity * base color vector3 localColor(vector3 intersect, vector3 baseColor, vector3 normal) { // calculate unit vector vector3 origin = lightPosition.subtract(intersect); vector3 unitVec = origin.normalize(); // calculate dot product float dotProd = unitVec.dot(normal); // calculate intensity float ambientContr = ambient_coef; float diffuseContr = diffuse_coef * dotProd; float intensity = ambientContr + diffuseContr; if (intensity > 1.0) intensity = 1.0; float r = intensity * baseColor.x; float g = intensity * baseColor.y; float b = intensity * baseColor.z; return vector3(r,g,b); } // render void render() { // render the floor for (int i=-75; i < 75; i++) { for (int j=-75; j < 75; j++) { vector3 intersect = vector3(i,0,j); vector3 color = localColor(intersect, grey, floorNormal); // local color of the floor // reflection color of floor vector3 reflectColor(0.0, 0.0, 0.0); vector3 mixedColor(0.0, 0.0, 0.0); vector3 reflect = intersect.subtract(viewer).reflect(floorNormal); vector3 sm = wall.intersect(intersect, intersect.add(reflect)); // test for inclusion - hardcoded if ( (sm.x >= -25.0) && (sm.x = 0.0) && (sm.yRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.