Models & HierarchiesCSE167: Computer GraphicsInstructor: Steve RotenbergUCSD, Fall 2006Normals The concept of normals is essential to lighting Intuitively, we might think of a flat triangle as having a constant normal across the front face However, in computer graphics, it is most common to specify normals and perform lighting at the vertices This gives us a method of modeling smooth surfaces as a mesh of triangles with ‘shared’ normals at the vertices We will talk about lighting in the next lecture, but for today, we will still think of our vertex as containing a normalModels We will extend our concept of a Model to include normals We can do this by simply extending our vertex class:class Vertex {Vector3 Position;Vector3 Color;Vector3 Normal;public:void Draw() {glColor3f(Color.x, Color.y, Color.z);glNormal3f(Normal.x, Normal.y, Normal.z);glVertex3f(Position.x, Position.y, Position.z); // This has to be last}}Model Data Structures Everybody knows that a cube has 8 vertices If we need to render a cube, however, each of those vertices requires 3 different normals. In other words, we might really need 3*8=24 vertices If we render it as triangles, each 4-sided face actually requires 6 vertices, meaning that we might need to store and process 36 different vertices!Indexed Models So far, we have simply thought of a model as an array of triangles, each triangle storing 3 unique vertices A more common method of storing a model is as an array of vertices, and an array of triangles In the second method, each triangle stores an index (or pointer) to a vertex instead of storing the vertex data explicitly This is called an indexed model or single indexed model Indexing will almost always save memory, as models often have shared vertices that are used by several triangles Large, smooth meshes will often share a single vertex between 4-6 triangles (or more) Indexing can also save processing time as the vertex array can first be transformed and lit, and then the triangle array can be clipped and scan converted…Single Indexed Modelclass Vertex {Vector3 Position;Vector3 Color;Vector3 Normal;};class Triangle {Vertex *Vert[3]; // or int Vert[3];};class Model {int NumVerts,NumTris;Vertex *Vert;Triangle *Tri;};Index vs. Pointer Should we store the triangle verts as integers (indexing into the array of actual Vertex’s) ?int Vert[3]; Or should we store them as pointers to the actual Vertex’s themselves ?Vertex *Vert[3]; Memory: In most systems an int is 4 bytes and a pointer is 4 bytes, so there isn’t a big difference in memory However, for smaller models, you could benefit from using short ints, which are 2 bytes each. This would cut the triangle size in half, but limit you to 65536 vertices Performance: Storing Vertex*’s gives the triangle direct access to the data so should be faster Other Issues: It’s definitely more convenient to store the pointers instead of integers One important reason to consider storing integers instead of pointers, however, is if you are using some type of dynamic array for the vertices (such as an STL vector). Pointers to members of these arrays are considered dangerous, since the array may have to reallocate itself if more vertices are addedVertex Buffers Hardware rendering API’s (like Direct3D and OpenGL) support some type of vertex buffer system as well (but everybody has a different name for it) This is essentially an unindexed or single indexed model format You start by defining your specific vertex type. Verts usually have a position and normal, and might have one or more colors, texture coordinates, or other properties You then request a vertex buffer of whatever memory size you want. This memory is usually the actual video memory on the graphics board (not main memory) The vertex buffer can then be filled up with vertex data as a single large array One can then draw from the vertex buffer with a command like this:DrawSomething(int type,int first vert,int numverts); // type: triangles, strips, lines… The advantage is that a large number of triangles can be drawn with a single CPU call and all of the work then takes place entirely on the graphics boardIndex Buffers An index buffer (or whatever name one calls it) is an array of (usually 2 byte or 4 byte) integers It is stored in video memory like the vertex buffer The integers index into a vertex buffer array One can then draw triangles (or other primitives) by specifying a range of these indexes Using vertex/index buffers is most likely going to be the fastest way to render on modern graphics hardwareTriangles, Strips, Fans Graphics hardware usually supports slightly more elaborate primitives than single triangles Most common extensions are strips and fansv0v1v2v4v6v8v7v5v3v0v1v2v3v4v5v6v7Materials & Grouping Usually models are made up from several different materials The triangles are usually grouped and drawn by materialHierarchical TransformationsHierarchical Transformations We have seen how a matrix can be used to place an individual object into a virtual 3D world Sometimes, we have objects that are grouped together in some way For example, we might have an articulated figure which contains several rigid components connected together in some fashion Or we might have several objects sitting on a tray that is beingcarried around Or we might have a bunch of moons and planets orbiting around ina solar system Or we might have a hotel with 1000 rooms, each room containing abed, chairs, table, etc. In each of these cases, the placement of objects is described more easily when one considers their locations relative to each other We will see how hierarchical transformations can be used to describe their placementScene Hierarchy If a scene contains 1000 objects, we might think of a simple organization like this:SceneObject 1 Object 2 Object 3 Object 1000…Scene Hierarchy Or we could go for a more hierarchical grouping like:SceneRoom 1 Room 2 Room 3Chair 1 Chair 2 TableBook MonitorBed Dresseretc…Scene Hierarchy It is very common in computer graphics to define a complex scene in some sort of hierarchical fashion The individual objects are grouped into a tree like structure (upside down tree) Each moving part is a single node in the tree The node at the top is the root node A node directly above another is that node’s parent A
View Full Document