## programing a raytracer

164 posts
IanT
Posts: 153
Joined: Fri Aug 25, 2006 3:13 am
Deus wrote:While we are at it do this in Java

http://betterexplained.com/articles/und ... uare-root/
OK...

Code: Select all

``````public static float InvSqrt(float x){
float xhalf = 0.5f * x;
int i = Float.floatToIntBits(x);
i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method
x = Float.intBitsToFloat(i); // convert new bits into float
x = x*(1.5f - xhalf*x*x); // One round of Newton's method
return x;
}``````
Deus wrote:Well this takes a couple of clock cycles but in C++ but HUNDREDS in Java (because you cannot use the same method)
I just used the same method
Deus wrote:But you dont do b= a / sqrt(x^2+y^2+z^2) in raytracers so this optimization is not relevant right?
See above.

Firstly, you were a bit naive to think the same optimisation isn't possible in Java. Secondly, you were even more naive to think that an approximation like this has any use in a ray tracer so yes, it is totally irrelevant

Ian.

eman7613
Posts: 597
Joined: Sat Sep 16, 2006 2:52 pm
oodmb wrote:i just wrote a code for refraction, although it seems to be doing very little to nothing if the ior is at 1.5 at 15 it works although little, i think i might be missing a cosine or something in my code:

Code: Select all

``````    public static Ray Refract(double n1, double n2, Ray Ir, Ray Nr){
Vector I=Ir.Direction;
Vector N=Nr.Direction;
//Ir=new Ray(Nr.Origin, I);
I.normalise();
N.normalise();
double LN=I.dot(N);
if (LN<0){
Nr.Direction.negate();
N=Nr.Direction;
LN=I.dot(N);
}
double NON2=n1/n2;
Ray toI=new Ray(Nr.getPointAt(LN), Ir.getPointAt(1));
double theta=Math.acos(LN);
double sizeB=NON2*Math.sin(theta);
Ray returner= new Ray(Nr.Origin, toI.getPointAt(sizeB));
returner.ior=n2;
return returner;
}``````
can you provide where you found teh algorithum for this? I dont know this kinda stuff off the top of my head or have played with 3d in this manner befor so i can just pop out an awnser
Yes i know, my spelling sucks

IanT
Posts: 153
Joined: Fri Aug 25, 2006 3:13 am
oodmb,

Try the following...

Code: Select all

``````public Vector refractRay(Vector rayDirection, Vector normal, double entryIOR, double exitIOR) {
Vector wi = rayDirection.negate();
double k = entryIOR / exitIOR;
double cos = normal.dotProduct(wi);
double cosA = Math.abs(cos);
double ior2 = k*k;
double sqrtOperand = 1.0 - (ior2 * (1.0 - (cosA*cosA)));

if (sqrtOperand < 0.0) {
// Total internal reflection
return wi.subtract(normal.scale(2.0 * cos));
}

double cosR = Math.sqrt(sqrtOperand);
double kN = cosR - (k * cosA);
return wi.multiply(k).subtract(normal.multiply(kN));
}``````
It assumes the following:

- the rayDirection vector is normalised
- the normal has been corrected (if necessary) to point against the rayDirection vector (e.g. it was flipped if the ray hit the inside of the surface)
- entryIOR is the IOR of the media that the ray is currently inside
- exitIOR is the IOR of the exit media

I hacked this down from much more complex code that I have already just to remove handling of a media stack, fresnel reflection and russian roulette, so it might not be guaranteed to work as it stands

Ian.

oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:
can you provide where you found teh algorithum for this? I dont know this kinda stuff off the top of my head or have played with 3d in this manner befor so i can just pop out an awnser

i made it up- its very similar to how i got my reflection algorithm (except that one worked the second i did it)

IanT- thanks again- i'll try that, right now i'm remaking some of the classes and switch data so that its easier to code and the ior stuff works correctly for every interaction between two materials. i'll try using this method.
a shiny monkey is a happy monkey

oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:
it works! realy well.
now that my program has a photon ray system to hold stuff like current ior data and its own power, it should be realy easy to add more stuff.
a shiny monkey is a happy monkey

IanT
Posts: 153
Joined: Fri Aug 25, 2006 3:13 am
oodmb wrote:it works! realy well.
now that my program has a photon ray system to hold stuff like current ior data and its own power, it should be realy easy to add more stuff.
Cool Are you going to implement photon-mapping in the near future?

Ian.

oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:
ok- it doenst realy work realy well- i dont think its an object id thing because that only works with shadow

-yes! i have already set up the camera to be able to collect rays.
a shiny monkey is a happy monkey

oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:
i got the fresnel working with refraction (i think)!!! i'm going to releise a buggy coppy of it with a sceneloader soon so people can test it out.

a shiny monkey is a happy monkey

oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:
one new and intereseting problem, although one i am going to be too lazy to fix- when the sphere is set to an ior of 1 it becomes lighter then the rest of the air around it thus causing the scene to look like is has a dark screen with a whole in it.
a shiny monkey is a happy monkey

Kram1032
Posts: 6649
Joined: Tue Jan 23, 2007 3:55 am
Location: Austria near Vienna
hmmm...
reflections with IoR 1 = invisible = black...

oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:
well normaly ior of 1 just equals completely invisible, but my fresnel settings should be making the sphere darker at 1 or at least a bit reflective (i dont remember if i had reflect on or off on this test). but in my case the fresnel is making the sphere intensify the color of whatever is behind it.
a shiny monkey is a happy monkey

tinman999
Posts: 23
Joined: Tue Apr 17, 2007 1:29 pm
Location: UK
Contact:
oodmb wrote:well normaly ior of 1 just equals completely invisible, but my fresnel settings should be making the sphere darker at 1 or at least a bit reflective (i dont remember if i had reflect on or off on this test). but in my case the fresnel is making the sphere intensify the color of whatever is behind it.
Look's find to me, the value of fresnel equation approach to 1 as you approach grazing angle and at grazing angle (90 deg) fresnel will always be 1 no matter what ior you used, so you will get 100% reflection

oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:
Look's find to me
thats because this is a picture of it with an ior of 1.5 and working. im still finding bugs in my colour class because i put virtualy no thaught into it.
a shiny monkey is a happy monkey

oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:
ok, so i'm rewriting my whole renderer because since i started my renderer i've learned alot about java that i didnt know before and its been hard to keep my source code consistent while learning so much and thus very hard to debug stuff. this time i'm making it with threads so that you can see render progress and it shows the current time. however, i'm just noticing that when i start my program, windows shows it to start with 14 threads. this is a bit odd to me since i only set up 3 threads to run. could this possibly degrade the performance of my final renderer or is this just a general java thing?
a shiny monkey is a happy monkey

eman7613
Posts: 597
Joined: Sat Sep 16, 2006 2:52 pm
oodmb wrote:ok, so i'm rewriting my whole renderer because since i started my renderer i've learned alot about java that i didnt know before and its been hard to keep my source code consistent while learning so much and thus very hard to debug stuff. this time i'm making it with threads so that you can see render progress and it shows the current time. however, i'm just noticing that when i start my program, windows shows it to start with 14 threads. this is a bit odd to me since i only set up 3 threads to run. could this possibly degrade the performance of my final renderer or is this just a general java thing?
I jsut started dipping into threads for some work i'm doing.

a) Any swing component uses theads automatically, that might be the threads your seeing (they are daemon theards)

c) see b

d) becarful when debugging with how you initialized a thread, if you do something along the lines of

Code: Select all

`` class LearningThreads extends Thread``
there are ways to trick the compiler where you compile, and crash on execution (i don't remember how i did it, but i did)

e) I found this usefull for myself, but using threads + others often means throwing a lot of acceptions. If you anticipate whats going on, just add

Code: Select all

`` throws throwable``