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  -hs, 0.0f, -hs, 0.0f, 1.0f, 0.0f,
30  hs, 0.0f, -hs, 0.0f, 1.0f, 0.0f,
31  hs, 0.0f, hs, 0.0f, 1.0f, 0.0f,
32  -hs, 0.0f, hs, 0.0f, 1.0f, 0.0f
33  };
34 
35  uint32 indices[] = { 0, 1, 2, 2, 3, 0 };
36 
37  Mesh mesh;
38  mesh.vertex_buffer = gen_buffer(GL_ARRAY_BUFFER, sizeof(vertices), vertices);
39  mesh.index_buffer = gen_buffer(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices);
40  mesh.num_indices = 6;
41  mesh.num_vertices = 4;
42  return mesh;
43 }
44 
46 {
47  const float hs = 1.0f;
48 
49  // All faces are oriented counter-clockwise outwards
50  float vertices[] = {
51  -hs, -hs, 0.0f, 0.0f, 0.0f,
52  hs, -hs, 0.0f, 1.0f, 0.0f,
53  hs, hs, 0.0f, 1.0f, 1.0f,
54  -hs, hs, 0.0f, 0.0f, 1.0f
55  };
56 
57  uint32 indices[] = { 0, 1, 2, 2, 3, 0 };
58 
59  Mesh mesh;
60  mesh.vertex_buffer = gen_buffer(GL_ARRAY_BUFFER, sizeof(vertices), vertices);
61  mesh.index_buffer = gen_buffer(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices);
62  mesh.num_indices = 6;
63  mesh.num_vertices = 4;
64  return mesh;
65 }
66 
67 Mesh gen_unit_sphere(int t_samples, int s_samples)
68 {
69  vec3 *vertices = new vec3[t_samples * s_samples * 4];
70  uint32 *indices = new uint32[t_samples * s_samples * 6];
71 
72  uint32 vertex_index = 0;
73  uint32 index_index = 0;
74  float dtheta = 2.0f * PI / float(t_samples);
75  float dphi = PI / float(s_samples);
76  for (int t = 0; t < t_samples; ++t)
77  {
78  for (int s = 0; s < s_samples; ++s)
79  {
80  float theta = t * dtheta;
81  float phi = s * dphi;
82 
83  float r0 = sin(phi);
84  float r1 = sin(phi + dphi);
85 
86  vec3 v00(r0 * cos(theta), cos(phi), r0 * sin(theta));
87  vec3 v10(r0 * cos(theta + dtheta), cos(phi), r0 * sin(theta + dtheta));
88  vec3 v01(r1 * cos(theta), cos(phi + dphi), r1 * sin(theta));
89  vec3 v11(r1 * cos(theta + dtheta), cos(phi + dphi), r1 * sin(theta + dtheta));
90 
91  vertices[vertex_index + 0] = v00;
92  vertices[vertex_index + 1] = v01;
93  vertices[vertex_index + 2] = v11;
94  vertices[vertex_index + 3] = v10;
95 
96  indices[index_index + 0] = vertex_index + 0;
97  indices[index_index + 1] = vertex_index + 1;
98  indices[index_index + 2] = vertex_index + 2;
99  indices[index_index + 3] = vertex_index + 2;
100  indices[index_index + 4] = vertex_index + 3;
101  indices[index_index + 5] = vertex_index + 0;
102 
103  vertex_index += 4;
104  index_index += 6;
105  }
106  }
107 
108  Mesh mesh;
109  mesh.vertex_buffer = gen_buffer(GL_ARRAY_BUFFER, t_samples * s_samples * 4 * sizeof(vec3), vertices);
110  mesh.index_buffer = gen_buffer(GL_ELEMENT_ARRAY_BUFFER, t_samples * s_samples * 6 * sizeof(uint32), indices);
111  mesh.num_indices = index_index;
112  mesh.num_vertices = vertex_index;
113  return mesh;
114 }
115 
117 {
120 }
121 
123 {
124  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
125  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
126 }
int num_vertices
Definition: primitives.h:31
float phi(vec3 p)
Definition: update.cs:59
Definition: matrix.h:51
Mesh gen_unit_sphere(int t_samples, int s_samples)
Definition: primitives.cpp:67
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
void bind()
Definition: primitives.cpp:122
GLuint index_buffer
Definition: primitives.h:29
unsigned int uint32
Definition: common.h:32
int num_indices
Definition: primitives.h:30
GLsizei GLenum const void * indices
Definition: gl2ext.h:322
Mesh gen_tex_quad()
Definition: primitives.cpp:45
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
precision highp float
Definition: hiz_cull.cs:37
static Mesh * mesh[2]
Definition: ocean.cpp:59