Introduction to OpenGL Introduction to OpenGL Graphics API Window - - PDF document

introduction to opengl introduction to opengl
SMART_READER_LITE
LIVE PREVIEW

Introduction to OpenGL Introduction to OpenGL Graphics API Window - - PDF document

Falko Kuester BERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO SANTA BARBARA SANTA CRUZ ECE 104 ECE 104 Fundamentals of Computer Graphics Fundamentals of Computer Graphics OpenGL Slides courtesy


slide-1
SLIDE 1

Falko Kuester 1

OpenGL

ECE 104 ECE 104

Fundamentals of Computer Graphics Fundamentals of Computer Graphics

BERKELEY • DAVIS • IRVINE • LOS ANGELES • RIVERSIDE • SAN DIEGO • SAN FRANCISCO SANTA BARBARA • SANTA CRUZ

Slides courtesy of Dave Shreine, Ed Angel and Vicki Shreiner (SIGGRAPH 2001).

Introduction to OpenGL Introduction to OpenGL

Graphics API Window system independent Operating system independent Geometric and image primitives

slide-2
SLIDE 2

Falko Kuester 2

OpenGL Architecture OpenGL Architecture

Display List Polynomial Evaluator Per Vertex Operations & Primitive Assembly Rasterization Per Fragment Operations Frame Buffer Texture Memory

CPU

Pixel Operations

OpenGL as a Renderer OpenGL as a Renderer

Geometric primitives – points, lines, triangles, quadrilaterals, polygons Image Primitives – images and bitmaps – separate pipeline for images and geometry Rendering depends on state – Colors – Materials – Lights

slide-3
SLIDE 3

Falko Kuester 3

Related APIs Related APIs

AGL, GLX, WGL – glue between OpenGL and windowing systems GLU (OpenGL Utility Library) – part of OpenGL – NURBS, tessellators, quadric shapes, etc. GLUT (OpenGL Utility Toolkit) – portable windowing API – not officially part of OpenGL

OpenGL and Related APIs OpenGL and Related APIs

GLUT GLU GL

GLX, AGL

  • r WGL

X, Win32, Mac O/S software and/or hardware application program

OpenGL Motif widget or similar

slide-4
SLIDE 4

Falko Kuester 4

Preliminaries Preliminaries

Libraries Headers Files – #include <GL/gl.h> – #include <GL/glu.h> – #include <GL/glut.h>

GLUT Basics GLUT Basics

  • Application Structure
  • 1. Configure and open window
  • 2. Initialize OpenGL state
  • 3. Register input callback functions
  • render
  • resize
  • input: keyboard, mouse, etc.
  • 4. Enter event processing loop
slide-5
SLIDE 5

Falko Kuester 5

GLUT Display Modes GLUT Display Modes

GLUT_RGBA Select an RGBA mode window. This is the default if neither GLUT_RGBA nor GLUT_INDEX are specified. GLUT_RGB same as GLUT_RGBA. GLUT_INDEX Select color index window mode. This overrides GLUT_RGBA. GLUT_SINGLE Select a single buffered window. This is the default. GLUT_DOUBLE Select a double buffered window. This overrides GLUT_SINGLE. GLUT_ACCUM Select a window with an accumulation buffer. GLUT_ALPHA Select a window with an alpha component to the color buffer(s). GLUT_DEPTH Select a window with a depth buffer. GLUT_STENCIL Select a window with a stencil buffer. GLUT_MULTISAMPLE Select a window with multismapling support. GLUT_STEREO Select a stereo window. GLUT_LUMINANCE Select a stereo window with a "luminance" color model

Sample Program Sample Program

void main( int argc, char** argv ) { int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode( mode ); glutCreateWindow( argv[0] ); init(); glutDisplayFunc( display ); glutReshapeFunc( resize ); glutKeyboardFunc( key ); glutIdleFunc( idle ); glutMainLoop(); }

slide-6
SLIDE 6

Falko Kuester 6

OpenGL Initialization OpenGL Initialization

Configure the state machine

void init( void ) { glClearColor( 0.0, 0.0, 0.0, 1.0 ); glClearDepth( 1.0 ); glEnable( GL_LIGHT0 ); glEnable( GL_LIGHTING ); glEnable( GL_DEPTH_TEST ); }

GLUT Callback Functions GLUT Callback Functions

Routine to call when something happens – window resize or redraw – user input – Animation Register callbacks with GLUT

glutDisplayFunc( display ); glutIdleFunc( idle ); glutKeyboardFunc( keyboard );

slide-7
SLIDE 7

Falko Kuester 7

Rendering Callback Rendering Callback

Do all of your drawing here

glutDisplayFunc( display );

void display( void ) { glClear( GL_COLOR_BUFFER_BIT ); glBegin( GL_TRIANGLE_STRIP ); glVertex3fv( v[0] ); glVertex3fv( v[1] ); glVertex3fv( v[2] ); glVertex3fv( v[3] ); glEnd(); glutSwapBuffers(); }

Idle Callbacks Idle Callbacks

Use for animation and continuous update

glutIdleFunc( idle ); void idle( void ) { t += dt; glutPostRedisplay(); }

slide-8
SLIDE 8

Falko Kuester 8

User Input Callbacks User Input Callbacks

Process user input

glutKeyboardFunc( keyboard );

void keyboard( char key, int x, int y ) { switch( key ) { case ‘q’ : case ‘Q’ : exit( EXIT_SUCCESS ); break; case ‘r’ : case ‘R’ : rotate = GL_TRUE; break; } }

