# SER332 Lecture 18

Introduction to Graphics and Game Development
Mesh Data Structure II
(201804)

March 29, 2018

## Transcript

1. jgs
SER332
Introduction to Graphics and Game
Development
Lecture 18: Mesh Data Structure II
Javier Gonzalez-Sanchez
[email protected]
PERALTA 230U
Office Hours: By appointment

2. jgs
Plane, Cylinder, and Box

3. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 3
jgs
Creating a Triangulated Plane
// Generate vertices in XZ-plane
for i = 0 to n
for j = 0 to n
Create vertex at location (i*xsize, 0, j*ysize)
// step 2. Generate indices
for i = 0 to ?
Create triangle with indices(i, i+1, i+n+1)
Create triangle with indices( i+1, i+n+2, i+n+1)
xsize
ysize vn
v0
v1
v
n+1
vn+2

4. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 4
jgs
Creating a Triangulated Plane
// creating a triangulated plane
Mesh* createPlane(int arena_width, int arena_depth, int arena_cell) {
Mesh *me = new Mesh;
int n = arena_width / arena_cell;
int m = arena_depth / arena_cell;
// vertices
for (int i = 0; ifor (int j = 0; j < m; j++) {
me->dot_vertex.push_back(Vec3(i*arena_cell, 0.0, j*arena_cell));
}
}
// faces
for (int i = 0; i<(n*m) - m; i++) {
if ((i + 1) % n == 0) continue;
me->face_index_vertex.push_back(i);
me->face_index_vertex.push_back(i + 1);
me->face_index_vertex.push_back(i + n);
me->face_index_vertex.push_back(i + 1);
me->face_index_vertex.push_back(i+ n + 1);
me->face_index_vertex.push_back(i + n);
}
return me;
}

5. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 5
jgs
Creating a Triangulated Plane
§ Use colors to verify right-hand rule.

6. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 6
jgs
Creating a Triangulated Cylinder
// Generate vertices on top
for i = 0 to n
angle = (i*360) / (n+1)
create vertex at (cos( angle), height, sin(angle))
// Generate vertices on the bottom
// same process but height = 0
// Generate vertex in the middle of the top
// Generate vertex in the middle of the bottom
// Generate triangles on top
// Generate triangles on the bottom
// Generate triangles on the side
v1
Vn+2
v0
Vn+1

7. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 7
jgs
Creating a Triangulated Cylinder
// create a triangulated cylinder
Mesh* createCylinder(int h, int radio) {
int n = 100;
double angle;
Mesh *m =new Mesh;
// Generate vertices on top
m->vertex.push_back(Vec3(0.0, h, 0.0));
for (int i = 0; iangle = (M_PI/180)*(1.0*i*360)/n;
m->vertex.push_back(Vec3
}
// Generate vertices on bottom
for (int i = 0; iangle = (M_PI/180)*(i*360)/n;
m->vertex.push_back(Vec3
}

8. jgs
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 8
// Generate triangles on top
for (int i = 1; i<=n; i++) {
m->vertexIndex.push_back(0); m->vertexIndex.push_back(i);
if (i==n) {
m->vertexIndex.push_back(1);
}else{
m->vertexIndex.push_back(i+1);
}
}
// There is no bottom. Use the floor.
// Generate triangles on the side
for (int i = 1; i<=n; i++) {
m->vertexIndex.push_back(i); m->vertexIndex.push_back(n+i);
if (i!=n) {
m->vertexIndex.push_back(n+i+1);
} else {
m->vertexIndex.push_back(n+1);
}
m->vertexIndex.push_back(i);
if (i!=n) {
m->vertexIndex.push_back(i+1); m->vertexIndex.push_back(n+i+1);
}else{
m->vertexIndex.push_back(1); m->vertexIndex.push_back(n+1);
}
}
return m;
}

9. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 9
jgs
Creating a Triangulated Cylinder

10. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 10
jgs
Creating a Triangulated Cube
// Generate vertices on top
// Generate vertices on the bottom
// Generate triangles on top
// Generate triangles on the bottom
// Generate triangles on the sides

11. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 11
jgs
Creating a Triangulated Cube

12. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 12
jgs
Source Code
§ https://github.com/javiergs/SER332/blob/master/Lecture18/main.cpp

13. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 13
jgs
Test Yourselves
§ Include methods to create cubes, planes, and cylinders
§ Include methods to create spheres

14. jgs
SER332 Introduction to Graphics
Javier Gonzalez-Sanchez
[email protected]
Spring 2018
Disclaimer. These slides can only be used as study material for the class SER332 at ASU. They cannot be distributed or used for another purpose.