Top Banner
OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET
57
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

OpenGL Basics

SUJAN CHOWDHURY

Lecturer, CSE, CUET

Page 2: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

What is OpenGL

OpenGL is a software API to graphics hardware. designed as a streamlined, hardware-independent

interface to be implemented on many different hardware platforms

Intuitive, procedural interface with c binding No windowing commands ! No high-level commands for describing models of three-

dimensional objects The OpenGL Utility Library (GLU) provides many of the

modeling features, such as quadric surfaces and NURBS curves and surfaces

Page 3: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

SGI and GL

Silicon Graphics (SGI) revolutionized the graphics workstation by implementing the pipeline in hardware (1982)

To access the system, application programmers used a library called GL

With GL, it was relatively simple to program three dimensional interactive applications

Page 4: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

OpenGL

The success of GL lead to OpenGL (1992), a platform-independent API that was Easy to use Close enough to the hardware to get excellent

performance Focus on rendering Omitted windowing and input to avoid window

system dependencies

Page 5: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

OpenGL Evolution

Controlled by an Architectural Review Board (ARB) Members include SGI, Microsoft, Nvidia, HP,

3DLabs, IBM,……. Relatively stable (present version 2.0)

Evolution reflects new hardware capabilities 3D texture mapping and texture objects Vertex and fragment programs

Allows for platform specific features through extensions

Page 6: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

OpenGL – Open Graphics Library

What it is a s/w interface to graphics h/w mid-level, device-independent, portable graphics

subroutine package developed primarily by SGI (Silicon Graphics Inc.) 2D/3D graphics, lower-level primitives (polygons) does not include low-level I/O management basis for higher-level libraries/toolkits

Page 7: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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 AGL : Apple Macintosh system

Page 8: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Software Organization

GLUT

GLU

OpenGL

GLX, AGLor WGL

X, Win32, Mac O/S

software and/or hardware

application program

OpenGL Motifwidget or similar

Page 9: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

OpenGL #defines

Most constants are defined in the include files gl.h, glu.h and glut.h Note #include <GL/glut.h> should

automatically include the others Examples glBegin(GL_POLYGON) glClear(GL_COLOR_BUFFER_BIT)

include files also define OpenGL data types: GLfloat, GLdouble,….

Page 10: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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 Functions starting with glut are from the

GLUT library.

Page 11: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

OpenGL function format

glVertex3f(x,y,z)

belongs to GL library

function name

x,y,z are floats

glVertex3fv(p)

p is a pointer to an array

dimensions

Page 12: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Parameter Types

b 8-bit integer GLbyte

s 16-bit integer GLshort

i 32-bit integer GLint

f 32-bit float GLfloat

d 64-bit float GLdouble

ub 8-bit unsigned int GLubyte

us 16-bit unsigned int GLushort

ui 32-bit unsigned int GLuint

Page 13: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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 glColor4fv() takes an array of 4 floats

Page 14: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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

possible values

Page 15: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Glut Routines

Initialization: glutInit() processes (and removes) command line arguments that may be of interest to glut and the window system and does general initialization of Glut and OpenGL

Must be called before any other glut routines Display Mode: The next procedure, glutInitDisplayMode(),

performs initializations informing OpenGL how to set up the frame buffer.

Display Mode Meaning GLUT_RGB Use RGB colors GLUT_RGBA Use RGB plus alpha (for transparency) GLUT_INDEX Use indexed colors (not recommended)

GLUT_DOUBLE Use double buffering (recommended) GLUT_SINGLE Use single buffering (not recommended)

GLUT_DEPTH Use depth buffer (for hidden surface removal.)

Page 16: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Glut Routines

Window Setup glutInitWindowSize(int width, int height) glutInitWindowPosition(int x, int y) glutCreateWindow(char* title)

Page 17: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Primitive Types

GL_POINTS

V0

V1V2

V3

V5

V4

GL_LINE_LOOP

V0