Elementary Rendering Elementary Rendering

Geometric Primitives OpenGL State Machine OpenGL Buffers

slide-9
SLIDE 9

Falko Kuester 9

OpenGL Geometric Primitives OpenGL Geometric Primitives

All geometric primitives are specified by vertices GL_QUAD_STRIP GL_QUAD_STRIP GL_POLYGON GL_POLYGON GL_TRIANGLE_STRIP GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_TRIANGLE_FAN GL_POINTS GL_POINTS GL_LINES GL_LINES GL_LINE_LOOP GL_LINE_LOOP GL_LINE_STRIP GL_LINE_STRIP GL_TRIANGLES GL_TRIANGLES GL_QUADS GL_QUADS

Simple Example Simple Example

void drawRhombus( GLfloat color[] ) {

glBegin( GL_QUADS ); glColor3fv( color ); glVertex2f( 0.0, 0.0 ); glVertex2f( 1.0, 0.0 ); glVertex2f( 1.5, 1.118 ); glVertex2f( 0.5, 1.118 ); glEnd();

}

slide-10
SLIDE 10

Falko Kuester 10

OpenGL Command Formats OpenGL Command Formats

glVertex3fv( glVertex3fv( v v ) )

Number of Number of components components

2 2 -

  • (x,y)

(x,y) 3 3 -

  • (x,y,z)

(x,y,z) 4 4 -

  • (x,y,z,w)

(x,y,z,w)

Data Type Data Type

b b -

  • byte

byte ub ub -

  • unsigned byte

unsigned byte s s -

  • short

short us us -

  • unsigned short

unsigned short i i -

  • int

int ui ui -

  • unsigned int

unsigned int f f -

  • float

float d d -

  • double

double

Vector Vector

  • mit “v” for
  • mit “v” for

scalar form scalar form glVertex2f( x, y ) glVertex2f( x, y )

Specifying Geometric Primitives Specifying Geometric Primitives

Primitives are specified using

glBegin( glBegin( primType primType ); ); glEnd(); glEnd();

– primType determines how vertices are combined

GLfloat red, greed, blue; GLfloat red, greed, blue; Glfloat coords[3]; Glfloat coords[3]; glBegin( glBegin( primType primType ); ); for ( i = 0; i < nVerts; ++i ) { for ( i = 0; i < nVerts; ++i ) { glColor3f( red, green, blue ); glColor3f( red, green, blue ); glVertex3fv( coords ); glVertex3fv( coords ); } } glEnd(); glEnd();

slide-11
SLIDE 11

Falko Kuester 11

OpenGL Color Models OpenGL Color Models

RGBA or Color Index

color index mode

Display

1

2 4 8 16

  • Red

Green Blue 1 2 3 24 25 26 123 219 74

  • RGBA mode

CPU CPU DL DL Poly. Poly. Per Vertex Per Vertex Raster Raster Frag Frag FB FB Pixel Pixel Texture Texture

Shapes Tutorial Shapes Tutorial

slide-12
SLIDE 12

Falko Kuester 12

Controlling Rendering Appearance Controlling Rendering Appearance

From Wireframe to Texture Mapped

OpenGL’s State Machine OpenGL’s State Machine

All rendering attributes are encapsulated – rendering styles – shading – lighting – texture mapping

slide-13
SLIDE 13

Falko Kuester 13

Manipulating OpenGL State Manipulating OpenGL State

Appearance is controlled by current state for each ( primitive to render ) {

update OpenGL state render primitive

} Manipulating vertex attributes is most

common way to manipulate state

glColor*() / glIndex*() glColor*() / glIndex*() glNormal*() glNormal*() glTexCoord*() glTexCoord*()

Controlling current state Controlling current state

Setting State

glPointSize( glPointSize( size size ); ); glLineStipple( glLineStipple( repeat repeat, , pattern pattern ); ); glShadeModel( glShadeModel( GL GL_ _SMOOTH SMOOTH ); );

Enabling Features

glEnable( glEnable( GL GL_ _LIGHTING LIGHTING ); ); glDisable( glDisable( GL_TEXTURE_2D GL_TEXTURE_2D ); );

slide-14
SLIDE 14

Falko Kuester 14

Transformations in OpenGL Transformations in OpenGL

Modeling Viewing – Viewpoint (camera location and orientations) – Projection Animation Map to screen

Camera Analogy Camera Analogy

Real-time photography

camera tripod model viewing volume

slide-15
SLIDE 15

Falko Kuester 15

Camera Analogy and Transformations Camera Analogy and Transformations

Projection transformations – adjust the lens of the camera Viewing transformations – tripod–define position and orientation of the viewing volume in the world Modeling transformations – moving the model Viewport transformations – enlarge or reduce the physical photograph

Coordinate Systems and Transforms Coordinate Systems and Transforms

Steps in Forming an Image – specify geometry (world coordinates) – specify camera (camera coordinates) – project (window coordinates) – map to viewport (screen coordinates) Each step uses transformations Every transformation is equivalent to a change in

coordinate systems (frames)

slide-16
SLIDE 16

Falko Kuester 16

Affine Transformations Affine Transformations

Want transformations which preserve geometry – lines, polygons, quadrics Affine = line preserving – Rotation, translation, scaling – Projection – Concatenation (composition)

Homogeneous Coordinates Homogeneous Coordinates

– each vertex is a column vector – w is usually 1.0 – all operations are matrix multiplications – directions (directed line segments) can be represented with w = 0.0

            = w z y x v r