opengl, continued cs 543 - computer...

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

Upload: others

Post on 25-Jul-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

CS 5

43 - C

om

pute

r Gra

phic

s:

OpenG

L, C

ontin

ued

by

Robert W

. Lin

dem

an

gogo@

wpi.e

du

(with

help

from

Em

manuel A

gu ;-)

Page 2: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

2

Last tim

e…

.

OpenG

L s

et u

p

Basic

stru

ctu

re

OpenG

L s

kele

ton

Callb

ack fu

nctio

ns, etc

.

Page 3: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

3

Last T

ime: O

penG

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 4: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

4

This

Tim

e: D

o S

om

e R

eal S

tuff

How

to a

ctu

ally

dra

w s

om

eth

ing

OpenG

L c

om

mands &

data

types

Inte

ractio

n w

ith O

penG

L p

rogra

m

Inte

ractio

n e

xam

ple

s u

sin

g k

eyboard

and

mouse

Note

s:

Sta

te-b

ased s

yste

m

Set th

e s

tate

(s)

Send v

ertic

es

Page 5: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

5

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)

Recall, 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 6: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

6

Basic

Dra

win

g P

rimitiv

es

Dra

w p

oin

ts, lin

es, p

oly

lines, p

oly

gons

Prim

itives a

re s

pecifie

d u

sin

g fo

rmat:

glBegin( primType );

// define your primitives here

glEnd( );

primType

One o

f: GL_POINTS, GL_LINES, GL_POLYGON, etc

.

Page 7: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

7

Basic

Dra

win

g P

rimitiv

es:

Exam

ple

Exam

ple

: Dra

w fo

ur d

ots

Basic

idea

send s

equence o

f vertic

es

Connect th

em

in m

anner d

ete

rmin

ed b

y primType

glBegin( GL_POINTS );

glVertex2i( 100, 50 );

glVertex2i( 100, 130 );

glVertex2i( 150, 130 );

glVertex2i( 150, 50 );

glEnd( );

Page 8: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

8

Para

mete

rs fo

r glBegin( )

Dra

w d

ots

glBegin( GL_POINTS );

Dra

w lin

es

glBegin( GL_LINES );

Page 9: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

9

Para

mete

rs fo

r glBegin( )

Dra

w p

oly

lines

glBegin( GL_LINE_STRIP );

Dra

w c

onvex

poly

gons

glBegin( GL_POLYGON );

Page 10: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

10

Para

mete

rs fo

r glBegin( )

GL_POINTS // dots

GL_LINES // lines, in pairs

GL_LINE_STRIP // polylines

GL_LINE_LOOP // closed loop

GL_TRIANGLES // triangles, in threes

GL_QUADS // quad, in fours vertices

GL_POLYGON // convex filled polygon

Page 11: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

11

OpenG

L C

om

mand F

orm

at

glVertex2i( )

basic

com

mand

Vertex

Color

Clear

Flush

num

ber o

farg

um

ents

2 - (x,y)

3 - (x,y,z)

4 - (x,y,z,w) or

(r,g,b,a)

type o

f arg

um

ent

b - byte

ub - unsigned byte

s - short

us - unsigned short

i - int

ui - unsigned int

f - float

d - double

* - wildcard

libra

ry

gl

glu

glut

Page 12: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

12

Som

e O

penG

L C

om

mands

glVertex2i( ); // x,y vertex position

glColor3f( ); // RGB color

glRecti( ); // aligned rectangled

glClearColor( ); // clear color in RGB

glClear( ); // clears screen

glFlush( ); // forces image drawing

Page 13: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

13

OpenG

L D

ata

Types

GLuint

unsigned int

GLushort

unsigned short

GLubyte

unsigned char

GLDouble

double

GLFloat

float

GLInt

int

GLShort

short

GLByte

signed char

OpenGL

C++

Page 14: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

14

Keyboard

Inte

ractio

nD

ecla

re p

roto

type

myKeyboard( unsigned int key, int x, int y );

Regis

ter c

allb

ack

glutKeyboardFunc( myKeyboard );

Key v

alu

es

ASCII v

alu

e o

f key p

ressed

X, Y

valu

es

Coord

inate

s o

f mouse lo

catio

n

The fu

nctio

n myKeyboard( ) c

onta

ins a

larg

e s

witc

h

sta

tem

ent to

act d

ependin

g o

n w

hic

h k

ey w

as

pre

ssed

Page 15: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

15

Exam

ple

: Keyboard

Callb

ack

How

to u

se k

eyboard

to c

ontro

l pro

gra

m

1. R

egis

ter c

allb

ack in

main

( ) functio

n

glutKeyboardFunc( myKeyboard );

2. Im

ple

ment k

eyboard

functio

n

void myKeyboard( unsigned int key, int x, int y ) {

// put keyboard stuff here

...

switch( key ) { // check which key

case ‘f’:

// do stuff

break;

}

...

}

Page 16: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

16

Mouse In

tera

ctio

nD

ecla

re p

roto

type

myMouse( int button, int state, int x, int y )

myMovedMouse( ... );

Regis

ter c

allb

acks

glutMouseFunc( myMouse ) // when mouse button pressed

glutMotionFunc( myMovedMouse ) // when mouse moves

button p

ara

mete

r valu

e w

ill be o

ne o

fGLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON,

GLUT_RIGHT_BUTTON

state p

ara

mete

r valu

e w

ill be o

ne o

fGLUT_UP, GLUT_DOWN

x, y

para

mete

rs w

ill have th

e m

ouse c

oord

inate

s

Page 17: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

17

Mouse In

tera

ctio

n E

xam

ple

Each m

ouse c

lick g

enera

tes s

epara

teevents

So, n

eed to

do y

our o

wn c

heckin

g, etc

. inyour m

ouse fu

nctio

n

Exam

ple

: dra

w (o

r sele

ct ) re

cta

ngle

on

scre

en

Page 18: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

18

Mouse In

tera

ctio

n E

xam

ple

(cont.)

void myMouse( int button, int state, int x, int y ) {

static GLintPoint corner[2];

static int numCorners = 0; // initial value is 0

if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {

corner[numCorners].x = x;

corner[numCorners].y = screenHeight – y; //flip y coord

numCorners++;

if( numCorners == 2 ) {

// draw rectangle or do whatever you planned to do

glRecti( corner[0].x, corner[0].y, corner[1].x, corner[1].y );

numCorners = 0;

}

}

else {

if( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN ) {

glClear( GL_COLOR_BUFFER_BIT ); // clear the window

}

}

glFlush( );

}

Page 19: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

19

OpenG

L S

tate

OpenG

L tra

cks s

tate

sD

raw

ing c

olo

r

Poin

t siz

e

Rendere

d o

bje

ct's

appeara

nce b

ased o

ncurre

nt s

tate

Sta

te v

aria

ble

rem

ain

s a

ctiv

e u

ntil

changed

Page 20: OpenGL, Continued CS 543 - Computer Graphicsweb.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture01_p3.pdfOpenGL commands & data types Interaction with OpenGL program Interaction examples

R.W

. Lin

dem

an - W

PI D

ept. o

f Com

pute

r Scie

nce

20

Refe

rences

Hill, c

hapte

r 2