Open GL Objects Finalised Debugging Tip For Debugging
Open. GL Objects Finalised
Debugging Tip For Debugging your applications remember: • gl. Get. Error(); • glu. Error. String(); Don’t use these in release code (the render loop), as fetching the error from the card is slow!!!
Packing Your Objects • http: //www. fastmovevanlines. com/images/packing_box. jpg
My Stupid Tetrahedron gl. Begin( GL_TRIANGLE_FAN ); gl. Color 3 f(1. 0 f, 0. 0 f); gl. Vertex 3 f( 0. 0 f, 1. 0 f, 0. 0 f ); gl. Color 3 f(0. 0 f, 1. 0 f, 0. 0 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. Color 3 f(0. 0 f, 1. 0 f); gl. Vertex 3 f( -0. 5 f, 0. 0 f ); gl. Color 3 f(0. 0 f, 1. 0 f); gl. Vertex 3 f( 0. 0 f, -1. 0 f ); gl. Color 3 f(1. 0 f, 0. 0 f, 1. 0 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. End(); gl. Begin( GL_TRIANGLES ); gl. Color 3 f(0. 5 f, 1. 0 f, 0. 5 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. Vertex 3 f( -0. 5 f, 0. 0 f ); gl. Vertex 3 f( 0. 0 f, -1. 0 f ); gl. End();
The Basic Packing Code GLu. Int Beef = gl. Gen. Lists( 1); gl. New. List( Beef) ……\ Object List gl. End. List(); http: //obamarama. org/wp-content/uploads/2007/04/heres -the-beef. jpg
Lists can work in a hierarchy gl. New. List(Cow); gl. Translatef(…) gl. Call. List(Beef); gl. End. List(); http: //obamarama. org/wpcontent/uploads/2007/04/heres-the-beef. jpg http: //barfblog. foodsafety. ksu. edu/Happy. Cow. jpg
Old way of drawing the Tetrahedron gl. Begin( GL_TRIANGLE_FAN ); gl. Color 3 f(1. 0 f, 0. 0 f); gl. Vertex 3 f( 0. 0 f, 1. 0 f, 0. 0 f ); gl. Color 3 f(0. 0 f, 1. 0 f, 0. 0 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. Color 3 f(0. 0 f, 1. 0 f); gl. Vertex 3 f( -0. 5 f, 0. 0 f ); gl. Color 3 f(0. 0 f, 1. 0 f); gl. Vertex 3 f( 0. 0 f, -1. 0 f ); gl. Color 3 f(1. 0 f, 0. 0 f, 1. 0 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. End(); gl. Begin( GL_TRIANGLES ); gl. Color 3 f(0. 5 f, 1. 0 f, 0. 5 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. Vertex 3 f( -0. 5 f, 0. 0 f ); gl. Vertex 3 f( 0. 0 f, -1. 0 f ); gl. End();
New Way • gl. Call. List(Tetrahedron); http: //www. kjmaclean. c om/Geometry/Tetrahed ron_files/image 004. jpg
• Our Objects are now in nice packages – gl. Call. List(Cow) • Rendering code looks very nice • But it still requires a lot of calls to Open. GL
What Really Happens gl. New. List(Tetrahedron); gl. Begin( GL_TRIANGLE_FAN ); gl. Color 3 f(1. 0 f, 0. 0 f); gl. Vertex 3 f( 0. 0 f, 1. 0 f, 0. 0 f ); gl. Color 3 f(0. 0 f, 1. 0 f, 0. 0 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. Color 3 f(0. 0 f, 1. 0 f); gl. Vertex 3 f( -0. 5 f, 0. 0 f ); gl. Call. List(Tetrahedron); gl. Color 3 f(0. 0 f, 1. 0 f); gl. Vertex 3 f( 0. 0 f, -1. 0 f ); gl. Color 3 f(1. 0 f, 0. 0 f, 1. 0 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. End(); gl. Begin( GL_TRIANGLES ); gl. Color 3 f(0. 5 f, 1. 0 f, 0. 5 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. Vertex 3 f( -0. 5 f, 0. 0 f ); gl. Vertex 3 f( 0. 0 f, -1. 0 f ); gl. End(); gl. End. List();
Say Hello to Vertex Arrays Array Types: Vertex Normal Colour Index Texture Coordinate Array Edge Flag Array
Declaring an Array GLfloat vertices[8] = { (0. 0, 1. 0, 0. 0), (0. 5, 0. 0), (-0. 5, 0. 0), (0. 0, -1. 0), (0. 5, 0. 0), (0. 0, -1. 0), (-0. 5, 0. 0, -0. 0. 0), (0. 0, -1. 0), };
Enabling and Disabling your Arrays • gl. Enable. Client. State(array); • gl. Disable. Client. State(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!
Accessing Your Array • • • gl. Vertex. Pointer(): specify pointer to vertex coordinates array gl. Normal. Pointer(): specify pointer to normal array gl. Color. Pointer(): specify pointer to RGB colour array gl. Index. Pointer(): specify pointer to indexed colour array gl. Tex. Coord. Pointer(): specify pointer to texture cordinates array gl. Edge. Flag. Pointer(): specify pointer to edge flag array
Drawing From an Array • Set The Vertex Pointer – gl. Vertex. Pointer(…); • Use the array in the Draw Function – gl. Draw. Arrays();
Old Way of Drawing the Tetrahedron gl. Begin( GL_TRIANGLE_FAN ); gl. Color 3 f(1. 0 f, 0. 0 f); gl. Vertex 3 f( 0. 0 f, 1. 0 f, 0. 0 f ); gl. Color 3 f(0. 0 f, 1. 0 f, 0. 0 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. Color 3 f(0. 0 f, 1. 0 f); gl. Vertex 3 f( -0. 5 f, 0. 0 f ); gl. Color 3 f(0. 0 f, 1. 0 f); gl. Vertex 3 f( 0. 0 f, -1. 0 f ); gl. Color 3 f(1. 0 f, 0. 0 f, 1. 0 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. End(); gl. Begin( GL_TRIANGLES ); gl. Color 3 f(0. 5 f, 1. 0 f, 0. 5 f); gl. Vertex 3 f( 0. 5 f, 0. 0 f ); gl. Vertex 3 f( -0. 5 f, 0. 0 f ); gl. Vertex 3 f( 0. 0 f, -1. 0 f ); gl. End();
Drawing the Tetrahedron With an Array GLfloat vertices[8] = { (0. 0, 1. 0, 0. 0), (0. 5, 0. 0), (-0. 5, 0. 0), (0. 0, -1. 0), (0. 5, 0. 0), (0. 0, -1. 0), (-0. 5, 0. 0, -0. 0. 0), (0. 0, -1. 0), }; // Enable the vertex Array gl. Enable. Client. State(GL_VERTEX_ARRAY); gl. Vertex. Pointer(3, GL_FLOAT, 0, vertices); // draw a tetrahedron gl. Draw. Array(GL_TRIANGLE_FAN, 0, 4); gl. Draw. Array(GL_TRIANGLES, 3, 3; // deactivate vertex array gl. Disable. Client. State(GL_VERTEX_ARRAY);
Even Faster Ways of Drawing We have used gl. Draw. Arrays(mode, first, count); The other options we have are: – gl. Draw. Elements(mode, count, type, indices); – gl. Draw. Range. Elements(mode, start, end, count, type, indicies); // An optimised version of gl. Draw. Elements
Declaring a Better Array GLfloat vertices[4] = { (0. 0, 1. 0, 0. 0), (0. 5, 0. 0), (-0. 5, 0. 0), (0. 0, -1. 0)}; Glfloat indicies[12] = {0, 1, 2, 0, 2, 3, 0, 3, 1, 1, 2, 3};
Drawing the Tetrahedron Faster GLfloat vertices[4] = {. . . } Glflaot indicies[12] = {…} // Enable the vertex Array gl. Enable. Client. State(GL_VERTEX_ARRAY); gl. Vertex. Pointer(3, GL_FLOAT, 0, vertices); // draw a tetrahedron gl. Draw. Elements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, indices); // deactivate vertex array gl. Disable. Client. State(GL_VERTEX_ARRAY);
You can’t always share your normals! http: //www. songho. ca/opengl/gl_vertexarray. html
Partitioning your world http: //www. collinslefebvrestoneberger. com/artists/Jens /Clear. Cut. jpg
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
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)
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 2 D implementation!
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
Binary Data Trees http: //mathworld. wolfram. com/images/epsgif/Complete. Binary. Tree_1000. gif http: //www. gamedev. net/reference/programmin g/features/trees 2/Build. Tree 1. gif
The Principal to creating a BSP Tree • Each polygon (3 D) / line (2 D) creates a dividing line / plane in the tree where every other Polygon/line is either in front or behind.
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
Reordering the split • If we split using the red/green line first, our tree would require no splitting
Problems Creating BSP Lists Balancing the Tree Minimising Polygon Cuts http: //www. devmaster. net/articles/bsp-trees/ http: //beehivehairdresser. com/wp-content/uploads/2007/10/yin_yang. jpg
A short 2 D Example http: //web. cs. wpi. edu/~matt/courses/cs 563/talks/bsp. html
Creating a BSP Tree for Lines (Doom Style) http: //pauillac. inria. fr/~levy//bsp/
- Slides: 33