Docsity

OpenGL: Understanding State Variables and Drawing Triangles, Slides of Computer Graphics

Partial preview of the text

Download OpenGL: Understanding State Variables and Drawing Triangles and more Slides Computer Graphics in PDF only on Docsity!

Lecture 04:

OpenGL (Fixed-Function)

 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

 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?

 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?

 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

 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

 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?

 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?

Questions?

Typical OpenGL Application

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?

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

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

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