1 glBegin( ) Param eters glBegin( ) Param eters glBegin(GL_LI - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 glBegin( ) Param eters glBegin( ) Param eters glBegin(GL_LI - - PDF document

OpenGL Draw ing CS 4 7 3 1 Lecture 3 : OpenGL drawing usually done in display function I ntroduction to OpenGL and GLUT: Part I I Display function is called once when program starts Recall that first, register callback in m


slide-1
SLIDE 1

1

CS 4 7 3 1 Lecture 3 : I ntroduction to OpenGL and GLUT: Part I I Emmanuel Agu OpenGL Draw ing

  • OpenGL drawing usually done in display function
  • Display function is called once when program starts
  • Recall that first, register callback in m ain( ) function

glutDisplayFunc( display );

  • Then, im plem ent display function

void display( void ) { // put drawing stuff here ……. glBegin( GL_LINES ); glVertex3fv( v[0] ); glVertex3fv( v[1] ); …………… glEnd(); }

Basic Draw ing Prim itives

  • Draw points, lines, polylines, polygons
  • Prim itives are specified using form at:

glBegin(primType) // define your primitives here glEnd( )

  • primType: GL_POINTS, GL_LINES, GL_POLYGON….

Basic Draw ing Prim itives: Exam ple

  • Exam ple: to draw three dots:

glBegin(GL_POINTS) glVertex2i(100,50) glVertex2i(100,130) glVertex2i(150, 130) glEnd( )

slide-2
SLIDE 2

2

glBegin( ) Param eters

glBegin(GL_POI NTS)

– draws dots

glBegin(GL_LI NES)

– draws lines

glBegin( ) Param eters

glBegin(GL_LI NE_STRI P)

– polylines

glBegin( GL_POLYGON)

– convex filled polygon

glBegin( ) Param eters

  • GL_POI NTS - dots
  • GL_LI NES – lines, in pairs
  • GL_LI NE_STRI P – polylines
  • GL_LI NE_LOOP – closed loop
  • GL_TRI ANGLES – triangles, three vertices
  • GL_QUADS – quad, four vertices
  • GL_POLYGON – convex filled polygon

OpenGL Com m and Form at glVertex2i(… )

basic command

Vertex Color Clear Flush

number of argum ents

2 – (x,y) 3 – ( x,y,z) 4 – (x,y,z,w) or (r,g,b,a)

type of argum ent

b – byte ub- unsigned byte s - short us – unsigned short i – int ui – unsigned int f – float d - double * - wildcard

library

gl glu glut

slide-3
SLIDE 3

3

Som e OpenGL Com m ands

  • glVertex2i( ) – x,y vertex position
  • glColor3f( ) – RGB color
  • glRecti( ) – aligned rectangled
  • glClearColor – clear color in RGB
  • glClear( ) – clears screen
  • glFlush( ) – forces im age drawing

OpenGL Data Types

GLuint Unsigned int GLushort Unsigned short GLubyte Unsigned char GLDouble Double GLFloat Float GLInt Int GLShort Short GLByte Signed char OpenGL C+ +

Mouse I nteraction

  • Declare prototype
  • myMouse(int button, int state, int x, int y)
  • myMovedMouse
  • Register callbacks:
  • glutMouseFunc(myMouse): when mouse button pressed
  • glutMotionFunc(myMovedMouse): when mouse moves
  • Button returned values:
  • GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON,

GLUT_RIGHT_BUTTON

  • State returned values:
  • GLUT_UP, GLUT_DOWN
  • X,Y returned values:
x,y coordinat es of m ouse locat ion

Keyboard I nteraction

  • Declare prototype
  • myKeyboard(unsigned int key, int x, int y)
  • Register callback:
  • glutKeyboardFunc(myKeyboard): when keyboard is pressed
  • Key values:
  • ASCII value of key pressed
  • X,Y values:
  • Coordinates of mouse location
  • Large switch statem ent to check which key
slide-4
SLIDE 4

4

Exam ple: Keyboard Callback

  • How to use keyboard to control program ?
  • 1. register callback in m ain( ) function

glutKeyboardFunc( myKeyboard );

  • 2. im plem ent keyboard function

void myKeyboard(char key, int x, int y ) { // put keyboard stuff here ………. switch(key){ // check which key case ‘f’: // do stuff break; } …………… }

OpenGL State

  • OpenGL tracks states
  • Drawing color
  • Point size
  • Rendered objects appearance based on current state
  • State variable rem ains active till changed

m iniGL: W h a t ?

  • Object- oriented wrapper in C+ +
  • Allows you choose to either:
Call pure OpenGL call or Call your own OpenGL algorit hm im plem ent at ions
  • First few projects will use pure openGL option
  • Later, you will learn the algorithm s and im plem ent som e

OpenGL calls

  • Google note: there exists another m iniGL, OpenGL port to

PDAs

m iniGL: How ?

  • Can run executables (gears, bounce, your code) using two
  • ptions:
  • ope n GL opt ion sim ply ca lls pur e Ope nGL ca ll
  • cs4 7 3 1 GL opt ion ca lls y ou r code
  • I n beginning, both options are set up to call pure OpenGL

call

  • Later, you will replace some parts, which are called by –

cs4731GL option (* gulp!!* )

  • Exam ple:
I n beginning: -openGL , m gl. m glRotate m et hod calls glRotatlate Lat er: = cs4731GL, m gl. m glRotate will call

hom e_grown_glRotate

slide-5
SLIDE 5

5

m iniGL: How ?

  • m iniGL advantage: debugging, can test sam e code using

both pure calls and your code

  • How? When coding your functions, can always use –openGL
  • ption to debug
  • Design:
Encapsulat e OpenGL calls in class called m inigl Encapsulat e GLUT calls in class called miniglut Use inst ance of m inigl class called mgl Use inst ance of m iniglut class called m glut
  • Examples:
glRotate m gl. m glRotate glut MouseFunc m glut . m glutMouseFunc

m iniGL: W ho?

  • m iniGL written initially by Mark Stevens
  • Stevens, previously CS professor, taught this class before
  • m iniGL extended by Em m anuel Agu
  • Som e students (Paul Tessier, Tony Andrade, etc) subm itted

bug fixes, corrections, etc

  • miniGL m ostly stable, been used in this course 3 or 4

previous times

Hom ew ork 1

  • On class website
  • Goal: to get you going, work out platform issues!!
  • Get OpenGL and GLUT
  • Set up your program m ing environm ent
  • Com pile m iniGL code
  • Examples:
  • Read sect ions of Hill book
  • Convert exam ples t o m iniGL form at
  • Derive new HW1 class ( from cs4731app class)
  • Modify m iniGL draw funct ion
  • Type in m issing funct ion( s)
  • Com pile and run

Hom ew ork 1

  • I f you called OpenGL calls directly, it would work
  • Don’t make openGL calls directly
  • Always m ake call to m iniGL
  • For exam ple:
Do: mgl. m glBegin(m inigl: : MGL_QUADS) Don’t: glBegin( GL_QUADS)
  • miniGL calls either call openGL calls or cs4731app (your

hom e- grown functions).

  • Due on Friday, Sept. 5, 2003
slide-6
SLIDE 6

6

References

  • Hill, chapter 2