Lecture #2: Points. Lines. Triangles

Computer graphics in Game development

Ivan Belyavtsev

24.01.2020

Rasteriztion theory

It’s possible construct any image using the next operation:

  • draw a point
  • draw a line
  • draw a triangle

“Color space” experiment

Deduction phase

  • Pixel is 3 colored light sources
  • Does variation of pixel give a different color?
  • What about performance?

“Color space” experiment

Experiment

Let’s implement it together

“Color space” experiment

Reference

From: https://github.com/djbelyak/cg-template/blob/master/references/color_space.png

“Color space” experiment

What is the new knowledge?

  • We’re able to draw any color from a gamut using variation of intensity of red, green, and blue colors

Simple line equation

\[y = ax+b\]

Equation from 2 points

\[a = \frac{y_1 - y_2}{x_1 - x_2}\] \[b = y_1 - x_1\frac{y_1 - y_2}{x_1 - x_2}\]

\[y= y_1 + \frac{y_1 - y_2}{x_1 - x_2}(x - x_1)\]

Naïve’s draw line algorithm

slope = (y_begin - y_end)/(x_begin - x_end)
for x in range(x_begin, x_end):
    y = y_begin + slope*(x - x_begin)
    draw_point(x, round(y))

[1]

“Draw line” experiment

Deduction phase

  • Line could be represent as set of pixels
  • To draw line we can use Naïve’s draw line algorithm
  • What about performance?

“Draw line” experiment

Experiment

Let’s implement it together

“Draw line” experiment

Reference

From: https://github.com/djbelyak/cg-template/blob/master/references/draw_line.png

“Draw line” experiment

What is the new knowledge?

  • Naïve’s draw line algorithm has an issue with zero-division
  • Naïve’s draw line algorithm has an issue with empty spaces
  • Naïve’s draw line algorithm doesn’t fit to draw lines

Bresenham’s draw line algorithm

Improvements of Naïve’s draw line algorithm

  • Mirror of the best range
  • Don’t calculate a line function
  • Use error calculation to increment y value

[1]

“Draw line” experiment

Deduction phase

  • Line could be represent as set of pixels
  • To draw line we can’t use Naïve’s draw line algorithm
  • To draw line we can use Bresenham’s draw line algorithm
  • What about performance?

“Draw line” experiment

Experiment

Let’s implement it together

“Draw line” experiment

Reference

From: https://github.com/djbelyak/cg-template/blob/master/references/draw_line.png

“Draw line” experiment

What is the new knowledge?

  • Bresenham’s draw line algorithm is pretty good to use for draw lines

Famous references

Stanford bunny

From: https://casual-effects.com/data

[2]

Famous references

Utah teapot

From: https://casual-effects.com/data

[3]

Famous references

Cornel box

From: https://casual-effects.com/data

[4]

Famous references

Sponza palace

From: https://casual-effects.com/data

[3]

Wavefront OBJ format

  • # - comment
  • v - vertex
  • vn - normals
  • vt - texture coordinates
  • f - face (polygon) - list of vertexes, normals, and tex coordinates [5]

Cornell box example

## Object floor
v  -1.01  0.00   0.99
v   1.00  0.00   0.99
v   1.00  0.00  -1.04
v  -0.99  0.00  -1.04

g floor
usemtl floor
f -4 -3 -2 -1

## Object ceiling
v  -1.02  1.99   0.99  
v  -1.02  1.99  -1.04
v   1.00  1.99  -1.04
v   1.00  1.99   0.99

g ceiling
usemtl ceiling
f -4 -3 -2 -1

“Read obj” experiment

Deduction phase

  • Obj file contains faces
  • Each face contains vertexes
  • Does drawing line between vertexes in each face give us a wire frame image?
  • What about performance?

“Read obj” experiment

Experiment

Let’s implement it together

“Read obj” experiment

What is the new knowledge?

  • Image is not correct. We need more accurate translations

References

1. Marschner S., Shirley P. Fundamentals of computer graphics, fourth edition. 4th ed. Natick, MA, USA: A. K. Peters, Ltd., 2016.

2. Turk G. The stanford bunny [Electronic resource]. 2000. URL: https://www.cc.gatech.edu/~turk/bunny/bunny.html.

3. McGuire M. Computer graphics archive. 2017.

4. Computer Graphics C.U.P. of. Cornell box data [Electronic resource]. 2005. URL: http://www.graphics.cornell.edu/online/box/data.html.

5. Wavefront obj file format summary // FileFormat.Info.