Konzeptphase: Die Modelle sind fertig, Workshops und Baustation in Vorbereitung. Alles ist offen und frei nutzbar.
← Informatik-Challenge← Informatics challenge
Informatik-Challenge 2 · Rendern · Klasse 10–13Informatics challenge 2 · Rendering · grades 10–13

Render einen SteinRender a brick

Wie kommt ein 3D-Modell auf den Bildschirm? Du schreibst den Kern eines kleinen Renderers – Projektion, Perspektive, Licht und Schatten – und malst damit den Turm der Sparrenburg in Bielefeld aus unserem Steinmodell: 4.466 Steine, 24 × 24 Noppen, 40 Lagen. Das Programm vergleicht dein Bild Pixel für Pixel mit der Referenz.

How does a 3D model get onto the screen? You write the core of a small renderer – projection, perspective, light and shadow – and use it to paint the tower of the Sparrenburg in Bielefeld from our brick model: 4,466 bricks, 24 × 24 studs, 40 layers. The program compares your image pixel by pixel with the reference.

Level 1 · Parallelprojektion und Maler-Algorithmus

Jeder Stein hat sechs Seitenflächen; das Programm sammelt alle Flächen, die man sehen könnte. Deine Funktion project bildet jeden Eckpunkt auf das Bild ab, sortFaces legt die Reihenfolge fest: Wie ein Maler malst du zuerst das Hinterste und dann alles, was davor liegt.

project(p, cam) → [bildX, bildY, tiefe] · sortFaces(faces) → faces
  • Parallelprojektion: d = p − cam.target, dann bildX = Mitte + scale · (d · right), bildY = Mitte − scale · (d · up), tiefe = d · forward. Erklärung unten bei Mathe.
  • Die Steine sind 1 × 1 Noppe groß und 1,2 Noppen hoch (ein Stein ist 9,6 mm hoch, eine Noppe 8 mm breit).

Level 1 · Orthographic projection and the painter's algorithm

Every brick has six faces; the program collects all faces that could be seen. Your function project maps every corner onto the image, sortFaces decides the order: like a painter you paint the farthest first and then everything in front of it.

project(p, cam) → [imageX, imageY, depth] · sortFaces(faces) → faces
  • Orthographic projection: d = p − cam.target, then imageX = centre + scale · (d · right), imageY = centre − scale · (d · up), depth = d · forward. Explained below under Maths.
  • The bricks are 1 × 1 stud and 1.2 studs high (a brick is 9.6 mm high, a stud 8 mm wide).
Strg+Enter führt ausruns

Dein Code läuft nur in deinem Browser (Web Worker, Zeitlimit 6 s). console.log(…) schreibt unter den Editor – Vorsicht, project wird tausendfach aufgerufen.Your code runs only in your browser (web worker, 6 s time limit). console.log(…) prints below the editor – careful, project is called thousands of times.

Tests

    Dein BildYour image
    ReferenzReference
    Tipp 1: Was heißt „Anteil in Richtung right“?Hint 1: what does "the part along right" mean?

    Ist right ein Einheitsvektor, dann sagt dot(d, cam.right), wie weit d nach rechts zeigt – in Noppen. Mal cam.scale sind es Pixel.

    If right is a unit vector, dot(d, cam.right) tells you how far d points to the right – in studs. Times cam.scale it is pixels.

    Tipp 2: Warum minus bei y?Hint 2: why minus for y?

    Auf dem Canvas liegt (0, 0) oben links und y wächst nach unten. Was in der Welt höher ist, muss also eine kleinere Bild-y-Koordinate bekommen.

    On the canvas (0, 0) is the top left corner and y grows downwards. So something higher up in the world needs a smaller image y.

    Tipp 3: SortierenHint 3: sorting

    faces.sort((a, b) => b.depth - a.depth) sortiert absteigend nach Tiefe. Danach return faces; nicht vergessen.

    faces.sort((a, b) => b.depth - a.depth) sorts by depth, largest first. Don't forget return faces; afterwards.

    Die Mathematik dahinterThe maths behind it

    Alles, was ein Renderer braucht, steckt in Vektoren und einem einzigen Rechenzeichen: dem Skalarprodukt.

    Everything a renderer needs is made of vectors and a single operation: the dot product.

    θab a · b = |a|·|b|·cos θ
    Vektoren und Skalarprodukt a · b = aₓbₓ + a_yb_y + a_zb_z
    Ist b ein Einheitsvektor (Länge 1), dann ist a · b die Länge des „Schattens“ von a auf b (dick markiert): Es misst, wie weit a in Richtung b zeigt.
    Vectors and the dot product a · b = aₓbₓ + a_yb_y + a_zb_z
    If b is a unit vector (length 1), a · b is the length of the "shadow" of a on b (thick line): it measures how far a points in the direction of b.
    BildimageSteinebricks
    Parallelprojektion d = p − target
    bildX = W/2 + s·(d·right) bildY = H/2 − s·(d·up)
    Alle Sehstrahlen sind parallel. Man zerlegt d in die Richtungen „rechts“ und „oben“ der Kamera – entfernte Steine werden nicht kleiner, wie in einem technischen Plan.
    Orthographic projection d = p − target
    imageX = W/2 + s·(d·right) imageY = H/2 − s·(d·up)
    All rays of sight are parallel. You split d into the camera's "right" and "up" directions – distant bricks do not get smaller, as in a technical drawing.
    Augeeyey_Bildy_imgy fz Bildebeneimage plane
    Perspektive (Strahlensatz) y_Bild / f = y / z → y_Bild = f · y / z
    Der Sehstrahl vom Auge zum Punkt schneidet die Bildebene im Abstand f. Doppelt so weit weg heißt halb so groß. Die Tiefe z ist der Anteil von p − eye in Blickrichtung.
    Perspective (intercept theorem) y_img / f = y / z → y_img = f · y / z
    The ray from the eye to the point crosses the image plane at distance f. Twice as far away means half as big. The depth z is the part of p − eye along the viewing direction.
    12 3
    Maler-Algorithmus Fläche 1 liegt am weitesten hinten und wird zuerst gemalt, Fläche 3 zuletzt. Was davor liegt, übermalt einfach das Dahinter. Darum sortiert man die Flächen absteigend nach ihrer Tiefe.
    Painter's algorithm Face 1 is farthest back and is painted first, face 3 last. Whatever is in front simply paints over what is behind. That is why the faces are sorted by depth, largest first.
    nlθ max(0, n·l)
    Licht nach Lambert Helligkeit = max(0, n · l) = max(0, cos θ)
    Schräges Licht verteilt sich auf eine größere Fläche (gestrichelte Strahlen), darum wird die Fläche dunkler. n ist die Normale, l zeigt zur Lichtquelle.
    Lambert lighting brightness = max(0, n · l) = max(0, cos θ)
    Slanted light spreads over a larger area (dashed rays), so the face gets darker. n is the normal, l points to the light source.
    pStein → Schattenbrick → shadow
    3D-DDA (Amanatides & Woo) Der Strahl geht immer über die nächstgelegene Zellwand – die Achse mit dem kleinsten tMax – und prüft jede Zelle, durch die er läuft (hell). Trifft er einen Stein (dunkel), liegt p im Schatten.
    3D DDA (Amanatides & Woo) The ray always crosses the nearest cell wall – the axis with the smallest tMax – and checks every cell it passes (light). If it hits a brick (dark), p lies in shadow.