Skip to main content
GameDev.net gamedev.net
🔒 Locked

Ray Tracer problem - don't know what's happening

Started by kfboelter Sep 6, 2010 at 8:30 AM 7 replies 900+ views
Original Post
kfboelter
kfboelter
So.. I am building a ray tracer, but I am having a big problem with the diffuse ilumination. It's just not working... I believe I got the whole theory right, but for some reason it is just not working... things are wrong...

This is how it goes: I get a scene from a file, and then generate a .pnm with the scene image. All the objects are represented as quadrics... ray/quadric collision is working fine...

I tried debugging it in many different ways, but can't spot the error....
I would really need someone to maybe take a look at the whole code and try to catch the error... the code is very clear tho... I`m sure you will be able to get something out of it...

I am willing to send it to anyone who could perhaps try to take a look at it and spot the error...

So if anyone would be kind enough to help me out, I'll just send the code so far, and I will also be providing more detailed info on the code and the objectives...

I'm sry I can't be more specific about the errors... it's so frustrating not being able to spot what is going wrong on your code, after several hours (I truly mean it) of trying...

Thanks for your attention.
kfboelter
kfboelter
Thks for taking the time^^

It's weird, but i HAVE done all of those things.. I have spent a lot of time debugging it... it's really puzzling me...

I understand your meaning about "private" help...

of course I would be willing to post here what the solution was... I can also release it for download afterwards.. that would not be a problem...

The real problem is that I have no ideia of where the mistake is located.. so one would have to check the whole code for that...

again.. it's a pretty clear code...

thank you.
phresnel
phresnel
Quote:
The real problem is that I have no ideia of where the mistake is located..


Ditto.

Basically, your request is like "Program Crashes. Please help!", just for a ray tracer this time, hence we need more info, like the part of your code where you do your lighting math.
Darg
Darg
Why don't you post some screenshots for us. Post an image of the final result and also of the debug images phresnel recommended. Then we might be able to help more.
Portfolio & Blog:http://scgamedev.tumblr.com/
kfboelter
kfboelter
right on guys!
I am very thankful for you even replying my post... I know it sounds like I am just a lazy guy tryin to make someone else solve my problems...

so I'll just post some snippets, and if anyone spots some weird stuff please do tell me...

I'll start off with the casting of the rays...
void Scene::createCameraRays(){        double origin[3] = {*(eye->getPosition()), *(eye->getPosition()+1), *(eye->getPosition()+2)};	double dir[3] = {0, 0, 0};	cout << "ray origin: " << origin[0] << " " << origin[1] << " " << origin[2] << endl;	double z = 0; //as long as z = 0, we don`t worry about this one	double x, y; //values for the direction of the new Ray	int factor = 1; // determines how many rays there will be for each coordinate position		int xFac = sizeX*factor;//set number of rays created according to desired resolution.	int yFac = sizeY*factor;	double paceX = sizeX/(orthoX1 - orthoX0);//	double paceY = sizeY/(orthoY1 - orthoY0);//	double numX = (orthoX1 - orthoX0) / (xFac);	//value to be multiplied 	double numY = (orthoY0 - orthoY1) / (yFac);  //acoording to index increment	for(int i = 0; i < yFac; i++)	{		y = (orthoY1 + (i * numY));		for (int j = 0; j < xFac; j++)		{			x =(orthoX0 + (j * numX));			dir[0] = x - origin[0];			dir[1] = y - origin[1]; //Ray vectors are created 			dir[2] = z - origin[2]; //and normalized			 			//cout << dir[0] << " " << dir[1] << " " << dir[2] << endl;			calc->normalize(dir);			//cout << dir[0] << " " << dir[1] << " " << dir[2] << endl;					//cout << x << " " << y << endl;			createSingleRay(origin, dir);		}	}}


The rays are cast top~bottom, left~right... if you uncomment the console outputs u get the rays directions before and after they get normalized... I had to invert y because in ortho we start with y negative on top... but on space coordinates we get y positive on top...

