1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // geometry_utils:
7 //   Helper library for generating certain sets of geometry.
8 //
9 
10 #include "geometry_utils.h"
11 
12 #define _USE_MATH_DEFINES
13 #include <math.h>
14 
15 using namespace angle;
16 
SphereGeometry()17 SphereGeometry::SphereGeometry() {}
18 
~SphereGeometry()19 SphereGeometry::~SphereGeometry() {}
20 
CubeGeometry()21 CubeGeometry::CubeGeometry() {}
22 
~CubeGeometry()23 CubeGeometry::~CubeGeometry() {}
24 
CreateSphereGeometry(size_t sliceCount,float radius,SphereGeometry * result)25 void CreateSphereGeometry(size_t sliceCount, float radius, SphereGeometry *result)
26 {
27     size_t parellelCount = sliceCount / 2;
28     size_t vertexCount   = (parellelCount + 1) * (sliceCount + 1);
29     size_t indexCount    = parellelCount * sliceCount * 6;
30     float angleStep      = static_cast<float>(2.0f * M_PI) / sliceCount;
31 
32     result->positions.resize(vertexCount);
33     result->normals.resize(vertexCount);
34     for (size_t i = 0; i < parellelCount + 1; i++)
35     {
36         for (size_t j = 0; j < sliceCount + 1; j++)
37         {
38             Vector3 direction(sinf(angleStep * i) * sinf(angleStep * j), cosf(angleStep * i),
39                               sinf(angleStep * i) * cosf(angleStep * j));
40 
41             size_t vertexIdx             = i * (sliceCount + 1) + j;
42             result->positions[vertexIdx] = direction * radius;
43             result->normals[vertexIdx]   = direction;
44         }
45     }
46 
47     result->indices.clear();
48     result->indices.reserve(indexCount);
49     for (size_t i = 0; i < parellelCount; i++)
50     {
51         for (size_t j = 0; j < sliceCount; j++)
52         {
53             result->indices.push_back(static_cast<unsigned short>(i * (sliceCount + 1) + j));
54             result->indices.push_back(static_cast<unsigned short>((i + 1) * (sliceCount + 1) + j));
55             result->indices.push_back(
56                 static_cast<unsigned short>((i + 1) * (sliceCount + 1) + (j + 1)));
57 
58             result->indices.push_back(static_cast<unsigned short>(i * (sliceCount + 1) + j));
59             result->indices.push_back(
60                 static_cast<unsigned short>((i + 1) * (sliceCount + 1) + (j + 1)));
61             result->indices.push_back(static_cast<unsigned short>(i * (sliceCount + 1) + (j + 1)));
62         }
63     }
64 }
65 
GenerateCubeGeometry(float radius,CubeGeometry * result)66 void GenerateCubeGeometry(float radius, CubeGeometry *result)
67 {
68     result->positions.resize(24);
69     result->positions[0]  = Vector3(-radius, -radius, -radius);
70     result->positions[1]  = Vector3(-radius, -radius, radius);
71     result->positions[2]  = Vector3(radius, -radius, radius);
72     result->positions[3]  = Vector3(radius, -radius, -radius);
73     result->positions[4]  = Vector3(-radius, radius, -radius);
74     result->positions[5]  = Vector3(-radius, radius, radius);
75     result->positions[6]  = Vector3(radius, radius, radius);
76     result->positions[7]  = Vector3(radius, radius, -radius);
77     result->positions[8]  = Vector3(-radius, -radius, -radius);
78     result->positions[9]  = Vector3(-radius, radius, -radius);
79     result->positions[10] = Vector3(radius, radius, -radius);
80     result->positions[11] = Vector3(radius, -radius, -radius);
81     result->positions[12] = Vector3(-radius, -radius, radius);
82     result->positions[13] = Vector3(-radius, radius, radius);
83     result->positions[14] = Vector3(radius, radius, radius);
84     result->positions[15] = Vector3(radius, -radius, radius);
85     result->positions[16] = Vector3(-radius, -radius, -radius);
86     result->positions[17] = Vector3(-radius, -radius, radius);
87     result->positions[18] = Vector3(-radius, radius, radius);
88     result->positions[19] = Vector3(-radius, radius, -radius);
89     result->positions[20] = Vector3(radius, -radius, -radius);
90     result->positions[21] = Vector3(radius, -radius, radius);
91     result->positions[22] = Vector3(radius, radius, radius);
92     result->positions[23] = Vector3(radius, radius, -radius);
93 
94     result->normals.resize(24);
95     result->normals[0]  = Vector3(0.0f, -1.0f, 0.0f);
96     result->normals[1]  = Vector3(0.0f, -1.0f, 0.0f);
97     result->normals[2]  = Vector3(0.0f, -1.0f, 0.0f);
98     result->normals[3]  = Vector3(0.0f, -1.0f, 0.0f);
99     result->normals[4]  = Vector3(0.0f, 1.0f, 0.0f);
100     result->normals[5]  = Vector3(0.0f, 1.0f, 0.0f);
101     result->normals[6]  = Vector3(0.0f, 1.0f, 0.0f);
102     result->normals[7]  = Vector3(0.0f, 1.0f, 0.0f);
103     result->normals[8]  = Vector3(0.0f, 0.0f, -1.0f);
104     result->normals[9]  = Vector3(0.0f, 0.0f, -1.0f);
105     result->normals[10] = Vector3(0.0f, 0.0f, -1.0f);
106     result->normals[11] = Vector3(0.0f, 0.0f, -1.0f);
107     result->normals[12] = Vector3(0.0f, 0.0f, 1.0f);
108     result->normals[13] = Vector3(0.0f, 0.0f, 1.0f);
109     result->normals[14] = Vector3(0.0f, 0.0f, 1.0f);
110     result->normals[15] = Vector3(0.0f, 0.0f, 1.0f);
111     result->normals[16] = Vector3(-1.0f, 0.0f, 0.0f);
112     result->normals[17] = Vector3(-1.0f, 0.0f, 0.0f);
113     result->normals[18] = Vector3(-1.0f, 0.0f, 0.0f);
114     result->normals[19] = Vector3(-1.0f, 0.0f, 0.0f);
115     result->normals[20] = Vector3(1.0f, 0.0f, 0.0f);
116     result->normals[21] = Vector3(1.0f, 0.0f, 0.0f);
117     result->normals[22] = Vector3(1.0f, 0.0f, 0.0f);
118     result->normals[23] = Vector3(1.0f, 0.0f, 0.0f);
119 
120     result->texcoords.resize(24);
121     result->texcoords[0]  = Vector2(0.0f, 0.0f);
122     result->texcoords[1]  = Vector2(0.0f, 1.0f);
123     result->texcoords[2]  = Vector2(1.0f, 1.0f);
124     result->texcoords[3]  = Vector2(1.0f, 0.0f);
125     result->texcoords[4]  = Vector2(1.0f, 0.0f);
126     result->texcoords[5]  = Vector2(1.0f, 1.0f);
127     result->texcoords[6]  = Vector2(0.0f, 1.0f);
128     result->texcoords[7]  = Vector2(0.0f, 0.0f);
129     result->texcoords[8]  = Vector2(0.0f, 0.0f);
130     result->texcoords[9]  = Vector2(0.0f, 1.0f);
131     result->texcoords[10] = Vector2(1.0f, 1.0f);
132     result->texcoords[11] = Vector2(1.0f, 0.0f);
133     result->texcoords[12] = Vector2(0.0f, 0.0f);
134     result->texcoords[13] = Vector2(0.0f, 1.0f);
135     result->texcoords[14] = Vector2(1.0f, 1.0f);
136     result->texcoords[15] = Vector2(1.0f, 0.0f);
137     result->texcoords[16] = Vector2(0.0f, 0.0f);
138     result->texcoords[17] = Vector2(0.0f, 1.0f);
139     result->texcoords[18] = Vector2(1.0f, 1.0f);
140     result->texcoords[19] = Vector2(1.0f, 0.0f);
141     result->texcoords[20] = Vector2(0.0f, 0.0f);
142     result->texcoords[21] = Vector2(0.0f, 1.0f);
143     result->texcoords[22] = Vector2(1.0f, 1.0f);
144     result->texcoords[23] = Vector2(1.0f, 0.0f);
145 
146     result->indices.resize(36);
147     result->indices[0]  = 0;
148     result->indices[1]  = 2;
149     result->indices[2]  = 1;
150     result->indices[3]  = 0;
151     result->indices[4]  = 3;
152     result->indices[5]  = 2;
153     result->indices[6]  = 4;
154     result->indices[7]  = 5;
155     result->indices[8]  = 6;
156     result->indices[9]  = 4;
157     result->indices[10] = 6;
158     result->indices[11] = 7;
159     result->indices[12] = 8;
160     result->indices[13] = 9;
161     result->indices[14] = 10;
162     result->indices[15] = 8;
163     result->indices[16] = 10;
164     result->indices[17] = 11;
165     result->indices[18] = 12;
166     result->indices[19] = 15;
167     result->indices[20] = 14;
168     result->indices[21] = 12;
169     result->indices[22] = 14;
170     result->indices[23] = 13;
171     result->indices[24] = 16;
172     result->indices[25] = 17;
173     result->indices[26] = 18;
174     result->indices[27] = 16;
175     result->indices[28] = 18;
176     result->indices[29] = 19;
177     result->indices[30] = 20;
178     result->indices[31] = 23;
179     result->indices[32] = 22;
180     result->indices[33] = 20;
181     result->indices[34] = 22;
182     result->indices[35] = 21;
183 }
184