• No results found

OpenGL_Intro.ppt

N/A
N/A
Protected

Academic year: 2020

Share "OpenGL_Intro.ppt"

Copied!
44
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

OpenGL

Open G

raphics

L

ibrary

What it is

 a s/w interface to graphics h/w

 mid-level, device-independent, portable graphics

subroutine package

 developed primarily by SGI

 2D/3D graphics, lower-level primitives (polygons)  does not include low-level I/O management

(3)
(4)

OpenGL libraries

 GL – gl.h Opengl32.lib

 Provides basic commands for graphics drawing

GLU (OpenGL Utility Library) – glu.h glu32.lib

 Uses GL commands for performing compound graphics like

 viewing orientation and projection specification  polygon tessellations, surface rendering etc.

 GLUT (OpenGL Utility Toolkit) – glut.h glut.lib

 is a window system-independent toolkit for user interaction built on top

of OpenGL and WGL (Windows) or GLX (Linux).

 System-specific OpenGl extensions

 GLX : for X window system (Linux/Unix)  WGL: for Windows 95/98/2000/NT

(5)

OpenGL conventions

Functions in OpenGL start with

gl

Most functions use just

gl

(e.g.,

glColor()

)

Functions starting with

glu

are utility

functions (e.g.,

gluLookAt()

)

 Note that GLU functions can always be composed entirely from core GL functions

(6)

OpenGL conventions

Function names indicate argument type

and number

Functions ending with

f

take floats

Functions ending with

i

take ints

Functions ending with

b

take bytes

Functions ending with

ub

take unsigned bytes

Functions that end with

v

take an array.

Examples

glColor3f()

takes 3 floats

(7)
(8)

Step 1: Modeling Transform

 Vertices of an object are define in it’s own co-ordinate system (Object Space)

 A scene is composed of different objects and some times multiple copy of the same object

 Modeling transforms places all the objects in a world co-ordinate system (World Space)

 Model Transforms must be specified before specifying the vertices of an object

 glRotatef(30, 1, 0, 0);  glVertex3d(20, 30, 10);

 Basic Modeling Transforms are:

 Translate : glTranslate{f|d}(x,y,z)  Rotate : glRotate{f|d}(,x,y,z)

 Scale : glScale{f|d}(x,y,z) Object

Space World Space

(9)

Step 2: Viewing Transform

 A model can be viewed from different angles.

 Viewing Transform specify following information about the viewer:

 eye position  head up

 Look at direction

(10)

Step 2: Viewing Transform

void gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble

(11)

Step 3: Normalize & Clip

 Normalize View volume within an unit cube.

 Remove Primitives that are not in Normalized view volume

Eye Space Clipping Space

(12)

Step 4: Projection

 Maps 3D-coordinates to 2D-image coordinates

P ar al le l P ro je ct io n P er sp ec ti ve P ro je ct io n Types

Clipping Space

(13)

Step 4: Projection

(14)

Step 4: Projection

(15)

Step 4: Projection

(16)

Step 5: Rasterization

Projected image (vertices) in image space

has fractional x and y co-ordinates values

But raster scan device can only display

pixels at integer co-ordinates

Image Space

Screen Space

Rasterization

Some Algorithms:

– DDA (Digital Differential Analyzer

– Brasenham’s Algorithm

(17)

Step 6: Viewport Transformation

Maps rasterized 2D images onto graphical

device

(18)

Step 6: Viewport Transformation

void glViewport(GLint x, GLint y, GLsizei width, GLsizei height);

gluPerspective(fovy, 1.0, near, far); glViewport(0, 0, 400, 400);

(19)
(20)

Primitives

 Primitives: Points, Lines & Polygons

 Each object is specified by a set Vertices

• Grouped together by glBegin & glEnd glBegin(type)

glVertex*( ) glVertex*( ) …

glEnd( );

type can have 10

(21)

Primitive Types

GL_POINTS V0 V1 V2 V3 V5 V4 GL_LINE_LOOP V0 V1 V2 V3 V5 V4 GL_LINE V0 V1 V2 V3 V5 V4 GL_LINE_STRIP V0 V1 V2 V3 V5 V4 GL_POLYGON V0 V1 V2 V3 V4

Polygon must be: • Simple

• No-holes inside

• Convex Simple

Non-convex

Complex Convex

P1

(22)

Primitive Types

GL_TRIANGLE V0 V1 V2 V3 V4 V5 V6 V7 V8 GL_QUAD V0 V1 V2 V3 V4 V5 V6 V7 GL_TRIANGLE_STRIP V0 V1 V2 V3 V4 V5

Order of Vertex rendering

GL_TRIANGLE_STRIP

012, 213, 234, 435

GL_QUAD_STRIP V0 V1 V2 V3 V4 V5 V6 V7 GL_QUAD_STRIP

0132, 2354, 4576

GL_TRIANGLE_FAN V0 V1 V2 V3 V4 V5 GL_TRIANGLE_FAN

(23)

Configuring OpenGL in Visual C++

Files Required for GLUT:

glut32.dll

glut.h

(24)
(25)

Specify Canvas Color

Must always remember to clear canvas before

drawing

glClearColor( r , g , b , α )

 specify the color to clear the canvas to

 should generally set α to be 0 (i. e., fully transparent)  this is a state variable, and can be done only once

glClear( GL_ COLOR_ BUFFER_ BIT)

 actually clears the screen  glClear clears

(26)

Redrawing Window

 void glFlush(void);

 Forces previously issued OpenGL commands to begin execution  It returns before the execution ends.

 glutSwapBuffers() automatically calls glFlush()

 For single buffer display function should end with this command

 void glFinish(void);

 Forces previously issued OpenGL commands to complete

 This command doesn’t return until all effects from previous commands

are fully realized.

 void glutPostRedisplay(void);

 Causes the currently registered display function to be called at the

(27)

Initializing GLUT

Void glutInit( int argc, char **argv)

 initialize glut, process command line arguments such as

-geometry, -display etc.

void glutInitDisplayMode(unsigned int mode)

 Mode for later glutCreateWindow() call  mode is a bit-wised Ored combination of

Either GLUT_RGBA or GLUT_INDEX

 Either GLUT_SINGLE or GLUT_DOUBLE

One or more GLUT_DEPTH, GLUT_STENCIL, GLUT_ACCUM

buffers

(28)

Initializing GLUT

void glutInitWindowPosition(int x, int y)

 Initial location of window

void glutInitWindowSize(int width, int height)

 Initial size of window

int glutCreateWindow(char *name)

(29)

Event driven approach

void

glutMainLoop

(void);

 enters the GLUT event

processing loop.

 should be called at most once in

a GLUT program.

 Once called, this routine will

never return.

 It will call as necessary any

callbacks that have been registered.

While (TRUE) { e=getNextEvent(); switch (e) {

case (MOUSE_EVENT): call registered MouseFunc break;

case (RESIZE_EVENT):

call registered ReshapeFunc break; } Event Queue Keyboard Callback Mouse Callback Display Callback Keyboard Mouse Display OS MainLoop

(30)

Callback Functions

void glutDisplayFunc(void (*func) (void))

 Specifies the function that’s called whenever

 the window is initially opened

The content of the window is needed to be redrawnglutPostRedisplay() is explicitly called.

void glutReshapeFunc(

void (*func)(int width, int height));

 Specifies the function that’s called whenever

The window is resized or moved

 The function should perform following tasks

 Call glViewPort(0,0,width, height); // default behavior

(31)

Callback Functions

void glutKeyboardFunc(

void (* func)(unsigned int key, int x, int y)

);

Specifies the function that’s called whenever

 a key that generates an ASCII character is pressed.

 The key callback parameter is the generated ASCII value.

(32)

Callback Functions

void glutMouseFunc(

void (* func)(int button, int state, int x, int y));

 Specifies the function that’s called whenever a mouse

button is pressed or released.

 button callback parameter is one of

 GLUT_LEFT_BUTTON

 GLUT_MIDDLE_BUTTON

 GLUT_RIGHT_BUTTON

 state callback parameter is either

 GLUT_UP

 GLUT_DOWN

 The x and y callback parameters indicate the location of

(33)
(34)
(35)
(36)

Transformation in OpenGL

OpenGL uses 3 stacks to maintain

transformation matrices:

 Model & View transformation matrix stack  Projection matrix stack

 Texture matrix stack

You can load, push and pop the stack

The top most matrix from each stack is applied to

all graphics primitive until it is changed

M N

Model-View Matrix Stack

Projection Matrix Stack Graphics

Primitives (P)

(37)
(38)

Translation – 2D

(4,5) (7,5) Y X Before Translation                                                             1 * 1 0 0 1 0 0 1 1 y x d d y x T P P d d T y x P y x P y x y x Form s Homogeniou

x’ = x + dx y’ = y + dy

(7,1) (10,1)

X Y

(39)

Transformations and OpenGL

®

Each time an OpenGL transformation

M

is

called the current MODELVIEW matrix

C

is

altered:

Cv

v  v  CMv

glTranslatef(1.5, 0.0, 0.0); glRotatef(45.0, 0.0, 0.0, 1.0);

CTRv v 

(40)
(41)

Thinking About Transformations

As a Global System

 Objects moves but coordinates stay the same

Think of transformation

in reverse order as

they appear in code

As a Local System

 Objects moves and coordinates move with it

Think of transformation

in same order as they

appear in code

There is a World Coordinate System where:

All objects are defined

Transformations are in World Coordinate space

(42)

Local View

Translate ObjectThen Rotate

Order of Transformation T•R

glLoadIdentity();

glMultiMatrixf( T);

glMultiMatrixf( R);

draw_ the_ object( v); v’ = ITRv

Global ViewRotate ObjectThen Translate

(43)

Order of Transformation R•T

glLoadIdentity();

glMultiMatrixf( R);

glMultiMatrixf( T);

draw_ the_ object( v); v’ = ITRv

Local View

Rotate ObjectThen Translate Global View

Translate ObjectThen Rotate

(44)

References

Related documents

"Bank Profitability, Commercial Bank Lending, and Interstate Banking: Some Empirical Evidence" with Richard Rivard and Patricia Roberts, presented at the Eastern

genitalia and the observation that all the other Orthocladius species known from the West Palaearctic possess more robust and longer (about 70 μ m long) dorsocentrals allows us to

From a com- parison of mutation frequencies of successive one day broods from males which were mated immediately after irradiation with those from males which had been stored

Secure Data Group Sharing with Attribute and Time based.. encrypted data access

in the presence of additional “nondi ff usion” field gradients, such as imaging or susceptibility-related intrinsic gradients, signal attenuation depends on the square of the sum

Given that the linear lichenase carrying SpyTag generated after TVE protease digestion lost almost all of the activity on boiling heating, we can conclude only cyclized topology

Mitchell, Samuel, "The Third World War: American Hegemony in Latin America and the Overthrow of Salvador Allende" (2012).. CMC

GHGE: greenhouse gas emissions; C&D: construction and demolition; IOT: input–output table; LCA: life cycle assessment; IOA: input–output analysis; MIOT: monetary