Other things to do with scene Basic shapes graphs • Names/paths • Geometry objects for primitive shape types – Unique name to access any node in the graph – e.g. “WORLD/table1Trans/table1Rot/top1Trans/lampTrans” • Various exist. • Compute Model-to-world transform – Walk from node through parents to root, multiplying local transforms • We’ll focus first on fundamental: Collection • Bounding box or sphere of triangles – Quick summary of extent of object – Useful for culling – Compute hierarchically: – AKA Triangle Set • Bounding box is smallest box that encloses all children’s boxes – AKA Triangle Soup • Collision/contact calculation • Picking • How to store triangle set? – Click with cursor on screen, determine which node was selected • Edit: build interactive modeling systems – …simply as collection of triangles? Polygon Meshes Cube - raw triangles • 12 triangles: • Mesh Representations – (-1,-1,1) (1,-1,1) (1,1,1) – Independent faces – (-1,-1,1) (1,1,1) (-1,1,1) – (1,-1,1) (1,-1,-1) (1,1,-1) – Vertex and face tables QuickTime™ and a – (1,-1,1) (1,1,-1) (1,1,1) TIFF (LZW) decompressor – Adjacency lists are needed to see this picture. – (1,-1,-1) (-1,-1,-1) (-1,1,-1) – (1,-1,-1) (-1,1,-1) (1,1,-1) – Winged-Edge – (-1,-1,-1) (-1,-1,1) (-1,1,1) – (-1,-1,-1) (-1,1,1) (-1,1,-1) – (-1,1,1) (1,1,1) (1,1,-1) – (-1,1,1) (1,1,-1) (-1,1,-1) – (1,-1,1) (-1,-1,-1) (1,-1,-1) – (1,-1,1) (-1,-1, 1) (-1,-1,-1) • 12*3=36 vertices
Independent Faces But…. • Each Face Lists Vertex Coordinates • A cube only has 8 vertices! (x 4 , y 4 , z 4 ) • 36 vertices with x,y,z = 36*3 floats = 108 floats. (x 3 , y 3 , z 3 ) – Redundant vertices – Would waste memory to store all 36 vertices F 2 – No topology information – Would be slow to send all 36 vertices to GPU F 3 F 1 – (Especially when there is additional data per-vertex) (x 5 , y 5 , z 5 ) • Usually each vertex is used by at least 3 triangles--often (x 2 , y 2 , z 2 ) (x 1 , y 1 , z 1 ) 4 to 6 or more – Would use 4 to 6 times as much memory as needed, or more Face Table • Instead: Specify vertex data once, then reuse it – Assign a number to each vertex F 1 (x 1 , y 1 , z 1 ) (x 2 , y 2 , z 2 ) (x 3 , y 3 , z 3 ) – Specify triangles using vertex numbers F 2 (x 2 , y 2 , z 2 ) (x 4 , y 4 , z 4 ) (x 3 , y 3 , z 3 ) F 3 (x 2 , y 2 , z 2 ) (x 5 , y 5 , z 5 ) (x 4 , y 4 , z 4 ) Cube - indexed triangles Indexed Triangle set • 12 triangles: • 8 vertices: • Array of vertex locations, array of Triangle objects: – P4 P0 P3 – P0: ( 1,-1, 1) Point3 vertices[] = { – P4 P3 P7 ( 1,-1, 1), ( 1,-1,-1), – P1: ( 1,-1,-1) ( 1, 1,-1), – P0 P1 P2 ( 1, 1, 1), – P2: ( 1, 1,-1) (-1,-1, 1), – P0 P2 P3 (-1,-1,-1), QuickTime™ and a – P3: ( 1, 1, 1) (-1, 1,-1), TIFF (LZW) decompressor – P1 P5 P6 (-1, 1, 1)}; are needed to see this picture. class Triangle {short p1, p2, p3) triangles[] = { – P4: (-1,-1, 1) (4, 0, 3), – P1 P6 P2 (4, 3, 7), – P5: (-1,-1,-1) (0, 1, 2), – P5 P4 P7 (0, 2, 3), – P6: (-1, 1,-1) (1, 5, 6), – P5 P7 P6 (1, 6, 2), – P7: (-1, 1, 1) (5, 4, 7), – P7 P3 P2 (5, 7, 6), (7, 3, 2), (7, 2, 6), – P7 P2 P6 (0, 5, 1), (0, 4, 5)}; – P0 P5 P1 – 8 vertices*3 floats = 24 floats • Triangles refer to each vertex by its index in the vertex array – P0 P4 P5 12 triangles*3 points= 36 integers
Vertex & Face Tables Benefits of indexing • Each Face Lists Vertex References • Saves memory (x 4 , y 4 , z 4 ) • Saves data transmission time (x 3 , y 3 , z 3 ) – Shared vertices • Save rendering time: lighting calculation can be done F 2 – Still no topology information just one for each vertex F 3 F 1 • Easy model deformation (x 5 , y 5 , z 5 ) (x 2 , y 2 , z 2 ) (x 1 , y 1 , z 1 ) – Change vertex position data – Triangles automatically follow QuickTime™ and a Vertex Table Face Table TIFF (LZW) decompressor are needed to see this picture. • Topology (point connectivity) separate V 1 x 1 y 1 z 1 F 1 V 1 V 2 V 3 from shape (point locations) V 2 x 2 y 2 z 2 F 2 V 2 V 4 V 3 V 3 x 3 y 3 z 3 F 3 V 2 V 5 V 4 V 4 x 4 y 4 z 4 V 5 x 5 y 5 z 5 Normals (Index vs. pointer) • Normal = perpendicular to surface • Triangle stores indexes into the vertex array. • The normal is essential to lighting • Could also use pointer rather than index – Shading determined by relation of normal to eye & light • Collection of triangles with their normals: Facet Normals – Can be easier to work with – Store & transmit one normal per triangle – But uses more memory (if pointer is larger than short integer) – Normal constant on each triangle--but discontinuous at triangle edges – Can be fragile: if vertex array is reallocated pointers will dangle – Renders as facets – Good for faceted surfaces, such as cube • For curved surface that is approximated by triangles: Vertex Normals – Want normal to the surface, not to the triangle approximation – Don’t want discontinuity: share normal between triangles – Store & transmit one normal per vertex – Each triangle has different normals at its vertices • Lighting will interpolate (a few weeks) • Gives illusion of curved surface
Indexed Triangle Set with Normals Color &Colors • Arrays: • Color analogous to normal Point3 vertexes[]; – One color per triangle: faceted Vector3 normals[]; Color colors[]; – One color per vertex: smooth colors Triangle triangles[]; int numVertexes, numNormals, numColors, numTriangles; • Single base class to handle both: – Facets • one normal & color per triangle • numNormals = numColors = numTriangles – Smooth • one normal & color per vertex • numNormals = numColors = numVertexes Geometry objects base class Cube class • Base class may support an indexed triangle set class Cube(Geometry) { Cube() { numVertices = 8; class Geometry { numTriangles = numNormals = 12; Point3 vertices[]; vertices = { Vector3 normals[]; ( 1,-1, 1), ( 1,-1,-1), ( 1, 1,-1), ( 1, 1, 1), Color colors[]; (-1,-1, 1), (-1,-1,-1), (-1, 1,-1), (-1, 1, 1) }; Triangle triangles[]; triangles = { int numVerices,numNormals,numColors,numTriangles; (4, 0, 3), (4, 3, 6), }; (0, 1, 2), (0, 2, 3), class Triangle { (1, 5, 6), (1, 6, 2), int vertexIndices[3]; (5, 4, 7), (5, 7, 6), int normalIndices[3]; (7, 3, 2), (7, 2, 6), int colorIndices[3]; (0, 5, 1), (0, 4, 5) }; }; normals = { ( 0, 0, 1), ( 0, 0, 1), • Triangle indices: ( 1, 0, 0), ( 1, 0, 0), ( 0, 0,-1), ( 0, 0,-1), – For facet normals, set all three normalIndices of each triangle to (-1, 0, 0), (-1, 0, 0), same value ( 0, 1, 0), ( 0, 1, 0), ( 0,-1, 0), ( 0,-1, 0) }; – For vertex normals, normalIndices will be same as vertexIndices } – Likewise for color }
Smooth surfaces Drawing the indexed triangle set • Tesselation : approximating a smooth surface with a triangle mesh • OpenGL supports “vertex arrays” – Strictly speaking, “tesselation” refers to regular tiling patterns – This and “vertex buffers” are covered in CSE 781. – In computer graphics, often used to mean any triangulation • E.g. Sphere class fills in triangle set (will get to this shortly…) • So for Lab 3 and on-ward: class Sphere(Geom) { private: – Use indexed triangle set for base storage float radius; void tesselate() { – Draw by sending all vertex locations for each triangle: vertices = … triangles = … normals=… for (i=0; i<numTriangles; i++) { } glVertex3fv(vertexes[triangles[i].p1]); public: Sphere(float r) { radius = r;} glVertex3fv(vertexes[triangles[i].p2]); void setRadius(float r) { radius = r; } glVertex3fv(vertexes[triangles[i].p3]); } } • Other smooth surface types • So we get memory savings in Geometry class – Bezier patch (next week) – NURBS • We don’t get speed savings when drawing. – Subdivision surface – Implicit surface Triangles, Strips, Fans Model I/O • Basic indexed triangle set is unstructured: “triangle soup” • Usually have the ability to load data from • GPUs & APIs usually support slightly more elaborate structures some sort of file • Most common: triangle strips, triangle fans v 8 v 6 v 4 v 3 • There are a variety of 3D model formats, v 4 v 5 v 7 v 2 but no universally accepted standards v 2 v 5 v 6 v 3 • More formats for mostly geometry (e.g. v 0 v 1 v 0 v 7 v 1 indexed triangle sets) than for complete – Store & transmit ordered array of vertex indexes. complex scene graphs • Each vertex index only sent once, rather than 3 or 4-6 or more – Even better: store vertexes in proper order in array • File structure unsurprising: List of vertex data, • Can draw entire strip or fan by just saying which array and how many vertexes • No need to send indexes at all. list(s) of triangles referring to the vertex data by – Can define triangle meshes using adjacent strips name or number • Share vertexes between strips • But must use indexes
Recommend
More recommend