jim
New Member
Posts: 5
|
Post by jim on Feb 12, 2020 10:13:13 GMT
Hi,
I’m pretty sure this is me missing something but my transparent glass spheres are rendering as a solid colour.
I’m up to the fresnel effect bit and the last test for shadeHit for a refracted colour with a semi transparent glass pane and a sphere beneath fails. All the other tests pass.
At the moment my shade hit adds lighting to reflection to refraction and returns the result. The book tells me this should give functioning refraction which is a bit dodgy around the total internal reflection region.
I have not updated the lighting function though and that is returning a value that does not take transparency into account.
Did I just miss that bit somehow?
When I render an image with a black sphere with transparency set to 1 I imagine it should not render as a solid black sphere but it does currently.
So did I just miss the bit where lighting gets updated? In which case a page number would be super handy. Or is my belief that the lighting function should be updated incorrect?
My naive assumption is that the colour returned from lighting should be multiplied by 1 - transparency or something similar. I’ve not had a chance to try that yet.
Any pointers gratefully received.
|
|
fremag
Junior Member
Posts: 73
|
Post by fremag on Feb 12, 2020 11:11:14 GMT
Hi, My naive assumption is that the colour returned from lighting should be multiplied by 1 - transparency or something similar. I’ve not had a chance to try that yet. You're right.
In chapter 11, section "Transparency and Refraction", look at the paragraph "Test #7: Finding the Refracted Color"
In the refracted_color function, you should have a line like this: color = color_at(world, refract_ray, remaining - 1) * comps.object.material.transparency
Hope this helps.
If you have some code to show, I may have a look at it too.
|
|
jim
New Member
Posts: 5
|
Post by jim on Feb 12, 2020 17:49:08 GMT
You're right.
In chapter 11, section "Transparency and Refraction", look at the paragraph "Test #7: Finding the Refracted Color"
In the refracted_color function, you should have a line like this: color = color_at(world, refract_ray, remaining - 1) * comps.object.material.transparency
Hope this helps.
If you have some code to show, I may have a look at it too.
Hi fremag. Many thanks for your response, it helped! Well. That is definitely one of the problems with my code. I had completely failed to see that final multiplication! I note that in the test 'refracted colour with a refracted ray' transparency is set to 1.0 which means the test passes whether or not you have the multiply in there. I should probably have another test case with transparency set to 0.5 and expecting a value half the size. This made my test number 8 'shade hit with a transparent material' pass. Which is nice. My test render on the other hand is still showing a black sphere. Like so In that test I define the black sphere like so. val transform = Matrix.identity(4) .scale(1.4, 1.4, 1.4) .translate(-2.0, 1.4, 0.5)
val blackMaterial = Material( colour = Colour.black(), transparency = 1.0, refractiveIndex = 1.5 ) val s = Sphere(transform = transform, material = blackMaterial) Now my assumption had been that everything has a colour even if it is transparent. Which is why I was wondering about the lighting bit of shade_hit. Here is my shade hit function at the moment fun shadeHit(computations: PrecalculatedIntersection, recursiveDepth: Int): Colour { val light = lights.first() val isShadowed = isShadowed(computations.overPoint) with(computations) { val surface = light.lighting(obj.material, obj, point, eyeVector, normalVector, isShadowed)// should this be reduced? val reflected = reflectedColour(this, recursiveDepth) val refracted = refractedColour(this, recursiveDepth) return surface + reflected + refracted } } I have not adjusted what comes back from light.lighting. Could that be the issue as it will return values as though it is black and not transparent. Should the material not have a colour? I had decided that I didn't want nulls so my Material always has a default pattern which is a plain pattern which is set to the specified colour. So a pattern is always being used. Here is my refractedColour function which as the tests pass I now believe to be correct. fun refractedColour(comps: PrecalculatedIntersection, remaining: Int): Colour { val black = Colour.black() if(remaining == 0) return black if(comps.obj.material.transparency == 0.0) return black
val nRatio = comps.n1 / comps.n2 val cosI = comps.eyeVector.dot(comps.normalVector) val sin2t = (nRatio * nRatio) * (1 - (cosI * cosI))
if(sin2t > 1.0) return black
val cosT = sqrt(1.0 - sin2t) val direction = comps.normalVector * (nRatio * cosI - cosT) - comps.eyeVector * nRatio val refractRay = Ray(comps.underPoint, direction) val colour = colourAt(refractRay, remaining - 1) * comps.obj.material.transparency // Look mum it's the missing multiply
return colour } Any more pearls of wisdom you can throw my way? Thanks, Jim
|
|
fremag
Junior Member
Posts: 73
|
Post by fremag on Feb 13, 2020 8:02:16 GMT
Hi, Your code looks good to me (it looks like the code I wrote during chapter 11 if you want to check) So maybe the issue is somewhere else. Your sphere is black but it's also the default color returned in many tests / if blocks. You could change this. Replace black by blue/red/green/pink and see the results.
Let's imagine an issue with n1 or n2 and the following code always returning black.
val nRatio = comps.n1 / comps.n2 val cosI = comps.eyeVector.dot(comps.normalVector) val sin2t = (nRatio * nRatio) * (1 - (cosI * cosI))
if(sin2t > 1.0) return black Change the black color by red for instance.
If your sphere is red in your picture, you will have some information about your bug.
You can also render a more basic scene and play with your debugger.
I had some issues too with reflection/refraction and I made the same scene with PovRay to have a reference to compare with.
|
|
jim
New Member
Posts: 5
|
Post by jim on Feb 13, 2020 17:49:36 GMT
O.k. So I have tried a red sphere. The sphere comes out light red. If I don't add refracted colour then it is just straight red with the refracted colour added in shade hit the sphere is simply a lighter red. I got some printlns of the values for refracted colour and they were changing across the run and had various combinations of r g and b.
It seems to me that in this line of code val surface = light.lighting(obj.material, obj, point, eyeVector, normalVector, isShadowed) I am getting full red which is then being added to with refracted colour which means it gets lighter.
|
|
fremag
Junior Member
Posts: 73
|
Post by fremag on Feb 13, 2020 21:11:45 GMT
Well, without the code, it will be hard to guess where the problem is.
Do you have a way to share your project ? Github, pastebin, dropbox, mail (fremag.gh / gmail) ...
|
|
fremag
Junior Member
Posts: 73
|
Post by fremag on Feb 13, 2020 21:25:25 GMT
You're focusing on lighting and refractedColour functions but maybe the issue in with transparency. In your first picture, the black sphere was not transparent. Can you show the code for reflectedColour ? What is the default value for reflective field in Material ?
|
|
jim
New Member
Posts: 5
|
Post by jim on Feb 25, 2020 7:25:35 GMT
I had another pop and this is the result This image was with Camera, 0,0,-5 wall: pos, 0,0,2, size 40, 40, 0.01, rot 0,0,0 sphere: pos 0,0,0, size, 1,1,1, rot 0,0,0 lighting: ambient 0.1 diffuse 0.9, specular, 0.9, shininess 200 reflectivity 0.9, transparency 0.9, refractive index 1.5 colour 1 1 1 Could someone try that out in theirs to see if it is the same? Schlick is implemented and all tests passing. Only error I found was the one mentioned above. I think my scene earlier must have been a combination of positioning and lighting that caused it to render that way. Thanks, Jim
|
|
fremag
Junior Member
Posts: 73
|
Post by fremag on Feb 25, 2020 20:32:30 GMT
Hi Jim,
I rendered a scene with your parameters, here is what I got:
But with reflectivity = 0.1 (instead of 0.9) , I got this:
|
|
jim
New Member
Posts: 5
|
Post by jim on Mar 3, 2020 17:45:18 GMT
Many thanks for taking the time to do that freemag. Much appreciated. Sorry it takes me a while to respond as the only time I have to do this is on my commute to work.
|
|