UNC-Chapel Hill COMP 770 - Building and interacting with a 3D world

Unformatted text preview:

Building and interacting with a 3D worldFrom last time…OutlinePrimitive 3DSimple planesSpecifying a faceVertex specificationSmoothingDrawing faces in OpenGLAside: Dual graphDecoupling vertex and face attributesPolygon soup3D File formatsOBJ BasicsOBJ face varietiesOBJ extrasObj ExampleOBJ sourcesCode: VertexCode: NormalCode: TexCoordCode: FaceCode: WaveFrontOBJWaveFrontOBJ parserWaveFrontOBJ parser (cont.)WaveFrontOBJ Draw()Picking and selectionOpenGL selection buffersOpenGL selection buffersRendering to selection bufferSelection with the back bufferEncoding object IDsEncoding object IDsHiding selectionPerforming the selectionSelection buffer vs. Back bufferInteraction paradigmsInteraction paradigmsInteraction - TrackballA Virtual TrackballComputing the RotationWhere to rotate?The proof is in the codeDisplay routineTransformation HierarchiesUsing graphs to model hierarchiesCode example (Take one)Code example (Take two)Code Example (Take three)DemoKinematicsYour Next Project1/31/071Building and interacting with a 3D worldComputer GraphicsCOMP 770 (236)Spring 2007Instructor: Brandon Lloyd1/31/072From last time…■ Rendering pipeline■ Vector multiplication° Dot product, cross product, tensor product■ Modeling transformations° Translation, rotation, scale■ Viewing transformation■ Projections° Orthographic, perspective1/31/073Outline■ Representing 3D objects with a mesh■ Loading objects in the OBJ file format■ Interaction° pan, dolly, trackball■ Picking and selection■ Transformation hierarchies■ Assignment #11/31/074Primitive 3D■ How do we specify 3D objects?° Simple mathematical functions, z = f(x,y)° Parametric functions, (x(u,v), y(u,v), z(u,v))° Implicit functions, f(x,y,z) = 0■ Build up from simple primitives° Point – nothing really to see° Lines – nearly see through° Planes – a surface1/31/075Simple planes■ Surfaces modeled as connected planar facets° N (>3) vertices, each with 3 coordinates° Minimally a triangle1/31/076Specifying a face■ Face or facetFace [v0.x, v0.y, v0.z] [v1.x, v1.y, v1.z] [v2.x, v2.y, v2.z] … [vN.x, vN.y, vN.z]■ Sharing vertices via indirectionVertex[0] = [v0.x, v0.y, v0.z]Vertex[1] = [v1.x, v1.y, v1.z]Vertex[2] = [v2.x, v2.y, v2.z]:Vertex[N] = [vN.x, vN.y, vN.z]Face v0, v1, v2, … vNv0v1v2v31/31/077Vertex specification■ Where° Geometric coordinates [x, y, z]■ Attributes° Color values [r, g, b]° Texture Coordinates [u, v]■ Orientation° Inside vs. Outside° Encoded implicitly in ordering■ Geometry Nearby° Often we’d like to “fake” a more complex shape than our true faceted (piecewise-planar) model° Required for lighting and shading in OpenGL1/31/078Smoothing■ Normals° First-Order Taylor-series approximation of surface° Normals provide derivative information° A unit-vector perpendicular tothe actual surface a the specifiedvertex° 3 coordinates – 2 degrees of freedom[nx, ny, nz]° Normalized:2z2y2xzyxnnn]n,n,n[nˆ++=1/31/079Drawing faces in OpenGLglBegin(GL_POLYGON);foreach (Vertex v in Face) {glColor4d(v.red, v.green, v.blue, v.alpha);glNormal3d(v.norm.x, v.norm.y, v.norm.z);glTexCoord2d(v.texture.u, v.texture.v);glVertex3d(v.x, v.y, v.z);}glEnd();■ Heavy-weight model ° attributes specified for every vertex■ Redundant ° vertex positions often shared by at least 3 faces ° “vertex” attributes are often face attributes (e.g. face normal)1/31/0710Aside: Dual graph■ Why do I say that a vertex is generally shared by 3 or more faces?■ Constructing a “dual” graph representation of our mesh.° Replace each face with a point° Insert an edge between every pair of faces that share an edge° Where are vertices in this “dual” representation?■ In what cases are there fewer than 3 faces sharing a vertex?f0f1f2f3f41/31/0711Decoupling vertex and faceattributes■ Case for:° Most of the time vertices will be consistent■ Exceptions:° Regions where the surface changes materials° Regions of high curvature (a crease)■ This is possible with ‘Heavyweight’ vertices, but less efficient1/31/0712Polygon soup■ A collection of ° Vertices° Normals° Vertex colors & Texture coordinates■ Connected by facesVerticesNormalsColors & TextureFaces1/31/07133D File formats■ Typically textbooks invent something■ MAX – Studio Max■ DXF – AutoCAD° supports 2-D and 3-D; binary■ 3DS – 3D studio° flexible; binary■ VRML – Virtual reality modelling language° ASCII – Human readable (and writeable)■ OBJ – Wavefront OBJ format° ASCII° Extremely simple° widely supported1/31/0714OBJ Basics■ The most common Wavefront obj file tokens are listed below. # some text Rest of line is a comment v float float float A single vertex’s geometric position in space. vn float float floatA normal. vt float floatA texture coordinate.1/31/0715OBJ face varietiesfint int int... (vertex only)orfint/int int/int int/int. . . (vertex & texture)orfint/int/int int/int/int int/int/int… (vertex, texture, & normal)orfint//int int//int int//int… (vertex & normal)■ The arguments are 1-based indices into the arrays° vertex positions° texture coordinates° and normals, respectively. ■ One set of indices for each vertex1/31/0716OBJ extrasgstringgroup specification for the following faces. The string is the name of the groupsintsmoothing group ID for the following faces. Faces in the same smoothing group share vertex normals. Used if normals must be estimated.1/31/0717Obj Example# A simple cubev 1 1 1v 1 1 -1v 1 -1 1v 1 -1 -1v -1 1 1v -1 1 -1v -1 -1 1v -1 -1 -1f 1 3 4 2f 5 6 8 7f 1 2 6 5f 3 7 8 4f 1 5 7 3f 2 4 8 6■ Vertices followed by faces° Faces reference previousvertices by integer index° 1-based° Co-planarity of verticesis assumed1/31/0718OBJ sources■ Avalon – Viewpoint (http://avalon.viewpoint.com/)old standards■ 3D Café – (http://www.3dcafe.com/asp/meshes.asp)Nice thumbnail index■ Others■ Most modeling programs will export .OBJ files■ Most rendering packages will read in .OBJ files1/31/0719Code: Vertexclass Vertex:def __init__(self, *args):if (type(args[0]) is list):self.x, self.y, self.z = args[0]else:self.x, self.y, self.z = argsdef __sub__(self, v):return Vertex(self.x - v.x, self.y - v.y, self.z - v.z)1/31/0720Code: Normalclass Normal:def __init__(self, *args):if (type(args[0]) is list):nx, ny, nz = args[0]else:nx, ny, nz = argsl = math.sqrt(nx*nx + ny*ny + nz*nz)if (l != 0):l = 1/lself.nx, self.ny, self.nz = nx*l, ny*l, nz*l1/31/0721Code:


View Full Document

UNC-Chapel Hill COMP 770 - Building and interacting with a 3D world

Download Building and interacting with a 3D world
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Building and interacting with a 3D world and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Building and interacting with a 3D world 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?