ppt

trexpeeverΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 11 μήνες)

79 εμφανίσεις

COMP 175 | COMPUTER GRAPHICS

Remco Chang

1
/28

05


OpenGL

Lecture 04:

OpenGL (Fixed
-
Function)


COMP
175: Computer
Graphics

September 15, 2011

COMP 175 | COMPUTER GRAPHICS

Remco Chang

2
/28

05


OpenGL


Conceptual Graphics Framework








Graphics Library can be Direct3D, Java3D, OpenGL, etc.


It is a software API that controls the functions of a
piece of hardware


the graphics card.

Remember…

Graphics

System/

GPU

Application

Model/data
base

Software

Hardware

Application

program

Graphics

Library

COMP 175 | COMPUTER GRAPHICS

Remco Chang

3
/28

05


OpenGL


In Immediate
-
Mode Fixed
-
Function mode, OpenGL
acts as a state machine.



What is a state machine?



Every variable is a “global variable”


For example, the current color



You need to keep track of the states… Or query the
graphics card if you forget


Which, of course, is slow

Graphics Library

COMP 175 | COMPUTER GRAPHICS

Remco Chang

4
/28

05


OpenGL


Pseudo code:


SetState (LineStyle, DASHED);

SetState (LineColor, RED);

DrawLine ( PtStart = (x1,y1), PtEnd = (x2,y2) );

SetState (LineColor, BLUE);

DrawLine ( PtStart = (x2,y2), PtEnd = (x3,y3) );

SetState (LineStyle, SOLID);

DrawLine ( PtStart = (x3,y3), PtEnd = (x4,y4) );

State Variables

What color and shape?

What color and shape?

What color and shape?

COMP 175 | COMPUTER GRAPHICS

Remco Chang

5
/28

05


OpenGL


Pseudo code:


SetState (LineStyle, DASHED);

SetState (LineColor, RED);

DrawLine ( PtStart = (x1,y1), PtEnd = (x2,y2) );

SetState (LineStyle, DASHED);

SetState (LineColor, BLUE);

DrawLine ( PtStart = (x2,y2), PtEnd = (x3,y3) );

SetState (LineStyle, SOLID);

SetState (LineColor, BLUE);

DrawLine ( PtStart = (x3,y3), PtEnd = (x4,y4) );

State Variables

What color and shape?

What color and shape?

What color and shape?

COMP 175 | COMPUTER GRAPHICS

Remco Chang

6
/28

05


OpenGL


What if…?


function
DrawDashedTriangle

(pt1,pt2,p3
) {


SetState
(
LineStyle
, DASHED );


DrawLine
(
PtStart
=pt1,
PtStart
=p2 );


DrawLine
(
PtStart
=pt2,
PtStart
=p3 );


DrawLine
(
PtStart
=pt3,
PtStart
=p1 );

}


What color is the triangle?


Pros:??


Cons:??

State Variables


Pros and Cons

COMP 175 | COMPUTER GRAPHICS

Remco Chang

7
/28

05


OpenGL


What if…?


function
DrawDashedTriangle

(pt1,pt2,p3
) {


SetState
(
LineStyle
, DASHED );


DrawLine
(
PtStart
=pt1,
PtStart
=p2 );


DrawLine
(
PtStart
=pt2,
PtStart
=p3 );


DrawLine
(
PtStart
=pt3,
PtStart
=p1 );

}


What color is the triangle?


Pros: trickle down effect, caller can control the
subroutine’s behavior


Cons: the color is undefined! Who set my color?!

State Variables


Pros and Cons

COMP 175 | COMPUTER GRAPHICS

Remco Chang

8
/28

05


OpenGL


What’s right and what’s wrong with this?


function
DrawTriangle

