Original Post
Sorry for such a newbie-esque question, I'm sure this will be easy to answer for one of you C++ gurus out there. I'm traditionally a Java programmer with knowledge of C, and this project is my first attempt at a real foray into C++. I have searched through multiple books, google, and these forums, but I can't find a reason why (and a fix) I get a bad_alloc exception thrown in my code. I dont use any STL classes, I dont have any arrays (yet), and The place its being thrown doesnt seem to even have a new call. Sorry for posting so much code, but here it goes (the exception appears to happen right after the "hmm" comment): Here is the getPointAtTime(t) function as well: The exception also happens before the new Point(blabla) line is called. This has been driving me crazy for almost a week now, and any help will be greatly appreciated. If you're curious, I'm attempting to write a raytracer. [Edited by - Kronecker on April 20, 2006 11:31:26 PM]
CollisionPacket* Sphere::getIntersectionRays(Line* l){
Point* collisionPoint;
Line* transLine;
Line* reflLine;
Vect* unitNorm;
Vect* incident;
Vect* s1;
Vect* s2;
float t;
float a = pow(l->r0->i,2) + pow(l->r0->j,2) + pow(l->r0->k,2);
float b = 2.0*(l->r0->i*(l->p0->x-x0) +
l->r0->j*(l->p0->y-y0) +
l->r0->k*(l->p0->z-z0));
float c = pow(l->p0->x-x0,2)+pow(l->p0->y-y0,2)+pow(l->p0->z-z0,2) - r*r;
float disc = b*b-4*a*c;
if(disc < -ZERO){
return NULL;
} else if(disc <= ZERO && disc >= -ZERO){
t = -b / (2*a);
if(t <= 0)
return NULL;
} else {
float t0 = (-b + sqrt(disc)) / (2*a);
float t1 = (-b - sqrt(disc)) / (2*a);
if(t0 < t1){
if(t0 > 0)
t = t0;
else if(t1 > 0)
t = t1;
else
return NULL;
} else if(t1 < t0){
if(t1 > 0)
t = t1;
else if(t0 > 0)
t = t0;
else
return NULL;
} else {
return NULL;
}
}
float x = l->p0->x+l->r0->i*t;
float y = l->p0->y+l->r0->j*t;
float z = l->p0->z+l->r0->k*t;
//---------------------------------
// Hmmm.......
//---------------------------------
collisionPoint = l->getPointAtTime(t);
printf("Collision at (%0.2f, %0.2f, %0.2f)\n",collisionPoint->getX(),collisionPoint->getY(),collisionPoint->getZ());
unitNorm = new Vect((collisionPoint->x-x0)/r,
(collisionPoint->y-y0)/r,
(collisionPoint->z-z0)/r);
if(mat->getTrans() > 0.0){
// application of snell's law
float n1 = 0.0;
if(l->getLastMaterial() == NULL){
n1 = REFRACT_AIR;
} else {
n1 = l->getLastMaterial()->getRefr();
}
float n2 = mat->getRefr();
incident = l->r0->getNormalized();
incident->setI(-incident->i);
incident->setJ(-incident->j);
incident->setK(-incident->k);
s1 = unitNorm->scalarMultiple(unitNorm->dot(incident))->sub(incident);
s2 = s1->scalarMultiple(n1/n2);
transLine = new Line(collisionPoint, s2->sub(unitNorm), l->getIntensity()*mat->getTrans());
transLine->setLastMaterial(mat);
//delete(s1);
//delete(s2);
//delete(incident);
}
if(mat->getRefl() > 0.0){
reflLine = new Line(collisionPoint, l->getR0()->findReflection(unitNorm), l->getIntensity()*mat->getRefl());
reflLine->setLastMaterial(l->getLastMaterial());
}
//delete(unitNorm);
return new CollisionPacket(collisionPoint,transLine,reflLine);
}
Point* Line::getPointAtTime(float t){
float x = this->p0->x+this->r0->i*t;
float y = this->p0->y+this->r0->j*t;
float z = this->p0->z+this->r0->k*t;
return new Point(x,y,z);
}