Download Conceptual Graphics Framework - Introduction to Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity!
Lecture 05:
OpenGL 2 (Fixed-Function)
Remember…
• 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.
Graphics System/ GPU Application Model/data base
Software Hardware
Application
program
Graphics
Library
State Variables
• 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) );
What color and shape?
What color and shape?
What color and shape?
State Variables
• 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) );
What color and shape?
What color and shape?
What color and shape?
State Variables – Pros and Cons
• 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);
}
Forgetting the State?
• 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)
For Example
• 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?
Possible Answers?
1. Use global variables
2. Use mutual references. For example:
void setChild (CWidget* child)
{
myChild = child; child->setParent(this);
}
MyMouse* getMouse() {
return myMouse;
}
void setParent (PWidget* parent) { myParent = parent; }
void doStuff () { MyMouse mouse = myParent->getMouse(); doStuffWithMouse (mouse); }
Programming Strategies?
• 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?
Multi-Threading and OpenGL
Thread A
void drawRedDashedTriangle()
pushAttributes();
setColor (RED);
setStyle (DASHED);
drawLine (pt1+d, pt2+d);
drawLine (pt2+d, pt3+d);
drawLine (pt3+d, pt1+d);
d++;
popAttributes();
Thread B
void drawBlueSolidTriangle()
pushAttributes();
setColor (BLUE);
setStyle (SOLID);
drawLine (pt4-p, pt5-p);
drawLine (pt5-p, pt6-p);
drawLine (pt6-p, pt4-p);
p--;
popAttributes();
Typical OpenGL Application
Example
• 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.)