hi, 

below i attached sniplet of my code. its kinda massive sorry if its too hard to 
read. its a raytracing problem.
i did a benchmark and apparently the code is slower by time, or in this case, 
since it render from top to bottom, the further it go to the bottom the slower. 
i simplified the scene so all the row from top to bottom have an almost equal 
distance. im curious is there any problem in my code. 

i dont think i have problem in my algorithm, i just want to make sure there's 
nothing wrong with the code like bad memory management kind of stuff. any 
advice would be good :) 


Vec3 calculateLightShade(int objectIndex, int lightIndex, Ray ray, double 
distance) {
Vec3 color(0, 0, 0);
Vec3 intPoint = ray.getOrigin() + (ray.getDirection() * distance);
Vec3 pointToLight;
Vec3 lightPoint;
double totalShade = 0;
double shade = 1.0/(sample*1.0);
bool intersected = false;
Ray rayToLight;
float dot = 0;
int intersectIndex = -1;
double intersectDistance = 0;
for (int i = 0; i<sample; i++) {
lightPoint = tris[lightIndex].generateRandomPoint();
rayToLight.setOrigin(lightPoint);
rayToLight.setDirection(intPoint-lightPoint);
rayToLight.getDirection().normalize();
intersectIndex = intersect(rayToLight, intersectDistance);
if (intersectIndex == objectIndex) {
shade = 1.0/(sample*1.0f);
} else shade = 0.0f;
pointToLight = lightPoint - intPoint;
pointToLight.normalize();
Vec3 n = tris[objectIndex].normal;
n.normalize();
dot = pointToLight.dot(n);
if (tris[objectIndex].material.diffuse > 0) {
if (dot > 0) {
float diff = dot * tris[objectIndex].material.diffuse;
color = color + (tris[objectIndex].material.color * diff) * shade;
}
}
// specular
if (tris[objectIndex].material.specular > 0) {
Vec3 v = ray.getDirection();
v.normalize();
Vec3 r = pointToLight - tris[objectIndex].normal * (dot * 2.0f);
float dotProdSpec = v.dot(r);
if (dotProdSpec > 0) {
float spec = powf(dotProdSpec, 20) * tris[objectIndex].material.specular;
color = color + tris[lightIndex].material.color * spec * shade;
}
}
}
return color;
}

Vec3 radiance(Ray ray, int depth) {
Vec3 color(0.0, 0.0, 0.0);
Vec3 intPoint(0.0, 0.0, 0.0);
double distance = 0;
int index = intersect(ray, distance);
if (index == -1) {
return color;
}
intPoint = ray.getOrigin() + (ray.getDirection() * distance);
if (tris[index].material.emmision > 0) {
return tris[index].material.color;
}
for (int i = 0; i < nObject; i++) {
if (tris[i].material.emmision > 0) {
color = calculateLightShade(index, i, ray, distance);
}
}
if (tris[index].material.reflection > 0 && depth < maxDepth) {
if (tris[index].material.diff_refl > 0) {
float diff_refl = tris[index].material.diff_refl;
Ray reflRay = tris[index].getReflectionRay(ray, intPoint);
Vec3 reflDir = reflRay.getDirection();
Vec3 rn1 = Vec3(reflDir.z, reflDir.y, -1*reflDir.x);
Vec3 rn2 = reflDir.cross(rn1);
for (int i = 0; i < sample; i++) {
float xoffs, yoffs;
do {
xoffs = drand48() * diff_refl;
yoffs = drand48() * diff_refl;
} while ( (xoffs*xoffs + yoffs*yoffs) > (diff_refl * diff_refl) );
rn1 = rn1*xoffs;
rn2 = rn2*yoffs*diff_refl;
Vec3 r = reflDir + rn1 + rn2;
r.normalize();
Ray drRay;
drRay.setOrigin(intPoint);
drRay.setDirection(r);
Vec3 res = radiance(drRay, depth+1);
color = color + tris[index].material.color.mult(res * diff_refl);
}
}
else if (depth < 2) {
Ray reflRay = tris[index].getReflectionRay(ray, intPoint);
Vec3 reflColor = radiance(reflRay, depth+1);
color = color + (reflColor * 
tris[index].material.reflection).mult(tris[index].material.color);
}
}
return color;
}

 ================================= 
http://www.svnstrk.blogspot.com




________________________________
From: Paul Herring <[email protected]>
To: [email protected]
Sent: Thu, March 4, 2010 10:35:23 AM
Subject: Re: [c-prog] question about c++ memory allocation

  
On Wed, Mar 3, 2010 at 7:46 PM, Jos Timanta Tarigan
<jos_t_tarigan@ yahoo.com> wrote:

> so my code is a little bit rough so after i create an object, i tend not to
> destroy it. but then it happen in a method that being called a lot.
> is C++ care enough to destroy the object after it finished call
> the method?

C++ doesn't have 'garbage collection' by default so you should be
releasing objects created on the 'heap' (those created using new/clone
etc.)

Objects on the stack should be automatically released as soon as they
go out of scope.

-- 
PJH

http://shabbleland. myminicity. com/env
http://www.chavgang s.com/register. php?referer= 9375

 


      

[Non-text portions of this message have been removed]

Reply via email to