today … the rendering pipeline in detail what is opengl first opengl program details about...

47

Upload: edwina-waters

Post on 23-Dec-2015

248 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling
Page 2: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Today …

• The rendering pipeline in detail

• What is OpenGL

• First OpenGL program

• Details about modeling

Page 3: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Basic Rendering Pipeline

Projection

Illumination

Rasterization&Clipping &Display

1V

1R

N

L

Database of 3D models

Modeling Transformation(OS -> WS)

VisibilityCulling

ViewingTransformation (WS -> CS)

Page 4: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

OpenGL and GLUT Overview

• What is OpenGL & what can it do for me?

• OpenGL in windowing systems

• Why GLUT

• A GLUT program template

Page 5: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

What is a “library”.

• Static

• Dynamic

Page 6: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

•OpenGL applications use the window system’s window, input, and event mechanism •GLU supports quadrics, NURBS, complex polygons, matrix utilities, and more

Relationship between OpenGL GLU and windowing APIs.

Page 7: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

What Is OpenGL?

• Graphics rendering API– high-quality color images composed of

geometric and image primitives– window system independent– operating system independent

Page 8: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

OpenGL Library Functions

GLU

GL

GLUT

GL library contains all primitive and attribute functionsassociated with OpenGL

GLU library builds on the GL library to include morecomplex primitives (e.g. spheres) and convenience functions

GLUT (GL Utility Toolkit) includes functions to interfacewith the native window system, including window creation,management of input devices

Page 9: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

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

Page 10: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

OpenGL and Related APIs

GLUT

GLU

GL

GLX, AGLor WGL

X, Win32, Mac O/S

software and/or hardware

application program

OpenGL Motifwidget or similar

Page 11: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

OpenGL Architecture

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

RasterizationPer Fragment

OperationsFrameBuffer

TextureMemory

CPU

PixelOperations

Page 12: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Features of OpenGL

• Delivers fast and complete 3D hardware acceleration

• makes real-time 3D effects possible

• designed to support future innovations in software and hardware

• Available on many platforms

• Stable

Page 13: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

OpenGL as a Renderer

• Geometric primitives– points, lines and polygons

• Image Primitives– images and bitmaps– separate pipeline for images and geometry

• linked through texture mapping

• Rendering depends on state– colors, materials, light sources, etc.

Page 14: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Other issues

• Rendering acceleration (rasterization, texture map, spacial subdivision, collision detection, progressive rendering, view dependent rendering, image-based rendering,…)

• Color, Texture, Anti-aliasing

• Animation or simulation

Page 15: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Student 007: “I am dying to see an OpenGL program !…”

Prof: Be patient, it is only few more block(diagram)s away !!

Page 16: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Block Diagram

Page 17: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Preliminaries• Headers Files

• #include <GL/gl.h>• #include <GL/glu.h>• #include <GL/glut.h>

• Libraries• Enumerated Types

– OpenGL defines numerous types for compatibility

– GLfloat, GLint, GLenum, etc.

Page 18: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

GLUT Basics

• Application Structure– Configure and open window– Initialize OpenGL state– Register input callback functions

• render

• resize

• input: keyboard, mouse, etc.

– Enter event processing loop

Page 19: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Sample Program – draw()functionvoid drawScene(void){

// Clear the rendering window glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Set drawing color to white glColor3f( 1.0, 1.0, 1.0 );

// Draw triangleglBegin(GL_TRIANGLES);

glColor3f( 1.0, 0.0, 0.0 );glVertex2f( 1.0, 1.0 );glVertex2f( 2.0, 1.0 );glVertex2f( 2.0, 2.0 );

glEnd();

// Flush the pipeline. (Not usually necessary.) glFlush();}

Page 20: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

OpenGL Command Formats

glVertex3fv( glVertex3fv( vv ) )

Number ofNumber ofcomponentscomponents

2 - (x,y) 2 - (x,y) 3 - (x,y,z)3 - (x,y,z)4 - (x,y,z,w)4 - (x,y,z,w)

Data TypeData Typeb - byteb - byteub - unsigned byteub - unsigned bytes - shorts - shortus - unsigned shortus - unsigned shorti - inti - intui - unsigned intui - unsigned intf - floatf - floatd - doubled - double

VectorVector

omit “v” foromit “v” forscalar formscalar form

glVertex2f( x, y )glVertex2f( x, y )

Page 21: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Specifying Geometric Primitives• Primitives are specified using

glBegin( glBegin( primType primType ););

glEnd();glEnd();

– primType determines how vertices are combinedGLfloat 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();

Page 22: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

OpenGL Command Syntax• Function calls: glXxxxx[type] ( [args] )

– Example: glVertex2f (1.0, 2.0);• Defined constants: GL_XXXXXXXXXX

– Example: GL_COLOR_BUFFER_BIT• Type definition: GLxxxxxxx

– Example: GLfloat

Page 23: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Command Suffixes/ Argument Data Types

Suffix D ata Type Typ ica l C orresponding C -Language Type

O penG L Type D efin ition

b 8-b it in teger s igned char G Lbyte

s 16-b it in teger short G Lshort

f 32-b it in teger in t o r long G Lin t, G Lbyte

i 32-b it floating -po in t floa t G Lfloat, G Lclam pf

d 64-b it floating-po in t doub le G Ldouble ,G Lclam pd

ub 8-b it unsigned in teger unsinged char G Lubyte,G Lboolean

us 16-b it unsinged in teger unsinged short G Lushort

u i 32-b it unsinged in teger unsinged in t o r unsinged long

G Luin t, G Lenum , G Lbitfie ld

Page 24: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Shared Namespace ModelModel

Application

Graphics Lib.

O I

FunctionCalls

OS

} Run in same process