(
pt1,pt2,p3,





int
origColor
, int
curColor
,





int
origStyle
, int
curStyle

) {


SetState
(
LineStyle
,
curStyle

);


SetState
(
LineColor
,
curColor
);


DrawLine
(
PtStart
=pt1,
PtStart
=p2 );


DrawLine
(
PtStart
=pt2,
PtStart
=p3 );


DrawLine
(
PtStart
=pt3,
PtStart
=p1
);


SetState
(
LineStyle
,
origStyle

);


SetState
(
LineColor
,
origColor
);

}

State Variables


Pros and Cons

COMP 175 | COMPUTER GRAPHICS

Remco Chang

9
/28

05


OpenGL


Bundles and unsets…



function
DrawRedDashedTriangle
(pt1,pt2,p3){



PushAttributeState
();




SetState
(
LineStyle
, DASHED
);




SetState
(
LineColor
, RED
);




...



PopAttributeState
();


}



This eliminates the state variable problem.

Still A Pain, but Cleaner…

COMP 175 | COMPUTER GRAPHICS

Remco Chang

10
/28

05


OpenGL


What if I forget to set some state?


DrawLine

(
PtStart

= (x1,y1),
PtEnd

= (x2,y2) );



What if I set the wrong state?


SetState
(
LineStyle
, 12345);


DrawLine
(
PtStart
=pt1,
PtStart
=p2
);



What if I forget the state a variable is in?



Forgetting the State?

COMP 175 | COMPUTER GRAPHICS

Remco Chang

11
/28

05


OpenGL


What if I forget to set some state?


DrawLine

(
PtStart

= (x1,y1),
PtEnd

= (x2,y2) );


OpenGL provides some good default values. For example, for color, it’s set to
(1,1,1,1)


non transparent white


How do I know if something worked?


SetState
(
LineStyle
, 12345);


DrawLine
(
PtStart
=pt1,
PtStart
=p2
);


You don’t. Since it’s not clear if some configuration of states will send OpenGL
spinning, if you suspect an error from OpenGL, call



Glenum

glGetError
(void)


What if I forget the state a variable is in?


You should use
this sparingly…
But you can use



void
glGetBooleanv
(
Glenum

paraname
,
Glboolean
*
params
)



void
glGetFixedv
(
Glenum

paraname
,
Glfixed
*
params
)



void
glGetFloatv
(
Glenum

paraname
,
Glfloat
*
params
)



void
glGetIntegerv
(
Glenum

paraname
,
Glint*
params
)




Forgetting the State?

COMP 175 | COMPUTER GRAPHICS

Remco Chang

12
/28

05


OpenGL



What do you think is the best way to deal with the
state machine and maintain state variables?


How to access local variables (that correspond to state
variables)?


Efficiency considerations?



How do you think the programming with a state
machine affects multi
-
threaded applications?

Programming Strategies?

COMP 175 | COMPUTER GRAPHICS

Remco Chang

13
/28

05


OpenGL





Questions?


COMP 175 | COMPUTER GRAPHICS

Remco Chang

14
/28

05


OpenGL


Typical OpenGL Application

COMP 175 | COMPUTER GRAPHICS

Remco Chang

15
/28

05


OpenGL


Here we use GLUT, which is a basic GL window
implementation that is on all platforms.









Pros: cross
-
platform, command line, easy to use


Cons: no GUI support (no buttons, menus, etc.)

Example

COMP 175 | COMPUTER GRAPHICS

Remco Chang

16
/28

05


OpenGL

int
main( int
argc
, char**
argv

) {


glutInit
( &
argc
,
argv

); // Boilerplate
initialization


glutInitDisplayMode
(GLUT_RGB
| GLUT_DOUBLE | GLUT_DEPTH);



glutInitWindowPosition
( 50, 50
); //upper left


glutInitWindowSize
( 640, 480 ); // width, height, in pixels


glutCreateWindow
( "OpenGL Example" ); // window
title



glViewport
(



/* lower left
corner of the viewport */ 0, 0,



/*
width, height of the viewport */ 640, 480
); //lower left

Example OpenGL Application

What’s going on
here?

COMP 175 | COMPUTER GRAPHICS

Remco Chang

17
/28

05


OpenGL


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


glutInit( &argc, argv ); // Boilerplate initialization


glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);



