Introduction to OpenGL: A 2D & 3D Graphics Library, Study notes of Computer Graphics

An introduction to opengl, a standard library for 2d and 3d drawing. Opengl maps directly to graphics hardware, has a state-based architecture, and uses immediate mode drawing. This guide explains why opengl is used, decodes function names, and covers the structure of an opengl program. It also discusses event-driven interactive programs and the differences between immediate and retained mode rendering.

Typology: Study notes

Pre 2010

Uploaded on 03/16/2009

koofers-user-46c
koofers-user-46c 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to OpenGL
OpenGL is a standard library for 2-D & 3-D drawing
maps fairly directly to graphics hardware
state-based architecture
immediate mode drawing
doesn’t address windows or input events (we’ll use FLTK)
Why we are using OpenGL
clean design
originally by SGI, but enjoys broad cross-platform support
a free software implementation (www.mesa3d.org)
Decoding OpenGL function names
general rule: glFunc[234][dfis][v]
example: glVertex3f(x,y,z) — vertex position as 3 floats
dimension type (e.g., i=int)
v = pass pointer rather than values
Overview of an OpenGL Program
System-dependent initialization
setup a window on the screen
bind OpenGL to this window
OpenGL initialization
setup coordinate system
setup initial state values
Begin event loop
wait for system to deliver event (e.g., mouse moved)
figure out how this will change the scene, if at all
if redraw event, execute OpenGL commands to draw scene
Event Driven Interactive Programs
This is the model for modern GUI programs
run some initialization code at startup
everything after that happens in response to events
Typical events
window resized
redraw window
mouse moved
button clicked
key pressed
•…
Most frameworks use callbacks
procedures bound to specific events
for instance, call function when a button is pressed
main()
{
InitializeApp();
while( !done ) {
event = WaitForOne();
handle(event);
}
}
OpenGL Uses Immediate Mode Rendering
Execute drawing commands every time window is repainted
or every time the scene changes (e.g., for animation)
vs. retained mode where system maintains model of scene
OpenGL does provide some retained mode features
display lists — capture & store immediate mode stream
vertex arrays — pass large array of points in one call
Immediate Mode:
void initialize() {}
void on_redraw() {
for(all triangles t)
draw_triangle(t);
}
Retained Mode:
void initialize()
{
load_triangle_list(T);
}
void on_redraw() {}
pf3
pf4
pf5

Partial preview of the text

Download Introduction to OpenGL: A 2D & 3D Graphics Library and more Study notes Computer Graphics in PDF only on Docsity!

Introduction to OpenGL OpenGL is a standard library for 2-D & 3-D drawing^ • maps fairly directly to graphics hardware• state-based architecture• immediate mode drawing• doesn’t address windows or input events (we’ll use FLTK) Why we are using OpenGL^ • clean design• originally by SGI, but enjoys broad cross-platform support• a free software implementation (www.mesa3d.org) Decoding OpenGL function names^ • general rule: gl

Func [234][dfis][v] dimension • example: glVertex3f(x,y,z) — vertex position as 3 floats

v = pass pointer rather than values type (e.g., i=int)

Overview of an OpenGL Program System-dependent initialization^ • setup a window on the screen• bind OpenGL to this window OpenGL initialization^ • setup coordinate system• setup initial state values Begin event loop^ • wait for system to deliver event (e.g., mouse moved)• figure out how this will change the scene, if at all• if redraw event, execute OpenGL commands to draw scene

Event Driven Interactive Programs This is the model for modern GUI programs^ • run some initialization code at startup• everything after that happens in response to events Typical events^ • window resized• redraw window• mouse moved• button clicked• key pressed• … Most frameworks use callbacks^ • procedures bound to specific events• for instance, call function when a button is pressed

main(){ InitializeApp();while( !done ) {event = WaitForOne();handle(event);}}

OpenGL Uses Immediate Mode Rendering^ Execute drawing commands every time window is repainted^ • or every time the scene changes (e.g., for animation)• vs. retained mode where system maintains model of scene^ Immediate Mode:^ void^ initialize()^ OpenGL does provide some retained mode features^ • display lists — capture & store immediate mode stream• vertex arrays — pass large array of points in one call

void^ on_redraw()

for(all^ triangles

t) draw_triangle(t);}

Retained Mode: void^ initialize(){ load_triangle_list(T);} void^ on_redraw()

Extensive State Information Is always a “current” state, initialized with default values^ • changes to the state apply to all subsequent operations^ set_color(Red);draw_triangle(t1); Examples of state information include^ • current drawing color, line width, point size, …• coordinate system (defined by transformation matrix)• enabled features: depth tests, alpha blending, lighting, …

//^ red draw_triangle(t2);

//^ red draw_triangle(t3);

//^ red set_color(Blue);draw_triangle(t4);

//^ blue

Setting Up the Drawing Canvas First, we need to define the coordinate system^ • specifies how to map vertex coordinates onto window• for now, we’re focusing on 2-D — gluOrtho2D(

left ,^ right ,^ bottom

,^ top )

Example: gluOrtho2D(-1, 1, -1, 1)^ -^

OpenGL coordinates

w Window Pixel Coordinatesh (^1) -

Setting Up the Drawing Canvas Must always remember to clear canvas before drawing^ • glClearColor(

r ,^ g ,^ b ,^ α) – specify the color to clear the canvas to– should generally set^ α^ to be 0 (i.e., fully transparent)– this is a state variable, and can be done only once• glClear(GL_COLOR_BUFFER_BIT)– actually clears the screen– there are other things that can be cleared at the same time– such as the depth buffer GL_DEPTH_BUFFER_BIT– but we’re not using it right now And always set the current color before you start drawing^ • for example, glColor3f(0.8, 0.2, 0.2) for a red shade

Geometric Primitives Three common modeling primitives^ • Points• Line segments• Filled polygons These are very convenient^ • the math is simple — just linear equations• direct hardware support — they’re efficient• universal support — all graphics software understands them Primitives always bracketed by calls to glBegin() … glEnd()^ • argument to glBegin() determines primitive type• For example, glBegin(GL_POLYGONS)

Common Simplification: Triangles A particular special case of polygon with several advantages^ • number of vertices is always fixed• triangles in 3-D are necessarily planar• they must also always be convex• these make it particularly attractive for hardware design Can convert any planar polygon into exactly equivalenttriangles^ • trivial if polygon is convex: connect all vertices to a point ofinterior• requires more sophisticated algorithms for general polygons• but can still be done efficiently

Plane Equations in 3-D Triangles define a unique plane in 3-D^ • set of all points satisfying equation• vector^ [^ a b c

]^ is the plane normal• hence perpendicular to the triangle• typically use unit normal vector ax^ by^ Normals will show up again and again • especially in rendering cz^ d + + +^ = 0 (^2 2 2) a b c + + = 1

p^1

p^2

p^3

a ⎡ ⎤ ⎢ ⎥ b = n ⎢ ⎥ ⎢ ⎥ c ⎣ ⎦

An Object Modeled with Triangles

Generating Animation We create animated behavior just like movie projectors^ • display a sequence of still images in rapid succession• creates the illusion of continuous motion• around 10 Hz is the minimum acceptable rate• film projectors generally run at 24 Hz• broadcast and video are usually at 30 Hz Assuming our redraws are fast, it’s easy to program^ • set up a system timer to go off every 1/n of a second• generate a redraw event when the timer goes off Immersive applications must pay close attention to frame rate^ • flight simulators are a good example• inconsistent frame rates lead to incredible nausea

Single vs. Double Buffering Single Buffering^ • draw into same frame bufferbeing used to generate videosignal• can draw on top of existingimage if appropriate• tends to lead to flickering

Double Buffering^ • draw in separate frame buffer• front buffer — for video signal• back buffer — drawing target• must redraw entire sceneevery frame• must swap buffers whendrawing is finished• avoids flickering