open gl programming speaker: 彭任右 date: 2005/10/3

35
Open GL Programming Speaker: 彭彭彭 Date: 2005/10/3

Upload: isabella-lilian-mcdaniel

Post on 28-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Open GL Programming

Speaker: 彭任右Date: 2005/10/3

Page 2: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Coordinate System

• World

• Window

(0,0)

(w,h)

xx

yy

z+

z+

left handed right handed

(0,0)

(w,h)

View Port

Page 3: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

OpenGL Color Models

• RGBA or Color Index• RGBA or Color Index

color index mode

Display12

48

16

Red Green Blue

0123

242526

123 219 74

RGBA mode

Page 4: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

OpenGL Command FormatsglVertex3fv( glVertex3fv( vv ) )

Number ofNumber ofcomponentscomponents

2 - (x,y) 2 - (x,y) 3 - (x,y,z)3 - (x,y,z)4 - (x,y,z,w)4 - (x,y,z,w)

Data TypeData Typeb - byteb - byteub - unsigned byteub - unsigned bytes - shorts - shortus - unsigned shortus - unsigned shorti - inti - intui - unsigned intui - unsigned intf - floatf - floatd - doubled - double

VectorVector

omit “v” foromit “v” forscalar formscalar form

glVertex2f( x, y )glVertex2f( x, y )

Page 5: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Buffers

• Color Buffer

• Depth Buffer

• Stencil Buffer

• Accumulation Buffer

• Selection Buffer

Page 6: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

State Machine

• Set State– glPointSize(size);– glLineWidth(width);– glLineStipple(repeat, pattern);– glShadeModel(GL_SMOOTH);

• Get State– glGet*();

• Enable Features– glEnable(glEnable(GLGL__LIGHTINGLIGHTING););– glDisable(glDisable(GL_TEXTURE_2DGL_TEXTURE_2D););– glIsEnable(GL_DEPTH_TEST);glIsEnable(GL_DEPTH_TEST);

Page 7: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Framework

• Clear the window• Set the Viewing, Projection, and Viewport t

ransformation • Loop for each primitive

– Modeling Transformation– Specify the primitive type, attributes, and rende

ring states

• Flush or Swap buffer

Page 8: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Clearing the Window• glClearColor(red, green, blue, alphglClearColor(red, green, blue, alpha);a);– From 0.0f to 1.0f

• glClearDepth(1.0f);glClearDepth(1.0f);• glClear();glClear();

– glClear(GL_COLOR_BUFFER_BIT);– glClear(GL_DEPTH_BUFFER_BIT);– Use the logical or ( | ) to combine

Page 9: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Specifying a Color

• glColor3f()– glColor3f(0.0, 0.0, 0.0) – black– glColor3f(1.0, 0.0, 0.0) – red– glColor3f(0.0, 1.0, 0.0) – green– glColor3f(0.0, 0.0, 1.0) – blue– glColor3f(1.0, 1.0, 0.0) – yellow– glColor3f(0.0, 0.0, 0.0) – magenta– glColor3f(0.0, 1.0, 1.0) – cyan– glColor3f(1.0, 1.0, 1.0) – White

Page 10: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

MVPV

Page 11: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Matrix Operation

• glMatrixMode()– GL_PROJECTION– GL_MODELVIEW– GL_TEXTURE

• glLoadIdentity()• glLoadMatrix()• glMultMatrix()• glPushMatrix() • glPopMatrix()

Page 12: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Matrix Multiplication

• glMatrixMode(GL_MODELVIEW);• glLoadIdentity();• glMultMatrix(N);• glMultMatrix(M);• glMultMatrix(L);• glBegin(GL_POINTS);• glVertex3f(v);• glEnd();

• The vertex transformation is N(M(Lv))

Page 13: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Modeling Transformation

• Move object

glTranslate{234}{fd}( x, y, z )

• Rotate object around arbitrary axis

glRotate{234}{fd}( angle, x, y, z )

– angle is in degrees

• Stretch, shrink, or mirror object

glScale{234}{fd}( x, y, z )

Page 14: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Transformation Tutorial

Page 15: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Viewing Transformation

• Position the camera/eye in the scene– gluLookAt( posx, posy, posz,viewx, viewy, viewz, upx, upy, upz);

Page 16: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Projection Transformation

• Orthogonal Projection

• Perspective Projection

Page 17: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Orthogonal Projection