Example OpenGL Application

#define GLUT_RGB 0

#define GLUT_RGBA GLUT_RGB

#define GLUT_INDEX 1

#define GLUT_SINGLE 0

#define GLUT_DOUBLE 2

#define GLUT_ACCUM 4

#define GLUT_ALPHA 8

#define GLUT_DEPTH 16

#define GLUT_STENCIL 32

#if {GLUT_API_VERSION >= 2}


#define GLUT_MULTISAMPLE 128


#define GLUT_STEREO 256

#
endif


#if {GLUT_API_VERSION >= 3}


#define GLUT_LUMINANCE 512

#
endif


COMP 175 | COMPUTER GRAPHICS

Remco Chang

18
/28

05


OpenGL

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


glutInit( &argc, argv ); // Boilerplate initialization


glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );




glutInitWindowPosition( 50, 50 ); //upper left


glutInitWindowSize( 640, 480 ); // width, height, in pixels


glutCreateWindow( "OpenGL Example" ); // window title



glViewport(



/* lower left corner of the viewport */ 0, 0,



/* width, height of the viewport */ 640, 480 ); //lower left

Example OpenGL Application

Careful!!

COMP 175 | COMPUTER GRAPHICS

Remco Chang

19
/28

05


OpenGL

int
main( int
argc
, char**
argv

) {


glutInit
( &
argc
,
argv

); // Boilerplate
initialization


glutInitDisplayMode
( GLUT_RGB | GLUT_DEPTH
);




glutInitWindowPosition
( 50, 50
); //upper left


glutInitWindowSize
( 640, 480 ); // width, height, in pixels


glutCreateWindow
( "OpenGL Example" ); // window
title



glViewport
(



/* lower left
corner of the viewport */ 0, 0,



/* width, height of the viewport */ 640, 480 ); //lower left


glShadeModel
( GL_SMOOTH );


glPolygonMode
(
GL_FRONT
,
GL_FILL );


setupLighting
();


setupCamera
(
640, 480
);


registerCallBacks
();


glutMainLoop
();

}


Example OpenGL Application

Asks OpenGL to
render with smooth
(
Gouraud
) shading.
Other option is
GL_FLAT

COMP 175 | COMPUTER GRAPHICS

Remco Chang

20
/28

05


OpenGL

int
main( int
argc
, char**
argv

) {


glutInit
( &
argc
,
argv

); // Boilerplate
initialization


glutInitDisplayMode
( GLUT_RGB | GLUT_DEPTH
);




glutInitWindowPosition
( 50, 50
); //upper left


glutInitWindowSize
( 640, 480 ); // width, height, in pixels


glutCreateWindow
( "OpenGL Example" ); // window
title



glViewport
(



/* lower left
corner of the viewport */ 0, 0,



/* width, height of the viewport */ 640, 480 ); //lower left


glShadeModel
( GL_SMOOTH );


glPolygonMode
(
GL_FRONT
,
GL_FILL );


setupLighting
();


setupCamera
(
640, 480
);


registerCallBacks
();


glutMainLoop
();

}


Example OpenGL Application

Asks OpenGL to render fill the polygons but only in
the front
-
facing side

Options are GL_BACK, GL_FRONT_AND_BACK, and
GL_POINT, GL_LINE, GL_FILL

COMP 175 | COMPUTER GRAPHICS

Remco Chang

21
/28

05


OpenGL

int
main( int
argc
, char**
argv

) {


glutInit
( &
argc
,
argv

); // Boilerplate
initialization


glutInitDisplayMode
( GLUT_RGB | GLUT_DEPTH
);




glutInitWindowPosition
( 50, 50
); //upper left


glutInitWindowSize
( 640, 480 ); // width, height, in pixels


glutCreateWindow
( "OpenGL Example" ); // window
title



glViewport
(



/* lower left
corner of the viewport */ 0, 0,



/* width, height of the viewport */ 640, 480 ); //lower left


glShadeModel
( GL_SMOOTH );


glPolygonMode
(
GL_FRONT
,
GL_FILL );


setupLighting
();


setupCamera
(
640, 480
);


registerCallBacks
();


glutMainLoop
();

}


