Thread: [Java] My raytracer project(s)

Page 1 of 3 123 LastLast
Results 1 to 10 of 25
  1. #1 [Java] My raytracer project(s) 
    Renown Programmer
    Harha's Avatar
    Join Date
    Jul 2006
    Age
    30
    Posts
    433
    Thanks given
    3
    Thanks received
    31
    Rep Power
    339
    So, I got interested in 3D graphics programming a while ago and decided to write a raytracer in pure java after reading a bit about raytracing. I'm just posting this small project of mine here for no particular reason, but hopefully someone will learn something from it or something. If you don't know what raytracing is then I highly suggest you read a bit about it because it's such a simple way of rendering a 3D scene which will give nearly photorealistic results.

    It currently supports mathematical spheres, planes and triangles. .obj Models are supported too on a basic level (The vertex data has to be as triangles so no quads, no textures yet or anything like that) and they are built of obviously triangles. I wrote a simple "scene" loading system too, so you can easily just setup whatever objects or light sources you want in your scene using a text editor and then render it. Camera can be moved/rotated in realtime using a keyboard.

    Github link: https://github.com/Harha/JRay (My Java tracer)
    Github link: https://github.com/Harha/JRay-GLSL (My GLSL tracer)

    I'm also planning to develop this further using OpenGL and have already a basic working raytracer written in pure GLSL. I haven't figured out how to pass thousands of primitives to the shader program from the main java program yet though so the scene must be set inside the shader's source code. Well why am I then writing this whole thing over again so that it will be computed on a GPU instead of CPU? Well performace obviously, while my CPU tracer can render very simple scenes realtime with like 100fps in resolutions 256x256 and below this GLSL version can do much more in 1080p resolution while still maintaining framerates above 50 (Depending on the type of GPU used obviously). GLSL doesn't allow functions to be recursive though and my java tracer is basicly doing all the light bouncing recursively by calling the same function over and over inside itself so I had to write this thing using iterative recursion instead (in a loop) which wasn't that different tbh though.

    So yeah, that's it, decided to just post something in here for fun since I haven't really visited here after I lost interest in Runescape and it's private server scene.



    Just some random scene I rendered with the purely Java based raytracer. The teapot is weirdly missing some vertices in this picture and I really don't know why, possibly there was some "limit" exceeded or something since it had quite many of them. Haven't happened since rendering that picture though.

    I might still develop the Java version further and so on.... So I might update this thread once in a while with new additions to either programs.

    Some images rendered using a way older version of the java tracer:
    Attached image
    Attached image
    Reply With Quote  
     


  2. #2  
    Donator

    Join Date
    Jul 2014
    Posts
    6
    Thanks given
    0
    Thanks received
    3
    Rep Power
    0
    Hi, did you do all the lightings and shadows? Nice raytracing bro
    Reply With Quote  
     

  3. #3  
    Renown Programmer
    Harha's Avatar
    Join Date
    Jul 2006
    Age
    30
    Posts
    433
    Thanks given
    3
    Thanks received
    31
    Rep Power
    339
    Quote Originally Posted by Lycus View Post
    Hi, did you do all the lightings and shadows? Nice raytracing bro
    "Did I do all the lightings and shadows?" ?

    What do you exactly mean by that? Yes, I made this program completely by myself after reading about the concept behind raytracing, if that's what you're asking. Lighting works simplifiedly and in a nutshell by calculating the distance from the desired surface you want to lit up to the lightsource and then taking the intensity of that specific lightsource and doing some other calculations and then dividing the whole number by the square of the |distance| to the light from the surface. During that calculation I also check if something blocks the way between the surface (Which is a point in 3D space) and the lightsource (Which is also obviously a point in 3D space) and if it happens so that something is between them, the color for the surface at that particular point won't be calculated so it's final color black + ambient lighting.

    That's like, the most simple way you can calculate shadows and shade a surface, at least when doing raytracing, as far as I know. I don't know much about rasterization but probably if you're writing a rasterizer instead of a raytracer you take the surface which is made of triangles and then calculate the color of all point on the triangle's surface by doing the exact same shading calculation as I told earlier but this time you only do it for the 3 vertices of the triangle and the colors on the surface of the triangle are calculated by interpolating between the results of the colors of the three vertices.

    I don't know why I started wondering about shading in a rasterizing based 3d renderer but whatever...

    Edit: Got rid of phong shading and implemented Cook-Torrance shading to my GLSL tracer which is way more realistic.



    My shading function:

    Code:
    //// Color shading
    //// References:
    //// http://content.gpwiki.org/index.php/D3DBook:%28Lighting%29_Cook-Torrance
    //// http://ruh.li/GraphicsCookTorrance.html
    vec3 shadeCookTorrance(in Ray r, in Intersection x, in Light l)
    {
      // Specify some variables
      float R = x.mat.roughness;
      float RR = R * R;
      float F = x.mat.fresnel;
      float K = x.mat.density;
      vec3 VD = -r.dir;
      vec3 L = normalize(l.pos - x.pos);
      vec3 H = normalize(VD + L);
      float NdotL = clamp(dot(x.norm, L), 0.0, 1.0);
      float NdotH = clamp(dot(x.norm, H), 0.0, 1.0);
      float NdotV = clamp(dot(x.norm, VD), 0.0, 1.0);
      float VdotH = clamp(dot(H, VD), 0.0, 1.0);
      
      // Geometric attenuation
      float NH2 = 2.0 * NdotH / VdotH;
      float geo_b = NH2 * NdotV;
      float geo_c = NH2 * NdotL;
      float geo = min(1.0, min(geo_b, geo_c));
      
      // Roughness (Beckmann distribution function)
      float r1 = 1.0 / (4.0 * RR * pow(NdotH, 4.0));
      float r2 = (NdotH * NdotH - 1.0) / (RR * NdotH * NdotH);
      float roughness = r1 * exp(r2);
      
      // Fresnel
      float fresnel = pow(1.0 - VdotH, 5.0);
      fresnel *= (1.0 - F);
      fresnel += F;
      
      // Color calculations
      vec3 color_spec = vec3(0.0);
      if (NdotV * NdotL <= -EPSILON || NdotV * NdotL >= EPSILON)
        color_spec = vec3(fresnel * geo * roughness) / (NdotV * NdotL);
      vec3 color_final = NdotL * ((1.0 - K) * color_spec + K * x.mat.col) * l.col;
      return color_final;
    }
    Reply With Quote  
     

  4. #4  
    Respected Member

    Situations's Avatar
    Join Date
    Sep 2008
    Posts
    3,903
    Thanks given
    230
    Thanks received
    1,695
    Rep Power
    5000
    Looks sexy, man. Nice job.
    Reply With Quote  
     

  5. #5  
    Renown Programmer
    Harha's Avatar
    Join Date
    Jul 2006
    Age
    30
    Posts
    433
    Thanks given
    3
    Thanks received
    31
    Rep Power
    339
    Thanks, though there isn't that much work put into this yet but I guess it's something...

    Proper refraction support, material density can be set for each solid primitive. Each ray knows whether it's inside a medium or not.



    Edit: Some progress, shading works way better now.

    Reply With Quote  
     

  6. #6  
    Community Veteran


    Join Date
    Sep 2007
    Posts
    370
    Thanks given
    127
    Thanks received
    170
    Rep Power
    417
    this is hot
    Original founder of this site
    8 Year RSPS Veteran
    Java programmer of 7 years
    Advanced PHP Programmer
    Reply With Quote  
     

  7. #7  
    Renown Programmer
    Harha's Avatar
    Join Date
    Jul 2006
    Age
    30
    Posts
    433
    Thanks given
    3
    Thanks received
    31
    Rep Power
    339
    Quote Originally Posted by Gander View Post
    this is hot
    Are you really the guy I think you are? I find that a bit hard to believe... Anyways, cool to see that you still lurk around here.

    I made a WebGL version for fun, it can be played realtime in shadertoy: https://www.shadertoy.com/view/MdSSRc

    Doesn't require you to install anything, works if your device supports modern OpenGL.
    Reply With Quote  
     

  8. #8  
    Registered Member Tx-Sec's Avatar
    Join Date
    Jan 2008
    Age
    30
    Posts
    520
    Thanks given
    34
    Thanks received
    15
    Rep Power
    68
    so good i loved it!
    Reply With Quote  
     

  9. #9  
    Registered Member Motions's Avatar
    Join Date
    Jul 2013
    Age
    28
    Posts
    316
    Thanks given
    73
    Thanks received
    92
    Rep Power
    69
    impressive


    Reply With Quote  
     

  10. #10  
    Renown Programmer
    Harha's Avatar
    Join Date
    Jul 2006
    Age
    30
    Posts
    433
    Thanks given
    3
    Thanks received
    31
    Rep Power
    339
    Glad to hear so positive opinions about it.

    I remade the whole color shading part of my GLSL tracer because it was really unaccurate and didn't take light distances and things like that in account too well.

    Code:
    //// Color shading
    //// References:
    //// http://content.gpwiki.org/index.php/D3DBook:%28Lighting%29_Cook-Torrance
    //// http://ruh.li/GraphicsCookTorrance.html
    vec3 shadeCookTorrance(in Ray r, in Intersection x, in Light l)
    {
    	// Attenuation threshold, return vec3(0.0) if we fall below this limit
    	vec3 L = l.pos - x.pos;
    	float dist = length(L);
    	float A = (1.0 + l.intensity) / dist;
    	if (A < ATTENUATE_THRESHOLD)
    		return vec3(0.0);
    	// Specify surface material values
    	float R = x.mat.roughness;
    	float F = x.mat.fresnel;
    	float K = x.mat.density;
    	// Specify some other variables
    	L = normalize(L);
    	vec3 N = normalize(x.norm);
    	vec3 V = normalize(-r.dir);
    	vec3 LCOL = l.col * l.intensity;
    	float NdotL = max(dot(N, L), 0.0);
    	float specular = 0.0;
    	if (NdotL > 0.0)
    	{
    		// Calculate intermediary values
    		vec3 H = normalize(V + L);
    		float NdotH = max(dot(N, H), 0.0);
    		float NdotV = max(dot(N, V), 0.0);
    		float VdotH = max(dot(V, H), 0.0);
    		float RR = R * R;
    		
    		// Geometric attenuation
    		float NH2 = 2.0 * NdotH;
    		float g1 = (NH2 * NdotV) / VdotH;
    		float g2 = (NH2 * NdotL) / VdotH;
    		float geo = min(1.0, min(g1, g2));
    		
    		// Roughness (Microfacet/Beckmann distribution function)
    		float r1 = 1.0 / (4.0 * RR * pow(NdotH, 4.0));
    		float r2 = (NdotH * NdotH - 1.0) / (RR * NdotH * NdotH);
    		float rough = r1 * exp(r2);
    		
    		// Fresnel (Schlick approximation)
    		float fresnel = pow(1.0 - VdotH, 5.0);
    		fresnel *= (1.0 - F);
    		fresnel += F;
    		
    		// Final specular highlight
    		specular = (fresnel * geo * rough) / (NdotV * NdotL);
    	}
    	
    	return smoothstep(vec3(0.0), vec3(1.0), A * LCOL * NdotL * (K * x.mat.col + specular * (1.0 - K)));
    }
    That piece of code leads to results like in the screenshots below. Notice the yellow reflective floor in the first picture, it's supposed to be like some really shiny and polished metal'ish material.




    Short low quality video for the more curious people: https://www.dropbox.com/s/37otyilgk4...bmhd.webm?dl=0

    Edit: Playing around with some trigonometric functions for fun. It seems to be quite easy to simulate a 'liquid' type of surface by assigning a simple mixture of sine and cosine waves modulated over time to a surface's y coordinate as example and make it depend on the current surface x and z coordinates or whatever you wish.



    WebGL version: https://www.shadertoy.com/view/lsBXzt
    Reply With Quote  
     

Page 1 of 3 123 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Project Java - My First Project
    By Project Java in forum Projects
    Replies: 14
    Last Post: 05-07-2013, 12:42 PM
  2. My Current Project!
    By Kolli` in forum Website Development
    Replies: 9
    Last Post: 05-14-2008, 05:24 PM
  3. name for my new project
    By Arvid in forum RS2 Server
    Replies: 2
    Last Post: 03-20-2008, 11:32 AM
  4. Some CC Needed on my Site for my ICT Project
    By Impulser in forum Showcase
    Replies: 7
    Last Post: 03-18-2008, 04:29 PM
  5. My newest project!
    By newservermaker in forum RS2 Client
    Replies: 3
    Last Post: 01-05-2008, 10:56 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •