Selaa lähdekoodia

Started treating meshes as resources, disabled skeletal animation for now, meshes dont get properly destroyed

Ivan Safrin 9 vuotta sitten
vanhempi
sitoutus
fe6ac9990b

+ 6 - 1
include/polycode/core/PolyOpenGLGraphicsInterface.h

@@ -27,6 +27,7 @@ THE SOFTWARE.
 #include "polycode/core/PolyGlobals.h"
 #include "polycode/core/PolyGlobals.h"
 #include "polycode/core/PolyRenderer.h"
 #include "polycode/core/PolyRenderer.h"
 #include "polycode/core/PolyTexture.h"
 #include "polycode/core/PolyTexture.h"
+#include "polycode/core/PolyMesh.h"
 
 
 #if PLATFORM == PLATFORM_MAC
 #if PLATFORM == PLATFORM_MAC
     
     
@@ -89,6 +90,11 @@ namespace Polycode {
         void createProgram(ShaderProgram *program);
         void createProgram(ShaderProgram *program);
         void destroyProgram(ShaderProgram *program);
         void destroyProgram(ShaderProgram *program);
         
         
+        void createVBOForVertexArray(VertexDataArray *array);
+        
+        void createMesh(Mesh *mesh);
+        void destroyMesh(Mesh *mesh);
+        
         void createShader(Shader *shader);
         void createShader(Shader *shader);
         void destroyShader(Shader *shader);
         void destroyShader(Shader *shader);
         
         
@@ -104,7 +110,6 @@ namespace Polycode {
         void createIndexBuffer(IndexDataArray *dataArray);
         void createIndexBuffer(IndexDataArray *dataArray);
         void destroyBuffer(RenderDataArray *array);
         void destroyBuffer(RenderDataArray *array);
         
         
-        
         void drawIndices(int type, IndexDataArray *indexArray);
         void drawIndices(int type, IndexDataArray *indexArray);
         void drawArrays(int type, unsigned int vertexCount);
         void drawArrays(int type, unsigned int vertexCount);
         
         

+ 8 - 0
include/polycode/core/PolyRenderer.h

@@ -54,6 +54,9 @@ namespace Polycode {
             virtual void createTexture(Texture *texture) = 0;
             virtual void createTexture(Texture *texture) = 0;
             virtual void destroyTexture(Texture *texture) = 0;
             virtual void destroyTexture(Texture *texture) = 0;
         
         
+            virtual void createMesh(Mesh *mesh) = 0;
+            virtual void destroyMesh(Mesh *mesh) = 0;
+        
             virtual void setViewport(unsigned int x,unsigned  int y,unsigned  int width, unsigned height) = 0;
             virtual void setViewport(unsigned int x,unsigned  int y,unsigned  int width, unsigned height) = 0;
             virtual void clearBuffers(const Color &clearColor, bool colorBuffer, bool depthBuffer, bool stencilBuffer) = 0;
             virtual void clearBuffers(const Color &clearColor, bool colorBuffer, bool depthBuffer, bool stencilBuffer) = 0;
             virtual void createProgram(ShaderProgram *program) = 0;
             virtual void createProgram(ShaderProgram *program) = 0;
@@ -166,6 +169,8 @@ namespace Polycode {
             static const int JOB_SET_TEXTURE_PARAM = 14;
             static const int JOB_SET_TEXTURE_PARAM = 14;
             static const int JOB_DESTROY_SHADER_BINDING = 16;
             static const int JOB_DESTROY_SHADER_BINDING = 16;
             static const int JOB_DESTROY_SHADER_PARAM = 17;
             static const int JOB_DESTROY_SHADER_PARAM = 17;
+            static const int JOB_CREATE_MESH = 18;
+            static const int JOB_DESTROY_MESH = 19;
         
         
         protected:
         protected:
         
         
@@ -232,6 +237,9 @@ namespace Polycode {
         void setAnisotropyAmount(Number amount);
         void setAnisotropyAmount(Number amount);
         Number getAnisotropyAmount();
         Number getAnisotropyAmount();
         
         
+        Mesh *createMesh(const String &fileName);
+        void destroyMesh(Mesh *mesh);
+        
         static Vector3 unProject(const Vector3 &position, const Matrix4 &modelMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport);
         static Vector3 unProject(const Vector3 &position, const Matrix4 &modelMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport);
         static Vector3 project(const Vector3 &position, const Matrix4 &modelMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport);
         static Vector3 project(const Vector3 &position, const Matrix4 &modelMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport);
         
         

+ 7 - 1
include/polycode/core/PolyResourceManager.h

@@ -121,8 +121,14 @@ namespace Polycode {
         FT_Library FTLibrary;
         FT_Library FTLibrary;
     };
     };
     
     
+    class _PolyExport MeshResourceLoader : public ResourceLoader {
+    public:
+        MeshResourceLoader();
+        Resource *loadResource(const String &path, ResourcePool *targetPool);
+    };
+    
 	/**
 	/**
-	* Manages loading and unloading of resources from directories and archives. Should only be accessed via the CoreServices singleton. 
+	* Manages loading and unloading of resources from directories and archives. Should only be accessed via the CoreServices singleton.
 	*/ 
 	*/ 
 	class _PolyExport ResourceManager : public EventDispatcher {
 	class _PolyExport ResourceManager : public EventDispatcher {
 		public:
 		public:

+ 29 - 0
src/core/PolyOpenGLGraphicsInterface.cpp

@@ -191,6 +191,35 @@ void OpenGLGraphicsInterface::setParamInShader(Shader *shader, ProgramParam *par
     }
     }
 }
 }
 
 
+void OpenGLGraphicsInterface::createVBOForVertexArray(VertexDataArray *array) {
+    if(array->getDataSize() == 0) {
+        return;
+    }
+    GLuint bufferID;
+    glGenBuffers(1, &bufferID);
+    glBindBuffer(GL_ARRAY_BUFFER, bufferID);
+    glBufferDataARB(GL_ARRAY_BUFFER, array->getDataSize() * sizeof(PolyRendererVertexType), array->getArrayData(), GL_STATIC_DRAW);
+    array->hasVBO = true;
+    array->platformData = (void*) malloc(sizeof(GLuint));
+    *((GLuint*)array->platformData) = bufferID;
+}
+
+void OpenGLGraphicsInterface::createMesh(Mesh *mesh) {
+    createVBOForVertexArray(&mesh->vertexPositionArray);
+    createVBOForVertexArray(&mesh->vertexTexCoordArray);
+    createVBOForVertexArray(&mesh->vertexNormalArray);
+    createVBOForVertexArray(&mesh->vertexColorArray);
+    createVBOForVertexArray(&mesh->vertexTangentArray);
+    createVBOForVertexArray(&mesh->vertexBoneIndexArray);
+    createVBOForVertexArray(&mesh->vertexBoneWeightArray);
+    createVBOForVertexArray(&mesh->vertexTexCoord2Array);
+    createVBOForVertexArray(&mesh->vertexBoneIndexArray);
+}
+
+void OpenGLGraphicsInterface::destroyMesh(Mesh *mesh) {
+    
+}
+
 void OpenGLGraphicsInterface::setBlendingMode(unsigned int blendingMode) {
 void OpenGLGraphicsInterface::setBlendingMode(unsigned int blendingMode) {
     if(blendingMode == Renderer::BLEND_MODE_NONE) {
     if(blendingMode == Renderer::BLEND_MODE_NONE) {
         glDisable(GL_BLEND);
         glDisable(GL_BLEND);

+ 23 - 0
src/core/PolyRenderer.cpp

@@ -464,6 +464,19 @@ void RenderThread::processJob(const RendererThreadJob &job) {
             }
             }
         }
         }
         break;
         break;
+        case JOB_CREATE_MESH:
+        {
+            Mesh *mesh = (Mesh*) job.data;
+            graphicsInterface->createMesh(mesh);
+        }
+        break;
+        case JOB_DESTROY_MESH:
+        {
+            Mesh *mesh = (Mesh*) job.data;
+            graphicsInterface->destroyMesh(mesh);
+        }
+        break;
+            
     }
     }
     unlockRenderMutex();
     unlockRenderMutex();
 }
 }