Example OpenGL Application

void
setupLighting
() {


glEnable

(GL_LIGHTING);


glEnable

(GL_LIGHT0);




GLfloat

whiteColor
[] =


{0.25f, 0.25f, 0.25f, 1.0f};


glLightfv

(GL_LIGHT0, GL_AMBIENT,


whiteColor
);



//directional (diffuse) light


GLfloat

whiteFull
[] =


{1.0f, 1.0f, 1.0f, 1.0f};


GLfloat

lightPos
[] =


{
-
1.0f, 1.0f, 1.0f, 0.0f};


glLightfv
(GL_LIGHT0, GL_DIFFUSE,


whiteFull
);


glLightfv
(GL_LIGHT0, GL_POSITION,


lightPos
);



GLfloat

grey[] =


{0.1f, 0.1f, 0.1f, 1.0f};


glMaterialfv
(GL_FRONT, GL_DIFFUSE,


grey); //reflect 10% diffuse


glMaterialfv
(GL_FRONT, GL_AMBIENT,


whiteFull
);}//reflect full ambient

COMP 175 | COMPUTER GRAPHICS

Remco Chang

22
/28

05


OpenGL

int
main( int
argc
, char**
argv

) {


glutInit
( &
argc
,
argv

); // Boilerplate
initialization


glutInitDisplayMode
( GLUT_RGB | GLUT_DEPTH
);




glutInitWindowPosition
( 50, 50
); //upper left


glutInitWindowSize
( 640, 480 ); // width, height, in pixels


glutCreateWindow
( "OpenGL Example" ); // window
title



glViewport
(



/* lower left
corner of the viewport */ 0, 0,



/* width, height of the viewport */ 640, 480 ); //lower left


glShadeModel
( GL_SMOOTH );


glPolygonMode
(
GL_FRONT
,
GL_FILL );


setupLighting
();


setupCamera
(
640, 480
);


registerCallBacks
();


glutMainLoop
();

}


Example OpenGL Application

void
setupCamera
(int w, int h) {


glMatrixMode

(GL_PROJECTION);




glLoadIdentity
();




//left, right, bottom, top


gluOrtho
(
-
1, 1,
-
1, 1);



//same as: (adds near, far)


//
glOrtho
(
-
1,1,
-
1,1,
-
1,1);



//or perspective transform


//
gluPerspective
(


// 45, //y
-
axis field of view


// ((float)w/(float)h),


// ratio of FOV(x) to FOV(y)


// 0.02,


//distance to near clip plane


// 1000


//distance to far clip plane


// );



glMatrixMode

(GL_MODELVIEW);


glLoadIdentity
();

}

COMP 175 | COMPUTER GRAPHICS

Remco Chang

23
/28

05


OpenGL

int
main( int
argc
, char**
argv

) {


glutInit
( &
argc
,
argv

); // Boilerplate
initialization


glutInitDisplayMode
( GLUT_RGB | GLUT_DEPTH
);




glutInitWindowPosition
( 50, 50
); //upper left


glutInitWindowSize
( 640, 480 ); // width, height, in pixels


glutCreateWindow
( "OpenGL Example" ); // window
title



glViewport
(



/* lower left
corner of the viewport */ 0, 0,



/* width, height of the viewport */ 640, 480 ); //lower left


glShadeModel
( GL_SMOOTH );


glPolygonMode
(
GL_FRONT
,
GL_FILL );


setupLighting
();


setupCamera
(640, 480);


registerCallBacks
();


glutMainLoop
();

}


Example OpenGL Application

void
registerCallbacks
() {


glutReshapeFunc

(
myReshape
);


glutKeyboardFunc

(
myKeyboard
);


glutMouseFunc

(
myMouseClick
);


glutMotionFunc

(
myMouseMove
);


glutDisplayFunc

(
myDisplay
);


glutIdleFunc

(
myIdle
);

}

COMP 175 | COMPUTER GRAPHICS

Remco Chang

24
/28

05


OpenGL

void myReshape(GLint width, GLint height) {


//called when the window is resized



glViewport(0, 0, width, height);


setupCamera(width, height);

}


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


//called on keyboard event




switch (key) {


case ‘a’:



someGlobalVariable = 1;


:


:


}

}

