MarkE

MarkE

Gradients: Checking my reasoning

Hi Jamis,

I’ve been working my way through Chapter 10: Generalising Patterns and came up agains a problem with the gradient. When applied to a Sphere, I found that the gradient was running twice across it with a hard line at X=0 (same issue as logged here: The Ray Tracer Challenge: Gradient pattern is not a gradient

I just want to double-check my interpretation of your answer is correct.

I have an AbstractPattern class which implements the general colour_at_object method. This translates the point to pattern-space via object-space thus:

    def colour_at_object(self, obj: "AbstractObject", p: Point) -> Colour:
        # Convert point to object space
        object_point = cast(Point, obj.inverse_transform * p)

        # Convert object point to pattern space
        pattern_point = cast(Point, self.inverse_transform * object_point)

        return self.colour_at(pattern_point)

Then, in the Gradient sub-class I’m implementing the colour_at method as follows:

    def colour_at(self, p: Point) -> Colour:
        distance = cast(Colour, self.b - self.a)
        # Map pattern space from [-1, 1] to [0, 1] so pattern doesn't repeat
        multiplier = (p.x + 1) / 2

        return self.a + distance * multiplier

This seems to correctly map the returned colour so that the gradient will now work across a unit sphere (breaks the tests, mind so I’ll need to fix those) … but just want to ensure that my reasoning is sound and there’s not something I’ve missed along the way?

First Post!

Shadoka

Shadoka

Hi MarkE,

I’m the author of the thread that you linked and I’m pretty sure I tried something similar to your approach at first :slight_smile:

The problem with your solution is that you shift your whole gradient, so it won’t render as you might want.

I rendered two images, one with my (I mean, Jamis really) implementation and one with yours.
Both gradients should go from red to green.

My implementation:

Your implementation:

And it makes sense when you think about it. Plug in -1 for X and the term in your method evaluates to 0. Which means that the leftmost point is going to get mapped to exactly the middle of the gradient.

Hope this helps!

Where Next?

Popular Pragmatic Bookshelf topics Top

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New
PragmaticBookshelf
Explore the power of Ash Framework by modeling and building the domain for a real-world web application. Rebecca Le @sevenseacat and ...
New
mindriot
Ok, well here are some thoughts and opinions on some of the ergonomic keyboards I have, I guess like mini review of each that I use enoug...
New

Sub Categories: