Reputation:
I need to make a sphere ray-tracer in c/c++ without the use of OpenGL. I'm confused though at how to put a sphere or light in a scene without any gl functions. Can someone please explain how this can be done?
Upvotes: 0
Views: 1538
Reputation: 20087
Raytracing has nothing to do with opengl. It can be done with a desktop calculator.
The point is that it's pure geometry done with vectors, that are essentially three floating point variables. (or even integers).
You "put" your camera at origin: ox=0, oy=0, oz=0.
You "put" your sphere at 5 "meters" or units towards z-axis: sx=0,sy=0,sz=5;
You start to cast rays at 90 degree Field of View towards z-axis:
for (i=-1;i<1; i+=0.01) {
for (j=-1;j<1; j+=0.01) {
dx=i; dy=j;dz=1; // perhaps you then need to normalize the "vector" dx,dy,dz
// check if the ray hits the sphere with radius 2.3 (located at 0,0,5)
// if it does, calculate the angle of the normal of the hit point and
// the light source at position lx=1,ly=-0.5;lz=-2.33;
// if normal dot lightray is positive, calculate angle, apply Phong model
// add lambertian model, distance attenuation, fog, texturemapping
}
}
In the end you have calculated pixel intensities or color values for ~200 x 200 image. This example uses 90 degree FoV.
Upvotes: 1