Merged KRMesh into KRModel

--HG--
extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40113
This commit is contained in:
kearwood
2012-10-03 19:55:34 +00:00
parent e6e90c1945
commit b13de25a4c
9 changed files with 391 additions and 526 deletions

View File

@@ -60,10 +60,8 @@
E46C214815364BC8009CABF3 /* tinyxml2.h in Headers */ = {isa = PBXBuildFile; fileRef = E46C214315364BC8009CABF3 /* tinyxml2.h */; settings = {ATTRIBUTES = (Public, ); }; }; E46C214815364BC8009CABF3 /* tinyxml2.h in Headers */ = {isa = PBXBuildFile; fileRef = E46C214315364BC8009CABF3 /* tinyxml2.h */; settings = {ATTRIBUTES = (Public, ); }; };
E46C214B15364DEC009CABF3 /* KRSceneManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E46C214A15364DEC009CABF3 /* KRSceneManager.cpp */; }; E46C214B15364DEC009CABF3 /* KRSceneManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E46C214A15364DEC009CABF3 /* KRSceneManager.cpp */; };
E46C214C15364DEC009CABF3 /* KRSceneManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E46C214A15364DEC009CABF3 /* KRSceneManager.cpp */; }; E46C214C15364DEC009CABF3 /* KRSceneManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E46C214A15364DEC009CABF3 /* KRSceneManager.cpp */; };
E46DBE7B1512ACE600D59F86 /* KRMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E49E79FE1512615F009CF99C /* KRMesh.cpp */; };
E46DBE7F1512AF0200D59F86 /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E46DBE7D1512AD4900D59F86 /* OpenGL.framework */; }; E46DBE7F1512AF0200D59F86 /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E46DBE7D1512AD4900D59F86 /* OpenGL.framework */; };
E46DBE801512AF4600D59F86 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E491016413C99B9E0098455B /* Foundation.framework */; }; E46DBE801512AF4600D59F86 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E491016413C99B9E0098455B /* Foundation.framework */; };
E46DBE811512AFE600D59F86 /* KRMesh.h in Headers */ = {isa = PBXBuildFile; fileRef = E49E79FC15126146009CF99C /* KRMesh.h */; settings = {ATTRIBUTES = (Public, ); }; };
E46DBE851512B9FA00D59F86 /* KREngine-common.h in Headers */ = {isa = PBXBuildFile; fileRef = E46DBE841512B9E200D59F86 /* KREngine-common.h */; settings = {ATTRIBUTES = (Public, ); }; }; E46DBE851512B9FA00D59F86 /* KREngine-common.h in Headers */ = {isa = PBXBuildFile; fileRef = E46DBE841512B9E200D59F86 /* KREngine-common.h */; settings = {ATTRIBUTES = (Public, ); }; };
E46F4A00155DF46700CCF8B8 /* KRWorld.h in Headers */ = {isa = PBXBuildFile; fileRef = E46F49FF155DF46700CCF8B8 /* KRWorld.h */; }; E46F4A00155DF46700CCF8B8 /* KRWorld.h in Headers */ = {isa = PBXBuildFile; fileRef = E46F49FF155DF46700CCF8B8 /* KRWorld.h */; };
E46F4A01155DF46700CCF8B8 /* KRWorld.h in Headers */ = {isa = PBXBuildFile; fileRef = E46F49FF155DF46700CCF8B8 /* KRWorld.h */; settings = {ATTRIBUTES = (Public, ); }; }; E46F4A01155DF46700CCF8B8 /* KRWorld.h in Headers */ = {isa = PBXBuildFile; fileRef = E46F49FF155DF46700CCF8B8 /* KRWorld.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -129,7 +127,6 @@
E497B954151BEDA600D3DC67 /* KRResource+fbx.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E497B952151BEDA600D3DC67 /* KRResource+fbx.cpp */; }; E497B954151BEDA600D3DC67 /* KRResource+fbx.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E497B952151BEDA600D3DC67 /* KRResource+fbx.cpp */; };
E497B95D151BF05F00D3DC67 /* CoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E497B95C151BF05F00D3DC67 /* CoreServices.framework */; }; E497B95D151BF05F00D3DC67 /* CoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E497B95C151BF05F00D3DC67 /* CoreServices.framework */; };
E497B95F151BF09600D3DC67 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E497B95E151BF09600D3DC67 /* SystemConfiguration.framework */; }; E497B95F151BF09600D3DC67 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E497B95E151BF09600D3DC67 /* SystemConfiguration.framework */; };
E49E79FF1512615F009CF99C /* KRMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E49E79FE1512615F009CF99C /* KRMesh.cpp */; };
E4AFC6B615F7C46800DDB4C8 /* KRAABB.cpp in Headers */ = {isa = PBXBuildFile; fileRef = E40BA45215EFF79500D7C3DD /* KRAABB.cpp */; settings = {ATTRIBUTES = (Public, ); }; }; E4AFC6B615F7C46800DDB4C8 /* KRAABB.cpp in Headers */ = {isa = PBXBuildFile; fileRef = E40BA45215EFF79500D7C3DD /* KRAABB.cpp */; settings = {ATTRIBUTES = (Public, ); }; };
E4AFC6B915F7C7B200DDB4C8 /* KROctree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4924C2415EE95E700B965C6 /* KROctree.cpp */; settings = {ATTRIBUTES = (Public, ); }; }; E4AFC6B915F7C7B200DDB4C8 /* KROctree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4924C2415EE95E700B965C6 /* KROctree.cpp */; settings = {ATTRIBUTES = (Public, ); }; };
E4AFC6BA15F7C7C300DDB4C8 /* KRSkyBox.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1000469B15E6EF550053B072 /* KRSkyBox.cpp */; }; E4AFC6BA15F7C7C300DDB4C8 /* KRSkyBox.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1000469B15E6EF550053B072 /* KRSkyBox.cpp */; };
@@ -287,8 +284,6 @@
E497B952151BEDA600D3DC67 /* KRResource+fbx.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = "KRResource+fbx.cpp"; path = "Classes/KRResource+fbx.cpp"; sourceTree = "<group>"; }; E497B952151BEDA600D3DC67 /* KRResource+fbx.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = "KRResource+fbx.cpp"; path = "Classes/KRResource+fbx.cpp"; sourceTree = "<group>"; };
E497B95C151BF05F00D3DC67 /* CoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreServices.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/CoreServices.framework; sourceTree = SDKROOT; }; E497B95C151BF05F00D3DC67 /* CoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreServices.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/CoreServices.framework; sourceTree = SDKROOT; };
E497B95E151BF09600D3DC67 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; }; E497B95E151BF09600D3DC67 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
E49E79FC15126146009CF99C /* KRMesh.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; lineEnding = 0; name = KRMesh.h; path = Classes/KRMesh.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
E49E79FE1512615F009CF99C /* KRMesh.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; name = KRMesh.cpp; path = Classes/KRMesh.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
E4A9DEBD154120C4009DF363 /* light_point.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = light_point.fsh; path = Shaders/light_point.fsh; sourceTree = "<group>"; }; E4A9DEBD154120C4009DF363 /* light_point.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = light_point.fsh; path = Shaders/light_point.fsh; sourceTree = "<group>"; };
E4A9DEC0154120E8009DF363 /* light_point.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = light_point.vsh; path = Shaders/light_point.vsh; sourceTree = "<group>"; }; E4A9DEC0154120E8009DF363 /* light_point.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = light_point.vsh; path = Shaders/light_point.vsh; sourceTree = "<group>"; };
E4BBBB7E1512A40300F43B5B /* krengine_osx.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = krengine_osx.framework; sourceTree = BUILT_PRODUCTS_DIR; }; E4BBBB7E1512A40300F43B5B /* krengine_osx.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = krengine_osx.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -367,12 +362,11 @@
E461A170152E598200F2044A /* Resources */ = { E461A170152E598200F2044A /* Resources */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
E461A174152E5A1300F2044A /* Meshes */,
E497B949151BCEE900D3DC67 /* KRResource.h */, E497B949151BCEE900D3DC67 /* KRResource.h */,
E497B94C151BCF2500D3DC67 /* KRResource.cpp */, E497B94C151BCF2500D3DC67 /* KRResource.cpp */,
E497B94F151BD2CE00D3DC67 /* KRResource+obj.cpp */, E497B94F151BD2CE00D3DC67 /* KRResource+obj.cpp */,
E46A6B6C1559E97D000DBD37 /* KRResource+blend.cpp */,
E46A6B6F1559EF0A000DBD37 /* KRResource+blend.h */, E46A6B6F1559EF0A000DBD37 /* KRResource+blend.h */,
E46A6B6C1559E97D000DBD37 /* KRResource+blend.cpp */,
E497B952151BEDA600D3DC67 /* KRResource+fbx.cpp */, E497B952151BEDA600D3DC67 /* KRResource+fbx.cpp */,
); );
name = Resources; name = Resources;
@@ -412,15 +406,6 @@
name = Math; name = Math;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
E461A174152E5A1300F2044A /* Meshes */ = {
isa = PBXGroup;
children = (
E49E79FC15126146009CF99C /* KRMesh.h */,
E49E79FE1512615F009CF99C /* KRMesh.cpp */,
);
name = Meshes;
sourceTree = "<group>";
};
E46C214015364BB8009CABF3 /* tinyxml2 */ = { E46C214015364BB8009CABF3 /* tinyxml2 */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@@ -714,7 +699,6 @@
E4F975331536220900FD60B2 /* KRNode.h in Headers */, E4F975331536220900FD60B2 /* KRNode.h in Headers */,
E4B2A4391523B027004CB0EC /* KRMaterial.h in Headers */, E4B2A4391523B027004CB0EC /* KRMaterial.h in Headers */,
E46F4A0C155E002100CCF8B8 /* KRDataBlock.h in Headers */, E46F4A0C155E002100CCF8B8 /* KRDataBlock.h in Headers */,
E46DBE811512AFE600D59F86 /* KRMesh.h in Headers */,
E4F9754115362CD900FD60B2 /* KRScene.h in Headers */, E4F9754115362CD900FD60B2 /* KRScene.h in Headers */,
E4F9754A153632AA00FD60B2 /* KRModelManager.cpp in Headers */, E4F9754A153632AA00FD60B2 /* KRModelManager.cpp in Headers */,
E4F9754215362D0D00FD60B2 /* KRInstance.h in Headers */, E4F9754215362D0D00FD60B2 /* KRInstance.h in Headers */,
@@ -846,7 +830,6 @@
E414BAE91435585A00A668C4 /* KRScene.cpp in Sources */, E414BAE91435585A00A668C4 /* KRScene.cpp in Sources */,
E414BAED14355EFF00A668C4 /* KRBoundingVolume.cpp in Sources */, E414BAED14355EFF00A668C4 /* KRBoundingVolume.cpp in Sources */,
E48B3CC014393E30000C50E2 /* KRCamera.cpp in Sources */, E48B3CC014393E30000C50E2 /* KRCamera.cpp in Sources */,
E49E79FF1512615F009CF99C /* KRMesh.cpp in Sources */,
E497B946151BA99500D3DC67 /* KRVector2.cpp in Sources */, E497B946151BA99500D3DC67 /* KRVector2.cpp in Sources */,
E497B94D151BCF2500D3DC67 /* KRResource.cpp in Sources */, E497B94D151BCF2500D3DC67 /* KRResource.cpp in Sources */,
E497B950151BD2CE00D3DC67 /* KRResource+obj.cpp in Sources */, E497B950151BD2CE00D3DC67 /* KRResource+obj.cpp in Sources */,
@@ -883,7 +866,6 @@
files = ( files = (
E461A17A152E5C9100F2044A /* KRMat4.cpp in Sources */, E461A17A152E5C9100F2044A /* KRMat4.cpp in Sources */,
E461A175152E5C4800F2044A /* KRLight.cpp in Sources */, E461A175152E5C4800F2044A /* KRLight.cpp in Sources */,
E46DBE7B1512ACE600D59F86 /* KRMesh.cpp in Sources */,
E4BBBBA71512A6DC00F43B5B /* KRVector3.cpp in Sources */, E4BBBBA71512A6DC00F43B5B /* KRVector3.cpp in Sources */,
E4B2A43B1523B02E004CB0EC /* KRMaterial.cpp in Sources */, E4B2A43B1523B02E004CB0EC /* KRMaterial.cpp in Sources */,
E4BBBB8E1512A40300F43B5B /* krengine_osx.m in Sources */, E4BBBB8E1512A40300F43B5B /* krengine_osx.m in Sources */,

View File

@@ -32,7 +32,7 @@
#include <iostream> #include <iostream>
#import "KRInstance.h" #import "KRInstance.h"
#import "KRContext.h" #import "KRContext.h"
#import "KRMesh.h" #import "KRModel.h"
#include <assert.h> #include <assert.h>
KRInstance::KRInstance(KRScene &scene, std::string instance_name, std::string model_name, std::string light_map) : KRNode(scene, instance_name) { KRInstance::KRInstance(KRScene &scene, std::string instance_name, std::string model_name, std::string light_map) : KRNode(scene, instance_name) {
@@ -131,8 +131,7 @@ void KRInstance::calcExtents(KRContext *pContext)
KRNode::calcExtents(pContext); KRNode::calcExtents(pContext);
loadModel(); loadModel();
if(m_pModel != NULL) { if(m_pModel != NULL) {
KRMesh *pMesh = m_pModel->getMesh(); KRBoundingVolume mesh_bounds = KRBoundingVolume(m_pModel->getMinPoint(), m_pModel->getMaxPoint(), m_modelMatrix);
KRBoundingVolume mesh_bounds = KRBoundingVolume(pMesh->getMinPoint(), pMesh->getMaxPoint(), m_modelMatrix);
if(m_pExtents) { if(m_pExtents) {
*m_pExtents = m_pExtents->get_union(mesh_bounds); *m_pExtents = m_pExtents->get_union(mesh_bounds);
} else { } else {
@@ -154,9 +153,8 @@ KRAABB KRInstance::getBounds() {
loadModel(); loadModel();
assert(m_pModel != NULL); assert(m_pModel != NULL);
KRMesh *pMesh = m_pModel->getMesh(); KRVector3 meshMin = m_pModel->getMinPoint();
KRVector3 meshMin = pMesh->getMinPoint(); KRVector3 meshMax = m_pModel->getMaxPoint();
KRVector3 meshMax = pMesh->getMaxPoint();
KRVector3 min, max; KRVector3 min, max;
for(int iCorner=0; iCorner < 8; iCorner++) { for(int iCorner=0; iCorner < 8; iCorner++) {

View File

@@ -1,327 +0,0 @@
//
// KRMesh.cpp
// KREngine
//
// Copyright 2012 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#include "KRMesh.h"
#import "KRShader.h"
#import "KRContext.h"
#import <stdint.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
KRMesh::KRMesh(KRContext &context, std::string name) : KRResource(context, name) {
m_pData = new KRDataBlock();
}
KRMesh::~KRMesh() {
clearData();
if(m_pData) delete m_pData;
}
std::string KRMesh::getExtension() {
return "krobject";
}
void KRMesh::clearData() {
m_pData->unload();
}
void KRMesh::clearBuffers() {
m_submeshes.clear();
}
void KRMesh::loadPack(KRDataBlock *data) {
clearData();
delete m_pData;
m_pData = data;
pack_header *pHeader = (pack_header *)m_pData->getStart();
m_minPoint = KRVector3(pHeader->minx, pHeader->miny, pHeader->minz);
m_maxPoint = KRVector3(pHeader->maxx, pHeader->maxy, pHeader->maxz);
}
bool KRMesh::save(const std::string& path) {
clearBuffers();
return m_pData->save(path);
}
GLfloat KRMesh::getMaxDimension() {
GLfloat m = 0.0;
if(m_maxPoint.x - m_minPoint.x > m) m = m_maxPoint.x - m_minPoint.x;
if(m_maxPoint.y - m_minPoint.y > m) m = m_maxPoint.y - m_minPoint.y;
if(m_maxPoint.z - m_minPoint.z > m) m = m_maxPoint.z - m_minPoint.z;
return m;
}
vector<KRMesh::Submesh *> KRMesh::getSubmeshes() {
if(m_submeshes.size() == 0) {
pack_header *pHeader = (pack_header *)m_pData->getStart();
pack_material *pPackMaterials = (pack_material *)(pHeader+1);
m_submeshes.clear();
for(int iMaterial=0; iMaterial < pHeader->submesh_count; iMaterial++) {
pack_material *pPackMaterial = pPackMaterials + iMaterial;
Submesh *pSubmesh = new Submesh();
pSubmesh->start_vertex = pPackMaterial->start_vertex;
pSubmesh->vertex_count = pPackMaterial->vertex_count;
strncpy(pSubmesh->szMaterialName, pPackMaterial->szName, 256);
pSubmesh->szMaterialName[255] = '\0';
//fprintf(stderr, "Submesh material: \"%s\"\n", pSubmesh->szMaterialName);
m_submeshes.push_back(pSubmesh);
}
}
return m_submeshes;
}
void KRMesh::renderSubmesh(int iSubmesh) {
VertexData *pVertexData = getVertexData();
pack_header *pHeader = (pack_header *)m_pData->getStart();
int cBuffers = (pHeader->vertex_count + MAX_VBO_SIZE - 1) / MAX_VBO_SIZE;
vector<KRMesh::Submesh *> submeshes = getSubmeshes();
Submesh *pSubmesh = submeshes[iSubmesh];
int iVertex = pSubmesh->start_vertex;
int iBuffer = iVertex / MAX_VBO_SIZE;
iVertex = iVertex % MAX_VBO_SIZE;
int cVertexes = pSubmesh->vertex_count;
while(cVertexes > 0) {
GLsizei cBufferVertexes = iBuffer < cBuffers - 1 ? MAX_VBO_SIZE : pHeader->vertex_count % MAX_VBO_SIZE;
int vertex_size = sizeof(VertexData) ;
assert(pVertexData + iBuffer * MAX_VBO_SIZE * vertex_size >= m_pData->getStart());
void *vbo_end = (unsigned char *)pVertexData + iBuffer * MAX_VBO_SIZE * vertex_size + vertex_size * cBufferVertexes;
void *buffer_end = m_pData->getEnd();
assert(vbo_end <= buffer_end);
assert(cBufferVertexes <= 65535);
m_pContext->getModelManager()->bindVBO((unsigned char *)pVertexData + iBuffer * MAX_VBO_SIZE * vertex_size, vertex_size * cBufferVertexes, true, true, true, true, true);
if(iVertex + cVertexes >= MAX_VBO_SIZE) {
assert(iVertex + (MAX_VBO_SIZE - iVertex) <= cBufferVertexes);
GLDEBUG(glDrawArrays(GL_TRIANGLES, iVertex, (MAX_VBO_SIZE - iVertex)));
cVertexes -= (MAX_VBO_SIZE - iVertex);
iVertex = 0;
iBuffer++;
} else {
assert(iVertex + cVertexes <= cBufferVertexes);
GLDEBUG(glDrawArrays(GL_TRIANGLES, iVertex, cVertexes));
cVertexes = 0;
}
}
}
KRMesh::VertexData *KRMesh::getVertexData() {
pack_header *pHeader = (pack_header *)m_pData->getStart();
pack_material *pPackMaterials = (pack_material *)(pHeader+1);
return (VertexData *)(pPackMaterials + pHeader->submesh_count);
}
void KRMesh::LoadData(std::vector<KRVector3> vertices, std::vector<KRVector2> uva, std::vector<KRVector2> uvb, std::vector<KRVector3> normals, std::vector<KRVector3> tangents, std::vector<int> submesh_starts, std::vector<int> submesh_lengths, std::vector<std::string> material_names) {
clearData();
int submesh_count = submesh_lengths.size();
int vertex_count = vertices.size();
size_t new_file_size = sizeof(pack_header) + sizeof(pack_material) * submesh_count + sizeof(VertexData) * vertex_count;
m_pData->expand(new_file_size);
pack_header *pHeader = (pack_header *)m_pData->getStart();
memset(pHeader, 0, sizeof(pack_header));
pHeader->submesh_count = submesh_lengths.size();
pHeader->vertex_count = vertices.size();
strcpy(pHeader->szTag, "KROBJPACK1.0 ");
pack_material *pPackMaterials = (pack_material *)(pHeader+1);
for(int iMaterial=0; iMaterial < pHeader->submesh_count; iMaterial++) {
pack_material *pPackMaterial = pPackMaterials + iMaterial;
pPackMaterial->start_vertex = submesh_starts[iMaterial];
pPackMaterial->vertex_count = submesh_lengths[iMaterial];
strncpy(pPackMaterial->szName, material_names[iMaterial].c_str(), 63);
}
bool bFirstVertex = true;
VertexData *pVertexData = (VertexData *)(pPackMaterials + pHeader->submesh_count);
VertexData *pVertex = pVertexData;
for(int iVertex=0; iVertex < vertices.size(); iVertex++) {
memset(pVertex, 0, sizeof(VertexData));
KRVector3 source_vertex = vertices[iVertex];
pVertex->vertex.x = source_vertex.x;
pVertex->vertex.y = source_vertex.y;
pVertex->vertex.z = source_vertex.z;
if(bFirstVertex) {
bFirstVertex = false;
m_minPoint = source_vertex;
m_maxPoint = source_vertex;
} else {
if(source_vertex.x < m_minPoint.x) m_minPoint.x = source_vertex.x;
if(source_vertex.y < m_minPoint.y) m_minPoint.y = source_vertex.y;
if(source_vertex.z < m_minPoint.z) m_minPoint.z = source_vertex.z;
if(source_vertex.x > m_maxPoint.x) m_maxPoint.x = source_vertex.x;
if(source_vertex.y > m_maxPoint.y) m_maxPoint.y = source_vertex.y;
if(source_vertex.z > m_maxPoint.z) m_maxPoint.z = source_vertex.z;
}
if(uva.size() > iVertex) {
KRVector2 source_uva = uva[iVertex];
pVertex->uva.u = source_uva.x;
pVertex->uva.v = source_uva.y;
} else {
pVertex->uva.u = 0.0;
pVertex->uva.v = 0.0;
}
if(uvb.size() > iVertex) {
KRVector2 source_uvb = uvb[iVertex];
pVertex->uvb.u = source_uvb.x;
pVertex->uvb.v = source_uvb.y;
} else {
pVertex->uvb.u = 0.0;
pVertex->uvb.v = 0.0;
}
if(normals.size() > iVertex) {
KRVector3 source_normal = normals[iVertex];
pVertex->normal.x = source_normal.x;
pVertex->normal.y = source_normal.y;
pVertex->normal.z = source_normal.z;
} else {
pVertex->normal.x = 0.0f;
pVertex->normal.y = 0.0f;
pVertex->normal.z = 0.0f;
}
if(tangents.size() > iVertex) {
KRVector3 source_tangent = tangents[iVertex];
pVertex->tangent.x = source_tangent.x;
pVertex->tangent.y = source_tangent.y;
pVertex->tangent.z = source_tangent.z;
} else {
pVertex->tangent.x = 0.0f;
pVertex->tangent.y = 0.0f;
pVertex->tangent.z = 0.0f;
}
pVertex++;
}
pHeader->minx = m_minPoint.x;
pHeader->miny = m_minPoint.y;
pHeader->minz = m_minPoint.z;
pHeader->maxx = m_maxPoint.x;
pHeader->maxy = m_maxPoint.y;
pHeader->maxz = m_maxPoint.z;
// Calculate missing surface normals and tangents
//cout << " Calculate surface normals and tangents\n";
VertexData *pStart = pVertexData;
VertexData *pEnd = pStart + vertex_count;
for(VertexData *pVertex = pStart; pVertex < pEnd; pVertex+=3) {
KRVector3 p1(pVertex[0].vertex.x, pVertex[0].vertex.y, pVertex[0].vertex.z);
KRVector3 p2(pVertex[1].vertex.x, pVertex[1].vertex.y, pVertex[1].vertex.z);
KRVector3 p3(pVertex[2].vertex.x, pVertex[2].vertex.y, pVertex[2].vertex.z);
KRVector3 v1 = p2 - p1;
KRVector3 v2 = p3 - p1;
// -- Calculate normal --
if(pVertex->normal.x == 0 && pVertex->normal.y == 0 && pVertex->normal.z == 0) {
KRVector3 normal = KRVector3::Cross(v1, v2);
normal.normalize();
pVertex[0].normal.x = normal.x;
pVertex[0].normal.y = normal.y;
pVertex[0].normal.z = normal.z;
pVertex[1].normal.x = normal.x;
pVertex[1].normal.y = normal.y;
pVertex[1].normal.z = normal.z;
pVertex[2].normal.x = normal.x;
pVertex[2].normal.y = normal.y;
pVertex[2].normal.z = normal.z;
}
// -- Calculate tangent vector for normal mapping --
if(pVertex->tangent.x == 0 && pVertex->tangent.y == 0 && pVertex->tangent.z == 0) {
TexCoord st1; // = pVertex[2].texcoord;
TexCoord st2; // = pVertex[1].texcoord;
st1.u = pVertex[1].uva.u - pVertex[0].uva.u;
st1.v = pVertex[1].uva.v - pVertex[0].uva.v;
st2.u = pVertex[2].uva.u - pVertex[0].uva.u;
st2.v = pVertex[2].uva.v - pVertex[0].uva.v;
double coef = 1/ (st1.u * st2.v - st2.u * st1.v);
pVertex[0].tangent.x = coef * ((v1.x * st2.v) + (v2.x * -st1.v));
pVertex[0].tangent.y = coef * ((v1.y * st2.v) + (v2.y * -st1.v));
pVertex[0].tangent.z = coef * ((v1.z * st2.v) + (v2.z * -st1.v));
KRVector3 tangent(
coef * ((v1.x * st2.v) + (v2.x * -st1.v)),
coef * ((v1.y * st2.v) + (v2.y * -st1.v)),
coef * ((v1.z * st2.v) + (v2.z * -st1.v))
);
tangent.normalize();
pVertex[0].tangent.x = tangent.x;
pVertex[0].tangent.y = tangent.y;
pVertex[0].tangent.z = tangent.z;
pVertex[1].tangent.x = tangent.x;
pVertex[1].tangent.y = tangent.y;
pVertex[1].tangent.z = tangent.z;
pVertex[2].tangent.x = tangent.x;
pVertex[2].tangent.y = tangent.y;
pVertex[2].tangent.z = tangent.z;
}
}
}
KRVector3 KRMesh::getMinPoint() const {
return m_minPoint;
}
KRVector3 KRMesh::getMaxPoint() const {
return m_maxPoint;
}

View File

@@ -1,133 +0,0 @@
//
// KRMesh.h
// KREngine
//
// Copyright 2012 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#import <stdint.h>
#import <vector>
#import <set>
#import <list>
#import <string>
#import "KRMesh.h"
#import "KRVector2.h"
#import "KRVector3.h"
#import "KRResource.h"
#import "KRDataBlock.h"
#import "KREngine-common.h"
using std::vector;
using std::set;
using std::list;
#define MAX_VBO_SIZE 65535
// MAX_VBO_SIZE must be divisible by 3 so triangles aren't split across VBO objects...
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
#ifndef KREngine_KRMesh_h
#define KREngine_KRMesh_h
using std::vector;
class KRMesh : public KRResource {
public:
KRMesh(KRContext &context, std::string name);
virtual ~KRMesh();
virtual std::string getExtension();
virtual bool save(const std::string& path);
void LoadData(std::vector<KRVector3> vertices, std::vector<KRVector2> uva, std::vector<KRVector2> uvb, std::vector<KRVector3> normals, std::vector<KRVector3> tangents, std::vector<int> submesh_starts, std::vector<int> submesh_lengths, std::vector<std::string> material_names);
void loadPack(KRDataBlock *data);
void renderSubmesh(int iSubmesh);
GLfloat getMaxDimension();
KRVector3 getMinPoint() const;
KRVector3 getMaxPoint() const;
typedef struct {
GLint start_vertex;
GLsizei vertex_count;
char szMaterialName[256];
} Submesh;
typedef struct {
GLfloat x;
GLfloat y;
GLfloat z;
} KRVector3D;
typedef struct {
GLfloat u;
GLfloat v;
} TexCoord;
typedef struct {
KRVector3D vertex;
KRVector3D normal;
KRVector3D tangent;
TexCoord uva;
TexCoord uvb;
} VertexData;
VertexData *getVertexData();
vector<Submesh *> getSubmeshes();
typedef struct {
int32_t start_vertex;
int32_t vertex_count;
char szName[256];
} pack_material;
protected:
KRVector3 m_minPoint, m_maxPoint;
KRDataBlock *m_pData;
typedef struct {
char szTag[16];
float minx, miny, minz, maxx, maxy, maxz;
int32_t vertex_count;
int32_t submesh_count;
} pack_header;
vector<Submesh *> m_submeshes;
void clearData();
void clearBuffers();
};
#endif

View File

@@ -46,22 +46,45 @@
#import "KRShaderManager.h" #import "KRShaderManager.h"
#import "KRContext.h" #import "KRContext.h"
KRModel::KRModel(KRContext &context, std::string name, KRDataBlock *data) : KRContextObject(context) {
m_name = name; KRModel::KRModel(KRContext &context, std::string name) : KRResource(context, name) {
m_hasTransparency = false; m_hasTransparency = false;
m_materials.clear(); m_materials.clear();
m_uniqueMaterials.clear(); m_uniqueMaterials.clear();
m_pMesh = new KRMesh(*m_pContext, name); m_pData = new KRDataBlock();
m_pMesh->loadPack(data);
} }
std::string KRModel::getName() { KRModel::KRModel(KRContext &context, std::string name, KRDataBlock *data) : KRResource(context, name) {
return m_name; m_hasTransparency = false;
m_materials.clear();
m_uniqueMaterials.clear();
m_pData = new KRDataBlock();
loadPack(data);
} }
KRModel::~KRModel() { KRModel::~KRModel() {
clearData();
if(m_pData) delete m_pData;
} }
std::string KRModel::getExtension() {
return "krobject";
}
bool KRModel::save(const std::string& path) {
clearBuffers();
return m_pData->save(path);
}
void KRModel::loadPack(KRDataBlock *data) {
clearData();
delete m_pData;
m_pData = data;
pack_header *pHeader = (pack_header *)m_pData->getStart();
m_minPoint = KRVector3(pHeader->minx, pHeader->miny, pHeader->minz);
m_maxPoint = KRVector3(pHeader->maxx, pHeader->maxy, pHeader->maxz);
}
#if TARGET_OS_IPHONE #if TARGET_OS_IPHONE
void KRModel::render(KRCamera *pCamera, KRContext *pContext, KRMat4 &matModelToView, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRTexture *pLightMap, KRNode::RenderPass renderPass) { void KRModel::render(KRCamera *pCamera, KRContext *pContext, KRMat4 &matModelToView, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRTexture *pLightMap, KRNode::RenderPass renderPass) {
@@ -70,9 +93,9 @@ void KRModel::render(KRCamera *pCamera, KRContext *pContext, KRMat4 &matModelToV
if(renderPass != KRNode::RENDER_PASS_FLARES) { if(renderPass != KRNode::RENDER_PASS_FLARES) {
if(m_materials.size() == 0) { if(m_materials.size() == 0) {
vector<KRMesh::Submesh *> submeshes = m_pMesh->getSubmeshes(); vector<KRModel::Submesh *> submeshes = getSubmeshes();
for(std::vector<KRMesh::Submesh *>::iterator itr = submeshes.begin(); itr != submeshes.end(); itr++) { for(std::vector<KRModel::Submesh *>::iterator itr = submeshes.begin(); itr != submeshes.end(); itr++) {
const char *szMaterialName = (*itr)->szMaterialName; const char *szMaterialName = (*itr)->szMaterialName;
KRMaterial *pMaterial = pContext->getMaterialManager()->getMaterial(szMaterialName); KRMaterial *pMaterial = pContext->getMaterialManager()->getMaterial(szMaterialName);
m_materials.push_back(pMaterial); m_materials.push_back(pMaterial);
@@ -95,7 +118,7 @@ void KRModel::render(KRCamera *pCamera, KRContext *pContext, KRMat4 &matModelToV
KRMaterial *pPrevBoundMaterial = NULL; KRMaterial *pPrevBoundMaterial = NULL;
char szPrevShaderKey[128]; char szPrevShaderKey[128];
szPrevShaderKey[0] = '\0'; szPrevShaderKey[0] = '\0';
int cSubmeshes = m_pMesh->getSubmeshes().size(); int cSubmeshes = getSubmeshes().size();
if(renderPass == KRNode::RENDER_PASS_SHADOWMAP) { if(renderPass == KRNode::RENDER_PASS_SHADOWMAP) {
for(int iSubmesh=0; iSubmesh<cSubmeshes; iSubmesh++) { for(int iSubmesh=0; iSubmesh<cSubmeshes; iSubmesh++) {
KRMaterial *pMaterial = m_materials[iSubmesh]; KRMaterial *pMaterial = m_materials[iSubmesh];
@@ -104,7 +127,7 @@ void KRModel::render(KRCamera *pCamera, KRContext *pContext, KRMat4 &matModelToV
if(!pMaterial->isTransparent()) { if(!pMaterial->isTransparent()) {
// Exclude transparent and semi-transparent meshes from shadow maps // Exclude transparent and semi-transparent meshes from shadow maps
m_pMesh->renderSubmesh(iSubmesh); renderSubmesh(iSubmesh);
} }
} }
@@ -122,19 +145,19 @@ void KRModel::render(KRCamera *pCamera, KRContext *pContext, KRMat4 &matModelToV
switch(pMaterial->getAlphaMode()) { switch(pMaterial->getAlphaMode()) {
case KRMaterial::KRMATERIAL_ALPHA_MODE_OPAQUE: // Non-transparent materials case KRMaterial::KRMATERIAL_ALPHA_MODE_OPAQUE: // Non-transparent materials
case KRMaterial::KRMATERIAL_ALPHA_MODE_TEST: // Alpha in diffuse texture is interpreted as punch-through when < 0.5 case KRMaterial::KRMATERIAL_ALPHA_MODE_TEST: // Alpha in diffuse texture is interpreted as punch-through when < 0.5
m_pMesh->renderSubmesh(iSubmesh); renderSubmesh(iSubmesh);
break; break;
case KRMaterial::KRMATERIAL_ALPHA_MODE_BLENDONESIDE: // Blended alpha with backface culling case KRMaterial::KRMATERIAL_ALPHA_MODE_BLENDONESIDE: // Blended alpha with backface culling
m_pMesh->renderSubmesh(iSubmesh); renderSubmesh(iSubmesh);
break; break;
case KRMaterial::KRMATERIAL_ALPHA_MODE_BLENDTWOSIDE: // Blended alpha rendered in two passes. First pass renders backfaces; second pass renders frontfaces. case KRMaterial::KRMATERIAL_ALPHA_MODE_BLENDTWOSIDE: // Blended alpha rendered in two passes. First pass renders backfaces; second pass renders frontfaces.
// Render back faces first // Render back faces first
GLDEBUG(glCullFace(GL_BACK)); GLDEBUG(glCullFace(GL_BACK));
m_pMesh->renderSubmesh(iSubmesh); renderSubmesh(iSubmesh);
// Render front faces second // Render front faces second
GLDEBUG(glCullFace(GL_BACK)); GLDEBUG(glCullFace(GL_BACK));
m_pMesh->renderSubmesh(iSubmesh); renderSubmesh(iSubmesh);
break; break;
} }
} }
@@ -150,11 +173,264 @@ void KRModel::render(KRCamera *pCamera, KRContext *pContext, KRMat4 &matModelToV
#endif #endif
KRMesh *KRModel::getMesh() { GLfloat KRModel::getMaxDimension() {
return m_pMesh; GLfloat m = 0.0;
if(m_maxPoint.x - m_minPoint.x > m) m = m_maxPoint.x - m_minPoint.x;
if(m_maxPoint.y - m_minPoint.y > m) m = m_maxPoint.y - m_minPoint.y;
if(m_maxPoint.z - m_minPoint.z > m) m = m_maxPoint.z - m_minPoint.z;
return m;
} }
bool KRModel::hasTransparency() { bool KRModel::hasTransparency() {
return m_hasTransparency; return m_hasTransparency;
} }
vector<KRModel::Submesh *> KRModel::getSubmeshes() {
if(m_submeshes.size() == 0) {
pack_header *pHeader = (pack_header *)m_pData->getStart();
pack_material *pPackMaterials = (pack_material *)(pHeader+1);
m_submeshes.clear();
for(int iMaterial=0; iMaterial < pHeader->submesh_count; iMaterial++) {
pack_material *pPackMaterial = pPackMaterials + iMaterial;
Submesh *pSubmesh = new Submesh();
pSubmesh->start_vertex = pPackMaterial->start_vertex;
pSubmesh->vertex_count = pPackMaterial->vertex_count;
strncpy(pSubmesh->szMaterialName, pPackMaterial->szName, 256);
pSubmesh->szMaterialName[255] = '\0';
//fprintf(stderr, "Submesh material: \"%s\"\n", pSubmesh->szMaterialName);
m_submeshes.push_back(pSubmesh);
}
}
return m_submeshes;
}
void KRModel::renderSubmesh(int iSubmesh) {
VertexData *pVertexData = getVertexData();
pack_header *pHeader = (pack_header *)m_pData->getStart();
int cBuffers = (pHeader->vertex_count + MAX_VBO_SIZE - 1) / MAX_VBO_SIZE;
vector<KRModel::Submesh *> submeshes = getSubmeshes();
Submesh *pSubmesh = submeshes[iSubmesh];
int iVertex = pSubmesh->start_vertex;
int iBuffer = iVertex / MAX_VBO_SIZE;
iVertex = iVertex % MAX_VBO_SIZE;
int cVertexes = pSubmesh->vertex_count;
while(cVertexes > 0) {
GLsizei cBufferVertexes = iBuffer < cBuffers - 1 ? MAX_VBO_SIZE : pHeader->vertex_count % MAX_VBO_SIZE;
int vertex_size = sizeof(VertexData) ;
assert(pVertexData + iBuffer * MAX_VBO_SIZE * vertex_size >= m_pData->getStart());
void *vbo_end = (unsigned char *)pVertexData + iBuffer * MAX_VBO_SIZE * vertex_size + vertex_size * cBufferVertexes;
void *buffer_end = m_pData->getEnd();
assert(vbo_end <= buffer_end);
assert(cBufferVertexes <= 65535);
m_pContext->getModelManager()->bindVBO((unsigned char *)pVertexData + iBuffer * MAX_VBO_SIZE * vertex_size, vertex_size * cBufferVertexes, true, true, true, true, true);
if(iVertex + cVertexes >= MAX_VBO_SIZE) {
assert(iVertex + (MAX_VBO_SIZE - iVertex) <= cBufferVertexes);
GLDEBUG(glDrawArrays(GL_TRIANGLES, iVertex, (MAX_VBO_SIZE - iVertex)));
cVertexes -= (MAX_VBO_SIZE - iVertex);
iVertex = 0;
iBuffer++;
} else {
assert(iVertex + cVertexes <= cBufferVertexes);
GLDEBUG(glDrawArrays(GL_TRIANGLES, iVertex, cVertexes));
cVertexes = 0;
}
}
}
KRModel::VertexData *KRModel::getVertexData() {
pack_header *pHeader = (pack_header *)m_pData->getStart();
pack_material *pPackMaterials = (pack_material *)(pHeader+1);
return (VertexData *)(pPackMaterials + pHeader->submesh_count);
}
void KRModel::LoadData(std::vector<KRVector3> vertices, std::vector<KRVector2> uva, std::vector<KRVector2> uvb, std::vector<KRVector3> normals, std::vector<KRVector3> tangents, std::vector<int> submesh_starts, std::vector<int> submesh_lengths, std::vector<std::string> material_names) {
clearData();
int submesh_count = submesh_lengths.size();
int vertex_count = vertices.size();
size_t new_file_size = sizeof(pack_header) + sizeof(pack_material) * submesh_count + sizeof(VertexData) * vertex_count;
m_pData->expand(new_file_size);
pack_header *pHeader = (pack_header *)m_pData->getStart();
memset(pHeader, 0, sizeof(pack_header));
pHeader->submesh_count = submesh_lengths.size();
pHeader->vertex_count = vertices.size();
strcpy(pHeader->szTag, "KROBJPACK1.0 ");
pack_material *pPackMaterials = (pack_material *)(pHeader+1);
for(int iMaterial=0; iMaterial < pHeader->submesh_count; iMaterial++) {
pack_material *pPackMaterial = pPackMaterials + iMaterial;
pPackMaterial->start_vertex = submesh_starts[iMaterial];
pPackMaterial->vertex_count = submesh_lengths[iMaterial];
strncpy(pPackMaterial->szName, material_names[iMaterial].c_str(), 63);
}
bool bFirstVertex = true;
VertexData *pVertexData = (VertexData *)(pPackMaterials + pHeader->submesh_count);
VertexData *pVertex = pVertexData;
for(int iVertex=0; iVertex < vertices.size(); iVertex++) {
memset(pVertex, 0, sizeof(VertexData));
KRVector3 source_vertex = vertices[iVertex];
pVertex->vertex.x = source_vertex.x;
pVertex->vertex.y = source_vertex.y;
pVertex->vertex.z = source_vertex.z;
if(bFirstVertex) {
bFirstVertex = false;
m_minPoint = source_vertex;
m_maxPoint = source_vertex;
} else {
if(source_vertex.x < m_minPoint.x) m_minPoint.x = source_vertex.x;
if(source_vertex.y < m_minPoint.y) m_minPoint.y = source_vertex.y;
if(source_vertex.z < m_minPoint.z) m_minPoint.z = source_vertex.z;
if(source_vertex.x > m_maxPoint.x) m_maxPoint.x = source_vertex.x;
if(source_vertex.y > m_maxPoint.y) m_maxPoint.y = source_vertex.y;
if(source_vertex.z > m_maxPoint.z) m_maxPoint.z = source_vertex.z;
}
if(uva.size() > iVertex) {
KRVector2 source_uva = uva[iVertex];
pVertex->uva.u = source_uva.x;
pVertex->uva.v = source_uva.y;
} else {
pVertex->uva.u = 0.0;
pVertex->uva.v = 0.0;
}
if(uvb.size() > iVertex) {
KRVector2 source_uvb = uvb[iVertex];
pVertex->uvb.u = source_uvb.x;
pVertex->uvb.v = source_uvb.y;
} else {
pVertex->uvb.u = 0.0;
pVertex->uvb.v = 0.0;
}
if(normals.size() > iVertex) {
KRVector3 source_normal = normals[iVertex];
pVertex->normal.x = source_normal.x;
pVertex->normal.y = source_normal.y;
pVertex->normal.z = source_normal.z;
} else {
pVertex->normal.x = 0.0f;
pVertex->normal.y = 0.0f;
pVertex->normal.z = 0.0f;
}
if(tangents.size() > iVertex) {
KRVector3 source_tangent = tangents[iVertex];
pVertex->tangent.x = source_tangent.x;
pVertex->tangent.y = source_tangent.y;
pVertex->tangent.z = source_tangent.z;
} else {
pVertex->tangent.x = 0.0f;
pVertex->tangent.y = 0.0f;
pVertex->tangent.z = 0.0f;
}
pVertex++;
}
pHeader->minx = m_minPoint.x;
pHeader->miny = m_minPoint.y;
pHeader->minz = m_minPoint.z;
pHeader->maxx = m_maxPoint.x;
pHeader->maxy = m_maxPoint.y;
pHeader->maxz = m_maxPoint.z;
// Calculate missing surface normals and tangents
//cout << " Calculate surface normals and tangents\n";
VertexData *pStart = pVertexData;
VertexData *pEnd = pStart + vertex_count;
for(VertexData *pVertex = pStart; pVertex < pEnd; pVertex+=3) {
KRVector3 p1(pVertex[0].vertex.x, pVertex[0].vertex.y, pVertex[0].vertex.z);
KRVector3 p2(pVertex[1].vertex.x, pVertex[1].vertex.y, pVertex[1].vertex.z);
KRVector3 p3(pVertex[2].vertex.x, pVertex[2].vertex.y, pVertex[2].vertex.z);
KRVector3 v1 = p2 - p1;
KRVector3 v2 = p3 - p1;
// -- Calculate normal --
if(pVertex->normal.x == 0 && pVertex->normal.y == 0 && pVertex->normal.z == 0) {
KRVector3 normal = KRVector3::Cross(v1, v2);
normal.normalize();
pVertex[0].normal.x = normal.x;
pVertex[0].normal.y = normal.y;
pVertex[0].normal.z = normal.z;
pVertex[1].normal.x = normal.x;
pVertex[1].normal.y = normal.y;
pVertex[1].normal.z = normal.z;
pVertex[2].normal.x = normal.x;
pVertex[2].normal.y = normal.y;
pVertex[2].normal.z = normal.z;
}
// -- Calculate tangent vector for normal mapping --
if(pVertex->tangent.x == 0 && pVertex->tangent.y == 0 && pVertex->tangent.z == 0) {
TexCoord st1; // = pVertex[2].texcoord;
TexCoord st2; // = pVertex[1].texcoord;
st1.u = pVertex[1].uva.u - pVertex[0].uva.u;
st1.v = pVertex[1].uva.v - pVertex[0].uva.v;
st2.u = pVertex[2].uva.u - pVertex[0].uva.u;
st2.v = pVertex[2].uva.v - pVertex[0].uva.v;
double coef = 1/ (st1.u * st2.v - st2.u * st1.v);
pVertex[0].tangent.x = coef * ((v1.x * st2.v) + (v2.x * -st1.v));
pVertex[0].tangent.y = coef * ((v1.y * st2.v) + (v2.y * -st1.v));
pVertex[0].tangent.z = coef * ((v1.z * st2.v) + (v2.z * -st1.v));
KRVector3 tangent(
coef * ((v1.x * st2.v) + (v2.x * -st1.v)),
coef * ((v1.y * st2.v) + (v2.y * -st1.v)),
coef * ((v1.z * st2.v) + (v2.z * -st1.v))
);
tangent.normalize();
pVertex[0].tangent.x = tangent.x;
pVertex[0].tangent.y = tangent.y;
pVertex[0].tangent.z = tangent.z;
pVertex[1].tangent.x = tangent.x;
pVertex[1].tangent.y = tangent.y;
pVertex[1].tangent.z = tangent.z;
pVertex[2].tangent.x = tangent.x;
pVertex[2].tangent.y = tangent.y;
pVertex[2].tangent.z = tangent.z;
}
}
}
KRVector3 KRModel::getMinPoint() const {
return m_minPoint;
}
KRVector3 KRModel::getMaxPoint() const {
return m_maxPoint;
}
void KRModel::clearData() {
m_pData->unload();
}
void KRModel::clearBuffers() {
m_submeshes.clear();
}

View File

@@ -32,7 +32,6 @@
#import <vector> #import <vector>
#import <set> #import <set>
#import <string> #import <string>
#import "KRMesh.h"
#import "KRVector2.h" #import "KRVector2.h"
#import "KRContext.h" #import "KRContext.h"
@@ -40,6 +39,13 @@
using std::vector; using std::vector;
using std::set; using std::set;
using std::list;
#define MAX_VBO_SIZE 65535
// MAX_VBO_SIZE must be divisible by 3 so triangles aren't split across VBO objects...
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
#ifndef KRMODEL_I #ifndef KRMODEL_I
#define KRMODEL_I #define KRMODEL_I
@@ -49,10 +55,11 @@ using std::set;
class KRMaterial; class KRMaterial;
class KRModel : public KRContextObject { class KRModel : public KRResource {
public: public:
KRModel(KRContext &context, std::string name, KRDataBlock *data); KRModel(KRContext &context, std::string name, KRDataBlock *data);
KRModel(KRContext &context, std::string name);
virtual ~KRModel(); virtual ~KRModel();
bool hasTransparency(); bool hasTransparency();
@@ -63,16 +70,78 @@ public:
#endif #endif
KRMesh *getMesh(); virtual std::string getExtension();
std::string getName(); virtual bool save(const std::string& path);
void LoadData(std::vector<KRVector3> vertices, std::vector<KRVector2> uva, std::vector<KRVector2> uvb, std::vector<KRVector3> normals, std::vector<KRVector3> tangents, std::vector<int> submesh_starts, std::vector<int> submesh_lengths, std::vector<std::string> material_names);
void loadPack(KRDataBlock *data);
void renderSubmesh(int iSubmesh);
GLfloat getMaxDimension();
KRVector3 getMinPoint() const;
KRVector3 getMaxPoint() const;
typedef struct {
GLint start_vertex;
GLsizei vertex_count;
char szMaterialName[256];
} Submesh;
typedef struct {
GLfloat x;
GLfloat y;
GLfloat z;
} KRVector3D;
typedef struct {
GLfloat u;
GLfloat v;
} TexCoord;
typedef struct {
KRVector3D vertex;
KRVector3D normal;
KRVector3D tangent;
TexCoord uva;
TexCoord uvb;
} VertexData;
VertexData *getVertexData();
vector<Submesh *> getSubmeshes();
typedef struct {
int32_t start_vertex;
int32_t vertex_count;
char szName[256];
} pack_material;
private: private:
vector<KRMaterial *> m_materials; vector<KRMaterial *> m_materials;
set<KRMaterial *> m_uniqueMaterials; set<KRMaterial *> m_uniqueMaterials;
KRMesh *m_pMesh;
std::string m_name;
bool m_hasTransparency; bool m_hasTransparency;
KRVector3 m_minPoint, m_maxPoint;
KRDataBlock *m_pData;
typedef struct {
char szTag[16];
float minx, miny, minz, maxx, maxy, maxz;
int32_t vertex_count;
int32_t submesh_count;
} pack_header;
vector<Submesh *> m_submeshes;
void clearData();
void clearBuffers();
}; };

View File

@@ -187,41 +187,41 @@ void KRModelManager::configureAttribs(bool enable_vertex, bool enable_normal, bo
if(reconfigured || true) { if(reconfigured || true) {
int data_size = 0; int data_size = 0;
if(enable_vertex) { if(enable_vertex) {
data_size += sizeof(KRMesh::KRVector3D); data_size += sizeof(KRModel::KRVector3D);
} }
if(enable_normal) { if(enable_normal) {
data_size += sizeof(KRMesh::KRVector3D); data_size += sizeof(KRModel::KRVector3D);
} }
if(enable_tangent) { if(enable_tangent) {
data_size += sizeof(KRMesh::KRVector3D); data_size += sizeof(KRModel::KRVector3D);
} }
if(enable_uva) { if(enable_uva) {
data_size += sizeof(KRMesh::TexCoord); data_size += sizeof(KRModel::TexCoord);
} }
if(enable_uvb) { if(enable_uvb) {
data_size += sizeof(KRMesh::TexCoord); data_size += sizeof(KRModel::TexCoord);
} }
int offset = 0; int offset = 0;
if(enable_vertex) { if(enable_vertex) {
GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_VERTEX, 3, GL_FLOAT, 0, data_size, BUFFER_OFFSET(offset))); GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_VERTEX, 3, GL_FLOAT, 0, data_size, BUFFER_OFFSET(offset)));
offset += sizeof(KRMesh::KRVector3D); offset += sizeof(KRModel::KRVector3D);
} }
if(enable_normal) { if(enable_normal) {
GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_NORMAL, 3, GL_FLOAT, 0, data_size, BUFFER_OFFSET(offset))); GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_NORMAL, 3, GL_FLOAT, 0, data_size, BUFFER_OFFSET(offset)));
offset += sizeof(KRMesh::KRVector3D); offset += sizeof(KRModel::KRVector3D);
} }
if(enable_tangent) { if(enable_tangent) {
GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TANGENT, 3, GL_FLOAT, 0, data_size, BUFFER_OFFSET(offset))); GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TANGENT, 3, GL_FLOAT, 0, data_size, BUFFER_OFFSET(offset)));
offset += sizeof(KRMesh::KRVector3D); offset += sizeof(KRModel::KRVector3D);
} }
if(enable_uva) { if(enable_uva) {
GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TEXUVA, 2, GL_FLOAT, 0, data_size, BUFFER_OFFSET(offset))); GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TEXUVA, 2, GL_FLOAT, 0, data_size, BUFFER_OFFSET(offset)));
offset += sizeof(KRMesh::TexCoord); offset += sizeof(KRModel::TexCoord);
} }
if(enable_uvb) { if(enable_uvb) {
GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TEXUVB, 2, GL_FLOAT, 0, data_size, BUFFER_OFFSET(offset))); GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TEXUVB, 2, GL_FLOAT, 0, data_size, BUFFER_OFFSET(offset)));
offset += sizeof(KRMesh::TexCoord); offset += sizeof(KRModel::TexCoord);
} }
} }
} }

View File

@@ -22,7 +22,7 @@
#include "KRResource.h" #include "KRResource.h"
#include "KRMesh.h" #include "KRModel.h"
#include "KRMaterial.h" #include "KRMaterial.h"
#include "KRLight.h" #include "KRLight.h"
#include "KRPointLight.h" #include "KRPointLight.h"
@@ -576,7 +576,7 @@ void LoadMesh(KRNode *parent_node, std::vector<KRResource *> &resources, KFbxGeo
// ----====---- Generate Output Mesh Object ----====---- // ----====---- Generate Output Mesh Object ----====----
KRMesh *new_mesh = new KRMesh(parent_node->getContext(), pNode->GetName()); KRModel *new_mesh = new KRModel(parent_node->getContext(), pNode->GetName());
new_mesh->LoadData(vertices, uva, uvb, normals, tangents, submesh_starts, submesh_lengths, material_names); new_mesh->LoadData(vertices, uva, uvb, normals, tangents, submesh_starts, submesh_lengths, material_names);
resources.push_back(new_mesh); resources.push_back(new_mesh);

View File

@@ -17,13 +17,13 @@
#include <assert.h> #include <assert.h>
#include "KRResource.h" #include "KRResource.h"
#include "KRMesh.h" #include "KRModel.h"
std::vector<KRResource *> KRResource::LoadObj(KRContext &context, const std::string& path) std::vector<KRResource *> KRResource::LoadObj(KRContext &context, const std::string& path)
{ {
std::vector<KRResource *> resources; std::vector<KRResource *> resources;
KRMesh *new_mesh = new KRMesh(context, KRResource::GetFileBase(path)); KRModel *new_mesh = new KRModel(context, KRResource::GetFileBase(path));
resources.push_back(new_mesh); resources.push_back(new_mesh);
std::vector<KRVector3> vertices; std::vector<KRVector3> vertices;
std::vector<KRVector2> uva; std::vector<KRVector2> uva;
@@ -42,7 +42,7 @@ std::vector<KRResource *> KRResource::LoadObj(KRContext &context, const std::str
int *pFaces = NULL; int *pFaces = NULL;
vector<KRMesh::pack_material *> m_materials; vector<KRModel::pack_material *> m_materials;
if(data.load(path)) { if(data.load(path)) {
// -----=====----- Get counts -----=====----- // -----=====----- Get counts -----=====-----
@@ -246,7 +246,7 @@ std::vector<KRResource *> KRResource::LoadObj(KRContext &context, const std::str
std::vector<std::string>::iterator material_itr = material_names_t.begin(); std::vector<std::string>::iterator material_itr = material_names_t.begin();
KRMesh::pack_material *pMaterial = new KRMesh::pack_material(); KRModel::pack_material *pMaterial = new KRModel::pack_material();
pMaterial->start_vertex = iVertex; pMaterial->start_vertex = iVertex;
pMaterial->vertex_count = 0; pMaterial->vertex_count = 0;
memset(pMaterial->szName, 256, 0); memset(pMaterial->szName, 256, 0);
@@ -311,7 +311,7 @@ std::vector<KRResource *> KRResource::LoadObj(KRContext &context, const std::str
} }
pMaterial->vertex_count = iVertex - pMaterial->start_vertex; pMaterial->vertex_count = iVertex - pMaterial->start_vertex;
if(*pFace != 0) { if(*pFace != 0) {
pMaterial = new KRMesh::pack_material(); pMaterial = new KRModel::pack_material();
pMaterial->start_vertex = iVertex; pMaterial->start_vertex = iVertex;
pMaterial->vertex_count = 0; pMaterial->vertex_count = 0;
memset(pMaterial->szName, 256, 0); memset(pMaterial->szName, 256, 0);
@@ -324,7 +324,7 @@ std::vector<KRResource *> KRResource::LoadObj(KRContext &context, const std::str
} }
for(int iMaterial=0; iMaterial < m_materials.size(); iMaterial++) { for(int iMaterial=0; iMaterial < m_materials.size(); iMaterial++) {
KRMesh::pack_material *pNewMaterial = m_materials[iMaterial]; KRModel::pack_material *pNewMaterial = m_materials[iMaterial];
if(pNewMaterial->vertex_count > 0) { if(pNewMaterial->vertex_count > 0) {
material_names.push_back(std::string(pNewMaterial->szName)); material_names.push_back(std::string(pNewMaterial->szName));
submesh_starts.push_back(pNewMaterial->start_vertex); submesh_starts.push_back(pNewMaterial->start_vertex);