Anda di halaman 1dari 4

Ume University Department of Computing Science Computer Graphics and Visualization Pedher Johansson pedher@cs.umu.

se

Exercise 1 !1"#!1#1"

Sid 1 $"%

Exercise 1

Reading OFF-files

Storing Polygon data


A model that is to be visualized is usually represented as a polygon mesh. This is a collection of edges, vertices and polygons connected such that each edge is shared by at least one but at most two polygons. In normal rendering only polygons are important, but in some situations (e.g., when doing sub-division or drawing with wire-frame or cartoon shading) edges are also important. As always in computer graphics, speed is of importance, i.e., we must optimize our data representation for fast access. Polygon meshes can be represented many different ways and are evaluated according to space and time. More complex representations such as the Half-edge or Wing-edge data representation also exists., but below we look at three variants.
Explicit Representation

In this representation, each polygon is represented by a list of vertex coordinates. P = ((x1, y1, z1), (x2, y2, z2), ... , (xn, yn, zn))
Disadvantages

Takes space. For more than one polygon space is wasted because vertices are duplicated. It is also risky to duplicate data when the data may change. Takes time since there is no explicit representation of edges and vertices, an interactive move of a vertex involves finding all polygons that share the vertex. Polygon edges are drawn twice since there is no easy way to find edges.
V2 P1 P2 V4 V3

Pointers to a Vertex List

Each vertex is stored once in a vertex list. The polygon, P, is a list of indices to a vertex list. V = (V1, V2, V3, V4) = ((x1, y1, z1), ... , (x4 , y4 , z4 )) P1 = (1, 2, 4) P2 = (4, 2, 3)
V1

Sid

$"%

Advantages

Space saved because each vertex is stored once. Coordinates of a vertex can be changed easily. Difficult to find polygons that share edges. Polygon edges are drawn twice since there is no easy way to find edges.
V2 E1 V1 E5 P1 E4 P2 E3 V4 E2 V3

Disadvantage

Pointers to an Edge List

In this format a polygon is represented by a pointer to the edge list V = (V1, V2, V3, V4) = ((x1, y1, z1), ... , (x4 , y4 , z4 )) E1 = (V1 , V2, P1, ) E2 = (V2 , V3, P2, ) E3 = (V3 , V4, P2, ) E4 = (V4 , V2, P1, P2) E5 = (V4 , V1, P1, ) P1 = (E1 , E4, E5) P2 = (E2 , E3, E4)
Advantages

Displays edges rather than polygons. Eliminates redundant clipping, transformation and scan conversion. Filled polygons are more easily clipped. The order of vertices may be important in drawing, i.e., the vertices in all polygons should be in clock-wise or counter clock-wise order. This is tricky in this representation.

Disadvantage

In all three cases, the determining of which edges are incident to a vertex is not easy. All edges must be inspected. If this is important (optionally in Assignment 3), the mentioned Half-edge representations is probably better.

Object File Format


As with data representation, many formats exists to store models in files. In this course we will use a simple format called Object File Format, OFF. This format is basically pointers to a vertex list as described above. In addition, it can represent simple color data which is good. On the other hand it has limited ways to represent textures and more advances model properties and polygons may also be non-planar which is bad. OFF files are ASCII files formatted as follows.

Sid & $"%

OFF #header Nvertices Nfaces Nedges X[0] Y[0] Z[0] : : : X[Nv-1] Y[Nv-1] Z[Nv-1] NV1 V[0] V[1] ... V[NV-1] COLOR : NVN V[0] V[1] ... V[NV-1] COLOR The optional color may take several forms. Line breaks are significant here: the color description begins after V[NV-1] and ends with the end of the line (or the next # comment). A color may be: nothing the default color one integer index into the colormap; three or four integers RGB[A] and possibly alpha values in the range [0,255] three or four floating-point numbers RGB[A] and possibly alpha values in the range [0, 1]

OFF 8 6 24 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 4 0 1 3 2 4 2 3 7 6 4 4 6 7 5 4 0 4 5 1 4 1 5 7 3 4 0 2 6 4 A representation of a cube without color information.

Triangulation
Drawing polygon data is much faster if all polygons have the same number of vertices. Since there can be any number of vertices in a polygon according to the OFF format we should fall back to triangles, i.e., each polygon read from the OFF file should be converted to a triangle. There are many algorithms for this, but a simple one will do. Below is a suggestion in some pseudo code (Vi is the i:th vertex in a polygon P with n vertices). for i=2 to n -1 do: store(new Polygon(V0, Vi-1, Vi))

Write your own OFF file reader


For use in later assignments you should write your own OFF file reader and represent the data. The file reader should be implemented in C or C++. Consider the following. Avoid duplicate data. No need for fast access to edge data. It should be fast to copy data to a buffer, especially vertex data. Avoid linked lists for that. Triangulation is not a requirement but strongly encouraged. Color information can be fun but not a requirement.

Sid " $"%

Vertex data is usually 3D float (not double) data. 4D is also possible to use, where the 4th element is called w and usually set to 1.0 (or 0 for vectors). If you use color data, our applications only use 3D colors (RGB). Prepare for additional vector data (normals) to your polygons (Used in the Assignment 3). Also look at Exercise 2 for inspiration about data storage.

Anda mungkin juga menyukai