Example OpenGL Application

void
registerCallbacks
() {


glutReshapeFunc

(
myReshape
);


glutKeyboardFunc

(
myKeyboard
);


glutMouseFunc

(
myMouseClick
);


glutMotionFunc

(
myMouseMove
);


glutDisplayFunc

(
myDisplay
);


glutIdleFunc

(
myIdle
);

}

COMP 175 | COMPUTER GRAPHICS

Remco Chang

25
/28

05


OpenGL

v
oid
myMouseClick
(int button, int state, int x, int y) {


//called when a mouse click occurs



if (button == GLUT_LEFT_BUTTON) {


if (state == GLUT_DOWN) {


globalVariableMouseDown

= true;



}


else {


globalVariableMouseDown

=
false;


}


}

}


v
oid
myMouseMove

(int x, int y) {



//
called when a mouse
move occurs



if (
globalVariableMouseDown
==true)



{


//dragging occurs



}

}


Example OpenGL Application

void
registerCallbacks
() {


glutReshapeFunc

(
myReshape
);


glutKeyboardFunc

(
myKeyboard
);


glutMouseFunc

(
myMouseClick
);


glutMotionFunc

(
myMouseMove
);


glutDisplayFunc

(
myDisplay
);


glutIdleFunc

(
myIdle
);

}

COMP 175 | COMPUTER GRAPHICS

Remco Chang

26
/28

05


OpenGL

v
oid display(void) {


//called when the window needs to paint itself






glClear

(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



//draw some stuff...



glutSwapBuffers
();



numberOfFrames
++;

}


v
oid
myIdle

(void) {



//called ALL THE TIME!!




DWORD
time_now

=
GetTickCount
();


float diff
= (float
)(
time_now
-
last_check_time
)/1000.0;



if (diff > 1.0) {


float
frameRate

=
numberOfFrames

/ (float)diff;



numberOfFrames

= 0;


last_check_time

=
time_now
;




}


glutPostRedisplay
();

}


Example OpenGL Application

void
registerCallbacks
() {


glutReshapeFunc

(
myReshape
);


glutKeyboardFunc

(
myKeyboard
);


glutMouseFunc

(
myMouseClick
);


glutMotionFunc

(
myMouseMove
);


glutDisplayFunc

(
myDisplay
);


glutIdleFunc

(
myIdle
);

}

COMP 175 | COMPUTER GRAPHICS

Remco Chang

27
/28

05


OpenGL

int
main( int
argc
, char**
argv

) {


glutInit
( &
argc
,
argv

); // Boilerplate
initialization


glutInitDisplayMode
( GLUT_RGB | GLUT_DEPTH
);




glutInitWindowPosition
( 50, 50
); //upper left


glutInitWindowSize
( 640, 480 ); // width, height, in pixels


glutCreateWindow
( "OpenGL Example" ); // window
title



glViewport
(



/* lower left
corner of the viewport */ 0, 0,



/* width, height of the viewport */ 640, 480 ); //lower left


glShadeModel
( GL_SMOOTH );


glPolygonMode
(
GL_FRONT
,
GL_FILL );


setupLighting
();


setupCamera
(
640, 480
);


registerCallBacks
();


glutMainLoop
();

}


Example OpenGL Application

No turning back at
this point, we enter
the infinite loop!

COMP 175 | COMPUTER GRAPHICS

Remco Chang

28
/28

05


OpenGL





Questions?