Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ray Tracing help needed....

3 views
Skip to first unread message

Scott

unread,
Apr 10, 2004, 8:04:37 PM4/10/04
to
Hi,
I'm trying to write a VERY simple ray tracer (1 sphere) and can't make it
work. There are a
lot of great examples on the web but they are way too advanced for my
current understanding.
I need to understand and make sure the initial routines and sphere data are
setup correctly.

I'm trying to figure out why this code doesn't work. Any input would be
greatly appreciated!
Here are the three main routines (I will gradly send anyone the project file
if they'd like-it's
done using Code Warriror 11 using power plant:

Can someone point out where I am going wrong and why please??? I just want
to have the right
data to display a sphere centered in the background.
TIA!!!
--Scott
Please reply to scott...@mac.com

/***************************************************************************
***************
/
/ CRaytracerApplication::Render
the image()
/
/ Use: Renders the image to the current window.
/
/
/***************************************************************************
***************/
void
CRaytracerApplication::Draw()
{
short ix = 0;
short iy = 0;
double x = 0;
double y = 0;


CRay eyeRay;
RGBColor eyeColor={0xffff,0,0};
CPrimative* closesCPrimative = NULL;

// Create a sphere
CSphere* aSphere = new CSphere;
aSphere->InitializeSphere(0.0,0.0,5.0,1.0);
mDataArray->AddItem(&aSphere,sizeof(CSphere*));

// Set the eye's origin
eyeRay.origin.x = 0.0;
eyeRay.origin.y = 0.0;
eyeRay.origin.z = 0.0;

// Traverse the image casting rays as we go
for(ix = 0; ix < cImageHeight; ix++)
{
x = (ix - cImageWidth / 2) / 72.0;
for(iy = 0; iy < cImageHeight; iy++)
{
y = (iy - cImageHeight / 2)/72.0;
eyeRay.direction.x = x;
eyeRay.direction.y = y;
eyeRay.direction.z = cImageHeight / 72.0;
eyeRay.direction = UnitNormal(eyeRay.direction);
eyeColor = Trace(eyeRay);
RGBForeColor(&eyeColor);
MoveTo(ix,iy);
Line(0,0);
}
}

/***************************************************************************
***************
/
/
CRaytracerApplication::FindClosest()
/
/ Use: Find the closest object to the ray and ask it for it's
shading properties.
/
/
/***************************************************************************
***************/
CPrimative*
CRaytracerApplication::FindClosest(CRay aRay)
{
CPrimative* closestPrimative = NULL; // The
closest primative found in our search
CPrimative* tempPrimative = NULL; //
Temp variable during our search for the closest one
double itsTime = 0;
// The searches time
double closestTime = 1000000000000.0; //
The current closest running time

short theArrayCount = mDataArray->GetCount(); //
Get array count

if(theArrayCount > 0)
// Make sure we have objects
for(short index = 1; index <= theArrayCount; index++) //
Travers the array
{
mDataArray->FetchItemAt(index,&tempPrimative); //
Get a object
if(tempPrimative != NULL)
// If we got one...
{
itsTime = tempPrimative->Intersect(aRay);
// See if it intersects
if((itsTime > 0.0) && (itsTime < closestTime)) //
If it intersects and is closer...
{
// Save the time and Primative
closestTime = itsTime;
closestPrimative = tempPrimative;
}
}
}
return(closestPrimative);
// Return the primative
}


/***************************************************************************
***************
/
/
CSphere::Intersect()
/
/ Use:Takes in a Ray and determines if there is an intersection.
/
/
/***************************************************************************
***************/
double
CSphere::Intersect(CRay aRay)
{
double a;
double b;
double c;
double t1;
double t2;
double t3;
double close=-1;
double farther=-1;
double intersect=-1;
CVector transCenter;

a = VecDotProd(aRay.direction, aRay.direction);
if(a != 0)
{
transCenter.x = aRay.origin.x - fCenter.x;
transCenter.y = aRay.origin.y - fCenter.y;
transCenter.z = aRay.origin.z - fCenter.z;
b = 2.0 * VecDotProd(transCenter,aRay.direction);
c = VecDotProd(transCenter,transCenter)-fRadius2;
t1 = (b *b) - 4.0 *a*c;
if(t1>0.0)
{
t2 = std::sqrt(t1);
t3 = 2.0 * a;
close = -(b + t2)/ t3;
farther = -(-b - t2)/t3;
}
if (close <farther)
{
intersect = close;
}else{

intersect = farther;
}
}
return(intersect);
}


/***************************************************************************
***************
/
/
CSphere::InitializePlane()
/
/ Use:Initialize the sphere.
/
/
/***************************************************************************
***************/
void
CSphere::InitializeSphere(double cx,double cy,double cz,double r)
{
fCenter.x = cx;
fCenter.y = cy;
fCenter.z = cz;
fRadius = r;
fRadius2 = r * r;
} // CSphere::InitializePlane

/***************************************************************************
***************
/
/ CSphere::Color()
/
/ Use:Returns the color to be desplayed based on a ray and a time of
intersect.
/
/
/***************************************************************************
***************/
RGBColor
CSphere::Color(CRay,double)
{
RGBColor aRGBColor={0,0,0xfff0}; // For now just return a nice
color
return(aRGBColor);
} // CSphere::Intersect

Jörg 'Yadgar' Bleimann

unread,
Apr 20, 2004, 9:16:10 AM4/20/04
to
High!

Scott schrieb:

> Can someone point out where I am going wrong and why please??? I just want
> to have the right
> data to display a sphere centered in the background.
> TIA!!!
> --Scott

Sad to say that this newsgroup is as dead as a dodo, about five
raytracing-related postings/year, all the rest is just disgusting spam crap...

...but concerning your programming problem, what about looking up the on-screen
manual of PoV-Ray 3.5? It in fact also contains a tutorial how to program a
raytracer WITHIN (!!!) PoV-Ray - transferring the algorithms used there to your
development system shouldn't be that difficult!

You can download PoV-Ray 3.5 at http://www.povray.org !

See you in Khyberspace!

Yadgar

0 new messages