Basic Graphics Programming
Basic Graphics Programming
Lecture 2
• z coordinate defaults to 0
• Calls to other functions are allowed between
glBegin(type) and glEnd();
convex
(b) non-simple
01/16/2003 15-462 Graphics I 17
Why Polygon Restrictions?
• Non-convex and non-simple polygons are
expensive to process and render
• Convexity and simplicity is expensive to test
• Behavior of OpenGL implementation on
disallowed polygons is “undefined”
• Some tools in GLU for decomposing complex
polygons (tessellation)
• Triangles are most efficient
G R
Amplitude
B G V
G
R
R
B
G H
R B S
01/16/2003 15-462 Graphics I 25
Outline
1. A Graphics Pipeline
2. The OpenGL API
3. Primitives: vertices, lines, polygons
4. Attributes: color
5. Example: drawing a shaded triangle
...
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
/* glShadeModel (GL_FLAT); */
glShadeModel (GL_SMOOTH);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT); /* clear buffer */
triangle (); /* draw triangle */
glFlush (); /* force display */
}
void triangle(void)
{
glBegin (GL_TRIANGLES);
glColor3f (1.0, 0.0, 0.0); /* red */
glVertex2f (5.0, 5.0);
glColor3f (0.0, 1.0, 0.0); /* green */
glVertex2f (25.0, 5.0);
glColor3f (0.0, 0.0, 1.0); /* blue */
glVertex2f (5.0, 25.0);
glEnd();
}
01/16/2003 15-462 Graphics I 31
The Image
• Color of last vertex with flat shading
glShadeModel(GL_FLAT) glShadeModel(GL_SMOOTH)