Hints for Project 1

This page contains some best-practice hints for how to get Project 1 working well. Feel free to ignore it and do something else if desired.

  1. Get an OpenGL window running.
  2. Set window size to at least 700 by 700 for this lab.
    1. You will likely want to shrink this for speed on later labs.
    2. Store width and height as variables. Don't (ever) use magic numbers.
  3. The first three projects all follow the basic outline:
    1. Read polygon data into memory—note that each project has a slightly different file format.
    2. Manipulate polygon data to place it on the screen (Projects 2 and 3 only, but remember it will happen as you write project 1…)
    3. Find the pixels each polygon covers
      1. You will need to interpolate data to each point. In Project 1, we interpolate color; in Project 2 we interpolate depth; in Project 3 we interpolate depth and normals.
      2. You will generally draw these pixels to the screen, though in Project 2 you will add a check which sometimes decides not to draw a pixel.
  4. Data Structures:
    1. In graphics, nearly everything is a mathematical vector or a square matrix.
    2. For vectors, you might find the STL class valarray<double> useful
      1. a vector is a list of numbers, eg (1, 3, 2, -5)
      2. vector * number multiplies all elements of the vector by the number
      3. (1, 3, 2, -5) + (0, 1, -1, 11) = (1, 4, 1, 6); - is similar
      4. there are four kinds of vector-vector multiplication:
        1. element-wise: (1, 3, 2, -5) * (0, 1, -1, 11) = (0, 3, -2, -55)
        2. “dot” or “inner” products: (1, 3, 2, -5) dot (0, 1, -1, 11) = sum(0, 3, -2, -55) = -54
        3. “cross” products (for 3-vectors only): (a,b,c) x (d,e,f) = (b*e-c*f, c*d-a*f, a*e-b*d)
        4. “outer” products, which we won't use in this class
    3. There are no matrices in Project 1
    4. Triangles
      1. Are three points, possibly with extra data
        1. Points are vectors
        2. In Project 1, points are 2-vectors, and have color which is a 3-vector
      2. You can treat, eg, (x,y) (r,g,b) as (x,y,r,g,b) or ((x,y),(r,g,b))
      3. Make sure you use floating-point data for vector data, not integer types!
  5. Modularity:
    1. Make an abstract class, class Shape { public: virtual void draw() const = 0; };
    2. Make “Triangle” be a “Shape”: class Triangle: public Shape { … };
    3. Make “Polygon” be a “Shape” which is a wrapper for a collection of triangles.
  6. DO NOT draw shapes as you read them from the file. Read them into a data structure, then draw them when the draw() method is called. Drawing as you read will make things much too slow for the later projects especially.
  7. How to draw a polygon (a rough outline):
    1. We will treat polygons GL_TRIANGLE_FANs; this means a polygon with “n” points has “n – 2” triangles with points (0, 1, 2), (0, 2, 3), (0, 3, 4), … (0, n – 2, n – 1)
    2. To scan a triangle
      1. Sort the three points in y. Call them p_min, p_mid, p_max. It doesn't matter if multiple points have the same y.
      2. Scan the bottom part:
        1. Create left and right, both copies of p_min
        2. DDA left and right vertically upward; that is
          1. Create the DDA step points dLeft and dRight
            • (p_mid – p_min) / (y_mid – y_min), similarly for p_max
          2. while the left's y is less than y_mid
            1. left += dLeft, similarly for right
            2. DDA horizontally between left and right
              • Note, left and right have non-integer x values. Fill between floor(minX) and ceil(maxX)
      3. Scan the top part
        • Just like bottom, but work downward from p_max intead of upward from p_min
  8. Speed won't be a problem in this Project, but a fast Project 1 leads faster later projects:
    1. Graphics is pleasant in that
      1. Small, clean, maintainable code is nearly always faster than larger, uglier code.
      2. It can be very clean. If you have duplication or near-duplication anywhere in you code, you likely don't understand something.
    2. Compile with optimization enabled
      1. In my experience, GCC is 2 to 5 times better at the kinds of optimizations that these projects use than is VisualC++; I have no idea why.
      2. optimizers do better if you
        1. avoid if-statements
        2. write “p += dp;” rather than “p.x += dp.x; p.y += dp.y; p.r += dp.r;” etc.
        3. use “++i” not “i++” in for loops
        4. declare as many methods and variable as possible as being “const”
page_revision: 2, last_edited: 1209841408|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.