glFlush();unnecessary

Page 25: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Client-Server Model:

GraphicsServer

O

I

Network Protocol

Model

Application

Graphics Lib.

FunctionCalls

OS OS

glFlush();May be necessary

Page 26: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

OpenGL Architecture

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

RasterizationPer Fragment

OperationsFrameBuffer

TextureMemory

CPU

PixelOperations

Page 27: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

State Management and Drawing in OpenGL

Page 28: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

OpenGL Initialization

• Set up whatever state you’re going to use

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 );}

Page 29: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

GLUT Callback Functions

• Routine to call when something happens– window resize or redraw– user input– animation

• “Register” callbacks with GLUTglutDisplayFunc( display );

glutIdleFunc( idle );

glutKeyboardFunc( keyboard );

Page 30: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

A Typical Event-Driven Program

Initialization

Main Loop

InputProcessingProcedures

BackgroundProcessingProcedure

EventHandler

Page 31: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Route messages to the application

EventButton on Mouse

Windows Route Event to Correct Application

Windows Application

WinMain()

WinProc()

Message

Left Button Down

Message Processed

Page 32: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

CallFunction n

GLUT Event Processing

Eventin

Queue

Pop EventFrom Queue

CallFunction n

Switch onEvent Type

CallFunction n

Call IdleFunction

No

Yes

CallFunction nCall

Function nCallFunction n

Page 33: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Callbacks

Event A

Event B

Event C

Event D

Event E

Event F

No Event Idle function( )

m1, m2 function_A(m1, m2)

function_B( )

function_C( )

Event Type “Registered” Function

NB: Function parameters must match measures contained in associated event.

Page 34: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Display EventTrigger: GLUT determines that the window needs to be redisplayed. A display event is generated when the windowis first drawn.

Callback function form:

void display();

Registration:

glutDisplayFunc(display);

A display callback function must be registered for each window.

Page 35: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

GLUT Mouse Event

Trigger: Any mouse button is depressed or released.

Callback function form:

void mouse_callback_func(int button, int state, int x, int y);

Registration:

glutMouseFunc(mouse_callback_function);

Page 36: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

GLUT Defined Mouse Constants

GLUT_LEFT_BUTTONGLUT_MIDDLE_BUTTONGLUT_RIGHT_BUTTON

GLUT_UPGLUT_DOWN

Systems with only one mouse button can only generatea GLUT_LEFT_BUTTON callback.

Page 37: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

GLUT Reshape Event

Trigger: Active window is resized

Callback function form:

void reshape_func(GLsizei w, GLsizei h);

Registration:

glutReshapeFunc(reshape_func);

Page 38: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

GLUT Move Event

Trigger: The mouse is moved while one or more mousebuttons are pressed.

Callback function form:

void motion_func(int x, int y);

Registration:

glutMotionFunc(motion_func);

Page 39: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

GLUT Keyboard Event

Trigger: Any key is depressed.

Callback function form:

void keyboard_function(unsigned char key, int x, int y);

Registration:

glutKeyboardFunc(keyboard_function);

Page 40: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Idle Callbacks

• Use for animation and continuous updateglutIdleFunc( idle );

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

Page 41: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Other Defined Events in GLUTglutPassiveMotionFuncglutVisibilityFuncglutEntryFuncglutSpecialFuncglutSpaceballMotionFuncglutSpaceballRotateFuncglutSpaceballButtonFuncglutButtonBoxFuncglutDialsFuncglutTabletMotionFuncglutTabletButtonFuncglutMenuStatusFunc

Page 42: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Simple GLUT Window Management Functions

glutInit(int *argc, char** argv);Initializes a window session.

glutCreateWindow(char *name);Creates a window with title *name.

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);Sets the display mode to single buffered and RGB color.

glutInitWindowSize (GLsizei h, GLsizei w);Sets initial window size to h x w.

glutInitWindowPosition(x,y);Sets initial window position to (x, y).

Page 43: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

• TA help is there, but use “Help” and try to help yourselves !!

• Show simple demo program

Page 44: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

OpenGL Lib

GL Commands

GDI Processor

GraphicsHardware

OpenGL in MS Windows

GDI – Graphics Device Interface (Windows-specific)

Page 45: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Basic Program Skeleton

• Header files, Global variables

• Rendering initialization

• Keyboard / Mouse functions

• Drawing / animation

• Window resizing

• Idle function

• main()

Page 46: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling

Try this …

Draw a red line in a window with black backgroundTry changing line thicknessChange color combinationWhat if you want a regular triangle as against a “filled” oneNow try drawing various figures from the libraryTry modeling concave polygons

Page 47: Today … The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling