CSC 315-LECTURE4
Transcript of CSC 315-LECTURE4
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 1/43
INTRODUCTION TO OPENGLy
OpenGL is Open Graphics Library in fully It is a software interface to graphics hardware. This
interface consists of about 120 distinct commands,
which you use to specify the objects andoperations needed to produce interactive three-
dimensional applications.
y
It supports raster graphics library (accepting thevertices, normals and other scene data) and its an
industry standard software (specification is
publicly avalable and are supported across many
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 2/43
OpenGL Architecture
yOpenGL uses a client-server model for its
operation. The client and the server may or may
not be the same computer.y In a network environment, the computer on which
your program runs and issues OpenGL drawing
commands is called the client, and the computerthat receives those commands and performs the
drawing is called the server.
yProtocol is the format for transmitting these cmds
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 3/43
OpenGL Architecture
yThe client sends commands to the server, the
server interprets, processes commands and sends
the output to the clientyAn example of the interaction:
Client
(OpenGL Program)
Server
(OpenGL/GPU)
begin trianglenormal (0, 0, -1)
vertex (-1, 1, -1, 1)
vertex (1, -1, -1, 1)
vertex (-1, -1, -1, 1)
end triangle
<scan converts the given
triangle with normal (0,0,-1)
on all vertices>
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 4/43
OpenGL Architecture
yOpenGL is both state-full and procedural
yState-full in that the current OpenGL state
(collectively called a context) contains datadescribing how rendering should proceed
yEg. current color, clip planes, lights, textures etc
yThe state does not change until explicitly sety once some state is set, it remains in effect until
changed making us have precise control over the
GPU
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 5/43
OpenGL Architecture
yProcedural model
y usually accessed through a plain C API
yNOT object-oriented at all
yCan be cumbersome if you are used to working
with object-oriented systems
y one line can affect how all subsequent codeexecutes
y sometimes difficult to encapsulate functionality;
you have to be mindful of default state as well
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 6/43
OpenGL Contexts
yA context is basically an "instance" of OpenGL.
yEach context you create contains its own set of GL
State as well its own buffers for renderingyOne context is ´currentµ. Only the current
context is affected by GL calls
y In general, if you have multiple windows in whichyou want to do GL rendering, you'll create a
separate GL context for each.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 7/43
OpenGL Contexts
RenderingSurface
Context
Buffers (e.g.Framebuffer)
Render State (e.g.lighting, blending)
Objects (e.g.Textures)
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 8/43
OpenGL Contexts
yOpenGL contexts are NOT threadsafe
y If you try to manipulate the same GL context from
two threads, bad things will happen.yHow then, can we write multithreaded OpenGL?
This is obviously very important for the kind of
apps we would want to use OpenGL for.yOne Answer : Devote one thread to all things
OpenGL and don·t make any GL calls outside of
that thread.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 9/43
OpenGL Contexts
yProblem with that: Some GL calls are expensiveand not related to rendering. For example, in this
model, you must use the same thread for
rendering (needs to happen many times a second)and texture creation (can be very slow).
yBetter Answer : Shared Contexts. Shared contexts
allow certain states (e.g. texture objects) to beshared across multiple contexts. Any decent
OpenGL implementation has some notion of
shared contexts
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 10/43
OpenGL Context
y In this example, we can use the Worker thread to
create textures, leaving the Render thread free to
render all day longWorker Thread
Context
Render Thread
Context
Buffers,unshared state
Shared State(e.g Textures)
Buffers,unshared state
Shared
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 11/43
OpenGL State
yWhen it comes time to render to the screen, there
are a lot of things which determine how the result
looks. OpenGL asks itself several questions beforerendering anything«
yIs lighting enabled?
y
Is texturing enabled? Which should I apply to thisprimitive?
yShould back facing triangles be culled?
yWhich blending function should I use?, etc
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 12/43
OpenGL State
yThe answers to all of these questions are found in
the configuration of the render state, which is set
by the programmeryThere are many, many pieces of state which we can
configure to affect the results of rendering
ySome states are simple binary values, i.e on or off.
y ´Is lighting enabled?µ is a yes/no question whose
answer is found in the binary state
´GL_LIGHTINGµ
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 13/43
OpenGL State
yWe typically modify such state with the functions
glEnable and glDisable, passing the state we wish
to modify as an argument.glEnable(GL_LIGHTING);
// turns lighting on
glDisable(GL_DEPTH_TEST);// turns depth testing off
yNot all state is as simple as a simple on or off,
we·ll delve into a particular example, blending.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 14/43
OpenGL State: Blending
yWhen GL is marching along, drawing things to the
framebuffer, it may have to draw again on a pixel
that already has some color.yWe can use depth testing to simply throw away the
deeper color.
yOr we can use bl endingTo enable GL·s blending mechanism, we make a
call to glEnable, which you·ve already seen,
passing it the symbolic constand GL_BLEND.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 15/43
OpenGL State: Blending
glEnable(GL_BLEND);// blending is now
´switched onµ
Of course there are a lot of ways we could chooseto blend and we can specify exactly how we want
it to happen using the function glBlendFunc.
glBlendFunc takes two parameters.y The first parameter tells GL how to compute a
blending coefficient for the source color (the
current color of the pixel).
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 16/43
OpenGL State: Blending
y The second parameter tells the GL how to
compute the blending coefficient for the
destination color (the color being drawn).Hence the final pixel color will be (srcCoeff *
srcColor) + (dstCoeff * dstColor)
We could tell GL to weight both colors equally«glBlendFunc(GL_ONE, GL_ONE);
The symbol ic constant GL_ ONE tell s GL to simpl y use
1.0 as the bl ending factor
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 17/43
OpenGL State: Blending
Or perhaps use each color·s alpha value«glBlendFunc(GL_SRC_ALPHA,
GL_DST_ALPHA);
GL provides lots of more complicated blendingfunctions as well
yWe·ve been seeing a lot of what are known as
symbolic constants in our dealings with renderstate. Those ugly, capitalized symbols that begin
with ́ GL_µ, E.g. GL_LIGHTING, GL_ONE,
GL_SRC_ALPHA
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 18/43
OpenGL State: Blending
y
Symbolic constants are an essential part of themechanism we have for interacting with OpenGL,
and in particular, manipulating render state.
y
The symbolic constant GL_LIGHTINGrepresents the lighting render state
yThe symbolic constant GL_BLEND represents
the blending render stateyThe symbolic constants GL_ONE,
GL_SRC_ALPHA, and GL_DST_ALPHA are
used to configure the blending render state
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 19/43
OpenGL State: Blending
y
At the end of the day, all symbolic constants aredefined as enums, so they·re all basically ints.
yThis means we can write stupid things like«
glBlendFunc(GL_LIGHTING, GL_TEXTURE);
and our code will compile. However, a call to
glGetError would reveal an ´Invalid Enumerationµ
as a result of the previous line.This is GL·s way of saying
´ That symbol ic constant makes no sense w here you used itµ.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 20/43
OpenGL State: Blending
y
Why do things this way? It·s ugly and hard toremember all those constants
yRemember when we said OpenGL is a C API?
Later versions of OpenGL are supposed to bemore object oriented, but for now, this is how it
works.
y
This would however, make you control thestructure of all objects you intend to render and
make necessary renderings according to your
interest
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 21/43
A Very Simple OpenGL Program
y
Those few commands and what they do give youan idea of the kinds of things you can do with the
OpenGL graphics system.
y
The next several slides briefly describe the orderin which OpenGL performs the major graphics
operations necessary to render an image on the
screen.yGenerally to render an image on the screen, the
following steps are followed and outlined in the
simple OpenGL program below ;
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 22/43
Procedures
1. Construct shapes from geometric primitives,thereby creating mathematical descriptions of
objects. (OpenGL considers points, lines,
polygons, images, and bitmaps to beprimitives.)
2. Arrange the objects in three-dimensional space
and select the desired vantage point for viewingthe composed scene.
3. Calculate the color of all the objects. The color
might be explicitly assigned by the application,
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 23/43
Procedures
determined from specified lighting conditions,or obtained by pasting a texture onto the
objects.
4. Convert the mathematical description of objects and their associated color information
to pixels on the screen. This process is called
rasterization.During these stages, OpenGL might perform other
operations, such as eliminating parts of objects that
are hidden by other objects (the hidden parts won't
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 24/43
Procedures
be drawn, which might increase performance). Inaddition, after the scene is rasterized but just before
it's drawn on the screen, you can manipulate the
pixel data if you want.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 25/43
A Very Simple OpenGL Program
y
Because you can do so many things with theOpenGL graphics system, an OpenGL program
can be complicated. However, the basic structure
of a useful program can be simple: Its tasks are toinitialize certain states that control how OpenGL
renders and to specify objects to be rendered.
y
Before you look at an OpenGL program, let's goover a few terms.
yRendering, which you've already seen used, is
the process by which a computer creates images
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 26/43
A Very Simple OpenGL Program
y
from models. Thesemodels
, or objects, areconstructed from geometric primitives - points,
lines, and polygons - that are specified by their
vertices.yThe final rendered image consists of pixels drawn
on the screen;A pixel - short for picture element
- is the smallest visible element the displayhardware can put on the screen. Information about
the pixels (for instance, what color they're
supposed to be) is organized in system memory
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 27/43
A Very Simple OpenGL Program
y
into bitplanes/bitmap.A bitplane is an area of memory that holds one bit of information for
every pixel on the screen; the bit might indicate
how red a particular pixel is supposed to be, forexample. The bitplanes are themselves organized
into a f ramebuff er, which holds all the
information that the graphics display needs tocontrol the intensity of all the pixels on the screen.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 28/43
A Very Simple OpenGL Program
y
Now look at an OpenGL program. The imagerenders a white rectangle on a black background,
as shown in figure below.
yA White Rectangle on a Black Background
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 29/43
A Very Simple OpenGL Program
#include <whateverYouNeed.h>
main() {
OpenAWindowPlease();
glClearColor(0.0, 0.0, 0.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 30/43
A Very Simple OpenGL Program
glVertex2f(-0.5, 0.5);glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();glFlush();
KeepTheWindowOnTheScreenForAWhile();
}
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 31/43
A Very Simple OpenGL Program
The first line of themain() routine opens a windowon the screen: The OpenAWindowPlease()
routine is meant as a placeholder for a window
system-specific routine. The next two lines areOpenGL commands that clear the window to
black: glClearColor() establishes what color the
window will be cleared to, and glClear() actuallyclears the window. Once the color to clear to is
set, the window is cleared to that color whenever
glClear() is called.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 32/43
A Very Simple OpenGL Program
The clearing color can be changed with another callto glClearColor(). Similarly, the glColor3f ()
command establishes what color to use for
drawing objects - in this case, the color is white.All objects drawn after this point use this color,
until it's changed with another call to set the color.
The next OpenGL command used in the program,glOrtho(), specifies the coordinate system
OpenGL assumes as it draws the final image and
how the image gets mapped to the screen.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 33/43
A Very Simple OpenGL Program
The next calls, which are bracketed by glBegin()and glEnd(), define the object to be drawn - in
this example, a polygon with four vertices. The
polygon's "corners" are defined by theglVertex2f () commands. As you might be able to
guess from the arguments, which are (x, y )
coordinate pairs, the polygon is a rectangle.Finally, glFlush() ensures that the drawing
commands are actually executed, rather than
stored in a buffer awaiting additional OpenGL
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 34/43
A Very Simple OpenGL Program
commands. TheKeepTheWindowOnTheScreenForAWhile(
) placeholder routine forces the picture to remain
on the screen instead of immediately disappearing.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 35/43
OpenGL Command Syntax
As you might have observed from the simpleprogram in the previous section, OpenGL
commands use the prefix gl and initial capital
letters for each word making up the commandname (recall glClearColor(), for example).
Similarly, OpenGL defined constants begin with
GL_, use all capital letters, and use underscores toseparate words (like
GL_COLOR_BUFFER_BIT).
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 36/43
OpenGL Command Syntax
You might also have noticed some seeminglyextraneous letters appended to some command
names (the 3f in glColor3f (), for example). It's
true that the Color part of the command name isenough to define the command as one that sets the
current color. However, more than one such
command has been defined so that you can usedifferent types of arguments. In particular, the 3
part of the suffix indicates that three arguments
are given; another version of the Color command
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 37/43
OpenGL Command Syntax
And four arguments. The f part of the suffixindicates that the arguments are floating-point
numbers. Some OpenGL commands accept as
many as eight different data types for theirarguments. The letters used as suffixes to specify
these data types for ANSI C implementations of
OpenGL are shown in Table below, along with thecorresponding OpenGL type definitions. The
particular implementation of OpenGL that you're
using might not follow this scheme exactly; an
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 38/43
OpenGL Command Syntax
implementation in C++ or Ada, for example,wouldn't need to.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 39/43
OpenGL Command SyntaxTable : Command Suffixes and Argument Data Types
Suffix Data Type Typical CorrespondingC-Language Type
OpenGLTypeDefinition
b 8-bit integer signed char GLbyte
s 16-bit integer short GLshort
i 32-bit integer long GLint, GLsizei
f 32-bit floating-point float GLfloat, GLclampf
d 64-bit floating-point double GLdouble,
GLclampd
ub 8-bit unsigned integer unsigned char GLubyte,
GLboolean
us 16-bit unsigned integer unsigned short GLushort
ui 32-bit unsigned integer unsigned long GLuint, GLenum,
GLbitfield
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 40/43
OpenGL Command Syntax
y
Thus, the two commands;yglVertex2i(1, 3); and glVertex2f(1.0, 3.0); are
equivalent, except that the first specifies the
vertex's coordinates as 32-bit integers and thesecond specifies them as single-precision floating-
point numbers.
y
Some OpenGL commands can take a final letter v,which indicates that the command takes a pointer
to a vector (or array) of values rather than a series
of individual arguments. Many commands have
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 41/43
OpenGL Command Syntax
y
both vector and non-vector versions, but somecommands accept only individual arguments and
others require that at least some of the arguments
be specified as a vector. The following lines showhow you might use a vector and a non-vector
version of the command that sets the current
color:yglColor3f(1.0, 0.0, 0.0);
y float color_array[] = {1.0, 0.0, 0.0};
yglColor3fv(color_array);
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 42/43
OpenGL Command Syntax
In the rest of our lecture (except in actual codeexamples), OpenGL commands are referred to by
their base names only, and an asterisk is included
to indicate that there may be more to thecommand name.
For example, glColor*() stands for all variations of
the command you use to set the current color. If we want to make a specific point about one
version of a particular command, we include the
suffix necessary to define that version.
8/2/2019 CSC 315-LECTURE4
http://slidepdf.com/reader/full/csc-315-lecture4 43/43
OpenGL Command Syntax
For example, glVertex*v
() refers to all the vectorversions of the command you use to specify
vertices.
Finally, OpenGL defines the constant GLvoid; if you're programming in C, you can use this instead
of void.