And for the diffuse calculation:
void Ray::calcDifuse(Light *light, Quadric *quad, double dot){	double lp = light->getIntensity();	double kd = quad->getKd();	//double lamb = dot * kd;		double ipkd = lp * kd; 			rgbValue[0] += ipkd * dot * quad->getRed();	rgbValue[1] += ipkd * dot * quad->getGreen();	rgbValue[2] += ipkd * dot * quad->getBlue();	// ipkd * dot * quad->getCor()/}


That's some copy/paste of a model I am supposed to follow...

Well I guess thats it for a start... on a give case, eye position is 0, 0, 5... ortho is x0 = y0 = -1, and x1 = y1 = 1...

Heres a link for an image... its a sphere, with no ambient light, only diffuse...
http://img.photobucket.com/albums/v139/thuzad/diffuse.jpg

It`s all the same color.. maybe the dot variable is not getting the right result...
If you think I should post some more stuff please let me know.

Thanks so much
RDragon1
RDragon1
To debug this properly, set up a test case... Orient the camera and the surface in a specific orientation. This is easier if the surface you're drawing is a plane instead of a sphere. Then, you should have a known result for the calculations. Step through the code in a debugger and figure out why you aren't getting the correct result.
kfboelter
kfboelter
my friend, believe me, I have done some heavy debuggin here... one of the problems for the debugging is that I am using quadric surfaces to represent the objects on the scene... anyway, I followed step by step of my caluclations (normalizing vector, finding intersection point, findind intersection point normal, dot porduct) and still can't spot what's going wrong... I have a few guesses, but I can't go anywhere with them... perhaps my ortho/world space relation is not proper... really dunno...

anyway, I`m just gonna post some more of the code...

this is the part where rays are created, and shadow rays are cast, to check if there is any other object obstructing the light...

void Scene::createSingleRay(double *orig, double *d){	int quadricsVec = gina.size();	int lightsVec = lights.size();	ray = new Ray(orig, d);	int zM;	double val, min = 15000;	double zPoint;	bool hit = false;	double t;			for (int i=0 ; i < quadricsVec; i++)	{		t = ray->checkIntersection(gina.at(i));		if(t != -1)			{					zPoint = ray->getZ0() + (*(ray->getDirection()+2) * t);			val = zLength(ray, zPoint);			if (val < min)			{				min = val;				zM = i;				ray->calcIntersectionPoint();				ray->calcIntersectionNormal();				calc->normalize(ray->getIntersectionNormal());				hit = true;			}					}	}	if (hit)	{		ray->calcAmbient(ambient, gina.at(zM));					castShadowRays(ray, gina.at(zM));		rays.push_back(ray);		return;	}	else	{			ray->setRgbValue(getBackGroundColor());		rays.push_back(ray);	}}void Scene::castShadowRays(Ray *r, Quadric *quad){	double dir[3];	double check, dot;	bool hit = false;		for (int i =0; i<lights.size(); i++)	{		dir[0] = *(lights.at(i)->getPosition()+0) - *(r->getIntersectionPoint()+0);		dir[1] = *(lights.at(i)->getPosition()+1) - *(r->getIntersectionPoint()+1);		dir[2] = *(lights.at(i)->getPosition()+2) - *(r->getIntersectionPoint()+2);		calc->normalize(dir);		Ray *sRay = new Ray(r->getIntersectionPoint(), dir);		for (int j = 0; j < gina.size(); j++)		{			check = sRay->checkIntersection(gina.at(j));			if (check != -1)			{				hit = true;			}		}				if(hit == false)		{			dot = calc->dotProduct(sRay->getDirection(), r->getIntersectionNormal());			if (dot > 0.0)				r->calcDifuse(lights.at(i), quad, dot);		}								}}


I highly doubt that the bug is hiding somewhere else that the snipepts I've psoted here.. If you or anyone else wants some other specific part, just ask me^^

also, anyone can feel free to use anything they want... the overall structure is ok I guess...
thank you
cignox1
cignox1
Can't you post a few screenshots? It would be easier for us to spot the problem (or where it might be).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.