Graphics programming in open gl

64
Graphics Programming in OpenGL Arvind Devaraj

description

My presentation on Graphics programming

Transcript of Graphics programming in open gl

Page 1: Graphics programming in open gl

Graphics Programming in OpenGL

Arvind Devaraj

Page 2: Graphics programming in open gl

Arvind Devaraj2

OpenGL and GLUT Overview

�� OpenGL is graphics API.OpenGL is graphics API.�� Makes graphics programming Makes graphics programming h/wh/w

independent independent

�� GLUT is a toolkit GLUT is a toolkit library of utilities using OpenGLlibrary of utilities using OpenGLmakes easier to use OpenGL makes easier to use OpenGL

Page 3: Graphics programming in open gl

Arvind Devaraj

A

Page 4: Graphics programming in open gl

Arvind Devaraj

High Level

� Represent Objects as Primitives� Apply Transformations on objects

� Projection : Visible portions are rendered

� Add realism by adding Light and Texture

� Remember using State information� Customize using Shaders� Framebuffer operations

Page 5: Graphics programming in open gl

Arvind Devaraj5

OpenGL Geometric Primitives

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTS

GL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_QUADSGL_QUADS

Page 6: Graphics programming in open gl

Arvind Devaraj6

Shapes Tutorial

Page 7: Graphics programming in open gl

Arvind Devaraj

Transformations

Page 8: Graphics programming in open gl

Arvind Devaraj8

Camera Analogy

�� Graphics Rendering is like taking a Graphics Rendering is like taking a photograph photograph

�� 3D model 3D model �� 2D image2D image

camera

tripod model

viewingvolume

Page 9: Graphics programming in open gl

Arvind Devaraj9

Transformations

�� Steps in Forming an ImageSteps in Forming an Image� specify geometry (world coordinates)� specify camera (camera coordinates)� project (window coordinates)� map to viewport (screen coordinates)

�� Each step uses transformationsEach step uses transformations�� Transformation = change in coordinate Transformation = change in coordinate

systems systems

Page 10: Graphics programming in open gl

Arvind Devaraj10

Camera Analogy of Transformations

�� Projection transformationsProjection transformations� adjust the lens of the camera

�� Viewing transformationsViewing transformations� tripod–define position and orientation of

the viewing volume in the world�� Modeling transformationsModeling transformations

� moving the model�� ViewportViewport transformationstransformations

� enlarge or reduce the physical photograph

Page 11: Graphics programming in open gl

Arvind Devaraj11

Affine Transformations

�� Want transformations which preserve Want transformations which preserve geometrygeometry

� Rotation, translation, scaling� Projection� Concatenation (composition)

Page 12: Graphics programming in open gl

Arvind Devaraj

Affine Transformations

� Basic Transformations� Any transformation is Composition of these

Page 13: Graphics programming in open gl

Arvind Devaraj

Affine Transformation Matrix

Page 14: Graphics programming in open gl

Arvind Devaraj14

3D Transformations

�� A vertex is transformed by 4 x 4 matricesA vertex is transformed by 4 x 4 matrices� all affine operations are matrix multiplications� all matrices are stored column-major in

OpenGL� matrices are always post-multiplied� product of matrix and vector is

v�M

����

����

151173

141062

13951

12840

mmmm

mmmm

mmmm

mmmm

M

Page 15: Graphics programming in open gl

Arvind Devaraj15

Programming Transformations

�� Prior to rendering, view, locate, and Prior to rendering, view, locate, and orient:orient:� eye/camera position� 3D geometry

�� Manage the matricesManage the matrices� including matrix stack

�� Combine (composite) transformationsCombine (composite) transformations

Page 16: Graphics programming in open gl

Arvind Devaraj16

vertex

ModelviewMatrix

ProjectionMatrix

PerspectiveDivision

ViewportTransform

Modelview

Modelview

Projection

���

object eye clip normalizeddevice

window

TransformationPipeline

Page 17: Graphics programming in open gl

Arvind Devaraj17

Viewing Transformations

�� Position the camera/eye in the scenePosition the camera/eye in the scene� place the tripod down; aim camera

�� To To ““fly throughfly through”” a scenea scene� change viewing transformation and

