Quellcode durchsuchen

Added a non-templated "add" method to MeshBatch so Lua can call MeshBatch::add()
Also updated the lua bindings.

Darryl Gough vor 13 Jahren
Ursprung
Commit
fab194fea2

+ 66 - 0
gameplay/src/MeshBatch.cpp

@@ -1,5 +1,6 @@
 #include "Base.h"
 #include "Base.h"
 #include "MeshBatch.h"
 #include "MeshBatch.h"
+#include "Material.h"
 
 
 namespace gameplay
 namespace gameplay
 {
 {
@@ -42,6 +43,66 @@ MeshBatch* MeshBatch::create(const VertexFormat& vertexFormat, Mesh::PrimitiveTy
     return batch;
     return batch;
 }
 }
 
 
+void MeshBatch::add(void* vertices, size_t size, unsigned int vertexCount, unsigned short* indices, unsigned int indexCount)
+{
+    GP_ASSERT(vertices);
+    
+    unsigned int newVertexCount = _vertexCount + vertexCount;
+    unsigned int newIndexCount = _indexCount + indexCount;
+    if (_primitiveType == Mesh::TRIANGLE_STRIP && _vertexCount > 0)
+        newIndexCount += 2; // need an extra 2 indices for connecting strips with degenerate triangles
+    
+    // Do we need to grow the batch?
+    while (newVertexCount > _vertexCapacity || (_indexed && newIndexCount > _indexCapacity))
+    {
+        if (_growSize == 0)
+            return; // growing disabled, just clip batch
+        if (!resize(_capacity + _growSize))
+            return; // failed to grow
+    }
+    
+    // Copy vertex data.
+    GP_ASSERT(_verticesPtr);
+    unsigned int vBytes = vertexCount * _vertexFormat.getVertexSize();
+    memcpy(_verticesPtr, vertices, vBytes);
+    
+    // Copy index data.
+    if (_indexed)
+    {
+        GP_ASSERT(indices);
+        GP_ASSERT(_indicesPtr);
+
+        if (_vertexCount == 0)
+        {
+            // Simply copy values directly into the start of the index array.
+            memcpy(_indicesPtr, indices, indexCount * sizeof(unsigned short));
+        }
+        else
+        {
+            if (_primitiveType == Mesh::TRIANGLE_STRIP)
+            {
+                // Create a degenerate triangle to connect separate triangle strips
+                // by duplicating the previous and next vertices.
+                _indicesPtr[0] = *(_indicesPtr-1);
+                _indicesPtr[1] = _vertexCount;
+                _indicesPtr += 2;
+            }
+            
+            // Loop through all indices and insert them, with their values offset by
+            // 'vertexCount' so that they are relative to the first newly inserted vertex.
+            for (unsigned int i = 0; i < indexCount; ++i)
+            {
+                _indicesPtr[i] = indices[i] + _vertexCount;
+            }
+        }
+        _indicesPtr += indexCount;
+        _indexCount = newIndexCount;
+    }
+    
+    _verticesPtr += vBytes;
+    _vertexCount = newVertexCount;
+}
+
 void MeshBatch::updateVertexAttributeBinding()
 void MeshBatch::updateVertexAttributeBinding()
 {
 {
     GP_ASSERT(_material);
     GP_ASSERT(_material);
@@ -155,6 +216,11 @@ bool MeshBatch::resize(unsigned int capacity)
 
 
     return true;
     return true;
 }
 }
+
+void MeshBatch::add(float* vertices, unsigned int vertexCount, unsigned short* indices, unsigned int indexCount)
+{
+    add(vertices, sizeof(float), vertexCount, indices, indexCount);
+}
     
     
 void MeshBatch::start()
 void MeshBatch::start()
 {
 {

+ 25 - 1
gameplay/src/MeshBatch.h

@@ -2,11 +2,12 @@
 #define MESHBATCH_H_
 #define MESHBATCH_H_
 
 
 #include "Mesh.h"
 #include "Mesh.h"
-#include "Material.h"
 
 
 namespace gameplay
 namespace gameplay
 {
 {
 
 
+class Material;
+
 /**
 /**
  * Defines a class for rendering multiple mesh into a single draw call on the graphics device.
  * Defines a class for rendering multiple mesh into a single draw call on the graphics device.
  */
  */
@@ -92,6 +93,27 @@ public:
     template <class T>
     template <class T>
     void add(T* vertices, unsigned int vertexCount, unsigned short* indices = NULL, unsigned int indexCount = 0);
     void add(T* vertices, unsigned int vertexCount, unsigned short* indices = NULL, unsigned int indexCount = 0);
 
 
+    /**
+     * Adds a group of primitives to the batch.
+     *
+     * The vertex list passed in should be a pointer of floats where every X floats represent a
+     * single vertex (e.g. {x,y,z,u,v}).
+     *
+     * If the batch was created with 'indexed' set to true, then valid index data should be
+     * passed in this method. However, if 'indexed' was set to false, the indices and indexCount
+     * parameters can be omitted since only vertex data will be used.
+     *
+     * If the batch created to draw triangle strips, this method assumes that separate calls to
+     * add specify separate triangle strips. In this case, this method will automatically stitch
+     * separate triangle strips together using degenerate (zero-area) triangles.
+     *
+     * @param vertices Array of vertices.
+     * @param vertexCount Number of vertices.
+     * @param indices Array of indices into the vertex array (should be NULL for non-indexed batches).
+     * @param indexCount Number of indices (should be zero for non-indexed batches).
+     */
+    void add(float* vertices, unsigned int vertexCount, unsigned short* indices = NULL, unsigned int indexCount = 0);
+
     /**
     /**
      * Starts batching.
      * Starts batching.
      *
      *
@@ -131,6 +153,8 @@ private:
      */
      */
     MeshBatch& operator=(const MeshBatch&);
     MeshBatch& operator=(const MeshBatch&);
 
 
+    void add(void* vertices, size_t size, unsigned int vertexCount, unsigned short* indices, unsigned int indexCount);
+
     void updateVertexAttributeBinding();
     void updateVertexAttributeBinding();
 
 
     bool resize(unsigned int capacity);
     bool resize(unsigned int capacity);

+ 1 - 56
gameplay/src/MeshBatch.inl

@@ -11,63 +11,8 @@ Material* MeshBatch::getMaterial() const
 template <class T>
 template <class T>
 void MeshBatch::add(T* vertices, unsigned int vertexCount, unsigned short* indices, unsigned int indexCount)
 void MeshBatch::add(T* vertices, unsigned int vertexCount, unsigned short* indices, unsigned int indexCount)
 {
 {
-    GP_ASSERT(vertices);
     GP_ASSERT(sizeof(T) == _vertexFormat.getVertexSize());
     GP_ASSERT(sizeof(T) == _vertexFormat.getVertexSize());
-    
-    unsigned int newVertexCount = _vertexCount + vertexCount;
-    unsigned int newIndexCount = _indexCount + indexCount;
-    if (_primitiveType == Mesh::TRIANGLE_STRIP && _vertexCount > 0)
-        newIndexCount += 2; // need an extra 2 indices for connecting strips with degenerate triangles
-    
-    // Do we need to grow the batch?
-    while (newVertexCount > _vertexCapacity || (_indexed && newIndexCount > _indexCapacity))
-    {
-        if (_growSize == 0)
-            return; // growing disabled, just clip batch
-        if (!resize(_capacity + _growSize))
-            return; // failed to grow
-    }
-    
-    // Copy vertex data.
-    GP_ASSERT(_verticesPtr);
-    unsigned int vBytes = vertexCount * _vertexFormat.getVertexSize();
-    memcpy(_verticesPtr, vertices, vBytes);
-    
-    // Copy index data.
-    if (_indexed)
-    {
-        GP_ASSERT(indices);
-        GP_ASSERT(_indicesPtr);
-
-        if (_vertexCount == 0)
-        {
-            // Simply copy values directly into the start of the index array.
-            memcpy(_indicesPtr, indices, indexCount * sizeof(unsigned short));
-        }
-        else
-        {
-            if (_primitiveType == Mesh::TRIANGLE_STRIP)
-            {
-                // Create a degenerate triangle to connect separate triangle strips
-                // by duplicating the previous and next vertices.
-                _indicesPtr[0] = *(_indicesPtr-1);
-                _indicesPtr[1] = _vertexCount;
-                _indicesPtr += 2;
-            }
-            
-            // Loop through all indices and insert them, with their values offset by
-            // 'vertexCount' so that they are relative to the first newly inserted vertex.
-            for (unsigned int i = 0; i < indexCount; ++i)
-            {
-                _indicesPtr[i] = indices[i] + _vertexCount;
-            }
-        }
-        _indicesPtr += indexCount;
-        _indexCount = newIndexCount;
-    }
-    
-    _verticesPtr += vBytes;
-    _vertexCount = newVertexCount;
+    add(vertices, sizeof(T), vertexCount, indices, indexCount);
 }
 }
 
 
 }
 }

+ 1 - 0
gameplay/src/lua/lua_Gamepad.cpp

@@ -5,6 +5,7 @@
 #include "Button.h"
 #include "Button.h"
 #include "Game.h"
 #include "Game.h"
 #include "Gamepad.h"
 #include "Gamepad.h"
+#include "Platform.h"
 #include "lua_GamepadButtonMapping.h"
 #include "lua_GamepadButtonMapping.h"
 
 
 namespace gameplay
 namespace gameplay

+ 98 - 0
gameplay/src/lua/lua_MeshBatch.cpp

@@ -2,6 +2,7 @@
 #include "ScriptController.h"
 #include "ScriptController.h"
 #include "lua_MeshBatch.h"
 #include "lua_MeshBatch.h"
 #include "Base.h"
 #include "Base.h"
+#include "Material.h"
 #include "MeshBatch.h"
 #include "MeshBatch.h"
 #include "lua_MeshPrimitiveType.h"
 #include "lua_MeshPrimitiveType.h"
 
 
@@ -12,6 +13,7 @@ void luaRegister_MeshBatch()
 {
 {
     const luaL_Reg lua_members[] = 
     const luaL_Reg lua_members[] = 
     {
     {
+        {"add", lua_MeshBatch_add},
         {"draw", lua_MeshBatch_draw},
         {"draw", lua_MeshBatch_draw},
         {"finish", lua_MeshBatch_finish},
         {"finish", lua_MeshBatch_finish},
         {"getCapacity", lua_MeshBatch_getCapacity},
         {"getCapacity", lua_MeshBatch_getCapacity},
@@ -75,6 +77,102 @@ int lua_MeshBatch__gc(lua_State* state)
     return 0;
     return 0;
 }
 }
 
 
+int lua_MeshBatch_add(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 3:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                (lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TLIGHTUSERDATA) &&
+                lua_type(state, 3) == LUA_TNUMBER)
+            {
+                // Get parameter 1 off the stack.
+                ScriptUtil::LuaArray<float> param1 = ScriptUtil::getFloatPointer(2);
+
+                // Get parameter 2 off the stack.
+                unsigned int param2 = (unsigned int)luaL_checkunsigned(state, 3);
+
+                MeshBatch* instance = getInstance(state);
+                instance->add(param1, param2);
+                
+                return 0;
+            }
+
+            lua_pushstring(state, "lua_MeshBatch_add - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        case 4:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                (lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TLIGHTUSERDATA) &&
+                lua_type(state, 3) == LUA_TNUMBER &&
+                (lua_type(state, 4) == LUA_TTABLE || lua_type(state, 4) == LUA_TLIGHTUSERDATA))
+            {
+                // Get parameter 1 off the stack.
+                ScriptUtil::LuaArray<float> param1 = ScriptUtil::getFloatPointer(2);
+
+                // Get parameter 2 off the stack.
+                unsigned int param2 = (unsigned int)luaL_checkunsigned(state, 3);
+
+                // Get parameter 3 off the stack.
+                ScriptUtil::LuaArray<unsigned short> param3 = ScriptUtil::getUnsignedShortPointer(4);
+
+                MeshBatch* instance = getInstance(state);
+                instance->add(param1, param2, param3);
+                
+                return 0;
+            }
+
+            lua_pushstring(state, "lua_MeshBatch_add - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        case 5:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                (lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TLIGHTUSERDATA) &&
+                lua_type(state, 3) == LUA_TNUMBER &&
+                (lua_type(state, 4) == LUA_TTABLE || lua_type(state, 4) == LUA_TLIGHTUSERDATA) &&
+                lua_type(state, 5) == LUA_TNUMBER)
+            {
+                // Get parameter 1 off the stack.
+                ScriptUtil::LuaArray<float> param1 = ScriptUtil::getFloatPointer(2);
+
+                // Get parameter 2 off the stack.
+                unsigned int param2 = (unsigned int)luaL_checkunsigned(state, 3);
+
+                // Get parameter 3 off the stack.
+                ScriptUtil::LuaArray<unsigned short> param3 = ScriptUtil::getUnsignedShortPointer(4);
+
+                // Get parameter 4 off the stack.
+                unsigned int param4 = (unsigned int)luaL_checkunsigned(state, 5);
+
+                MeshBatch* instance = getInstance(state);
+                instance->add(param1, param2, param3, param4);
+                
+                return 0;
+            }
+
+            lua_pushstring(state, "lua_MeshBatch_add - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 3, 4 or 5).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_MeshBatch_draw(lua_State* state)
 int lua_MeshBatch_draw(lua_State* state)
 {
 {
     // Get the number of parameters.
     // Get the number of parameters.

+ 1 - 0
gameplay/src/lua/lua_MeshBatch.h

@@ -6,6 +6,7 @@ namespace gameplay
 
 
 // Lua bindings for MeshBatch.
 // Lua bindings for MeshBatch.
 int lua_MeshBatch__gc(lua_State* state);
 int lua_MeshBatch__gc(lua_State* state);
+int lua_MeshBatch_add(lua_State* state);
 int lua_MeshBatch_draw(lua_State* state);
 int lua_MeshBatch_draw(lua_State* state);
 int lua_MeshBatch_finish(lua_State* state);
 int lua_MeshBatch_finish(lua_State* state);
 int lua_MeshBatch_getCapacity(lua_State* state);
 int lua_MeshBatch_getCapacity(lua_State* state);

+ 1 - 0
gameplay/src/lua/lua_Terrain.cpp

@@ -11,6 +11,7 @@
 #include "ScriptController.h"
 #include "ScriptController.h"
 #include "ScriptTarget.h"
 #include "ScriptTarget.h"
 #include "Terrain.h"
 #include "Terrain.h"
+#include "TerrainPatch.h"
 #include "Transform.h"
 #include "Transform.h"
 #include "lua_CurveInterpolationType.h"
 #include "lua_CurveInterpolationType.h"
 #include "lua_TerrainFlags.h"
 #include "lua_TerrainFlags.h"