Hello all,

I’m a CS student and for our final we were told to construct the reflections on multiple spheres via ray tracing. That’s almost literally what we got for directions except a picture for how it should look when finished. So I need spheres, with they’re reflections (using ray tracing) mapped on them with the proper shading from a light.

Well I have all of it working, except having multiple spheres and the fact that it doesn’t look like the picture he gave us for a rubric.

The multiple spheres thing I’m not too sure how to do, but I’d say I need to store them in a 2D array and modify a few sections of code.

What I thought was modifying the sphere_intersect and find_reflect to include which sphere is being analyzed. Next, modify find_reflect so that when the new vector u is calculated its starting point (P0) is also updated. Then if the ray hits a sphere it will have to count how many times the ray has been reflected. At some point terminate (after 10 times maybe) and then I’ll just draw the pixel. For an added touch I’d like to add solid colors to the spheres which would call for finding the normal of a sphere I believe.

Anyways I’m going to attach a picture of his, a picture of mine, and the source code. Hopefully someone can help me out on this one.

``````

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#include <string>

#define screen_width 750
#define screen_height 750
#define true 1
#define false 0
#define perpendicular 0

int gridXsize = 20;
int gridZsize = 20;
float plane[] = {0.0, 1.0, 0.0, -50.0,};
float sphere[] = {250.0, 270.0, -100.0, 100.0};
float eye[] ={0.0, 400.0, 550.0};
float light[] = {250.0, 550.0, -200.0};

float dot(float *u, float *v)
{
return u*v + u*v + u*v;
}

void norm(float *u)
{
float norm = sqrt(abs(dot(u,u)));
for(int i =0; i <3; i++)
{
u[i] = u[i]/norm;
}

}

float plane_intersect(float *u, float *pO)
{
float normt = {plane, plane, plane};

float s;
if(dot(u,normt) == 0)
s = -10;
else
s = (plane-(dot(pO,normt)))/(dot(u,normt));

return s;
}

float sphere_intersect(float *u, float *pO)
{

float deltaP = {sphere-pO,sphere-pO,sphere-pO};
float deltLen = sqrt(abs(dot(deltaP,deltaP)));
float t=0;
float det;
if ((det =( abs(dot(u,deltaP)*dot(u,deltaP))-(deltLen*deltLen)+sphere*sphere)) < 0)
else
{
t =-1*dot(u,deltaP)- sqrt(det) ;
if(t>0)
}
}

void find_reflect(float *u, float s, float *pO)
{
float n = {pO+s*u-sphere,pO+s*u-sphere,pO+s*u-sphere};
float l = {s*u,s*u,s*u};
u =(2*dot(l,n)*n)-l;
u = (2*dot(l,n)*n)-l;
u =  (2*dot(l,n)*n)-l;
}

float find_shade(float *u,float s, float *pO )
{
float lightVec = {light-(pO+s*u), light-(pO+s*u), light-(pO+s*u)};
float n = {pO+s*u-sphere,pO+s*u-sphere,pO+s*u-sphere};
}

void init()
{
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,screen_width,0,screen_height);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
for(int i=0; i < screen_width;i++)
{
for(int j=0; j < screen_height; j++)
{
float ray = {1*(eye-i),-1*(eye-j),1*eye};
float point ={i,j,0};
norm(ray);
int plotted = false;
while(!plotted)
{
float s_plane = plane_intersect(ray, point);
float s_sphere = sphere_intersect(ray, point);

if(s_plane <= 0 && s_sphere <=0)
{
glColor3f(0,0,0);
glBegin(GL_POINTS);
glVertex3f(i,j,0);
glEnd();
plotted = true;
}
else if (s_sphere >= 0  && (s_plane <=0 || s_sphere <= s_plane))
{
find_reflect(ray, s_sphere, point);
}
else if(s_plane >=0 && (s_sphere <=0 ||s_plane <= s_sphere))
{
float xx = s_plane*ray + eye;
float z = s_plane*ray + eye;
if(abs((int)xx/gridXsize)%2 == abs((int)z/gridZsize)%2)
else
glBegin(GL_POINTS);
glVertex3f(i,j,0);
glEnd();
plotted = true;
}
}
}
}
glFlush();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutCreateWindow("Ray Trace with Sphere.");
glutInitWindowSize(screen_width,screen_height);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

``````

Hi,
You need to restructure your code. You should create a simple structure for holding the sphere’s position and radius like this,

``````
struct Sphere { vec3 center; float radius;};

``````

Next, modify the intersect function to accept a Sphere as parameter.

``````
float sphere_intersect(float *u, float *pO, Sphere& whichSphere)
{

}

``````

Now when raytracing, loop through all primitives i.e. all spheres and the ground plane and test each for the ray intersection however return the nearest hit. So at a high level, it is something like this,

``````
float tmin = INFINITY;
for each sphere s
float t = intersectSphere(ray, s);
if(t<tmin && t>0)
{
tmin = t;
}
end for

//do the same for other primitives

``````

Now at the end of the above calls, tmin contains the nearest t value (i.e. the nearest intersection point. get the points coord by simply putting in ray equation. For sphere’s normal simply subtract the hit point from its center.

Do this much and let me know if there is any progress.