-5

So this RT code creates a 3D image, with blur, through raw code. How is that actually done without any modelling tools?

I am currently working to understand how RT work and different ways to implement them, so this was kind of cool to see such a small amount of code producing a pretty impressive 3D image.

#include <stdlib.h>   // card > aek.ppm
#include <stdio.h>
#include <math.h>
#include <fstream>

typedef int i;
typedef float f;

struct v {
f x, y, z;
v operator+(v r) {
    return v(x + r.x, y + r.y, z + r.z);
}
v operator*(f r) {
    return v(x * r, y * r, z * r);
}
f operator%(v r) {
    return x * r.x + y * r.y + z * r.z;
}
v() {}
v operator^(v r) {
    return v(y * r.z - z * r.y, z * r.x - x * r.z, x * r.y - y * r.x);
}
v(f a, f b, f c) {x = a; y = b; z = c;}
v operator!() {
    return*this * (1 / sqrt(*this % *this));
}
};
i G[] = {247570, 280596, 280600, 249748, 18578, 18577, 231184, 16, 16};
f R()
{
return(f)rand() / RAND_MAX;
}
i T(v o, v d, f&t, v&n)
{
t = 1e9; i m = 0;
f p = -o.z / d.z;

if(.01 < p)t = p, n = v(0, 0, 1), m = 1;

for(i k = 19; k--;)
    for(i j = 9; j--;)if(G[j] & 1 << k) {
            v p = o + v(-k, 0, -j - 4);
            f b = p % d, c = p % p - 1, q = b * b - c;

            if(q > 0) {
                f s = -b - sqrt(q);

                if(s < t && s > .01)
                    t = s, n = !(p + d * t), m = 2;
            }
        }

return m;
} v S(v o, v d)
{
f t;
v n;
i m = T(o, d, t, n);

if(!m)return v(.7, .6, 1) * pow(1 - d.z, 4);

v h = o + d * t, l = !(v(9 + R(), 9 + R(), 16) + h * -1), r = d + n * (n % d * -2);

f b = l % n; if(b < 0 || T(h, l, t, n))b = 0;

f p = pow(l % r * (b > 0), 99);

if(m & 1) {
    h = h * .2;
    return((i)(ceil(h.x) + ceil(h.y)) & 1 ? v(3, 1, 1) : v(3, 3, 3)) * (b * .2 + .1);
} return v(p, p, p) + S(h, r) * .5;
} i

main()
{

FILE * pFile;
pFile = fopen("d:\\myfile3.ppm", "w");
fprintf(pFile,"P6 512 512 255 ");
v g = !v(-6, -16, 0), a = !(v(0, 0, 1) ^ g) * .002, b = !(g ^ a) * .002, c = (a + b) *  -256 + g;

for(i y = 512; y--;)
    for(i x = 512; x--;) {
        v p(13, 13, 13);

        for(i r = 64; r--;) {
            v t = a * (R() - .5) * 99 + b * (R() - .5) * 99;
            p = S(v(17, 16, 8) + t, !(t * -1 + (a * (R() + x) + b * (y + R()) + c) *        16)) * 3.5 + p;
        }

        fprintf(pFile, "%c%c%c", (i)p.x, (i)p.y, (i)p.z);

    }
}
unwind
  • 391,730
  • 64
  • 469
  • 606
