OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SolidSphere.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "SolidSphere.h"
22 #include "AstcTextures.h"
23 
24 #include <cstdlib>
25 #include <cmath>
26 
27 namespace AstcTextures
28 {
29  /* Please see header for specification. */
30  SolidSphere::SolidSphere(const float radius, const unsigned int rings, const unsigned int sectors)
31  {
32  unsigned int r = 0;
33  unsigned int s = 0;
34 
35  /* Number of cooridnates for vertex, texture and normal coordinates. */
36  const unsigned int n_vertex_coordinates = 3;
37  const unsigned int n_texture_coordinates = 2;
38  const unsigned int n_normal_coordinates = 3;
39  const unsigned int n_indices_per_vertex = 6;
40 
41  const float R = 1.0f / (float)(rings - 1);
42  const float S = 1.0f / (float)(sectors - 1);
43 
44  sphere_vertex_data_size = rings * sectors * n_vertex_coordinates * sizeof(float);
45  sphere_normal_data_size = rings * sectors * n_normal_coordinates * sizeof(float);
46  sphere_texcoords_size = rings * sectors * n_texture_coordinates * sizeof(float);
47  sphere_n_indices = rings * sectors * n_indices_per_vertex;
48 
50  MALLOC_CHECK(float*, sphere_normals, sphere_normal_data_size);
51  MALLOC_CHECK(float*, sphere_texcoords, sphere_texcoords_size);
52  MALLOC_CHECK(unsigned short*, sphere_indices, sphere_n_indices * sizeof(unsigned short));
53 
54  float* vertices = sphere_vertices;
55  float* normals = sphere_normals;
56  float* texcoords = sphere_texcoords;
57  unsigned short* indices = sphere_indices;
58 
59  for (r = 0; r < rings; r++)
60  {
61  for (s = 0; s < sectors; s++)
62  {
63  const float x = sinf(M_PI * r * R) * cosf(2 * M_PI * s * S);
64  const float y = sinf(-M_PI_2 + M_PI * r * R);
65  const float z = sinf(2.0f * M_PI * s * S) * sinf(M_PI * r * R);
66 
67  *texcoords++ = s * S;
68  *texcoords++ = r * R;
69 
70  *vertices++ = x * radius;
71  *vertices++ = y * radius;
72  *vertices++ = z * radius;
73 
74  *normals++ = x;
75  *normals++ = y;
76  *normals++ = z;
77  }
78  }
79 
80  for (r = 0; r < rings; r++)
81  {
82  for (s = 0; s < sectors; s++)
83  {
84  /* First triangle. */
85  *indices++ = r * sectors + s;
86  *indices++ = r * sectors + (s + 1);
87  *indices++ = (r + 1) * sectors + s;
88 
89  /* Second triangle. */
90  *indices++ = r * sectors + (s + 1);
91  *indices++ = (r + 1) * sectors + (s + 1);
92  *indices++ = (r + 1) * sectors + s;
93  }
94  }
95  }
96 
97  /* Please see header for specification. */
98  float* SolidSphere::getSphereVertexData(int* vertex_data_size)
99  {
100  if (vertex_data_size == NULL)
101  {
102  LOGF("Memory error: vertex_data_size = NULL");
103  exit(EXIT_FAILURE);
104  }
105 
106  *vertex_data_size = sphere_vertex_data_size;
107 
108  return sphere_vertices;
109  }
110 
111  /* Please see header for specification. */
112  float* SolidSphere::getSphereNormalData(int* normal_data_size)
113  {
114  if (normal_data_size == NULL)
115  {
116  LOGF("Memory error: normal_data_size = NULL");
117  exit(EXIT_FAILURE);
118  }
119 
120  *normal_data_size = sphere_normal_data_size;
121 
122  return sphere_normals;
123  }
124 
125  /* Please see header for specification. */
126  float* SolidSphere::getSphereTexcoords(int* texcoords_size)
127  {
128  if (texcoords_size == NULL)
129  {
130  LOGF("Memory error: texcoords_size = NULL");
131  exit(EXIT_FAILURE);
132  }
133 
134  *texcoords_size = sphere_texcoords_size;
135 
136  return sphere_texcoords;
137  }
138 
139  /* Please see header for specification. */
140  unsigned short* SolidSphere::getSphereIndices(int* n_indices)
141  {
142  if (n_indices == NULL)
143  {
144  LOGF("Memory error: n_indices = NULL");
145  exit(EXIT_FAILURE);
146  }
147 
148  *n_indices = sphere_n_indices;
149 
150  return sphere_indices;
151  }
152 }
#define MALLOC_CHECK(ptr_type, ptr, size)
Definition: AstcTextures.h:33
GLboolean r
Definition: gl2ext.h:306
const float vertices[]
Definition: Cube.h:30
#define LOGF(...)
Definition: AstcTextures.h:31
unsigned short * getSphereIndices(int *n_indices)
Returns sphere indices.
float * getSphereNormalData(int *normal_data_size)
Returns normal coordinates.
float * getSphereTexcoords(int *texcoords_size)
Returns texture coordinates.
GLsizei GLenum const void * indices
Definition: gl2ext.h:322
GLfloat GLfloat f
Definition: gl2ext.h:2707
SolidSphere(const float radius, const unsigned int rings, const unsigned int sectors)
Solid sphere constructor. It generates vertex position, normals and texture coordinates data based on...
Definition: SolidSphere.cpp:30
float * getSphereVertexData(int *vertex_data_size)
Returns sphere vertices.
Definition: SolidSphere.cpp:98
#define M_PI
The value of pi.
Definition: Mathematics.h:37
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
unsigned short * sphere_indices
Definition: SolidSphere.h:74
precision highp float
Definition: hiz_cull.cs:37
GLint y
Definition: gl2ext.h:179
GLfloat normals[]
Definition: Native.cpp:283