Question: Recursive reflection in OpenGL: Base on the recursive algorithm in unit 10 section 2 objective 3 ( shown below ), implement a recursive routine in

Recursive reflection in OpenGL:

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):

Recursive Ray Tracer Routine: Recursive Ray Tracer Return value: Colour Contribution Parameters: Incident ray, cumulative recursive ray tracer calls If termination condition is met // termination conditions: Return no contribution // maximum recursive calls, or // too far away Else Use the Incident Ray to find the nearest intersection Cast a ray from the intersection to the light sources For each light source Find if there is any intersection point in between If intersection exists Calculate lighting intensity with shadow Else Calculate lighting intensity without shadow Endif Calculate local color // the surface color of the local object Adjust for aliasing effect If the object at the intersection point is reflective Calculate reflective ray Call Recursive Ray Tracer to get reflective color Endif Color per light source = (Local color * local color contribution percentage) + (Reflective color * reflective color contribution percentage) EndFor Add all colors per light source to get color at the intersection point Return color at the intersection point 

Program of unit 11 section 2 objective 1 (uses basic ray tracing rather than a recursive ray tracing algorithm):

#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 <= 25.0) && (sm.y >= 0.0) && (sm.y <= 50.0) ) { // test for inclusion vector3 reflectColor = localColor(sm, lightRed, wallNormal); mixedColor = color.scalar(local_coef).add(reflectColor.scalar(reflect_coef)); } else { mixedColor = color; // outside of the reflection, use local color only } glColor3f(mixedColor.x, mixedColor.y, mixedColor.z); // use the small rectangles method glBegin(GL_POLYGON); glVertex3i(i, 0, j); glVertex3i(i+1, 0, j); glVertex3i(i+1, 0, j+1); glVertex3i(i, 0, j+1); glEnd(); } } // render the wall - non-reflective surface for (int m=-25; m<25; m++) { for(int n=0; n<50; n++) { vector3 color = localColor(vector3(m,n,0), lightRed, wallNormal); glColor3f(color.x, color.y, color.z); glRecti(m, n, m+1, n+1); } } } // display registry void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(viewer.x, viewer.y, viewer.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); render(); glutSwapBuffers(); } // reshape registry void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 200.0); glMatrixMode(GL_MODELVIEW); } // main program void main(int argc, char **argv) { glutInit( &argc, argv ); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) ; glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); int windowHandle = glutCreateWindow("Athabasca University - Comp390 U11 S2 O2"); glutSetWindow(windowHandle); glutDisplayFunc( display ); glutReshapeFunc( reshape ); initialize(); glutMainLoop(); } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!