1 / 55

Lecture 05: OpenGL 2 Fixed-Function

hamlet
Download Presentation

Lecture 05: OpenGL 2 Fixed-Function

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Lecture 05: OpenGL 2 (Fixed-Function) COMP 175: Computer Graphics September 19, 2011

    2. 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…

    3. 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

    4. 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

    5. 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

    6. 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

    7. 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

    8. 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

    9. 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…

    10. 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?

    11. 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?

    12. 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?

    13. How many parameters do you have to pass to render an object? void render () { obj1->drawSelf(myMouse, myScreenSize, myTimer, ...); obj2->drawSelf(myMouse, myScreenSize, myTimer, ...); obj3->drawSelf(myMouse, myScreenSize, myTimer, ...); } How do you enforce that there is only one instance of myMouse, myScreenSize, myTimer in code? For Example

    14. Use global variables Use mutual references. For example: Possible Answers?

    15. Problems: Need to guarantee a single instance of the states Don’t want to pass a billion parameters during the execution of every child object Don’t want the child to have to call parent->parent->parent->parent->getProperties(); One possible answer is to use Singletons Possible Answers?

    16. 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?

    17. Thread A Thread B void drawRedDashedTriangle() { pushAttributes(); setColor (RED); setStyle (DASHED); drawLine (pt1+d, pt2+d); drawLine (pt2+d, pt3+d); drawLine (pt3+d, pt1+d); d++; popAttributes(); } void drawBlueSolidTriangle() { pushAttributes(); setColor (BLUE); setStyle (SOLID); drawLine (pt4-p, pt5-p); drawLine (pt5-p, pt6-p); drawLine (pt6-p, pt4-p); p--; popAttributes(); } Multi-Threading and OpenGL

    18. Thread A Thread B void drawRedDashedTriangle() { mutex.lock(); pushAttributes(); setColor (RED); setStyle (DASHED); drawLine (pt1+d, pt2+d); drawLine (pt2+d, pt3+d); drawLine (pt3+d, pt1+d); d++; popAttributes(); mutex.unlock(); } void drawBlueSolidTriangle() { mutex.lock(); pushAttributes(); setColor (BLUE); setStyle (SOLID); drawLine (pt4-p, pt5-p); drawLine (pt5-p, pt6-p); drawLine (pt6-p, pt4-p); p--; popAttributes(); mutex.unlock(); } Multi-Threading and OpenGL

    19. Questions?

    20. Typical OpenGL Application

    21. 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

    22. 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

    23. int main( int argc, char** argv ) { glutInit( &argc, argv ); // Boilerplate initialization glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); Example OpenGL Application

    24. 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

    26. 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

    27. 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

    28. 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

    29. 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

    30. 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

    31. 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

    32. void 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; } } } void myMouseMove (int x, int y) { //called when a mouse move occurs if (globalVariableMouseDown==true) { //dragging occurs } } Example OpenGL Application

    33. void display(void) { //called when the window needs to paint itself glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawMyStuff(); glutSwapBuffers(); numberOfFrames++; } void 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

    34. 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

    35. Typical OpenGL Application

    36. Questions?

    37. void drawMyStuff(void) { glBegin (...); //glColor3f(1.0f, 1.0f, 1.0f); //glNormal3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f); glVertex3f(1.0f, 2.0f, 1.0f); : : glEnd(); } Drawing Primitives

    38. GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP GL_POLYGON OpenGL Primitives

    39. GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP GL_POLYGON OpenGL Primitives

    40. GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP GL_POLYGON OpenGL Primitives

    41. GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP GL_POLYGON OpenGL Primitives

    42. void glFrontFace (GLenum  mode) mode can be either GL_CW or GL_CCW GL_CCW is the default Ordering of Vertices

    43. GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP GL_POLYGON OpenGL Primitives

    44. GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP GL_POLYGON OpenGL Primitives

    45. GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP GL_POLYGON OpenGL Primitives

    46. GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP GL_POLYGON OpenGL Primitives

    47. GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP GL_POLYGON OpenGL Primitives

    48. glColor3f (1.0, 0.0f, 0.0f); glBegin(GL_TRIANGLES); glVertex3f(-1.0f, -0.5f, -4.0f); // A glVertex3f( 1.0f, -0.5f, -4.0f); // B glVertex3f( 0.0f, 0.5f, -4.0f); // C glEnd(); Color Blending

    49. glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -0.5f, -4.0f); // A glColor3f(0.0f, 1.0f, 0.0f); glVertex3f( 1.0f, -0.5f, -4.0f); // B glColor3f(0.0f, 0.0f, 1.0f); glVertex3f( 0.0f, 0.5f, -4.0f); // C glEnd(); Color Blending

    50. glShadeModel( GL_FLAT ); glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -0.5f, -4.0f); // A glColor3f(0.0f, 1.0f, 0.0f); glVertex3f( 1.0f, -0.5f, -4.0f); // B glColor3f(0.0f, 0.0f, 1.0f); glVertex3f( 0.0f, 0.5f, -4.0f); // C glEnd(); GL Shade Model

    51. The direction where the face of the triangle points towards Unit vector Or call ( glEnable(GL_NORMALIZE); ) GL Normal

    52. These two have the same number of quads… What is the difference? GL Normal

    53. GL Normal

    54. glNormal3f(nx, ny, nz); glBegin(GL_QUADS); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); glVertex3f(x4, y4, z4); glEnd(); GL Normal

    55. glBegin(GL_QUADS); glNormal3f(nx1, ny1, nz1); glVertex3f(x1, y1, z1); glNormal3f(nx2, ny2, nz2); glVertex3f(x2, y2, z2); glNormal3f(nx3, ny3, nz3); glVertex3f(x3, y3, z3); glNormal3f(nx4, ny4, nz4); glVertex3f(x4, y4, z4); glEnd(); GL Normal

    56. Questions?

More Related