user3333072
  • 139
  • 3
  • If I did not see the include of ``, I would say this is C code. And very ugly at that. Having the lower case, single character `struct` names looks really wrong. – crashmstr Feb 20 '14 at 14:28
  • @crashmstr Since when does C have operator overload? :) –  Feb 20 '14 at 14:30
  • @user3333072 what do you expect us to do? A line by line explanation of this code? Could you narrow down the topic a bit and tell us which part you don't understand? – Hulk Feb 20 '14 at 14:33
  • @Mayerz ooh, I missed that with all of the single letter lowercase names / variables. I think this code is very unreadable. Good luck trying to learn anything from it. – crashmstr Feb 20 '14 at 14:34
  • Also, the overridden operators on the vector struct add to the obfuscation. Or is this use of operators (`%` for element-wise multiplication etc) typical for this field? – Hulk Feb 20 '14 at 14:38
  • @user3333072 As to "How is that done?" - the answer is "Mathematics". If you want to get a more specific one, you'll need to be more specific in you question. – Hulk Feb 20 '14 at 14:48
  • Excellent, descriptive variable names. – Brett Hale Feb 20 '14 at 14:49
  • @BrettHale: See the answers. It is a simple ray tracer that was consciously obfuscated. – Sebastian Mach Feb 20 '14 at 14:50
  • It's not the actual code per se, it's the fact that an image is actually produced without any visual tools, i.e like guessing what pixels should be colored what. Either this guy is some kind of autistic savant with special powers or there is a logical way produce an image through code without using any visual tools for reference. I understand that images/3D images/models are in a sense pixel representations when exported, but you have a visual tool to work with. This is raw code. It's like saying, if you give you notepad and a compiler/linker then create a picture of the Empire state building – user3333072 Feb 20 '14 at 15:05
  • @user3333072 Everything on this website: https://www.shadertoy.com/, is done purely with code. I'm pretty sure a few people there can use a notepad and a compiler to make a picture of the Empire state building. – MikeMx7f Feb 20 '14 at 16:38
  • @user3333072: There is not much guessing. Ray tracing is a technique well-researched over the past, ..., I am getting old, almost 40 years. It's like doing geometry in school, where you sometimes have tasks without any images, and you have to find some formula or solution. Consider the code is the brush of the coder :) Once you know how a ray tracer works (and Heckbert knows very well, being a then active researcher), it's rather simple. If you want, I can tell you about a D or a C++ compile time ray tracer. There's even a postscript one. – Sebastian Mach Feb 20 '14 at 16:52
  • You don't need actual geometry representation (as in storing information about triangles in memory) to render geometry. Geometry, particularly simple shapes such as spheres, can be defined mathematically. This is called implicit modeling. Spheres for instance belong to a class of primitives called quadrics and computing intersection between a ray and quadrics can be done analytically (using the mathematical form of the sphere shape only). You can search on the net for procedural or implicit modeling. www.scratchapixel.com indeed is very good resource to learn all this. – user18490 Feb 25 '14 at 08:43

2 Answers2

2

My dear friend that's Paul Heckbert code's right?

You could at least mention it.

For people thinking this code is unreadable, here is why:

This guy made a code that could fit on a credit card, that was the goal :)

His website: http://www.cs.cmu.edu/~ph/

Edit: Knowing the source of this code may help you understand it. Even if it'snot your main motivation...

If you are really interested in raytracing, start with other sources. Take a look at this website http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-1-writing-a-simple-raytracer/source-code/ (Plus it talk about your code)

  • 1
    I agree that he should mention it, but your post is not an answer :( – Sebastian Mach Feb 20 '14 at 14:37
  • 1
    @phresnel Just trying to help remembering about Paul Heckbert. I think you can't really answer his question, but you can understand this code by knowing what motivated the design. –  Feb 20 '14 at 14:41
  • @phresnel Answer edited, giving the path I followed to make my first raytracers few years ago. –  Feb 20 '14 at 14:46
  • 1
    I've given it a try :) I am actually quite happy there are others around who recognized this code. – Sebastian Mach Feb 20 '14 at 14:51
2

This code is not really special. It is basically a ray tracer that was obfuscated into a form that makes it fit on a business card (see https://www.cs.cmu.edu/~ph/).

How is that actually done without any modelling tools?

You don't need tools to render anything. You could even create a complete game of WoW (or what's hip at the moment) without any modelling tool. Modelling tools just make your live easier w.r.t. certain kinds of scenes (read: very complex ones).

You could always hardcode these data, or hack them manually into some external file.

You could also use parametric generators; Perlin Noise is one of the more popular examples thereof.

In a ray tracer, it happens that it is very simple to start out without modelling tools, as it is very simple to calculate geometric intersections between the rendering primitive "ray" and any finite geometric primitive. E.g., intersection a non-approximated, "perfect" sphere is just a few lines of code.

tl;dr: Data is just data. How you create and crunch it is completely up to you.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
  • TL;DR, this code is just writing into a bmp. :p –  Feb 20 '14 at 15:19
  • @Mayerz: Oh you mean the asker and me mean different contexts of modelling tools? Like, I assumed the "how do I get models for my raytracer", vs. "How can this work without being embedded in a modelling tool"? – Sebastian Mach Feb 20 '14 at 15:26