Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005

download Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005

of 36

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005

  • Slide 1
  • Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005
  • Slide 2
  • Normals 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 normal
  • Slide 3
  • Models 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 }
  • Slide 4
  • 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!
  • Slide 5
  • 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
  • Slide 6
  • Single Indexed Model class 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; };
  • Slide 7
  • Index vs. Pointer Should we store the triangle verts as integers (indexing into the array of actual Vertexs) ? int Vert[3]; Or should we store them as pointers to the actual Vertexs themselves ? Vertex *Vert[3]; Memory: In most systems an int is 4 bytes and a pointer is 4 bytes, so there isnt 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: Its 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 added
  • Slide 8
  • Double Indexing If memory is really tight, one could also consider double indexing the model In this scheme, there are arrays of position, normal, and color vectors and vertices themselves index into those arrays This method was useful for realtime software renderers of a few years ago, but it not too common any more Most hardware renderers are designed to take models either un-indexed or single indexed
  • Slide 9
  • Double Indexing class Vertex { Vector3 *Position; Vector3 *Color; Vector3 *Normal; }; class Triangle { Vertex *Vert[3];// or int Vert[3]; }; class Model { int NumPositions,NumColors,NumNormals; Vector3 *Position,*Normal,*Color; int NumVerts,NumTris; Vertex *Vert; Triangle *Tri; };
  • Slide 10
  • Vertex Buffers Hardware rendering APIs (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 board
  • Slide 11
  • Index 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 hardware
  • Slide 12
  • Triangles, Strips, Fans Graphics hardware usually supports slightly more elaborate primitives than single triangles Most common extensions are strips and fans v0v0 v1v1 v2v2 v4v4 v6v6 v8v8 v7v7 v5v5 v3v3 v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7
  • Slide 13
  • Materials & Grouping Usually models are made up from several different materials The triangles are usually grouped and drawn by material
  • Slide 14
  • Model I/O Usually, a Model class would have the ability to load data from some sort of file There are a variety of 3D model formats out there, but unfortunately, there are no universally accepted standards
  • Slide 15
  • 3D Modelers There are a variety of 3D modeling programs in use today Popular ones include Maya (by Alias) and 3D Studio (by Autodesk). Interestingly, Autodesk bought Alias a couple weeks ago There are several other 3D programs out there as well, including some free ones on the web
  • Slide 16
  • Modeling Primitives Interactive 3D modeling tools usually provide higher level primitives than triangles Often, modelers allow the use of some type of curved surfaces Curved surfaces are usually defined by some sort of grid of points, which is then smoothly interpolated by an automatic algorithm Common surface types include: bicubic, Bezier, B- Spline, NURBS, subdivision surfaces, and more We will discuss these in more detail in a later lecture
  • Slide 17
  • Editable Models Some applications simply need to load and display 3D models (such as a video game or 3D renderer) Other applications need to dynamically modify or construct models on the fly (like a 3D modeling tool) One would make different choices about how these are stored and manipulated
  • Slide 18
  • Modeling Operations There are tons of different operations one might wish to perform in the interactive modeling process We will look at some of the most common ones We will assume that we are dealing with a single indexed model with an array of vertices and an array of triangles Most of these operations can be extended to more complex model and primitive types as well
  • Slide 19
  • Create / Delete The most basic operations are: Vertex *CreateVertex(); void DeleteVertex(int v); Triangle *CreateTriangle(); void DeleteTriangle(int t); Just about all higher level modeling functions can be broken down into these basic operations All higher level functions go through these interfaces to create and remove data These functions need to be fast and reliable The delete operations can be done in different ways and arent as simple as they might first look
  • Slide 20
  • Modeling Operations Digitize Copy/dupe Triangulate Extrude Lathe Border Extrude Boolean Procedural modeling, scripts
  • Slide 21
  • Hierarchical Transformations
  • Slide 22
  • We have seen how a matrix 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 being carried around Or we might have a bunch of moons and planets orbiting around in a solar system Or we might have a hotel with 1000 rooms, each room containing a bed, 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 placement
  • Slide 23
  • Scene Hierarchy If a scene contains 1000 objects, we might think of a simple organization like this: Scene Object 1Object 2Object 3Object 1000
  • Slide 24
  • Scene Hierarchy Or we could go for a more hierarchical grouping like: Scene Room 1Room 2Room 3 Chair 1Chair 2Table BookMonitor BedDresseretc
  • Slide 25
  • Scene Hierarchy I