OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
primitives.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 "primitives.h"
22 
24 {
25  const float hs = 1.0f;
26 
27  // All faces are oriented counter-clockwise outwards
28  float vertices[] = {
29  // Front
30  -hs, -hs, hs, 0.0f, 0.0f, 1.0f,
31  hs, -hs, hs, 0.0f, 0.0f, 1.0f,
32  hs, hs, hs, 0.0f, 0.0f, 1.0f,
33  -hs, hs, hs, 0.0f, 0.0f, 1.0f,
34 
35  // Back
36  hs, -hs, -hs, 0.0f, 0.0f, -1.0f,
37  -hs, -hs, -hs, 0.0f, 0.0f, -1.0f,
38  -hs, hs, -hs, 0.0f, 0.0f, -1.0f,
39  hs, hs, -hs, 0.0f, 0.0f, -1.0f,
40 
41  // Left
42  -hs, -hs, -hs, -1.0f, 0.0f, 0.0f,
43  -hs, -hs, hs, -1.0f, 0.0f, 0.0f,
44  -hs, hs, hs, -1.0f, 0.0f, 0.0f,
45  -hs, hs, -hs, -1.0f, 0.0f, 0.0f,
46 
47  // Right
48  hs, -hs, hs, 1.0f, 0.0f, 0.0f,
49  hs, -hs, -hs, 1.0f, 0.0f, 0.0f,
50  hs, hs, -hs, 1.0f, 0.0f, 0.0f,
51  hs, hs, hs, 1.0f, 0.0f, 0.0f,
52 
53  // Top
54  -hs, hs, hs, 0.0f, 1.0f, 0.0f,
55  hs, hs, hs, 0.0f, 1.0f, 0.0f,
56  hs, hs, -hs, 0.0f, 1.0f, 0.0f,
57  -hs, hs, -hs, 0.0f, 1.0f, 0.0f,
58 
59  // Bottom
60  hs, -hs, hs, 0.0f, -1.0f, 0.0f,
61  -hs, -hs, hs, 0.0f, -1.0f, 0.0f,
62  -hs, -hs, -hs, 0.0f, -1.0f, 0.0f,
63  hs, -hs, -hs, 0.0f, -1.0f, 0.0f
64  };
65 
66  uint32 indices[] = {
67  0, 1, 2, 2, 3, 0, // Front
68  4, 5, 6, 6, 7, 4, // Back
69  8, 9, 10, 10, 11, 8, // Left
70  12, 13, 14, 14, 15, 12, // Right
71  16, 17, 18, 18, 19, 16, // Top
72  20, 21, 22, 22, 23, 20 // Bottom
73  };
74 
75  Mesh mesh;
76  mesh.vertex_buffer = gen_buffer(GL_ARRAY_BUFFER, sizeof(vertices), vertices);
77  mesh.index_buffer = gen_buffer(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices);
78  mesh.num_indices = 36;
79  mesh.num_vertices = 24;
80  return mesh;
81 }
82 
84 {
85  const float hs = 1.0f;
86 
87  // All faces are oriented counter-clockwise outwards
88  float vertices[] = {
89  -hs, 0.0f, -hs, 0.0f, 1.0f, 0.0f,
90  hs, 0.0f, -hs, 0.0f, 1.0f, 0.0f,
91  hs, 0.0f, hs, 0.0f, 1.0f, 0.0f,
92  -hs, 0.0f, hs, 0.0f, 1.0f, 0.0f
93  };
94 
95  uint32 indices[] = { 0, 1, 2, 2, 3, 0 };
96 
97  Mesh mesh;
98  mesh.vertex_buffer = gen_buffer(GL_ARRAY_BUFFER, sizeof(vertices), vertices);
99  mesh.index_buffer = gen_buffer(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices);
100  mesh.num_indices = 6;
101  mesh.num_vertices = 4;
102  return mesh;
103 }
104 
106 {
107  const float hs = 1.0f;
108 
109  // All faces are oriented counter-clockwise outwards
110  float vertices[] = {
111  -hs, -hs, 0.0f,
112  hs, -hs, 0.0f,
113  hs, hs, 0.0f,
114  -hs, hs, 0.0f
115  };
116 
117  uint32 indices[] = { 0, 1, 2, 2, 3, 0 };
118 
119  Mesh mesh;
120  mesh.vertex_buffer = gen_buffer(GL_ARRAY_BUFFER, sizeof(vertices), vertices);
121  mesh.index_buffer = gen_buffer(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices);
122  mesh.num_indices = 6;
123  mesh.num_vertices = 4;
124  return mesh;
125 }
126 
127 Mesh gen_normal_sphere(int t_samples, int s_samples)
128 {
129  struct VertexType
130  {
131  VertexType() { }
132  VertexType(vec3 P, vec3 N) : position(P), normal(N) { }
133  vec3 position;
134  vec3 normal;
135  };
136 
137  VertexType *vertices = new VertexType[t_samples * s_samples * 4];
138  uint32 *indices = new uint32[t_samples * s_samples * 6];
139 
140  uint32 vertex_index = 0;
141  uint32 index_index = 0;
142  float dtheta = 2.0f * PI / float(t_samples);
143  float dphi = PI / float(s_samples);
144  for (int t = 0; t < t_samples; ++t)
145  {
146  for (int s = 0; s < s_samples; ++s)
147  {
148  float theta = t * dtheta;
149  float phi = s * dphi;
150 
151  float r0 = sin(phi);
152  float r1 = sin(phi + dphi);
153 
154  vec3 v00(r0 * cos(theta), cos(phi), r0 * sin(theta));
155  vec3 v10(r0 * cos(theta + dtheta), cos(phi), r0 * sin(theta + dtheta));
156  vec3 v01(r1 * cos(theta), cos(phi + dphi), r1 * sin(theta));
157  vec3 v11(r1 * cos(theta + dtheta), cos(phi + dphi), r1 * sin(theta + dtheta));
158 
159  vertices[vertex_index + 0] = VertexType(v00, normalize(v00));
160  vertices[vertex_index + 1] = VertexType(v10, normalize(v10));
161  vertices[vertex_index + 2] = VertexType(v11, normalize(v11));
162  vertices[vertex_index + 3] = VertexType(v01, normalize(v01));
163 
164  indices[index_index + 0] = vertex_index + 0;
165  indices[index_index + 1] = vertex_index + 1;
166  indices[index_index + 2] = vertex_index + 2;
167  indices[index_index + 3] = vertex_index + 2;
168  indices[index_index + 4] = vertex_index + 3;
169  indices[index_index + 5] = vertex_index + 0;
170 
171  vertex_index += 4;
172  index_index += 6;
173  }
174  }
175 
176  Mesh mesh;
177  mesh.vertex_buffer = gen_buffer(GL_ARRAY_BUFFER, t_samples * s_samples * 4 * sizeof(VertexType), vertices);
178  mesh.index_buffer = gen_buffer(GL_ELEMENT_ARRAY_BUFFER, t_samples * s_samples * 6 * sizeof(uint32), indices);
179  mesh.num_indices = index_index;
180  mesh.num_vertices = vertex_index;
181  return mesh;
182 }
183 
184 void Mesh::dispose()
185 {
188 }
189 
190 void Mesh::bind()
191 {
192  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
193  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
194 }
int num_vertices
Definition: primitives.h:31
float phi(vec3 p)
Definition: update.cs:59
Definition: matrix.h:51
const float vertices[]
Definition: Cube.h:30
void del_buffer(GLuint buffer)
Definition: glutil.cpp:137
Mesh gen_normal_plane()
Definition: primitives.cpp:23
#define PI
Definition: matrix.h:24
Mesh gen_normal_sphere(int t_samples, int s_samples)
Definition: primitives.cpp:127
void bind()
Definition: primitives.cpp:122
GLuint index_buffer
Definition: primitives.h:29
Mesh gen_quad()
Definition: primitives.cpp:105
unsigned int uint32
Definition: common.h:32
int num_indices
Definition: primitives.h:30
static vec3 normalize(const vec3 &v)
Definition: matrix.h:154
GLsizei GLenum const void * indices
Definition: gl2ext.h:322
void dispose()
Definition: primitives.cpp:116
GLuint vertex_buffer
Definition: primitives.h:28
GLuint gen_buffer(GLenum target, GLenum usage, GLsizei size, const void *data)
Definition: glutil.cpp:122
Mesh gen_normal_cube()
Definition: primitives.cpp:23
precision highp float
Definition: hiz_cull.cs:37
static Mesh * mesh[2]
Definition: ocean.cpp:59
#define N
Definition: geometry.cpp:25