implement heightmap grid
This commit is contained in:
109
engine/shape.cpp
109
engine/shape.cpp
@@ -4,7 +4,21 @@
|
||||
|
||||
using namespace ui;
|
||||
|
||||
bool Shape::create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize)
|
||||
bool ui::Shape::create_buffers(GLushort * idx, GLvoid * vertices, int isize, int vsize)
|
||||
{
|
||||
index_type = GL_UNSIGNED_SHORT;
|
||||
create_buffers_imp(idx, vertices, isize, vsize);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ui::Shape::create_buffers(GLuint* idx, GLvoid * vertices, int isize, int vsize)
|
||||
{
|
||||
index_type = GL_UNSIGNED_INT;
|
||||
create_buffers_imp(idx, vertices, isize, vsize);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Shape::create_buffers_imp(GLvoid* idx, GLvoid* vertices, int isize, int vsize)
|
||||
{
|
||||
use_idx = true;
|
||||
|
||||
@@ -80,6 +94,8 @@ bool Shape::create_buffers(GLvoid* vertices, int vsize)
|
||||
}
|
||||
void Shape::draw_fill() const
|
||||
{
|
||||
if (count[0] == 0) return;
|
||||
|
||||
GLenum type = GL_TRIANGLES;
|
||||
if (count[0] == 1)
|
||||
type = GL_POINTS;
|
||||
@@ -88,7 +104,7 @@ void Shape::draw_fill() const
|
||||
#if USE_VBO
|
||||
glBindVertexArray(arrays[0]);
|
||||
if (use_idx)
|
||||
glDrawElements(type, count[0], GL_UNSIGNED_SHORT, ioff[0]);
|
||||
glDrawElements(type, count[0], index_type, ioff[0]);
|
||||
else
|
||||
glDrawArrays(type, 0, count[0]);
|
||||
glBindVertexArray(0);
|
||||
@@ -113,13 +129,15 @@ void Shape::draw_fill() const
|
||||
}
|
||||
void Shape::draw_stroke() const
|
||||
{
|
||||
if (count[0] == 0) return;
|
||||
|
||||
GLenum type = GL_LINES;
|
||||
if (count[1] == 1)
|
||||
type = GL_POINTS;
|
||||
#if USE_VBO
|
||||
glBindVertexArray(arrays[1]);
|
||||
if (use_idx)
|
||||
glDrawElements(type, count[1], GL_UNSIGNED_SHORT, ioff[1]);
|
||||
glDrawElements(type, count[1], index_type, ioff[1]);
|
||||
else
|
||||
glDrawArrays(type, 0, count[1]);
|
||||
glBindVertexArray(0);
|
||||
@@ -219,17 +237,82 @@ void Plane::create_impl(float w, float h, int div, GLushort *idx, Shape::vertex_
|
||||
*idx++ = y;
|
||||
*idx++ = y + div * (div + 1);
|
||||
}
|
||||
//
|
||||
// // outline indices
|
||||
// *idx++ = 0; // A
|
||||
// *idx++ = (div+1)*(div); // B
|
||||
// *idx++ = (div+1)*(div); // B
|
||||
// *idx++ = (div+1)*(div+1)-1; // C
|
||||
// *idx++ = (div+1)*(div+1)-1; // C
|
||||
// *idx++ = div; // D
|
||||
// *idx++ = div; // D
|
||||
// *idx++ = 0; // A
|
||||
}
|
||||
|
||||
bool ui::HeightmapPlane::create(float w, float h, const Image& img, float scale)
|
||||
{
|
||||
int div = img.width - 1; // TODO: handle height also
|
||||
int idx_size = (div * div * 6) + (div * (div + 1) * 4);
|
||||
int vertices_size = (div + 1)*(div + 1);
|
||||
auto idx = std::make_unique<GLuint[]>(idx_size);
|
||||
auto vertices = std::make_unique<vertex_t[]>(vertices_size);
|
||||
|
||||
count[0] = div * div * 6;
|
||||
count[1] = div * (div + 1) * 4;
|
||||
ioff[0] = (GLvoid*)0;
|
||||
ioff[1] = (GLvoid*)(count[0] * sizeof(GLuint));
|
||||
|
||||
auto pv = vertices.get();
|
||||
auto pi = idx.get();
|
||||
auto px = (glm::u8vec4*)img.data();
|
||||
|
||||
const float dx = w / div;
|
||||
const float dy = h / div;
|
||||
const float ox = -w * 0.5f;
|
||||
const float oy = -h * 0.5f;
|
||||
for (int y = 0; y <= div; y++)
|
||||
{
|
||||
for (int x = 0; x <= div; x++)
|
||||
{
|
||||
vertex_t v;
|
||||
v.pos.x = ox + dx * (float)x;
|
||||
v.pos.y = oy + dy * (float)y;
|
||||
v.pos.z = (*px++).r / 255.f * scale;
|
||||
v.pos.w = 1;
|
||||
v.uvs = glm::vec2(x, y) / (float)div;
|
||||
*pv++ = v;
|
||||
}
|
||||
}
|
||||
|
||||
// generate indices
|
||||
for (int y = 0; y < div; y++)
|
||||
{
|
||||
int i = y * (div + 1);
|
||||
for (int x = 0; x < div; x++)
|
||||
{
|
||||
*pi++ = i;
|
||||
*pi++ = i + div + 1;
|
||||
*pi++ = i + div + 2;
|
||||
*pi++ = i;
|
||||
*pi++ = i + div + 2;
|
||||
*pi++ = i + 1;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// generate indices
|
||||
for (int y = 0; y <= div; y++)
|
||||
{
|
||||
int i = y * (div + 1);
|
||||
for (int x = 0; x <= div; x++)
|
||||
{
|
||||
if (x < div)
|
||||
{
|
||||
*pi++ = y * (div + 1) + x;
|
||||
*pi++ = y * (div + 1) + x + 1;
|
||||
}
|
||||
|
||||
if (y < div)
|
||||
{
|
||||
*pi++ = y * (div + 1) + x;
|
||||
*pi++ = (y + 1) * (div + 1) + x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return create_buffers(idx.get(), vertices.get(), sizeof(GLuint) * idx_size, sizeof(vertex_t) * vertices_size);
|
||||
}
|
||||
|
||||
void ui::Plane::update_vertices(const glm::vec4* data, const glm::vec2* uvs, const glm::vec2* uvs2)
|
||||
{
|
||||
static vertex_t vertices[4];
|
||||
|
||||
Reference in New Issue
Block a user