Here is a bit more information to add to what 3Dickulus and Patryk provided (lol... let's deluge ForestCat with info!!):
1) Look at the most basic "Fast-Raytracer.frag".
The code in between #vertex and #endvertex assigns your ray direction, based on window coordinates and Eye/Target position.
#vertex
#group Camera
// Field-of-view
uniform float FOV; slider[0,0.4,2.0] NotLockable
uniform vec3 Eye; slider[(-50,-50,-50),(0,0,-7),(50,50,50)] NotLockable
uniform vec3 Target; slider[(-50,-50,-50),(0,0,0),(50,50,50)] NotLockable
uniform vec3 Up; slider[(0,0,0),(0,1,0),(0,0,0)] NotLockable
//uniform float ApplyOnIteration;slider[0,0,30]
//uniform float FormulaType;slider[0,0,30]
//uniform float ApplicationType;slider[0,0,30]
varying vec3 dirDx;
varying vec3 dirDy;
varying vec3 from;
uniform vec2 pixelSize;
varying vec2 coord;
varying float zoom;
varying vec3 dir;
//varying vec3 Dir;
void main(void)
{
gl_Position = gl_Vertex;
coord = (gl_ProjectionMatrix*gl_Vertex).xy;
coord.x*= pixelSize.y/pixelSize.x;
// we will only use gl_ProjectionMatrix to scale and translate, so the following should be OK.
vec2 ps =vec2(pixelSize.x*gl_ProjectionMatrix[0][0], pixelSize.y*gl_ProjectionMatrix[1][1]);
zoom = length(ps);
from = Eye;
vec3 Dir = normalize(Target-Eye);
vec3 up = Up-dot(Dir,Up)*Dir;
up = normalize(up);
vec3 Right = normalize( cross(Dir,up));
dir = (coord.x*Right + coord.y*up )*FOV+Dir;
dirDy = ps.y*up*FOV;
dirDx = ps.x*Right*FOV;
}
#endvertex
http://www.songho.ca/opengl/index.html <-- good resource to help you visualize the openGL projection process, if you're unfamiliar with it. Look at the projection matrix part.
2) Skip to the bottom (Like Patryk said, order matters...
), to main () {..}
void main() {
init();
vec3 hitNormal = vec3(0.0);
vec3 hit;
depthFlag=true; // do depth on the first hit not on reflections
vec3 color = trace(from,dir,hit,hitNormal);
if (ShowDepth) color = vec3(depthMag * depth);
color = clamp(color, 0.0, 1.0);
gl_FragColor = vec4(color, 1.0); //color of vertex assigned
}
This assigns a color to every vertex with the trace function. We haven't made it to the DE yet....
3) Look at the trace function (sections between ******s):
3a) initialize variables
3b) DE loop. The DE function is generally in the other frag that includes this raytracer, although you could put the DE function in the raytracer like Patryk said
- from = Eye position- look at the openGL tutorial to get an idea of what they Eye position is
- dir = direction from Eye, through the window (look at the openGL tutorial, projection matrix part)
- dist = DE...
- totalDist = distance traveled along the ray from the Eye, towards the object.
- epsmodified = total distance traveled * "minimum distance" modifier
- check whether we hit (dist<epsmodified) or we are above the maximum allowed distance from object
3c) calculate normal (angle surface is oriented towards), color (assign with orbitTrap, or whatever), and light effects using normal for reflections, etc. Fast-Raytracer doesn't do much in the way of lighting effects, and uses the number of steps to calculate ambient occlusion. I wrote a couple of coloring modifications for the fast raytracer intended for archaic (in GPU years) hardware (like mine!) that should be around somewhere.
3D)ickulus's code to record depth for various things, including the project that I have been slacking on for a little bit. FeedBack. Which I've got to get around to, but.. it's 76 degrees, sunny, there is a dog that would enjoy going for a walk and chores to be done.
vec3 trace(vec3 from, vec3 dir, inout vec3 hit, inout vec3 hitNormal) {
// 3a) Initialize a couple of variables**************************************************
hit = vec3(0.0);
orbitTrap = vec4(10000.0);
vec3 direction = normalize(dir);
float dist = 0.0;
float totalDist = 0.0;
int steps;
colorBase = vec3(0.0,0.0,0.0);
// We will adjust the minimum distance based on the current zoom
float eps = minDist; // .001
float epsModified = 0.0;
// 3b) Check DE function, that is in the other frag!!!!) **************************************
for (steps=0; steps<MaxRaySteps; steps++) {
orbitTrap = vec4(10000.0); //initialize every time so we get the last trap
vec3 p = from + totalDist * direction;
dist = DE(p);
dist *= FudgeFactor;
totalDist += dist;
epsModified = pow(totalDist,ClarityPower)*eps;
if (dist < epsModified) break;
if (totalDist > MaxDistance) break;
}
//DE is checked ***********************************************************
// 3c) calculate color
vec3 hitColor;
float stepFactor = clamp((float(steps))/float(GlowMax),0.0,1.0);
vec3 backColor = BackgroundColor;
if (GradientBackground>0.0) {
float t = length(coord);
backColor = mix(backColor, vec3(0.0,0.0,0.0), t*GradientBackground);
}
if ( steps==MaxRaySteps) orbitTrap = vec4(0.0);
if ( dist < epsModified) {
// We hit something, or reached MaxRaySteps
hit = from + totalDist * direction;
float ao = AO.w*stepFactor ;
hitNormal= normal(hit-NormalBackStep*epsModified*direction, epsModified); // /*normalE*epsModified/eps*/
#ifdef providesColor
hitColor = mix(BaseColor, color(hit,hitNormal), OrbitStrength);
#else
ColorTex=BaseColorTex; //set colortex for base color texture
MapType=BaseMapType;
texturespeed=TextureSpeed*SpeedMult;
textoff=TextureOffset/100.0;
//orbitTrap = vec4(0.0);
// vec3 p = from + totalDist* direction + hitNormal*ColorDepth;
//float dist2 = DE(p);
hitColor = getColor();
#endif
hitColor = mix(hitColor, AO.xyz ,ao);
float shadowStrength = 0.0;
hitColor = lighting(hitNormal, hitColor, hit, direction,epsModified,shadowStrength);
// OpenGL GL_EXP2 like fog
float f = totalDist;
hitColor = mix(hitColor, backColor, 1.0-exp(-pow(Fog,4.0)*f*f));
}
else {
hitColor = backColor;
hitColor +=Glow.xyz*stepFactor* Glow.w;
}
//3D ********************************ickulus***************************
if(depthFlag) {
// do depth on the first hit not on reflections
depthFlag=false;
// for rendering depth to alpha channel in EXR images
// see http://www.fractalforums.com/index.php?topic=21759.msg87160#msg87160
depth = 1.0/totalDist;
//if(DepthToAlpha==true) gl_FragDepth = depth;
if(DepthToAlpha==true) gl_FragDepth = clamp(depth, 0.00001, 1000.0);
else
// sets depth for spline path occlusion
// see http://www.fractalforums.com/index.php?topic=16405.0
gl_FragDepth = ((1000.0 / (1000.0 - 0.00001)) +
(1000.0 * 0.00001 / (0.00001 - 1000.0)) /
clamp(totalDist, 0.00001, 1000.0));
}
return hitColor;
}