V1 V2

V3

V5

V4

GL_LINE

V0

V1V2

V3

V5

V4

GL_LINE_STRIP

V0

V1V2

V3

V5

V4

GL_POLYGON

V0

V1

V2

V3

V4

Polygon must be:•Simple•No-holes inside•Convex Simple

Non-convex

ComplexConvex

P1

P2

Page 18: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Primitive Types

GL_TRIANGLE

V0

V1

V2

V3

V4

V5

V6

V7V8

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_STRIP012, 213, 234, 435

GL_QUAD_STRIP

V0

V1

V2

V3

V4

V5

V6

V7

GL_QUAD_STRIP0132, 2354, 4576

GL_TRIANGLE_FAN

V0

V1

V2

V3

V4

V5

GL_TRIANGLE_FAN012, 023 , 034, 045

Page 19: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

An Example

void drawParallelogram( 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();

}

Page 20: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Vertices and Primitives

Points, GL_POINTS Individual points Point size can be altered

glPointSize (float size)

glBegin(GL_POINTS);glColor3fv( color );glVertex2f( P0.x, P0.y );glVertex2f( P1.x, P1.y );glVertex2f( P2.x, P2.y );glVertex2f( P3.x, P3.y );glVertex2f( P4.x, P4.y );glVertex2f( P5.x, P5.y );glVertex2f( P6.x, P6.y );glVertex2f( P7.x, P7.y );glEnd();

Page 21: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Vertices and Primitives

Lines, GL_LINES Pairs of vertices interpreted as individual line segments Can specify line width using:

glLineWidth (float width)

glBegin(GL_LINES);glColor3fv( color );glVertex2f( P0.x, P0.y );glVertex2f( P1.x, P1.y );glVertex2f( P2.x, P2.y );glVertex2f( P3.x, P3.y );glVertex2f( P4.x, P4.y );glVertex2f( P5.x, P5.y );glVertex2f( P6.x, P6.y );glVertex2f( P7.x, P7.y );glEnd();

Page 22: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Vertices and Primitives

Line Strip, GL_LINE_STRIP series of connected line segments

Page 23: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Vertices and Primitives

Line Loop, GL_LINE_LOOP Line strip with a segment added between last and first

vertices

Page 24: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Vertices and Primitives

Polygon , GL_POLYGON boundary of a simple, convex polygon

Page 25: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Vertices and Primitives

Triangles , GL_TRIANGLES triples of vertices interpreted as triangles

Page 26: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Vertices and Primitives

Triangle Strip , GL_TRIANGLE_STRIP linked strip of triangles

v0

v2

v1

v3

v4 v5

v6v7

Page 27: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Vertices and Primitives

Triangle Fan , GL_TRIANGLE_FAN

linked fan of triangles

v0

v1 v2v3

v4

v5

Page 28: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Vertices and Primitives

Quads , GL_QUADS quadruples of vertices interpreted as four-sided

polygons

Page 29: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Configuring OpenGL in Visual C++

Files Required for GLUT: glut32.dll glut.h glut32.lib

Page 30: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Sample Program

Page 31: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Closer Look at the main()

#include <GL/glut.h>

int main(int argc, char** argv){

glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(mydisplay);

init();

glutMainLoop();

}

includes gl.h

define window properties

set OpenGL state

enter event loop

display callback

Page 32: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

init.c

void init(){

glClearColor (0.0, 0.0, 0.0, 1.0);

glColor3f(1.0, 1.0, 1.0);

glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

}

black clear coloropaque window

fill/draw with white

viewing volume

Set up whatever state you’re going to useDon’t need this much detail unless working in 3D

Page 33: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

GLUT Callback Functions

Callback function : Routine to call when an event happens Window resize or redraw User input (mouse, keyboard) Animation (render many frames)

“Register” callbacks with GLUT glutDisplayFunc( my_display_func ); glutIdleFunc( my_idle_func ); glutKeyboardFunc( my_key_events_func ); glutMouseFunc ( my_mouse_events_func );

Page 34: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Rendering Callback

Callback function where all our drawing is done Every GLUT program must have a display callback

glutDisplayFunc( my_display_func ); /* this part is in main.c */

void my_display_func (void ){

glClear( GL_COLOR_BUFFER_BIT );glBegin( GL_TRIANGLE );

glVertex3fv( v[0] );glVertex3fv( v[1] );glVertex3fv( v[2] );

glEnd();glFlush();

}

Page 35: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Events in OpenGL

Event Example OpenGL Callback Function

Keypress

KeyDown

KeyUp

glutKeyboardFunc

Mouse leftButtonDown

leftButtonUp

glutMouseFunc

Motion With mouse press

Without

glutMotionFunc

glutPassiveMotionFunc

Window Moving

Resizing

glutReshapeFunc

System Idle

Timer

glutIdleFunc

glutTimerFunc

Software What to draw glutDisplayFunc

Page 36: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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 such as the depth buffer GL_ DEPTH_ BUFFER_ BIT but we’re not using it right now

Page 37: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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

next available opportunity.

Page 38: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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 default:RGBA & SINGLE

Page 39: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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) Called after Init, Displaymode, Position and Size calls Window will not appear until glutMainLoop is called Return value is a unique identifier for the window

Page 40: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

The command glOrtho() creates an orthographic parallel viewing volume. You specify the corners of the near clipping plane and the distance to the far clipping plane.void glOrtho(GLdouble left, GLdouble right, GLdouble bottom,GLdouble top, GLdouble near, GLdouble far);

Page 41: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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

EventQueue

Keyboard Callback

Mouse Callback

Display Callback

Keyboard

Mouse

Display

OS MainLoop

Each application has itsOwn event queue

Page 42: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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 redrawn glutPostRedisplay() 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 Redefine projection matrix to match aspect ratio of

image & view port

Page 43: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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. The x and y callback parameters indicate the location

of the mouse when the key was pressed.

Page 44: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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 the mouse when the event occurred.

Page 45: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Animation( Motion = Redraw+Swap )

Page 46: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Animation( Motion = Redraw+Swap )

Page 47: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Animation( Motion = Redraw+Swap )

Page 48: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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-ViewMatrix Stack

ProjectionMatrix Stack

GraphicsPrimitives

(P)

OutputN•M•P

Page 49: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.
Page 50: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Translation – 2D

(4,5) (7,5)

Y

XBefore Translation

1

*

100

10

01

1

Form sHomogeniou

y

x

d

d

y

x

TPPd

dT

y

xP

y

xP

y

x

y

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

(7,1) (10,1)

X

Y

Translation by (3,-4)

Page 51: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Transformations and OpenGL®

Each time an OpenGL transformation M is called the current MODELVIEW matrix C is altered:

Cvv CMvv

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

CTRvv

Note: v is any vertex placed in rendering pipeline v’ is the transformed vertex from v.

Page 52: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Matrix Operation

Page 53: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

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

Two Different Views

Page 54: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Local View Translate Object Then Rotate

Order of Transformation T•R

glLoadIdentity();glMultiMatrixf( T);

glMultiMatrixf( R);

draw_ the_ object( v);v’ = ITRv

Global View Rotate Object

Then Translate

Effect is same, but perception is different

Page 55: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Order of Transformation R•T

glLoadIdentity();glMultiMatrixf( R);

glMultiMatrixf( T);

draw_ the_ object( v);v’ = ITRv

Local View Rotate Object Then Translate

Global View Translate Object

Then Rotate

Effect is same, but perception is different

Page 56: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

OpenGL Programming Guide (3rd Edition) • M. Woo, J. Neider, T. Davis, D. Shreiner• Addison-Wesley, 2000

Page 57: OpenGL Basics SUJAN CHOWDHURY Lecturer, CSE, CUET.

Thank You