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





