CSCE 441 Lecture 2

From Notes
Jump to navigation Jump to search

« previous | Wednesday, January 15, 2014 | next »


OpenGL Command Formats

  • prefix: gl
  • name: Vertex
  • number of components: 2, 3, or 4
  • Data types
    • byte: b
    • unsigned byte: ub
    • short: s
    • unsigned short: us
    • integer: i
    • float: f
  • Optional "Vector form" (arguments specified as array): v

All geometric primitves are specified by vertices:

  • GL_POINTS draws dots on the screen
  • GL_LINES draws lines between consecutive points on the screen
  • GL_LINE_STRIP connect lines between all consecutive points
  • GL_LINE_LOOP same as GL_LINE_STRIP, except it connects the first and last line
  • GL_POLYGON similar to GL_LINE_LOOP, except that it fills the loop
  • GL_TRIANGLES makes triangle polygons from consecutive triplets of points
  • GL_TRIANGLES_STRIP draws triangles from all consecutive points
  • GL_TRIANGLES_FAN keep first point as vertex, and draw triangles between all remaining pairs of points
  • GL_QUADS take consecutive quadruples of points
  • GL_QUADS_STRIP take 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

Full description


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 is between and . Other lines can be drawn by negating or swapping roles of and .

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?