27 aabb.minpos =
vec4(0.0
f);
28 aabb.maxpos =
vec4(0.0
f);
30 GL_CHECK(glGenVertexArrays(1, &vertex_array));
31 GL_CHECK(glGenBuffers(1, &vertex_buffer));
32 GL_CHECK(glGenBuffers(1, &index_buffer));
34 GL_CHECK(glBindVertexArray(vertex_array));
36 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer));
44 static const uint16_t
indices[] = { 0, 1, 2, 3, 2, 1, };
46 GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
sizeof(vertices), vertices, GL_STATIC_DRAW));
48 GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer));
49 GL_CHECK(glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(indices), indices, GL_STATIC_DRAW));
51 GL_CHECK(glEnableVertexAttribArray(0));
52 GL_CHECK(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0));
55 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));
56 GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
64 num_elements = mesh.
ibo.size();
66 GL_CHECK(glGenVertexArrays(1, &vertex_array));
67 GL_CHECK(glGenBuffers(1, &vertex_buffer));
68 GL_CHECK(glGenBuffers(1, &index_buffer));
70 GL_CHECK(glBindVertexArray(vertex_array));
72 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer));
76 GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer));
77 GL_CHECK(glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.
ibo.size() *
sizeof(uint16_t),
78 &mesh.
ibo[0], GL_STATIC_DRAW));
81 GL_CHECK(glEnableVertexAttribArray(0));
82 GL_CHECK(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
sizeof(
Vertex),
83 reinterpret_cast<const GLvoid*>(offsetof(
Vertex, position))));
86 GL_CHECK(glEnableVertexAttribArray(1));
87 GL_CHECK(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
sizeof(
Vertex),
88 reinterpret_cast<const GLvoid*>(offsetof(
Vertex, normal))));
91 GL_CHECK(glEnableVertexAttribArray(2));
92 GL_CHECK(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE,
sizeof(
Vertex),
93 reinterpret_cast<const GLvoid*>(offsetof(
Vertex, tex))));
96 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));
97 GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
102 glDeleteVertexArrays(1, &vertex_array);
103 glDeleteBuffers(1, &vertex_buffer);
104 glDeleteBuffers(1, &index_buffer);
126 mesh.
vbo.resize(2 + vertices_per_circumference * vertices_per_circumference);
133 mesh.
vbo[vertices_per_circumference * vertices_per_circumference + 1] =
Vertex(
vec3(0, +1, 0) *
vec3(radius) + center,
vec3(0, +1, 0),
vec2(0.5
f, 1.0
f));
135 for (
unsigned y = 0;
y < vertices_per_circumference;
y++)
137 for (
unsigned x = 0;
x < vertices_per_circumference;
x++)
139 float pos_y = sin(
PI * (
float(
y + 1) / (vertices_per_circumference + 1) - 0.5
f));
140 float xz_mod = sqrt(1.0
f - pos_y * pos_y);
141 float radians_rot_y = 2.0f *
PI *
x / vertices_per_circumference;
142 float pos_x = xz_mod * cos(radians_rot_y);
143 float pos_z = xz_mod * -sin(radians_rot_y);
145 vec3 normal(pos_x, pos_y, pos_z);
147 mesh.
vbo[
y * vertices_per_circumference +
x + 1] =
Vertex(
148 vec3(radius) * normal + center,
150 vec2(
float(
x) / vertices_per_circumference, (
y + 1.0
f) / (vertices_per_circumference + 1))
156 for (
unsigned x = 0;
x < vertices_per_circumference - 1;
x++)
158 mesh.
ibo.push_back(
x + 1);
159 mesh.
ibo.push_back(0);
160 mesh.
ibo.push_back(
x + 2);
162 mesh.
ibo.push_back(vertices_per_circumference);
163 mesh.
ibo.push_back(0);
164 mesh.
ibo.push_back(1);
167 for (
unsigned y = 0;
y < vertices_per_circumference - 1;
y++)
169 for (
unsigned x = 0;
x < vertices_per_circumference - 1;
x++)
171 unsigned base = 1 +
y * vertices_per_circumference +
x;
172 mesh.
ibo.push_back(base);
173 mesh.
ibo.push_back(base + 1);
174 mesh.
ibo.push_back(base + vertices_per_circumference);
175 mesh.
ibo.push_back(base + vertices_per_circumference + 1);
176 mesh.
ibo.push_back(base + vertices_per_circumference);
177 mesh.
ibo.push_back(base + 1);
179 unsigned base = 1 +
y * vertices_per_circumference + (vertices_per_circumference - 1);
180 mesh.
ibo.push_back(base);
181 mesh.
ibo.push_back(base - (vertices_per_circumference - 1));
182 mesh.
ibo.push_back(base + vertices_per_circumference);
183 mesh.
ibo.push_back(base + 1);
184 mesh.
ibo.push_back(base + vertices_per_circumference);
185 mesh.
ibo.push_back(base - (vertices_per_circumference - 1));
189 for (
unsigned x = 0;
x < vertices_per_circumference - 1;
x++)
191 unsigned base = 1 + (vertices_per_circumference - 1) * vertices_per_circumference +
x;
192 mesh.
ibo.push_back(base);
193 mesh.
ibo.push_back(base + 1);
194 mesh.
ibo.push_back(vertices_per_circumference * vertices_per_circumference + 1);
196 mesh.
ibo.push_back(1 + (vertices_per_circumference - 1) * vertices_per_circumference + vertices_per_circumference - 1);
197 mesh.
ibo.push_back(1 + (vertices_per_circumference - 1) * vertices_per_circumference);
198 mesh.
ibo.push_back(vertices_per_circumference * vertices_per_circumference + 1);
208 static const Vertex vertex_data[] = {
240 static const GLushort
indices[] = {
266 for (
unsigned i = 0; i <
sizeof(vertex_data) /
sizeof(
Vertex); i++)
268 const Vertex &vert = vertex_data[i];
269 vec3 pos = half_position + half_distance * vert.
position;
unsigned get_num_elements() const
Mesh create_box_mesh(const AABB &aabb)
GLuint get_vertex_array() const
GLsizei GLenum const void * indices
GLint GLint GLint GLint GLint x
const AABB & get_aabb() const
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
Mesh create_sphere_mesh(float radius, vec3 center, unsigned vertices_per_circumference)