Fixed Mesh:setVertexMap when the mesh has more than 2^16-1 vertices (still needs more work to make it use smaller data types when available), added Mesh:setVertices

--HG--
branch : Mesh
This commit is contained in:
Alex Szpakowski
2013-09-30 04:30:17 -03:00
parent 857296f107
commit c426998b04
5 changed files with 61 additions and 19 deletions
+12 -12
View File
@@ -31,12 +31,12 @@ namespace opengl
{
Mesh::Mesh(const std::vector<Vertex> &verts, Mesh::DrawMode mode)
: vbo(0)
: vbo(nullptr)
, vertex_count(0)
, ibo(0)
, ibo(nullptr)
, element_count(0)
, draw_mode(mode)
, image(0)
, image(nullptr)
, colors_enabled(false)
{
setVertices(verts);
@@ -58,7 +58,7 @@ void Mesh::setVertices(const std::vector<Vertex> &verts)
if (vbo && size > vbo->getSize())
{
delete vbo;
vbo = 0;
vbo = nullptr;
}
if (!vbo)
@@ -84,7 +84,7 @@ void Mesh::setVertex(size_t index, const Vertex &v)
VertexBuffer::Bind vbo_bind(*vbo);
// We unmap the vertex buffer in Mesh::draw. This lets us coalesce the
// buffer transfer calls.
// buffer transfer calls into just one.
Vertex *vertices = (Vertex *) vbo->map();
vertices[index] = v;
}
@@ -106,7 +106,7 @@ size_t Mesh::getVertexCount() const
return vertex_count;
}
void Mesh::setVertexMap(const std::vector<uint16> &map)
void Mesh::setVertexMap(const std::vector<uint32> &map)
{
for (size_t i = 0; i < map.size(); i++)
{
@@ -114,12 +114,12 @@ void Mesh::setVertexMap(const std::vector<uint16> &map)
throw love::Exception("Invalid vertex map value: %d", map[i]);
}
size_t size = sizeof(uint16) * map.size();
size_t size = sizeof(uint32) * map.size();
if (ibo && size > ibo->getSize())
{
delete ibo;
ibo = 0;
ibo = nullptr;
}
if (!ibo)
@@ -140,14 +140,14 @@ void Mesh::setVertexMap(const std::vector<uint16> &map)
}
}
const uint16 *Mesh::getVertexMap() const
const uint32 *Mesh::getVertexMap() const
{
if (ibo && element_count > 0)
{
VertexBuffer::Bind ibo_bind(*ibo);
// We unmap the buffer in Mesh::draw and Mesh::setVertexMap.
return (uint16 *) ibo->map();
return (uint32 *) ibo->map();
}
return 0;
@@ -173,7 +173,7 @@ void Mesh::setImage()
if (image)
image->release();
image = 0;
image = nullptr;
}
Image *Mesh::getImage() const
@@ -249,7 +249,7 @@ void Mesh::draw(float x, float y, float angle, float sx, float sy, float ox, flo
ibo->unmap();
// Use the custom vertex map to draw the vertices.
glDrawElements(mode, element_count, GL_UNSIGNED_SHORT, ibo->getPointer(0));
glDrawElements(mode, element_count, GL_UNSIGNED_INT, ibo->getPointer(0));
}
else
{
+2 -2
View File
@@ -90,14 +90,14 @@ public:
* A 0-element vector is equivalent to the default vertex map:
* {0, 1, 2, 3, 4, ...}
**/
void setVertexMap(const std::vector<uint16> &map);
void setVertexMap(const std::vector<uint32> &map);
/**
* Gets a pointer to the vertex map array. The pointer is only valid until
* the next function call in the graphics module.
* May return null if the vertex map is empty.
**/
const uint16 *getVertexMap() const;
const uint32 *getVertexMap() const;
/**
* Gets the total number of elements in the vertex map array.
@@ -461,7 +461,7 @@ int w_newMesh(lua_State *L)
lua_rawgeti(L, 1, i);
if (lua_type(L, -1) != LUA_TTABLE)
return luax_typerror(L, 2, "table of tables");
return luax_typerror(L, 1, "table of tables");
for (int j = 1; j <= 8; j++)
lua_rawgeti(L, -j, j);
+45 -4
View File
@@ -93,6 +93,46 @@ int w_Mesh_getVertex(lua_State *L)
return 8;
}
int w_Mesh_setVertices(lua_State *L)
{
Mesh *t = luax_checkmesh(L, 1);
size_t vertex_count = lua_objlen(L, 2);
std::vector<Vertex> vertices;
vertices.reserve(vertex_count);
// Get the vertices from the table.
for (size_t i = 1; i <= vertex_count; i++)
{
lua_rawgeti(L, 2, i);
if (lua_type(L, -1) != LUA_TTABLE)
return luax_typerror(L, 2, "table of tables");
for (int j = 1; j <= 8; j++)
lua_rawgeti(L, -j, j);
Vertex v;
v.x = (float) luaL_checknumber(L, -8);
v.y = (float) luaL_checknumber(L, -7);
v.s = (float) luaL_checknumber(L, -6);
v.t = (float) luaL_checknumber(L, -5);
v.r = (unsigned char) luaL_optinteger(L, -4, 255);
v.g = (unsigned char) luaL_optinteger(L, -3, 255);
v.b = (unsigned char) luaL_optinteger(L, -2, 255);
v.a = (unsigned char) luaL_optinteger(L, -1, 255);
lua_pop(L, 9);
vertices.push_back(v);
}
EXCEPT_GUARD(t->setVertices(vertices);)
return 0;
}
int w_Mesh_getVertexCount(lua_State *L)
{
Mesh *t = luax_checkmesh(L, 1);
@@ -107,7 +147,7 @@ int w_Mesh_setVertexMap(lua_State *L)
bool is_table = lua_istable(L, 2);
int nargs = is_table ? lua_objlen(L, 2) : lua_gettop(L) - 1;
std::vector<uint16> vertexmap;
std::vector<uint32> vertexmap;
vertexmap.reserve(nargs);
for (int i = 0; i < nargs; i++)
@@ -115,11 +155,11 @@ int w_Mesh_setVertexMap(lua_State *L)
if (is_table)
{
lua_rawgeti(L, 2, i + 1);
vertexmap.push_back(uint16(luaL_checkinteger(L, -1) - 1));
vertexmap.push_back(uint32(luaL_checkinteger(L, -1) - 1));
lua_pop(L, 1);
}
else
vertexmap.push_back(uint16(luaL_checkinteger(L, i + 2) - 1));
vertexmap.push_back(uint32(luaL_checkinteger(L, i + 2) - 1));
}
EXCEPT_GUARD(t->setVertexMap(vertexmap);)
@@ -130,7 +170,7 @@ int w_Mesh_getVertexMap(lua_State *L)
{
Mesh *t = luax_checkmesh(L, 1);
const uint16 *vertex_map = t->getVertexMap();
const uint32 *vertex_map = t->getVertexMap();
size_t elements = t->getVertexMapCount();
lua_createtable(L, elements, 0);
@@ -216,6 +256,7 @@ static const luaL_Reg functions[] =
{
{ "setVertex", w_Mesh_setVertex },
{ "getVertex", w_Mesh_getVertex },
{ "setVertices", w_Mesh_setVertices },
{ "getVertexCount", w_Mesh_getVertexCount },
{ "setVertexMap", w_Mesh_setVertexMap },
{ "getVertexMap", w_Mesh_getVertexMap },
+1
View File
@@ -36,6 +36,7 @@ Mesh *luax_checkmesh(lua_State *L, int idx);
int w_Mesh_setVertex(lua_State *L);
int w_Mesh_getVertex(lua_State *L);
int w_Mesh_setVertices(lua_State *L);
int w_Mesh_getVertexCount(lua_State *L);
int w_Mesh_setVertexMap(lua_State *L);
int w_Mesh_getVertexMap(lua_State *L);