Download presentation
Presentation is loading. Please wait.
Published byDale Griffith Modified over 8 years ago
1
OpenGL Objects Finalised
2
Debugging Tip For Debugging your applications remember: glGetError(); gluErrorString(); Don’t use these in release code (the render loop), as fetching the error from the card is slow!!!
3
Packing Your Objects http://www.fastmovevanlines.com/images/packing_box.jpg
4
My Stupid Tetrahedron glBegin( GL_TRIANGLE_FAN ); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f ); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 0.5f, 0.0f, 0.0f ); glColor3f(0.0f,0.0f,1.0f); glVertex3f( -0.5f, 0.0f, 0.0f ); glColor3f(0.0f,1.0f,1.0f); glVertex3f( 0.0f, 0.0f, -1.0f ); glColor3f(1.0f,0.0f,1.0f); glVertex3f( 0.5f, 0.0f, 0.0f ); glEnd(); glBegin( GL_TRIANGLES ); glColor3f(0.5f,1.0f,0.5f); glVertex3f( 0.5f, 0.0f, 0.0f ); glVertex3f( -0.5f, 0.0f, 0.0f ); glVertex3f( 0.0f, 0.0f, -1.0f ); glEnd();
5
The Basic Packing Code GLuInt Beef = glGenLists( 1); glNewList( Beef) ……\\ Object List glEndList(); http://obamarama.org/wp- content/uploads/2007/04/heres-the-beef.jpg
6
Lists can work in a hierarchy glNewList(Cow); glTranslatef(…) glCallList(Beef); glTranslatef(…) glCallList(Beef); glTranslatef(…) glCallList(Beef); glEndList(); http://obamarama.org/wp- content/uploads/2007/04/heres-the-beef.jpg http://barfblog.foodsafety.ksu.edu/HappyCow.jpg
7
Old Old way of drawing the Tetrahedron glBegin( GL_TRIANGLE_FAN ); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f ); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 0.5f, 0.0f, 0.0f ); glColor3f(0.0f,0.0f,1.0f); glVertex3f( -0.5f, 0.0f, 0.0f ); glColor3f(0.0f,1.0f,1.0f); glVertex3f( 0.0f, 0.0f, -1.0f ); glColor3f(1.0f,0.0f,1.0f); glVertex3f( 0.5f, 0.0f, 0.0f ); glEnd(); glBegin( GL_TRIANGLES ); glColor3f(0.5f,1.0f,0.5f); glVertex3f( 0.5f, 0.0f, 0.0f ); glVertex3f( -0.5f, 0.0f, 0.0f ); glVertex3f( 0.0f, 0.0f, -1.0f ); glEnd();
8
New Way glCallList(Tetrahedron); http://www.kjmaclean.c om/Geometry/Tetrahedr on_files/image004.jpg
9
Our Objects are now in nice packages – glCallList(Cow) Rendering code looks very nice But it still requires a lot of calls to OpenGL
10
glNewList(Tetrahedron); glBegin( GL_TRIANGLE_FAN ); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f ); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 0.5f, 0.0f, 0.0f ); glColor3f(0.0f,0.0f,1.0f); glVertex3f( -0.5f, 0.0f, 0.0f ); glColor3f(0.0f,1.0f,1.0f); glVertex3f( 0.0f, 0.0f, -1.0f ); glColor3f(1.0f,0.0f,1.0f); glVertex3f( 0.5f, 0.0f, 0.0f ); glEnd(); glBegin( GL_TRIANGLES ); glColor3f(0.5f,1.0f,0.5f); glVertex3f( 0.5f, 0.0f, 0.0f ); glVertex3f( -0.5f, 0.0f, 0.0f ); glVertex3f( 0.0f, 0.0f, -1.0f ); glEnd(); glEndList(); What Really Happens glCallList(Tetrahedron);
11
Say Hello to Vertex Arrays Array Types: Vertex Normal Colour Index Texture Coordinate Array Edge Flag Array
12
Declaring an Array GLfloat vertices[8] = { (0.0,1.0,0.0), (0.5,0.0,0.0), (-0.5,0.0,0.0), (0.0,0.0,-1.0), (0.5,0.0,0.0), (0.0,0.0,-1.0), (-0.5,0.0,-0.0.0), (0.0,0.0,-1.0), };
13
Enabling and Disabling your Arrays glEnableClientState(array); glDisableClientState(array); Client state refers to system memory!! GL_COLOR_ARRAY – Array for Vertex Colours GL_EDGE_FLAG_ARRAY – Array for Wireframe (Line Drawing) GL_INDEX_ARRAY – Array of Arrays GL_NORMAL_ARRAY – Normals (typically Per Vertex) GL_TEXTURE_COORD_ARRAY – Duh! GL_VERTEX_ARRAY – Duh!
14
Accessing Your Array glVertexPointer(): specify pointer to vertex coordinates array glNormalPointer(): specify pointer to normal array glColorPointer(): specify pointer to RGB colour array glIndexPointer(): specify pointer to indexed colour array glTexCoordPointer(): specify pointer to texture cordinates array glEdgeFlagPointer(): specify pointer to edge flag array
15
Drawing From an Array Set The Vertex Pointer – glVertexPointer(…); Use the array in the Draw Function – glDrawArrays();
16
Old Way of Drawing the Tetrahedron glBegin( GL_TRIANGLE_FAN ); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f ); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 0.5f, 0.0f, 0.0f ); glColor3f(0.0f,0.0f,1.0f); glVertex3f( -0.5f, 0.0f, 0.0f ); glColor3f(0.0f,1.0f,1.0f); glVertex3f( 0.0f, 0.0f, -1.0f ); glColor3f(1.0f,0.0f,1.0f); glVertex3f( 0.5f, 0.0f, 0.0f ); glEnd(); glBegin( GL_TRIANGLES ); glColor3f(0.5f,1.0f,0.5f); glVertex3f( 0.5f, 0.0f, 0.0f ); glVertex3f( -0.5f, 0.0f, 0.0f ); glVertex3f( 0.0f, 0.0f, -1.0f ); glEnd();
17
Drawing the Tetrahedron With an Array GLfloat vertices[8] = { (0.0,1.0,0.0), (0.5,0.0,0.0), (-0.5,0.0,0.0), (0.0,0.0,-1.0), (0.5,0.0,0.0), (0.0,0.0,-1.0), (-0.5,0.0,-0.0.0), (0.0,0.0,-1.0), }; // Enable the vertex Array glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertices); // draw a tetrahedron glDrawArray(GL_TRIANGLE_FAN, 0, 4); glDrawArray(GL_TRIANGLES, 3, 3; // deactivate vertex array glDisableClientState(GL_VERTEX_ARRAY);
18
Even Faster Ways of Drawing We have used glDrawArrays(mode, first, count); The other options we have are: – glDrawElements(mode, count, type, indices); – glDrawRangeElements(mode, start, end, count, type, indicies); // An optimised version of glDrawElements
19
Declaring a Better Array GLfloat vertices[4] = { (0.0,1.0,0.0), (0.5,0.0,0.0), (-0.5,0.0,0.0), (0.0,0.0,-1.0)}; Glfloat indicies[12] = {0,1,2, 0,2,3, 0,3,1, 1,2,3};
20
Drawing the Tetrahedron Faster GLfloat vertices[4] = {...} Glflaot indicies[12] = {…} // Enable the vertex Array glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertices); // draw a tetrahedron glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, indices); // deactivate vertex array glDisableClientState(GL_VERTEX_ARRAY);
21
You can’t always share your normals! http://www.songho.ca/opengl/gl_vertexarray.html
22
Partitioning your world http://www.collinslefebvrestoneberger.com/artists/Jens/ ClearCut.jpg
23
Binary Spacial Partitioning (BSP) In today's gaming world, there are two important reasons for dividing the world – Rendering without the Z-Buffer – Rendering Translucent (Alpha Blend) Polygons
24
Why? Modern games have so many polygons that stretching the z buffer to cover the entire object range would result in many visual glitches, as many polygons start falling into the same depth levels of the buffer. Translucent Polygons are created using a blend function which requires back-to-front rendering (Painters algorithm)
25
How? There are many differing ways and methods to create maps of polygons, especially static collections One of the most common and a very efficient way is utilising BSP trees. – The first use of a BSP tree was Doom in 1993 It was only used in a 2D implementation!
26
A BSP is a specialised implementation of a Binary Data Tree Binary Trees are used because: the ability to be efficiently traversed in both directions The ability to add data randomly during the building of the tree The speed in which a random location can be found
27
Binary Data Trees http://mathworld.wolfram.com/images/eps- gif/CompleteBinaryTree_1000.gif http://www.gamedev.net/reference/programming /features/trees2/BuildTree1.gif
28
The Principal to creating a BSP Tree Each polygon (3D) / line (2D) creates a dividing line / plane in the tree where every other Polygon/line is either in front or behind.
29
Cutting your Polygons The offending Polygon / Line must be cut into two parts It then becomes two separate polygons / lines One infront of the division, the other behind
30
Reordering the split If we split using the red/green line first, our tree would require no splitting
31
Problems Creating BSP Lists http://www.devmaster.net/articles/bsp-trees/ http://beehivehairdresser.com/wp-content/uploads/2007/10/yin_yang.jpg Minimising Polygon Cuts Balancing the Tree
32
A short 2D Example http://web.cs.wpi.edu/~matt/courses/cs563/talks/bsp/bsp.html
33
Creating a BSP Tree for Lines (Doom Style) http://pauillac.inria.fr/~levy//bsp/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.