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

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
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
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New

Other popular topics Top

PragmaticBookshelf
Brace yourself for a fun challenge: build a photorealistic 3D renderer from scratch! In just a couple of weeks, build a ray tracer that r...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
PragmaticBookshelf
Tailwind CSS is an exciting new CSS framework that allows you to design your site by composing simple utility classes to create complex e...
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
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
New
New
First poster: AstonJ
Jan | Rethink the Computer. Jan turns your computer into an AI machine by running LLMs locally on your computer. It’s a privacy-focus, l...
New

Sub Categories: