20
CS 543 - Computer Graphics: Intro to OpenGL by Robert W. Lindeman [email protected] (with help from Emmanuel Agu ;-)

Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

CS 5

43 - C

om

pute

r Gra

phic

s:

Intro

to O

penG

L

by

Robert W

. Lin

dem

an

gogo@

wpi.e

du

(with

help

from

Em

manuel A

gu ;-)

Page 2: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

2

OpenG

L B

asic

s

Last tim

e:

What is

Com

pute

r Gra

phic

s?

What is

a g

raphic

s lib

rary

What to

expect fro

m c

lass

Intro

stu

ff….

Today:

Sta

rt learn

ing O

penG

L b

asic

s

OpenG

L p

rogra

m s

tructu

re

"Hello

, world

!" skele

ton

Note

: Only

basic

s h

ere

!

Learn

a b

unch o

n y

our o

wn (m

ore

fun a

nyw

ay!)

Page 3: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

3

OpenG

L B

asic

s

Prim

ary

goal: re

nderin

g

Renderin

g?

Convert g

eom

etric

/math

em

atic

al o

bje

ct a

nd

enviro

nm

ent d

escrip

tions in

to im

ages

OpenG

L c

an re

nder:

Geom

etric

prim

itives (lin

es, d

ots

, etc

.)

Bitm

ap im

ages (.b

mp, .jp

g, etc

.)

OpenG

L d

oes n

ot d

o w

indow

, mouse,

keyboard

, or d

evic

e m

anagem

ent

Page 4: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

4

OpenG

L B

asic

sLow

-level g

raphic

s re

nderin

g A

pplic

atio

nPro

gra

mm

ing In

terfa

ce (A

PI)

Wid

ely

used a

ll over th

e g

raphic

s fie

ldW

ill be u

sed in

this

cla

ss

Hig

hly

porta

ble

Dis

pla

y d

evic

e in

dependent

Win

dow

syste

m in

dependent (X

Win

dow

s, A

qua,

Win

dow

s, etc

.)O

S in

dependent (U

nix

, OS X

, Lin

ux, W

indow

s, etc.)

OpenG

L p

rogra

ms b

ehave s

am

e o

n d

iffere

nt

outp

ut d

evic

es a

nd O

Ss

Event-d

riven a

ppro

ach

Page 5: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

5

Left-m

ouse c

lick

Keyboard

‘h’ k

ey

OpenG

L: E

vent-D

riven

Pro

gra

mm

ing

Very

importa

nt p

rogra

mm

ing m

odel!

Pro

gra

m o

nly

responds to

events

Do n

oth

ing u

ntil e

vent o

ccurs

Sam

ple

Events

:m

ouse c

lick, k

ey s

troke, w

indow

resiz

e

Pro

gra

mm

er d

efin

es

Events

of in

tere

st

Actio

ns to

take u

pon e

vent o

ccurre

nce

Syste

mM

ain

tain

s a

n e

vent q

ueue

Dis

patc

hes e

vents

to p

rogra

mm

er-d

efin

ed c

ode

Page 6: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

6

OpenG

L: E

vent-D

riven v

s.

Sequentia

l Pro

gra

mm

ing

Sequentia

l pro

gra

mRead s

om

e d

ata

Do s

om

e p

rocessin

gPrin

t som

e re

sults

Event-d

riven p

rogra

mIn

itializ

e s

om

e th

ings

Ente

r an in

finite

loop

Wait u

ntil d

efin

ed e

vent o

ccurs

Take d

efin

ed a

ctio

n

What is

the w

orld

’s m

ost w

idely

used

event-d

riven p

rogra

m?

Page 7: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

7

OpenG

L: E

vent-D

riven

Pro

gra

mm

ing in

OpenG

L

How

in O

penG

L?

Pro

gra

mm

er re

gis

ters

callb

ack fu

nctio

ns

Callb

ack fu

nctio

n c

alle

d w

hen a

ssocia

ted

event o

ccurs

Exam

ple

:D

ecla

re a

functio

n m

yM

ouse to

respond to

mouse c

lick

Regis

ter it: T

ell O

penG

L to

call it w

hen

mouse c

licked

Code? g

lutM

ouseFunc( m

yM

ouse );

Notic

e th

is is

not a

n O

penG

L fu

nctio

n!

Page 8: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

8

OpenG

L U

tility T

oolk

it (GLU

T)

OpenG

LW

indow

syste

m in

dependent

Concern

ed o

nly

with

dra

win

gN

o w

indow

-managem

ent fu

nctio

ns (c

reate

, resiz

e,

etc

.)Very

porta

ble

GLU

T:

Min

imal w

indow

managem

ent: fa

st p

roto

typin

gIn

terfa

ces w

ith d

iffere

nt w

indow

ing s

yste

ms

Allo

ws e

asy p

ortin

g b

etw

een w

indow

ing s

yste

ms

By fa

r the m

ost-w

idely

used lib

rary

for O

penG

L

GLU

I:M

ore

-advanced G

UI w

idgets

Page 9: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

9

OpenG

L U

tility T

oolk

it (GLU

T)

No b

ells

nor w

his

tles

No s

liders

No d

ialo

g b

oxes

No m

enu b

ar, etc.

To a

dd b

ells

and w

his

tles, n

eed o

ther

API:G

LU

I

Qt

X w

indow

syste

m

Mic

rosoft: W

GL, etc

.

Page 10: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

10

Gettin

g S

tarte

d w

ith F

irst

OpenG

L P

rogra

m

At to

p o

f pro

gra

m, in

clu

de re

quire

dheaders

#include <gl/gl.h>

#include <gl/glu.h>

gl d

irecto

ry is

sub-d

irecto

ry o

f your

inclu

de file

locatio

n

Then in

clu

de G

LU

T fo

r win

dow

managem

ent

#include <gl/glut.h>

Page 11: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

11

Gettin

g S

tarte

d…

If you w

ant fu

ll-blo

wn, p

ull-d

ow

nw

indow

s (W

GL, A

GL, etc.)

#include <windows.h> // do this before gl.h, glu.h

Most O

penG

L a

pplic

atio

ns u

se s

tandard

Clib

rary

so

#include <stdlib.h>

#include <stdio.h>

Page 12: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

12

Pro

gra

m S

tructu

re

Config

ure

and o

pen w

indow

(GLU

T)

Initia

lize O

penG

L s

tate

Regis

ter in

put c

allb

ack fu

nctio

ns (G

LU

T)

Render

Resiz

e

Input: k

eyboard

, mouse, etc

.

My in

itializ

atio

nSet b

ackgro

und c

olo

r, cle

ar c

olo

r, dra

win

g c

olo

r,poin

t siz

e, e

sta

blis

h c

oord

inate

syste

m, etc

.

glutMainLoop( );

Dra

ws im

age a

nd w

aits

infin

itely

until a

ctio

n o

ccurs

Page 13: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

13

GLU

T: O

penin

g a

win

dow

glutInit( &argc, argv );

// initializes

glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );

// sets display mode (e.g., single buffer

with RGB colorspace)

glutInitWindowSize( 640, 480 );

// sets window size (WxH)

glutInitPosition( 100, 150 );

// sets upper left corner of window

glutCreateWindow( “my first attempt” );

// open window with title “my first

attempt”

Page 14: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

14

640

480

100

150

my fir

st a

ttem

pt

OpenG

L S

kele

ton

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

// First initialize toolkit, set display mode and create window

glutInit( &argc, argv );

glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );

glutInitWindowSize( 640, 480 );

glutInitWindowPosition( 100, 150 );

glutCreateWindow( “my first attempt” );

// ...then register callback functions

// ...do my initialization

// ...wait in glutMainLoop for events

}

Page 15: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

15

GLU

T C

allb

ack F

unctio

ns

Regis

ter a

ll events

your p

rogra

m w

ill react to

Event o

ccurs

=>

syste

m g

enera

tes c

allb

ack

Callb

ack: ro

utin

e s

yste

m c

alls

when e

vent

occurs

No re

gis

tere

d c

allb

ack =

no re

actio

n!

Exam

ple

: if you d

o n

ot d

efin

e a

keyboard

callb

ack fu

nctio

n, y

ou c

an b

ang o

n k

eyboard

all

you w

ant, N

O R

ESPO

NSE!!

Except fro

m y

our n

eig

hbor.

Page 16: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

16

GLU

T C

allb

ack F

unctio

ns in

Skele

ton

glutDisplayFunc( myDisplay );

// called when window contents need to be redrawn

glutReshapeFunc( myReshape );

// called when window is reshaped

glutMouseFunc( myMouse );

// called when mouse button is pressed

glutKeyboardFunc( myKeyboard );

// called when keyboard is pressed or released

glutMainLoop( );

// Now draw the initial picture and enter infinite

loop until a registered event occurs

Page 17: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

17

Exam

ple

: Renderin

g C

allb

ack

Do a

ll your d

raw

ing in

the d

ispla

y fu

nctio

nCalle

d in

itially

& w

hen p

ictu

re c

hanges (e.g., re

siz

e)

Firs

t, regis

ter c

allb

ack in

main

( ) functio

nglutDisplayFunc( myDisplay );

Then, im

ple

ment d

ispla

y fu

nctio

n

void myDisplay( void ) {

// put drawing stuff here

...

glBegin( GL_LINES );

glVertex3fv( v[0] );

glVertex3fv( v[1] );

...

glEnd( );

}

Page 18: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

18

OpenG

L S

kele

ton

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

// First initialize toolkit, set display mode and create window

glutInit( &argc, argv );

glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );

glutInitWindowSize( 640, 480 );

glutInitWindowPosition( 100, 150 );

glutCreateWindow( “my first attempt” );

// Now register callback functions

glutDisplayFunc( myDisplay );

glutReshapeFunc( myReshape );

glutMouseFunc( myMouse );

glutKeyboardFunc( myKeyboard );

myInit( );

glutMainLoop( );

}

Page 19: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

19

Fin

al T

houghts

Need fo

r glo

bal v

aria

ble

sCallb

ack A

PI is

pre

defin

ed

No w

ay to

add p

ara

mete

rs

Page 20: Intro to OpenGL CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01...R.W. Lindeman - WPI Dept. of Computer Science 14 640 480 100 150 my first attempt

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

20

Refe

rences

Hill, c

hapte

r 2