@@ -668,6 +681,16 @@ Vector3 Renderer::project(const Vector3 &position, const Matrix4 &modelMatrix, c
     return Vector3(in.x, in.y, in.z);
     return Vector3(in.x, in.y, in.z);
 }
 }
 
 
+Mesh *Renderer::createMesh(const String &fileName) {
+    Mesh *mesh = new Mesh(fileName);
+    renderThread->enqueueJob(RenderThread::JOB_CREATE_MESH, (void*)mesh);
+    return mesh;
+}
+
+void Renderer::destroyMesh(Mesh *mesh) {
+    renderThread->enqueueJob(RenderThread::JOB_DESTROY_MESH, (void*)mesh);
+}
+
 Vector3 Renderer::unProject(const Vector3 &position, const Matrix4 &modelMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport) {
 Vector3 Renderer::unProject(const Vector3 &position, const Matrix4 &modelMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport) {
 
 
     Matrix4 finalMatrix = modelMatrix * projectionMatrix;
     Matrix4 finalMatrix = modelMatrix * projectionMatrix;

+ 25 - 2
src/core/PolyResourceManager.cpp

@@ -28,7 +28,9 @@
 #include "polycode/core/PolyMaterial.h"
 #include "polycode/core/PolyMaterial.h"
 #include "polycode/core/PolyShader.h"
 #include "polycode/core/PolyShader.h"
 #include "polycode/core/PolyTexture.h"
 #include "polycode/core/PolyTexture.h"
+#include "polycode/core/PolyRenderer.h"
 #include "polycode/core/PolyFont.h"
 #include "polycode/core/PolyFont.h"
+#include "polycode/core/PolyMesh.h"
 #include "tinyxml.h"
 #include "tinyxml.h"
 
 
 using std::vector;
 using std::vector;
@@ -163,8 +165,13 @@ void ResourcePool::loadResourcesFromFolderWithLoader(const String &folder, bool
 }
 }
 
 
 Resource *ResourcePool::loadResource(const String &path) {
 Resource *ResourcePool::loadResource(const String &path) {
+    
+    Resource *newResource = getResourceByPath(path);
+    if(newResource) {
+        return newResource;
+    }
+    
     OSFileEntry entry(path, OSFileEntry::TYPE_FILE);
     OSFileEntry entry(path, OSFileEntry::TYPE_FILE);
-    Resource *newResource = NULL;
     for(int r = 0; r < Services()->getResourceManager()->getNumResourceLoaders(); r++) {
     for(int r = 0; r < Services()->getResourceManager()->getNumResourceLoaders(); r++) {
         ResourceLoader *loader = Services()->getResourceManager()->getResourceLoaderAtIndex(r);
         ResourceLoader *loader = Services()->getResourceManager()->getResourceLoaderAtIndex(r);
         if(loader->canHandleExtension(entry.extension)) {
         if(loader->canHandleExtension(entry.extension)) {
@@ -181,8 +188,13 @@ Resource *ResourcePool::loadResource(const String &path) {
 }
 }
 
 
 Resource *ResourcePool::loadResourceWithName(const String &path, const String &name) {
 Resource *ResourcePool::loadResourceWithName(const String &path, const String &name) {
+    
+    Resource *newResource = getResourceByPath(path);
+    if(newResource) {
+        return newResource;
+    }
+    
     OSFileEntry entry(path, OSFileEntry::TYPE_FILE);
     OSFileEntry entry(path, OSFileEntry::TYPE_FILE);
-    Resource *newResource = NULL;
     for(int r = 0; r < Services()->getResourceManager()->getNumResourceLoaders(); r++) {
     for(int r = 0; r < Services()->getResourceManager()->getNumResourceLoaders(); r++) {
         ResourceLoader *loader = Services()->getResourceManager()->getResourceLoaderAtIndex(r);
         ResourceLoader *loader = Services()->getResourceManager()->getResourceLoaderAtIndex(r);
         if(loader->canHandleExtension(entry.extension)) {
         if(loader->canHandleExtension(entry.extension)) {
@@ -275,6 +287,7 @@ ResourceManager::ResourceManager() : EventDispatcher() {
     resourceLoaders.push_back(new ProgramResourceLoader());
     resourceLoaders.push_back(new ProgramResourceLoader());
     resourceLoaders.push_back(new MaterialResourceLoader());
     resourceLoaders.push_back(new MaterialResourceLoader());
     resourceLoaders.push_back(new FontResourceLoader());
     resourceLoaders.push_back(new FontResourceLoader());
+    resourceLoaders.push_back(new MeshResourceLoader());
 }
 }
 
 
 ResourceManager::~ResourceManager() {
 ResourceManager::~ResourceManager() {
@@ -367,6 +380,16 @@ FontResourceLoader::~FontResourceLoader() {
     FT_Done_FreeType(FTLibrary);
     FT_Done_FreeType(FTLibrary);
 }
 }
 
 
+MeshResourceLoader::MeshResourceLoader() {
+    extensions.push_back("mesh");
+}
+
+Resource *MeshResourceLoader::loadResource(const String &path, ResourcePool *targetPool) {
+    Mesh *mesh = Services()->getRenderer()->createMesh(path);
+    return mesh;
+}
+
+
 Resource *FontResourceLoader::loadResource(const String &path, ResourcePool *targetPool) {
 Resource *FontResourceLoader::loadResource(const String &path, ResourcePool *targetPool) {
     OSFileEntry entry = OSFileEntry(path, OSFileEntry::TYPE_FILE);
     OSFileEntry entry = OSFileEntry(path, OSFileEntry::TYPE_FILE);
     Font *font = new Font(path, FTLibrary);
     Font *font = new Font(path, FTLibrary);

+ 5 - 4
src/core/PolySceneMesh.cpp

@@ -47,7 +47,6 @@ SceneMesh::SceneMesh(const String& fileName) : Entity(), material(NULL), skeleto
     loadFromFile(fileName);
     loadFromFile(fileName);
 	useVertexBuffer = false;
 	useVertexBuffer = false;
 	lineSmooth = false;
 	lineSmooth = false;
-	ownsMesh = true;
 	ownsSkeleton = true;
 	ownsSkeleton = true;
 	lineWidth = 1.0;
 	lineWidth = 1.0;
 	pointSmooth = false;
 	pointSmooth = false;
@@ -105,8 +104,8 @@ void SceneMesh::rebuildAttributes() {
     for(int i=0; i < shaderPasses.size(); i++) {
     for(int i=0; i < shaderPasses.size(); i++) {
         shaderPasses[i].setAttributeArraysFromMesh(mesh);
         shaderPasses[i].setAttributeArraysFromMesh(mesh);
         if(skeleton) {
         if(skeleton) {
-            shaderPasses[i].attributeArrays.push_back(&skeletalVertexPositions);
-            shaderPasses[i].attributeArrays.push_back(&skeletalVertexNormals);
+          //  shaderPasses[i].attributeArrays.push_back(&skeletalVertexPositions);
+          //  shaderPasses[i].attributeArrays.push_back(&skeletalVertexNormals);
         }
         }
         shaderPasses[i].shaderBinding->resetAttributes = true;
         shaderPasses[i].shaderBinding->resetAttributes = true;
     }
     }
@@ -171,7 +170,9 @@ void SceneMesh::loadFromFile(String fileName) {
     if(mesh && ownsMesh) {
     if(mesh && ownsMesh) {
         delete mesh;
         delete mesh;
     }
     }
-	mesh = new Mesh(fileName);
+    ResourcePool *pool = Services()->getResourceManager()->getGlobalPool();
+    mesh = (Mesh*) pool->loadResourceWithName(fileName, fileName);
+    ownsMesh = false;
 	setLocalBoundingBox(mesh->calculateBBox());
 	setLocalBoundingBox(mesh->calculateBBox());
     this->fileName = fileName;
     this->fileName = fileName;
 }
 }