redraw scene�� gluLookAtgluLookAt( ( eyeeyexx, , eyeeyeyy, , eyeeyezz,,

aimaimxx, , aimaimyy, , aimaimzz,,upupxx, , upupyy, , upupzz ))

� up vector determines unique orientation

tripod

Page 18: Graphics programming in open gl

Arvind Devaraj

Page 19: Graphics programming in open gl

Arvind Devaraj19

Transformations in OpenGL

�� ModelingModeling�� ViewingViewing

� orient camera� projection

�� AnimationAnimation�� Map to screenMap to screen

Page 20: Graphics programming in open gl

Arvind Devaraj20

Transformation Tutorial

Page 21: Graphics programming in open gl

Arvind Devaraj

Projection

Page 22: Graphics programming in open gl

Arvind Devaraj22

Projection Tutorial

Page 23: Graphics programming in open gl

Arvind Devaraj23

Connection: Viewing and Modeling

�� Moving camera is equivalent to moving Moving camera is equivalent to moving every object in the world towards a every object in the world towards a stationary camerastationary camera

�� Viewing transformations are equivalent Viewing transformations are equivalent to several modeling transformationsto several modeling transformations

Page 24: Graphics programming in open gl

Arvind Devaraj24

Double Buffering

12

48

16

12

48

16FrontBuffer

BackBuffer

Display

Page 25: Graphics programming in open gl

Arvind Devaraj25

Depth Buffering andHidden Surface Removal

12

48

16

12

48

16ColorBuffer

DepthBuffer

Display

Page 26: Graphics programming in open gl

Arvind Devaraj

Lighting

Page 27: Graphics programming in open gl

Arvind Devaraj27

Phong Principles

�� Lighting simulates how objects reflect Lighting simulates how objects reflect lightlight� material composition of object� light’s color and position

Page 28: Graphics programming in open gl

Arvind Devaraj

Page 29: Graphics programming in open gl

Arvind Devaraj

Various Lighting Models

Page 30: Graphics programming in open gl

Arvind Devaraj30

Simulating Lights

�� PhongPhong lighting modellighting model� Computed at vertices

�� Lighting contributorsLighting contributors� Surface material properties� Light properties� Lighting model properties

Page 31: Graphics programming in open gl

Arvind Devaraj31

SurfaceNormals

�� NormalsNormals define how a surface reflects define how a surface reflects lightlight

glNormal3f( glNormal3f( x, y, zx, y, z ))

� Current normal is used to compute vertex’s color

Page 32: Graphics programming in open gl

Arvind Devaraj32

Light Position Tutorial

Page 33: Graphics programming in open gl

Arvind Devaraj

Texture Mapping

Page 34: Graphics programming in open gl

Arvind Devaraj34

TextureMapping�� Apply image to geometry Apply image to geometry

�� UsesUses� Reduce Geometric complexity� Add Realism � Reflections

Page 35: Graphics programming in open gl

Arvind Devaraj

Texture Mapping

Reduce Geometric Complexity

Page 36: Graphics programming in open gl

Arvind Devaraj

Page 37: Graphics programming in open gl

Arvind Devaraj

Reflections – Environmental Map

Page 38: Graphics programming in open gl

Arvind Devaraj38

Texture Mapping

s

t

x

y

z

image

geometry screen

Page 39: Graphics programming in open gl

Arvind Devaraj39

Texture Example

�� The texture (below) is a The texture (below) is a 256 x 256 image that has been256 x 256 image that has beenmapped to a rectangularmapped to a rectangularpolygonpolygon

Page 40: Graphics programming in open gl

Arvind Devaraj40

Tutorial: Texture

Page 41: Graphics programming in open gl

Arvind Devaraj41

OpenGL’s State MachineAll Rendering info are remembered as state info

State InformationLightingShadingTexturingBlending

Page 42: Graphics programming in open gl

Arvind Devaraj42

OpenGL’s State MachineAll Rendering info are remembered as state info

Manipulating vertex attribute changes state info

glColor()glNormal()glTexCoord()

Page 43: Graphics programming in open gl

Arvind Devaraj

Shaders

� Allow these transformations to be user programmable

Page 44: Graphics programming in open gl

Arvind Devaraj

Page 45: Graphics programming in open gl

Arvind Devaraj

Shaders

Page 46: Graphics programming in open gl

