320 likes | 541 Views
OpenGL Basics Compared to DirectX. By Jin Li Jan 20th, 2005 Introduction to Computer Graphics. Review of the major similarities and differences. DirectX is more than just a graphics API, OpenGL is strictly a graphics API
E N D
OpenGL Basics Compared to DirectX By Jin Li Jan 20th, 2005 Introduction to Computer Graphics
Review of the major similarities and differences • DirectX is more than just a graphics API, OpenGL is strictly a graphics API • Both use the traditional hardware rendering pipeline, which is based on the z buffer. • Both describe vertices as a set of data consisting of coordinates. Graphics primitives, are defined as an ordered set of vertices. How each API handles vertices to form primitives is different
OpenGL Cross-platform open standard, easier to understand No support for create graphic user interface, needs other supports. • DirectX A Windows-specific proprietary standard, harder to understand, But with Managed DirectX (DirectX wrapped into very handy .NET classes), easier to use. Full support, a Multimedia API.
OpenGL First, from SGI (Silicon Graphics Inc.) Now, ARB( Architecture Review Board) New functionalities, through vendor-specific extensions. The base API changes only slowly. • DirectX Developed by Microsoft with some other vendors New functionalities, through API changes, quite severely.
How to Start • Windows – Since Window 98, the library has already been included. • LINUX/BSD – There are several free implementations, GLX , Mesa, TinyGL, Ygl
GL UTILITIES (GLU) OpenGL Utility Library, included in Standard OpenGL It includes a set of functions in • Texture mipmapping from a base image, • Draw quadric surfaces and NURBS • Camera management The GL Utility Library • Extends functionality of OpenGL library • General independent window support • Manages keyboard, mouse, window • Also provides some higher primitives • Simple non-convex polygo • Texturing • They need to be downloaded.
For UNIX or UNIX-like operating systems: • You need to point the compiler and linker to their location with the appropriate -I and -L options. -lglut -lGLU -lGL -lXmu -lX11 is typical.
OpenGL basics • API conventions • Function names begin with gl and use mixed case: • glBegin, glEnd, glFrontFace • Constants begin with GL and use only upper case: • GL_TRIANGLES, GL_BACK, GL_LIGHT0 • Types begin with GL and use only lower case: • GLbyte, GLubyte, GLint, GLfloat,...
Setup an OpenGL project in Microsoft Visual Studio .NET 2003 • Create and manage drawing windows through GLUT. Display Handler Main Event Loop Keyboard Handler Mouse Handler Event Driven Programming
Clearing the window (Clear color buffer and depth buffer) • glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); • glClearDepth (1.0); • Specifying a Color • glColor3f • glClearColor() takes four parameters, the fourth is alpha value. • Forcing Completion of Drawing (glFlush() and glFinish()) glFlush() causes all OpenGL commands currently queued to be submitted to the hardware for execution. glFinish() with the addition that glFinish() will block until all commands submitted have been executed.
Hidden-Surface Removal (Using a depth buffer) • OpenGL: glEnable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT); • DirectX: • presentParams.EnableAutoDepthStencil = true; • presentParams.AutoDepthStencilFormat = DepthFormat.D16; • device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0); • In both cases, there are more precision at the front of the depth buffer. This will cause the problem of less precision in large scene.
Describing Points, Lines, and Polygons • Using glBegin() and glEnd() • different from device.BeginScene() and device.EndScene() in DirectX. • Specifying Vertices • glVertex{234}{sifd}[v]() • glVertex3i(Glint x,Glint y,Glint z) • glVertex3fv(Glfloat *v) • argument types • – GL byte y, GL short GL int , GL float l GL double o • – unsigned byte (ub), short ( us ), ), int (ui) • v indicates vector
Drawing Primitives • OpenGL: glBegin(GLenum mode); • Primitive types • Points GL_POINTS • Lines GL_LINES,GL_LINE_STRIP,GL_LINE_LOOP • Triangles GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN • Quadrilaterals and all other polygons GL_QUADS,GL_QUAD_STRIP,GL_POLYGON • Ordering of vertices (corners) • GL_CCW < Default GL_CW
Culling Polygon Faces • OpenGL: • glEnable(GL_CULL_FACE); . • glFrontFace(GLenum mode); Define the orientation of front-facing polygons • GL_CW and GL_CCW are accepted. • DirectX • Sounterclockwise cull mode is the default for Direct3D device.RenderState.CullMode = Cull.None;(Turn off culling) Cull.None, Cull.CounterClockwise, Cull.Clockwise
Transformation Pipeline Stages of vertex transformations: Modelview Matrix Projection Matrix Viewport Mapping Object coords Camera coords Normalized coords Window coords Viewing and Transformations • OpenGL has only two matrices: • MODELVIEW and PROJECTION. glMatrixMode(GL_MODELVIEW) glMatrixMode(GL_PROJECTION);
DirectX has three matrices: World, View, Perspective; device.Transform.World = device.Transform.Projection = device.Transform.View =
Projection Transformations 1. Perspective Projection • gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) – (DirectX device.Transform.Projection = Matrix.PerspectiveFovLH()) • glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) 2. Orthographic Projection-- parallel viewing volume • glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);
Viewing Transformations • gluLookAt (eyeX , eyeY , eyeZ , centerX, centerY, centerZ, upX, upY, upZ) • device.Transform.View = Matrix.LookAtLH() • Modeling transformations • glTranslate*(), glRotate*(), and glScale*(). --- • device.Transform.World = Matrix.RotationZ
Multiply Current Matrix by a Rotation,Translation or Scaling Matrix • NEW_MATRIX = CURRENT_MATRIX * NEW_TRANSFORM • void glTranslatef(float x, y, z); • void glRotatef(float angle, x, y, z); • void glScalef(float x, y, z);
Order of Specification isOpposite Order of Operation • • Specification order in program code: • – First: glRotatef(…R2…) • – Second: glRotatef(…R1...) • – Third: glTranslatef( …T…) • • Resulting composed transformation: R2•R1•T. • • Order in which operations are applied to • vertices: First: T; Second: R1; Third: R2.
Manipulating the Matrix Stacks • glPushMatrix()-- It copies the current matrix and adds the copy to the top of the stack. • glPopMatrix(), which discards the top matrix on the stack and uses this matrix as the current matrix. • glLoadIdentity() command to clear the currently modifiable matrix for future transformation commands. Typically, you always call this command before specifying projection or viewing transformations
Transformation Pipeline Hierarchical Modelling: glTranslatef(waistx,waisty,waistz); glPushMatrix(); glRotatef(ra,rx,ry,rz); // draw right arm glPopMatrix(); glPushMatrix(); glRotatef(la,lx,ly,lz); // draw left arm glPopMatrix();
Lights • Three main types of light source in OpenGL, directional, point and spot lights. • To directional lights, the 4th value is different • GLfloat lightPosition[] = {0.7f, 0.7f, 0.7f, 0.0f} ; • To point lights, • GLfloat lightPosition[] = {0.7f, 0.7f, 0.7f, 1.0f} ;
Defining Lights glLightfv(GLenum light, GLenum pname, GLfloat *param) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) Parameters: GL_AMBIENT - RGBA ambient intensity GL_DIFFUSE - RGBA diffuse intensity GL_SPECULAR - RGBA specular intensity GL_POSITION – light position
Materials Defining Material Properties glMaterialfv(GLenum face, GLenum pname, GLfloat *param) Faces: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK Parameters: GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_EMISSION, GL_SHININESS
Drawing Example Adding Lighting: float lPos[] = {1.0,1.0,1.0,1.0}; glLightfv(GL_LIGHT0,GL_POSITION,lPos); float diffuse[] = {1.0,0.0,0.0,1.0}; glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glShadeModel(GL_SMOOTH); // Setup camera // Draw objects
Blending • OpenGL Alpha values can be specified with glColor*(), the 4th value. glBlendFunc(GLenum sfactor, GLenum dfactor); glEnable(GL_BLEND); GL_ZERO GL_ONE ,GL_DST_COLOR, GL_SRC_COLOR ,GL_ONE_MINUS_DST_COLOR ,GL_ONE_MINUS_SRC_COLOR ,GL_SRC_ALPHA………. • In DirectX, for example device.RenderState.AlphaBlendEnable = true; device.RenderState.DestinationBlend = Blend.One; device.RenderState.SourceBlend = Blend.SourceAlpha; device.RenderState.BlendOperation=BlendOperation.Max;
Texture mapping Texture Coordinate System in OpenGL Texture Coordinate System in DirectX
Steps to apply a texture: • Create a texture object and specify a texture for that object (Loading a texture from file is not a built-in feature of OpenGL,glaux.lib is used here. ) • Generate texture names void glGenTextures(GLsize n,GLuint *textures); • Bind texture objects to texture data & parameters void glBindTexture(GLenum target, GLuint textureName); • Indicate how the texture is to be applied to each pixel int gluBuild2DMipmaps(GLenum target,GLint components,GLint width,GLint height,GLenum format,GLenum type,const void *data) • Enable texture mapping glEnable(GL_TEXTURE_2D) • Draw the scene, supplying both texture and geometric coordinates glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);