Ray Tracing - Final Object Color wrong after refraction

I am in the process of implementing a Ray Tracing but I have found a problem with my refraction.

This are the two relevant methods,

Color Scene::traceRefractionRay(Ray &newRay, Ray r) { double tmin = INFINITE; Instance *obj = NULL; HitRecord hitrec; // process each primitive instace for (std::vector<Instance *>::iterator it = instances.begin(); it != instances.end(); it++) { HitRecord hr; real t = INFINITE; Instance *ins = *it; if (ins->intersects(r, t, hr)) { if (t < tmin) { tmin = t; obj = ins; hitrec = hr; } } } if (tmin != INFINITE) // We have a hit { newRay.o = hitrec.p; newRay.d = r.d; Vector3 l = lights[0]->sampleLight(hitrec.p); real dot = MAX(0, hitrec.n.dot(l)); return obj->m.kd * dot + obj->m.ka; } return background; } Color Scene::traceRay(const Ray &r) { double tmin = INFINITE; Instance *obj = NULL; HitRecord hitrec; // process each primitive instace for (std::vector<Instance *>::iterator it = instances.begin(); it != instances.end(); it++) { HitRecord hr; real t = INFINITE; Instance *ins = *it; bool isInShadow = false; if (ins->intersects(r, t, hr)) { if (t < tmin) { tmin = t; obj = ins; hitrec = hr; } } } if (tmin != INFINITE) // We have a hit { Color finalColor(0, 0, 0); int remainingRefractions = 4; for (int i = 0; i < lights.size(); i++) { //lightDir Vector3 l = lights[i]->sampleLight(hitrec.p); real cosI = -hitrec.n.dot(r.d); Color refractedColor(1.0, 1.0, 1.0); while (remainingRefractions > 0) { if (obj->m.kt.r != 0.0 && obj->m.kt.g != 0.0 && obj->m.kt.b != 0.0) if (cosI > 0) { real sinI = sqrt(1 - pow(cosI, 2)); Vector3 m = (hitrec.n * cosI - r.d) / sinI; real sinT = (obj->m.ni * sinI) / obj->m.nt; real cosT = sqrt(1 - pow(sinT, 2)); Vector3 t = m * sinT - hitrec.n * cosT; Ray newRay(hitrec.p, t); refractedColor += traceRefractionRay(newRay, newRay); } else { Ray newRay(hitrec.p, r.d); refractedColor += traceRefractionRay(newRay, newRay); } remainingRefractions--; } refractedColor = refractedColor / 4; //Diffuse lighting real diffuseDot = MAX(0, hitrec.n.normalize().dot(l)); real specularDot = 0.0; if (diffuseDot > 0.0) { //Specular lighting Vector3 viewDir = (-r.d).normalize(); //N //correto??? v = ray.inverso Vector3 halfDir = (l + viewDir).normalize(); //H real specAngle = MAX(hitrec.n.dot(halfDir), 0.0); //N.H specularDot = pow(specAngle, 64.0); //(N.H)^n } finalColor += obj->m.kd * diffuseDot + obj->m.ks * specularDot + obj->m.kt*refractedColor; } return obj->m.ka + finalColor; // + kt*Ot } return background; }

My problem is in my color combination, for instance:

I have a background blue, and I have a transparent green object, the color combination of the green object should be closer to blue, but it is a clear green.

If I dont apply any refractedColor my object would have a much more heavy green, so I can see a difference when I apply it, but not the expected one.

This is the picture of my torus:

Ray Tracing - Final Object Color wrong after refraction

Category:c# Time:2018-12-05 Views:1
Tags: raytracing

Related post

  • After Effects CC Ray-traced 3D renderer crash - Initial shader compile failed (5070 ::12) 2014-03-31

    Hello, I've recently bought a new laptop with i7 processor, 16GB RAM and GTX860M 4GB graphic card to be able to run After Effects smoothly. I wanted to take advantage of the Ray-traced 3D renderer feature but once I switch to it I instantly get three

  • Ray tracing in modern OpenGL. Where do I begin? 2010-09-08

    So I'm at a point that I should begin lighting my flatly colored models. The test application is a test case for the implementation of only latest methods so I realized that ideally it should be implementing ray tracing (since theoretically, it might

  • Ray-tracing a sphere with displacement mapping 2010-12-18

    I'm interested in building a simple "Google Earth" type app (for overlaying my own information, not the huge quantity of data that Google has). I'd like it to just be a simple X11 app that ray-traces a sphere with displacement (topographic) informati

  • ray tracing lighting 2011-10-17

    I am trying to implement specular and diffuse lighting for a simple sphere ray tracing application, but I am having problems with my vectors. I am trying to use the following to update the light, but the generated image looks exactly the same, so I k

  • Why are my 3d ray-traced shadows and surfaces rendering out all grainy and pixelated? 2012-09-24

    Hi folks I've been playing with ray traced extruded eps text and adding lights in After Effects CC 2014 and when I export through Adobe Media Encoder 2014, the shadows and surface of the shapes appear grainy and pixelated (as per below image). Ive tr

  • Is there a Java-based ray tracing model that can be adapted for use in underwater acoustics? 2008-09-19

    I am looking for an open-source Java-based raytracing system suitable for use in modeling underwater ray-based acoustics. Such a package might be similar to the Comprehensive Acoustic System Simulation (CASS) with the Gaussian Ray Bundle (GRAB) but I

  • Ask for resource about fast ray-tracing algorithm 2010-01-06

    First, I am sorry for this rough question, but I don't want to introduce too much details, so I just ask for related resource like articles, libraries or tips. My program need to do intensive computation of ray-triangle intersection (there are millio

  • final object in java 2010-07-17

    Possible Duplicate: When to use final Hi all, What is the use of declaring final keyword for objects? For example: final Object obj = new myclass(); --------------Solutions------------- Using the "final" keyword makes the the variable you are declari

  • What do I need to build simple 3d editor that Ray Tracing 3d images 2010-11-25

    What do I need to know and what libraries do I need to use if I like to build simple 3d editor ( like Xara3d for example ) that lts me edit simple 3d objects and the end result will produce me good quality Ray Traced image . simplicity in mind I will

  • Ray Tracing question, how to map screen coordinates to world coordinates? 2011-01-09

    I was studying Ray Tracing on http://www.devmaster.net/articles/raytracing_series/part1.php when I came across this piece of code: void Engine::InitRender() { // set first line to draw to m_CurrLine = 20; // set pixel buffer address of first pixel m_

  • get uvw coordinates from pixel coordinates for ray-tracing 2011-08-15

    I'm trying to implement a simple ray-tracing algorithm so the first step is to convert pixel coordinates into uvw coordinates system iam using those two equations that i found in a book where l,r,b,t are the view frustum points , (i,j) are the pixel

  • Ray tracing intersection test by transfoming the ray 2011-11-20

    I'm trying to implement a basic ray tracer, which involve transform each ray into each object space and test intersection with the affinely deformed object by multiply the inverse of the affine transformation matrix with the ray. The intersection tes

  • Ray tracing box intersections 2012-01-17

    So I'm back with another ray tracing question. My code renders spheres all fine and dandy, but cubes aren't really working. I'm using this code to test for intersections: http://pastebin.com/qgm6vpdx (It's a recursive function, t is the distance to t

  • Ray tracing - constructing ray through pixel 2012-04-07

    I have an assignment to implement ray tracing in Java. I'm not asking for much, just to have some information on how to construct the rays from the camera through a pixel given its x and y. I've found over the Internet a lot of sources that explain t

  • Why do I have to send the final object to the arguments list if I want to do something in a new thread? 2013-11-10

    For example, I try to do something in the separate thread: public void shareQuote(final Context context, final ArrayList<Quote> quotes, final int number) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { Toast warningWindow = To

  • Java instantiate final object for the second time 2014-05-17

    This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 54 answers This can't be achieved, you cannot instantiate an already instantiated final object: private void myMethod(){ final Object object = null; object = ne

  • Is there a really good book about ray tracing? 2009-03-01

    I need to do some research on ray tracing and create my own ray tracer. Are there any good books on the subject? --------------Solutions------------- Pharr and Humphreys, "Physically Based Rendering", Morgan-Kaufman 2004 Wann Jensen, "Realistic Image

  • Java's "public static final Object" in C++ 2010-03-06

    What would be the equivalent of this in C++ of the following snippet. I am in the process converting parts of an java application to C++. Here is that java class snippet: class container { Public static final Object CONTAINER_FULL = new Object { publ

  • iPhone error, 'Referenced-counted object is use after it is released error' ; memory leak cleanup 2011-01-17

    I am trying to clean up memory leaks and other issues in an existing iPhone app. I am a little new to Objective C, but have some good programming fundamentals and a general understanding of the memory management that is required when dev'ing iphone a

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 4.712 (s). 15 q(s)