CSCE 441 Lecture 2
« previous | Wednesday, January 15, 2014 | next »
OpenGL Command Formats
- prefix:
gl - name:
Vertex - number of components:
2,3, or4 - Data types
- byte:
b - unsigned byte:
ub - short:
s - unsigned short:
us - integer:
i - float:
f
- byte:
- Optional "Vector form" (arguments specified as array):
v
All geometric primitves are specified by vertices:
GL_POINTSdraws dots on the screenGL_LINESdraws lines between consecutive points on the screenGL_LINE_STRIPconnect lines between all consecutive pointsGL_LINE_LOOPsame asGL_LINE_STRIP, except it connects the first and last lineGL_POLYGONsimilar toGL_LINE_LOOP, except that it fills the loopGL_TRIANGLESmakes triangle polygons from consecutive triplets of pointsGL_TRIANGLES_STRIPdraws triangles from all consecutive pointsGL_TRIANGLES_FANkeep first point as vertex, and draw triangles between all remaining pairs of pointsGL_QUADStake consecutive quadruples of pointsGL_QUADS_STRIPtake all "doubly-consecutive" quadruples of points (pairs of lines)
Related Libraries
GLU (OpenGL Utility Library)
- Part of OpenGL
- Provides higher-level drawing routines like spheres, NURBS, tesselators, quadric shapes, etc.
GLUT (OpenGL Utility Toolkit)
- perform system-level I/O with OS (windows, user events)
- cross-platform for most purposes
- portable windowing API
- not officially part of OpenGL
#include <GLUT/glut.h>
void init();
void display();
void idle();
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
}
// tell GLUT that something has changed and it should redraw the screen
// calling display() won't do anything
glutPostRedisplay();
}
void mouse();
void mouseMove(int x, int y)
{
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow("HELLO"); // title of window
init(); // my initialization callback
glutDisplayFunc(display);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(mouseMove);
glutMainLoop();
return 0;
}Assignment 1
Build a simple OpenGL/GLUT application
Scan Conversion of Lines
All display devices are abstracted to discrete grids of pixels
- Integer coordinates
- Color information associated
Drawing lines
Draw a line between 2 points... which pixels should be on?
Obviously endpoints and any points that lie on the line between.
We'll assume slope Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle m} is between and . Other lines can be drawn by negating Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle m} or swapping roles of Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x} and Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle y} .
Simple Algorithm
int xL, yL, xH, yH;
int x = xL;
float y = (float)yL;
float m = ((float)yH - (float)yL) / ((float)xH - (float)xL);
for (int i = 0; i <= xH - xL; i++) {
drawPixel(x, round(y));
x++;
y += m;
}Problems:
- Floating point operations are expensive and have roundoff errors
- Round, Ceil, or Floor?