• glOrtho(left, right, bottom, top, near far);

• gluOrtho2D( left, right, bottom, top )

Page 18: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Perspective Projection• glFrustum(left, right, bottom, top, zNear, zFar);

• gluPerspective(fovy, aspect, zNear, zFar);

Page 19: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Projection Tutorial

Page 20: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Viewport Transformation

• View Port– usually same as window size

– viewport aspect ratio should be same as projection transformation or resulting image may be distorted

– glViewport(x, y, width, height);

(0,0)

(x,y) width

height

Page 21: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

OpenGL Geometric Primitives

• All geometric primitives are specified by vertices

• All geometric primitives are specified by vertices

GL_QUAD_STRIP

GL_POLYGON

GL_TRIANGLE_STRIP

GL_TRIANGLE_FAN

GL_POINTSGL_LINES

GL_LINE_LOOPGL_LINE_STRIP

GL_TRIANGLES

GL_QUADS

Page 22: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Shapes Tutorial

Page 23: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Drawing Primitives

• glBegin(GLenum mode);glBegin(GLenum mode);• glColor*();glColor*();• glVertex*();glVertex*();• glEnd();glEnd();

Page 24: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

glShadeModel()

• glShadeModel(GLenum mode);glShadeModel(GLenum mode);– Flat ShadingFlat Shading

•GL_FLAT

– Smooth ShadingSmooth Shading•GL_SMOOTH

Page 25: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

glPolygonMode()

• glPolygonMode(face, mode);glPolygonMode(face, mode);– GLenum face

•GL_FRONT•GL_BACK

– GLenum mode•GL_POINT•GL_LINE (hint: wireframe)•GL_FILL

Page 26: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Depth

• Depth

– think as distance from view point

• Depthe Test– glEnable(GL_DEPTH_TEST);– If we draw rectangle first, the ellipse will cove the rectangle when

the depth test is closed.

Z = 1.0

Z = 2.0

Without Depth Test

Z = 1.0

Z = 2.0

With Depth Test

x

y z+

left handed

Page 27: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Culling

• glFrontFace(GLenum mode);glFrontFace(GLenum mode);– GL_CW– GL_CCW

• glCullFace(GLenum mode);glCullFace(GLenum mode);– GL_FRONT– GL_BACK– GL_FRONT_AND_BACK

• glEnable(GL_CULL_FACE);glEnable(GL_CULL_FACE);

Counterclockwise winding (Front facing)

Page 28: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Force Completion of Drawing

• Win32Win32– SwapBuffers(hDC); SwapBuffers(hDC);

• GLUTGLUT– glutSwapBuffer();glutSwapBuffer();

Page 29: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

glRenderMode()

• GLint glRenderMode(GLenum modGLint glRenderMode(GLenum mode);e);– GL_RENDER– GL_SELECTION– GL_FEEDBACK

Page 30: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Selection Mode

• Method to determine which primitives are inside the viewing volume

• Select selection mode for rendering– glRenderMode(GL_SELECT);

Page 31: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Selection Buffer

• glSelectBuffer(GLsizei size, glSelectBuffer(GLsizei size, GLuint *buffer);GLuint *buffer);

• 4 elements per hit– # of names on names stack

– ZMin

– ZMax

– Bottom of names stack

Page 32: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Names Stack

• To identify a primitive, give it a name– “names” are just integer values, not strings

• Names are stack based– allows for hierarchies of primitives– glInitNames();– glPushName();– glPopName();– glLoadName();

Page 33: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Sample

void RenderScene(){

// “set the rendering statesglInitNames();glLoadName(1);“draw something”glLoadName(2); // draw something else … continue

}

Page 34: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Picking

• gluPickMatrix(x, y, width, height, viewport);– x, y should be set as xMouse and yMouse

• glGetIntegerv(GL_VIEWPORT, viewport);

Page 35: Open GL Programming Speaker: 彭任右 Date: 2005/10/3

Sample

void pick(xPos, yPos) {

glSelectBuffer(LENGTH, selectBuff);glMatrixMode(GL_PROJECTION);glRenderMode(GL_SELECTION);glLoadIdentity();gluPickMatrix(x, y, 2, 2, viewport);gluPerspective(fovy, aspect, near, far);RenderScene();hits = glRenderMode(GL_RENDER);Cout << we pick << selectBuff[3] << object;

}