Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
OpenGL
JimmyJohansson
C‐Research
LinköpingUniversity
Background
• So?wareinterfacetographicshardware• 250+commands• Objects(models)arebuiltfromgeometricprimiKves
• Points,lines,trianglesandpolygons
• CurrentVersions• 2.x• 3.x
Background
• Portability• Fastrendering• Onlythecore
• Otherfeaturesareavailableelsewhere• GLU,GLUT,GLEW,GLUI
TheStateMachine
• Noobjectorienteddesign
• Statemachine• Setstatesandparameters
• Executecommands
• States• Transformmode• Renderingmode
• Errorstate
TheStateMachine
• glEnableandglDisable• e.g.:GL_LIGHTING,GL_DEPTH_TEST
• Errorcontrolandfeedback• glGetError
• Parameters• Colour,normal,texture,lighKng
APIConvenKons
• FuncKonnamesbeginwithglandusemixedcase:glBegin, glEnd, glFrontFace
• ConstantsbeginwithGL_anduseonlyuppercase:GL_TRIANGLES, GL_BACK, GL_LIGHT0
• TypesbeginwithGLanduseonlylowercase:GLbyte, GLubyte, GLint, GLfloat, ...
APIconvenKons:FuncKonnamesshowarguments
• GlVertex{234}{sifd}[v]() • glVertex3i(Glint x,Glint y,Glint z) • glVertex3fv(Glfloat *v)
• argumenttypes:• GLbyte,GLshort,GLint,GLfloat,GLdouble• unsignedbyte(ub),short(us),int(ui)
• vindicatesvector
ExamplefuncKonnames
• glVertex3f(1.0,2.0,3.142);
• glVertex3dv(dvec);• GLdoubledvec[3]={1.0,2.0,3.142};
GLPrimiKves• Points
GL_POINTS• Lines
GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP• Triangles
GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN
• QuadrilateralsandallotherpolygonsGL_QUADS, GL_QUAD_STRIP, GL_POLYGON
GLPrimiKves
Example
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0);
glEnd(); glFlush(); UpdateTheWindowAndCheckForEvents(); }
Example
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0);
glEnd(); glFlush(); UpdateTheWindowAndCheckForEvents(); }
Example
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0);
glEnd(); glFlush(); UpdateTheWindowAndCheckForEvents(); }
Example
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0);
glEnd(); glFlush(); UpdateTheWindowAndCheckForEvents(); }
Example
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0);
glEnd(); glFlush(); UpdateTheWindowAndCheckForEvents(); }
Example
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0);
glEnd(); glFlush(); UpdateTheWindowAndCheckForEvents(); }
Example
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0);
glEnd(); glFlush(); UpdateTheWindowAndCheckForEvents(); }
Example
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0);
glEnd(); glFlush(); UpdateTheWindowAndCheckForEvents(); }
Rendering
• Vertex‐by‐vertex• Begingeometrystate• PutverKces• Endgeometrystate
• Arrayofdata• Vertexpointer• Drawarrays• Drawelements
Vertexbyvertex
glBegin(GL_TRIANGLES); /* First Triangle */ glVertex3f( 1.0, 0.0, 0.0); glVertex3f(-0.5, 1.0, 0.5); glVertex3f( 0.0,-0.5,-0.2);
/* Second Triangle */ glVertex3f( 1.0,-0.4, 0.0); glVertex3f( 0.0, 0.6, 0.0); glVertex3f(-0.6,-0.2, 0.4);
glEnd();
glBegin(GL_QUAD_STRIP); /* First quad */ glVertex3f(-0.5,-0.5,-0.5); glVertex3f(-0.5, 0.5,-0.5); glVertex3f( 0.5,-0.5,-0.5); glVertex3f( 0.5, 0.5,-0.5);
/* Second */ glVertex3f( 0.5,-0.5, 0.5); glVertex3f( 0.5, 0.5, 0.5);
/* Third */ glVertex3f(-0.5,-0.5, 0.5); glVertex3f(-0.5, 0.5, 0.5);
/* Fourth */ glVertex3f(-0.5,-0.5,-0.5); glVertex3f(-0.5, 0.5,-0.5);
glEnd();
Vertexbyvertex
static GLfloat vertices[] = { 1.0, 0.0, 0.0, -0.5, 1.0, 0.5, 0.0,-0.5,-0.2, 1.0,-0.4, 0.0, 0.0, 0.6, 0.0, -0.6,-0.2, 0.4 };
glVertexPointer( 3, GL_FLOAT, 0, vertices ); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays( GL_TRIANGLES, 0, 6 ); glDisableClientState(GL_VERTEX_ARRAY);
ArrayofData
static GLfloat vertices[] = { 1.0, 0.0, 0.0, -0.5, 1.0, 0.5, 0.0,-0.5,-0.2, 1.0,-0.4, 0.0, 0.0, 0.6, 0.0, -0.6,-0.2, 0.4 };
static GLubyte indices[] = { 0, 1, 2, 3, 4, 5 };
glVertexPointer( 3, GL_FLOAT, 0, vertices ); glEnableClientState(GL_VERTEX_ARRAY); glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices );
glDisableClientState(GL_VERTEX_ARRAY);
ArrayofData
OpenGLBlockDiagram
FRAME BUFFER
RASTERIZ- ATION EVALUATOR
TEXTURE MEMORY
PIXEL OPS
PER-VERTEX OPS
PRIMITIVE ASSEMBLY
PER-FRAG OPS
OpenGLBlockDiagram
FRAME BUFFER
RASTERIZ- ATION EVALUATOR
TEXTURE MEMORY
PIXEL OPS
PER-VERTEX OPS
PRIMITIVE ASSEMBLY
PER-FRAG OPS
Handles parametric shapes, derives vertices from control points
OpenGLBlockDiagram
FRAME BUFFER
RASTERIZ- ATION EVALUATOR
TEXTURE MEMORY
PIXEL OPS
PER-VERTEX OPS
PRIMITIVE ASSEMBLY
PER-FRAG OPS
Convert vertices into primitives. Clipping. Convert vertices into primitives. Clipping.
OpenGLBlockDiagram
FRAME BUFFER
RASTERIZ- ATION EVALUATOR
TEXTURE MEMORY
PIXEL OPS
PER-VERTEX OPS
PRIMITIVE ASSEMBLY
PER-FRAG OPS
Unpacking, scaling, clamping, etc. of pixel data Unpacking, scaling, clamping, etc. of pixel data
OpenGLBlockDiagram
FRAME BUFFER
RASTERIZ- ATION EVALUATOR
TEXTURE MEMORY
PIXEL OPS
PER-VERTEX OPS
PRIMITIVE ASSEMBLY
PER-FRAG OPS
Converting pixel and geometric data into fragments. Converting pixel and geometric data into fragments.
OpenGLBlockDiagram
FRAME BUFFER
RASTERIZ- ATION EVALUATOR
TEXTURE MEMORY
PIXEL OPS
PER-VERTEX OPS
PRIMITIVE ASSEMBLY
PER-FRAG OPS
Texturing, scissor-, alpha-, depth tests, blending, etc.
RasterizaKon
• Fragments‐datanecessarytopoten&allyupdateapixelintheframebuffer• depth• interpolatedajributes
• color• texturecoords• etc
• alpha• Normal
• ConvertprimiKvesintofragments
RasterizaKon
• glHint• AnKaliasing,perspecKvecorrecKon
Blending
• Combinecolourvalueoffragmentwithpixelvaluestoredinframebuffer• Orderdependent
Transforms
• Viewtransform
• Modeltransform• ProjecKontransform
• Viewporttransform
ViewTransforms
• ViewPoint/Camera• glOrtho( L, R, B, T, N, F )
• GluPerspective (fovy, aspect, zNear, zFar )
ModelTransform
• Management• GlLoadMatrix/Identity • glPush/PopMatrix
• ManipulaKon• glTranslate • glRotate • glScale
TransformsPutTogether
• Model+Viewtransform=Modelviewtransform
• ProjecKontransform• Viewporttransform
Colours
• glColor,glClearColor
• RGB,RGBA,
• ExecutewithglVertex,glClear
RenderingwithColours
glBegin(GL_TRIANGLES);
glColor4f( 1.0, 0.0, 0.0, 1.0 ); glVertex3f( 1.0, 0.0, 0.0); glVertex3f(-0.5, 1.0, 0.5); glVertex3f( 0.0,-0.5,-0.2);
glColor4f( 0.0, 0.0, 1.0, 0.5 ); glVertex3f( 1.0,-0.4, 0.0); glVertex3f( 0.0, 0.6, 0.0); glVertex3f(-0.6,-0.2, 0.4);
glEnd();
RenderingwithColourArray
static GLfloat vertices[] = { 1.0, 0.0, 0.0, -0.5, 1.0, 0.5, 0.0,-0.5,-0.2, 1.0,-0.4, 0.0, 0.0, 0.6, 0.0, -0.6,-0.2, 0.4 }; static GLfloat colors[] = { 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.5, 0.0, 0.0, 1.0, 0.5, 0.0, 0.0, 1.0, 0.5 };
glVertexPointer( 3, GL_FLOAT, 0, vertices ); glColorPointer( 3, GL_FLOAT, 0, colors );
glEnableClientState(GL_VERTEX_ARRAY|GL_COLOR_ARRAY); glDrawArrays( GL_TRIANGLES, 0, 6 ); glDisableClientState(GL_VERTEX_ARRAY|GL_COLOR_ARRAY);
RenderingwithInterleavedArrays
static GLfloat colverts[] = { 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, -0.5, 1.0, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0,-0.5,-0.2, 0.0, 0.0, 1.0, 0.5, 1.0,-0.4, 0.0, 0.0, 0.0, 1.0, 0.5, 0.0, 0.6, 0.0, 0.0, 0.0, 1.0, 0.5, -0.6,-0.2, 0.4 };
glInterleavedArrays( GL_C4F_V3F, 0, colverts ); glDrawArrays( GL_TRIANGLES, 0, 6 ); glDisableClientState(GL_VERTEX_ARRAY|GL_COLOR_ARRAY);
Shading
• AcKvateshading• glShadeModel,flatorsmooth• Requiresnormalizednormals
• AutomaKcnormalizaKon• glEnable(GL_NORMALIZE) • Expensive!
SmoothShading
Textures
• Texturestates• Targets:1D,2D,3Dtexture
• glEnable / glDisable • glTexEnv{if}[v], egGL_TEXTURE_ENV_MODE • What happens when a fragment is textured
• Decal,replace,modulate,blend,add,combine• glTexParameter{if}[v]
• egborder,interpolaKon,level‐of‐details• Texturestatesets
• glGenTextures • glBindTexture
TextureData
• Loaddataintomemory• glTexImage{123}D / glTexSubImage{123}D
• Internalformat,dataformat,border,size,type,etc
TextureCoordinates
• Vertex‐by‐vertex• glTexCoord
• Vertexarrays• glTexCoordPointer
• AutomaKccoordinates• glTexGen
2DTextureExample
2DTextureCodeExample
2DTextureExample
2DTextureExample
LighKng
• glLightModel• EglocalorinfiniKvelyfaraway,twosidedlighKng
• Lightsources• Eightsources• Headlightdefault(GL_LIGHT0)
• Parameters• glLight{if}[v](source,pname,value(s))• Ambient,diffuse,specular,posiKon(direcKon),
spotdirecKon,cutoff,ajenuaKon
Material
• Objectmaterials• AcKvatedbylighKng
• Parameters• glMaterial{if}[v](face,pname,value(s)) • Diffuse,ambient‐diffuse,
specular,shininess,emission
GLU(OpenGLUKlityLibrary)
• Composedgeometries• gluSphere,gluCylinder,etc
• Tesselators,Nurbs• Subdividecomplexpolygons
• Nurblines,nurbsurfaces• Cameramanagement
• gluPerspecKve,gluLookat
GLUT(OpenGLUKlityToolkit)
• Windowhandling• Initcontext,displaymode,etc• Createwindows,setsize,etc• EntrypointisglutMainLoop
• Events• Mouse,keyboard,display,etc
• Callbacks(hooks)• Postredisplay
• Complexobjects• Sphere,torus,teapot,etc
GLUI
• UserInterfacelibrary• Add‐ontoOpenGL• Portable(usesOpenGL)• Easytouse
GLEW(ExtensionWranglerLibrary)
• Cross‐plaqormopen‐sourceC/C++extensionloadinglibrary
• Headerexposescompleteset• StandardfuncKonsforanyOpenGLversion• Allextensions
• FuncKonstocheckcapabiliKes• ActualversionofOpenGL• ActuallysupportedfuncKonsandextensions