miniRT

by bhagenlo

written for version 7.1

Okay. Let's take a step back for a moment. Remember what we initially said?

We said that we want to provide a map for each project.

What would that look like for miniRT? What would that look like for a ray tracer in general?

Well, let's find out. But it's certainly gonna involve some math.

Ray tracing is a technique to render images. The general idea is that you have a camera from which you shoot rays, those rays hit things, and from those hitpoints you trace new rays into the lights of your scene. And depending on whether/how you hit objects along the ray, you display a color for this particular pixel (of your viewport).

Sounds good? Well, let's begin!

#Prerequisites

  • Write youself a parse_double() (and test it).
  • Find out what the dot product is. Calculate a few of them. Understand its geometrical interpretation (-> find out what it 'does' with two vectors in 3d space).
  • Find out what the cross product is. Calculate a few of them. Understand its geometrical interpretation (-> find out what it 'does' with two vectors in 3d space).

With that, you should be able to start.

If it's been some time since you last did math, and/or you did not take any math classes at university, it might be helpful if you do those as well:

  • Calculate the distance of two points in 3d.
  • Calculate the hitpoint(s) of a line and a plane.
  • Calculate the hitpoint(s) of a line and a sphere.
  • Calculate the angle between two vectors. What does happen to their dot product if they are perpendicular to each other?

#During

The nice thing about those graphics projects is that you can immediately see when something went wrong. Don't waste this advantage – get youself a working output as soon as possible!

Also, you might want to ensure to work with a freely positionable camera as early as possible.

In addition to that:

  • Crazy colors? It's quite likely you're not adding/scaling you're colors correctly.
  • Interesting distortions of any kind? Check whether you've normalized all the vectors that should be.

Ah, and please: Do youself the favor to do every calculation on the stack. It's just so much faster.

#Cleaning Up

Get yourself some example scenes, and test with them :)

In case you're field of view is rotated in any way: That's probably just your camera. And its orientation is not specified by the subject.

And make sure to name your executable miniRT.

#Aftercare

A programming language is a tool that has profound influence on our thinking habits.
Edsger Dijkstra

And the choice of the tool matters.

If you want to become an expert programmer (in anything, really), you not only should know how to use a tool, but also when to use which one: You should know your toolkit.

Up until now, if you didn't have any prior programming experience, you know only one such tool to solve the job: C.

(+ a little bit of bash, if you want to count that.)

But let us, for a moment, imagine you were such an expert (that knows what other tools looked like, and now wants to evaluate how C fits into his toolkit).
You just completed the last project that you had to code in C. What is C like, and when would you want to use it?

Feel free to use these questions for inspiration:

  • What do you really like about C? What don't you like?
  • For which problems is the language well-suited?
  • At which point(s) did you have the feeling the language got in your way?
  • How did it shape your thinking?

I encourage you to write those answers down. On your journey to becoming an expert, they will change. And whenever they'll do, you then know you learned something not only about your tool, but also about your toolkit.

Let's see what the answers look like after you create something bigger in another language. C++ awaits ;)

Huh. And with that: Goodbye, C, and welcome, C++.

And may your mathematics always work out.

#Pointers

← NetPractice

Found something to improve? Edit this page on Github!

cub3D →