Arvind Devaraj

Page 47: Graphics programming in open gl

Arvind Devaraj47

Getting to the Framebuffer

BlendingBlendingDepthTest

DepthTest DitheringDithering Logical

Operations

LogicalOperations

ScissorTest

ScissorTest

StencilTest

StencilTest

AlphaTest

AlphaTest

Frag

men

t

Fram

ebuf

fer

Page 48: Graphics programming in open gl

Arvind Devaraj

Blending

Stencil test

Depth test

Page 49: Graphics programming in open gl

Arvind Devaraj

Alpha : for blending effect

Page 50: Graphics programming in open gl

Arvind Devaraj

Page 51: Graphics programming in open gl

Arvind Devaraj51

Blending (using Alpha)

�� AlphaAlpha--Measure of OpacityMeasure of Opacity� simulate translucent objects

� glass, water, etc.

� composite images� AntialiasingglEnable( GL_BLEND )

Page 52: Graphics programming in open gl

Arvind Devaraj52

Blending

�� Combine pixels with whatCombine pixels with what’’s in alreadys in alreadyin the in the framebufferframebufferglBlendFuncglBlendFunc( ( srcsrc, , dstdst ))

FramebufferFramebufferPixelPixel((dstdst))

BlendingEquation

BlendingEquation

FragmentFragment((srcsrc))

BlendedBlendedPixelPixel

pfr CdstCsrcC���

��

Page 53: Graphics programming in open gl

Arvind Devaraj

Blending- Demo

� Exercises� add multiple shaders� simple blend� Demo (url devmaster)� Advanced (notes)

Other blending modesTexture blending

Page 54: Graphics programming in open gl

Arvind Devaraj

Stencil test

Page 55: Graphics programming in open gl

Arvind Devaraj

Stencil test

Page 56: Graphics programming in open gl

Arvind Devaraj

Accumulation buffer

� The accumulation buffer is simply an extra image buffer that is used to accumulate composite images.

Page 57: Graphics programming in open gl

Arvind Devaraj

Accumulation buffer

� Color buffer loses precision, so use float buffer

�� CompositingCompositing�� AntialiasingAntialiasing�� FilteringFiltering�� Motion BlurMotion Blur

Page 58: Graphics programming in open gl

Arvind Devaraj

OpenGLES

� There is no support for glBegin or glEnd. Use vertex arrays and vertex buffer objects instead.

� The only supported rasterizationprimitives are points, lines and triangles. Quads are not supported

� There is no support for display lists.

Page 59: Graphics programming in open gl

Arvind Devaraj

OpenGLES

� There is no support for the fixed-function graphics pipeline. You must use your own vertex and fragment shader programs.

� There is no support for viewing transforms such as glFrustumf. You must compute your own transformation matrix, pass it to the vertex shader as a uniform variable, and perform the matrix multiplication in the shader.

� There is no support for specialized functions such as glVertexPointer and glNormalPointer. Use glVertexAttribPointer instead.

Page 60: Graphics programming in open gl

Arvind Devaraj

OpenGLES- Buffer Objects

� OpenGL cannot directly see any of your memory

� Therefore, the first step is to allocate some memory that OpenGL can see . This is done with something called a buffer object.

Think of a buffer object as an array of GPU memory.

Page 61: Graphics programming in open gl

Arvind Devaraj

OpenGLES- Buffer Objects

� Pass arrays of vertices, colors, etc. to OpenGL in a large chunkglVertexPointer( 3, GL_FLOAT, 0, coords )glColorPointer( 4, GL_FLOAT, 0, colors )

glEnableClientState( GL_VERTEX_ARRAY ) glEnableClientState( GL_COLOR_ARRAY )

glDrawArrays( GL_TRIANGLE_STRIP, 0, numVerts );

Page 62: Graphics programming in open gl

Arvind Devaraj

OpenGLES - BufferObjects� void InitializeVertexBuffer() {

glGenBuffers(1, &positionBufferObject); glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);

glBufferData(GL_ARRAY_BUFFER, siz, vertexPositions, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, 0);

}

Page 63: Graphics programming in open gl

Arvind Devaraj

Exercises

� Moving ball animation� Blending� Texturing

Page 64: Graphics programming in open gl

�����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������