From 1ca0c8eb5124b86c52fd63604592e416b12a5204 Mon Sep 17 00:00:00 2001 From: kearwood Date: Fri, 14 Dec 2012 20:45:16 +0000 Subject: [PATCH] Implemented support for triangle strip format in KRModel Implemented stock cube and sphere models Implemented visualization of bones --HG-- extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40190 --- KREngine/KREngine.xcodeproj/project.pbxproj | 24 ++++ KREngine/KREngine/Classes/KRBone.cpp | 50 ++++++- KREngine/KREngine/Classes/KRBone.h | 7 +- KREngine/KREngine/Classes/KRMaterial.cpp | 12 ++ KREngine/KREngine/Classes/KRModel.cpp | 38 ++++- KREngine/KREngine/Classes/KRModel.h | 61 ++++---- KREngine/KREngine/Classes/KRModelCube.cpp | 76 ++++++++++ KREngine/KREngine/Classes/KRModelCube.h | 44 ++++++ KREngine/KREngine/Classes/KRModelManager.cpp | 14 +- KREngine/KREngine/Classes/KRModelManager.h | 1 + KREngine/KREngine/Classes/KRModelSphere.cpp | 143 +++++++++++++++++++ KREngine/KREngine/Classes/KRModelSphere.h | 44 ++++++ KREngine/KREngine/Classes/KRNode.cpp | 30 ++-- KREngine/KREngine/Classes/KRNode.h | 4 + KREngine/KREngine/Classes/KRResource+obj.cpp | 2 +- KREngine/KREngine/Shaders/ObjectShader.vsh | 28 ++-- 16 files changed, 522 insertions(+), 56 deletions(-) create mode 100644 KREngine/KREngine/Classes/KRModelCube.cpp create mode 100644 KREngine/KREngine/Classes/KRModelCube.h create mode 100644 KREngine/KREngine/Classes/KRModelSphere.cpp create mode 100644 KREngine/KREngine/Classes/KRModelSphere.h diff --git a/KREngine/KREngine.xcodeproj/project.pbxproj b/KREngine/KREngine.xcodeproj/project.pbxproj index 7531d53..8829290 100644 --- a/KREngine/KREngine.xcodeproj/project.pbxproj +++ b/KREngine/KREngine.xcodeproj/project.pbxproj @@ -182,6 +182,14 @@ E4BBBB9E1512A4AE00F43B5B /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E4BBBB961512A46700F43B5B /* AppKit.framework */; }; E4BBBB9F1512A4B100F43B5B /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E4BBBB941512A45500F43B5B /* Cocoa.framework */; }; E4BBBBA71512A6DC00F43B5B /* KRVector3.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017F13C99BDC0098455B /* KRVector3.cpp */; }; + E4C454AC167BB8EC003586CD /* KRModelCube.h in Headers */ = {isa = PBXBuildFile; fileRef = E4C454AB167BB8EC003586CD /* KRModelCube.h */; }; + E4C454AD167BB8EC003586CD /* KRModelCube.h in Headers */ = {isa = PBXBuildFile; fileRef = E4C454AB167BB8EC003586CD /* KRModelCube.h */; }; + E4C454AF167BB8FC003586CD /* KRModelCube.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4C454AE167BB8FC003586CD /* KRModelCube.cpp */; }; + E4C454B0167BB8FC003586CD /* KRModelCube.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4C454AE167BB8FC003586CD /* KRModelCube.cpp */; }; + E4C454B2167BC04C003586CD /* KRModelSphere.h in Headers */ = {isa = PBXBuildFile; fileRef = E4C454B1167BC04B003586CD /* KRModelSphere.h */; }; + E4C454B3167BC04C003586CD /* KRModelSphere.h in Headers */ = {isa = PBXBuildFile; fileRef = E4C454B1167BC04B003586CD /* KRModelSphere.h */; }; + E4C454B5167BC05C003586CD /* KRModelSphere.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4C454B4167BC05C003586CD /* KRModelSphere.cpp */; }; + E4C454B6167BC05C003586CD /* KRModelSphere.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4C454B4167BC05C003586CD /* KRModelSphere.cpp */; }; E4CA10E51637BD0A005D9400 /* KRTexturePVR.h in Headers */ = {isa = PBXBuildFile; fileRef = E4CA10E41637BD0A005D9400 /* KRTexturePVR.h */; }; E4CA10E61637BD0A005D9400 /* KRTexturePVR.h in Headers */ = {isa = PBXBuildFile; fileRef = E4CA10E41637BD0A005D9400 /* KRTexturePVR.h */; settings = {ATTRIBUTES = (Public, ); }; }; E4CA10E91637BD2B005D9400 /* KRTexturePVR.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4CA10E81637BD2B005D9400 /* KRTexturePVR.cpp */; }; @@ -377,6 +385,10 @@ E4BBBB961512A46700F43B5B /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; E4BBBB981512A47500F43B5B /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; }; E4BBBB9A1512A48200F43B5B /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; + E4C454AB167BB8EC003586CD /* KRModelCube.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRModelCube.h; path = Classes/KRModelCube.h; sourceTree = ""; }; + E4C454AE167BB8FC003586CD /* KRModelCube.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRModelCube.cpp; path = Classes/KRModelCube.cpp; sourceTree = ""; }; + E4C454B1167BC04B003586CD /* KRModelSphere.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRModelSphere.h; path = Classes/KRModelSphere.h; sourceTree = ""; }; + E4C454B4167BC05C003586CD /* KRModelSphere.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRModelSphere.cpp; path = Classes/KRModelSphere.cpp; sourceTree = ""; }; E4CA10E41637BD0A005D9400 /* KRTexturePVR.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRTexturePVR.h; path = Classes/KRTexturePVR.h; sourceTree = ""; }; E4CA10E81637BD2B005D9400 /* KRTexturePVR.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRTexturePVR.cpp; path = Classes/KRTexturePVR.cpp; sourceTree = ""; }; E4CA10EB1637BD47005D9400 /* KRTextureTGA.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRTextureTGA.h; path = Classes/KRTextureTGA.h; sourceTree = ""; }; @@ -619,6 +631,10 @@ E491018213C99BDC0098455B /* KRModelManager.cpp */, E491017A13C99BDC0098455B /* KRModel.h */, E491017913C99BDC0098455B /* KRModel.cpp */, + E4C454AB167BB8EC003586CD /* KRModelCube.h */, + E4C454AE167BB8FC003586CD /* KRModelCube.cpp */, + E4C454B1167BC04B003586CD /* KRModelSphere.h */, + E4C454B4167BC05C003586CD /* KRModelSphere.cpp */, ); name = Model; sourceTree = ""; @@ -849,6 +865,8 @@ E416AA9916713749000F6786 /* KRAnimationCurveManager.h in Headers */, E480BE681671C641004EC8AD /* KRBone.h in Headers */, 104A335F1672D31C001C8BA6 /* KRCollider.h in Headers */, + E4C454AC167BB8EC003586CD /* KRModelCube.h in Headers */, + E4C454B2167BC04C003586CD /* KRModelSphere.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -909,6 +927,8 @@ E461A169152E570700F2044A /* KRSpotLight.h in Headers */, E4324BA516444C0D0043185B /* KRParticleSystem.h in Headers */, E4324BAC16444DEF0043185B /* KRParticleSystemNewtonian.h in Headers */, + E4C454AD167BB8EC003586CD /* KRModelCube.h in Headers */, + E4C454B3167BC04C003586CD /* KRModelSphere.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1044,6 +1064,8 @@ E416AA9C1671375C000F6786 /* KRAnimationCurveManager.cpp in Sources */, E480BE6C1671C653004EC8AD /* KRBone.cpp in Sources */, 104A335E1672D31C001C8BA6 /* KRCollider.cpp in Sources */, + E4C454AF167BB8FC003586CD /* KRModelCube.cpp in Sources */, + E4C454B5167BC05C003586CD /* KRModelSphere.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1102,6 +1124,8 @@ E428C31A1669A25D00A16EDF /* KRAnimationAttribute.cpp in Sources */, E416AA9D1671375C000F6786 /* KRAnimationCurveManager.cpp in Sources */, E480BE6D1671C653004EC8AD /* KRBone.cpp in Sources */, + E4C454B0167BB8FC003586CD /* KRModelCube.cpp in Sources */, + E4C454B6167BC05C003586CD /* KRModelSphere.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/KREngine/KREngine/Classes/KRBone.cpp b/KREngine/KREngine/Classes/KRBone.cpp index ef0bfcf..daa566a 100644 --- a/KREngine/KREngine/Classes/KRBone.cpp +++ b/KREngine/KREngine/Classes/KRBone.cpp @@ -7,14 +7,15 @@ // #include "KRBone.h" +#include "KRContext.h" KRBone::KRBone(KRScene &scene, std::string name) : KRNode(scene, name) { + } KRBone::~KRBone() { - } std::string KRBone::getElementName() { @@ -32,3 +33,50 @@ void KRBone::loadXML(tinyxml2::XMLElement *e) { KRNode::loadXML(e); } + + +#if TARGET_OS_IPHONE + +void KRBone::render(KRCamera *pCamera, std::vector &lights, const KRViewport &viewport, KRNode::RenderPass renderPass) + { + + KRNode::render(pCamera, lights, viewport, renderPass); + + bool bVisualize = true; + + if(renderPass == KRNode::RENDER_PASS_FORWARD_TRANSPARENT && bVisualize) { + KRMat4 sphereModelMatrix = getModelMatrix(); + + KRShader *pShader = getContext().getShaderManager()->getShader("visualize_overlay", pCamera, lights, 0, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, renderPass); + + if(getContext().getShaderManager()->selectShader(*pCamera, pShader, viewport, sphereModelMatrix, lights, 0, renderPass)) { + + // Enable additive blending + GLDEBUG(glEnable(GL_BLEND)); + GLDEBUG(glBlendFunc(GL_ONE, GL_ONE)); + + + // Disable z-buffer write + GLDEBUG(glDepthMask(GL_FALSE)); + + // Enable z-buffer test + GLDEBUG(glEnable(GL_DEPTH_TEST)); + GLDEBUG(glDepthFunc(GL_LEQUAL)); + GLDEBUG(glDepthRangef(0.0, 1.0)); + + std::vector sphereModels = getContext().getModelManager()->getModel("__sphere"); + if(sphereModels.size()) { + for(int i=0; i < sphereModels[0]->getSubmeshCount(); i++) { + sphereModels[0]->renderSubmesh(i); + } + } + + // Enable alpha blending + GLDEBUG(glEnable(GL_BLEND)); + GLDEBUG(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); + } + } + } + +#endif + diff --git a/KREngine/KREngine/Classes/KRBone.h b/KREngine/KREngine/Classes/KRBone.h index 7567237..5869c3f 100644 --- a/KREngine/KREngine/Classes/KRBone.h +++ b/KREngine/KREngine/Classes/KRBone.h @@ -20,8 +20,13 @@ public: virtual std::string getElementName(); virtual tinyxml2::XMLElement *saveXML( tinyxml2::XMLNode *parent); virtual void loadXML(tinyxml2::XMLElement *e); +#if TARGET_OS_IPHONE -protected: + void render(KRCamera *pCamera, std::vector &lights, const KRViewport &viewport, KRNode::RenderPass renderPass); + +#endif + +private: }; diff --git a/KREngine/KREngine/Classes/KRMaterial.cpp b/KREngine/KREngine/Classes/KRMaterial.cpp index 67102af..4a73579 100644 --- a/KREngine/KREngine/Classes/KRMaterial.cpp +++ b/KREngine/KREngine/Classes/KRMaterial.cpp @@ -269,6 +269,18 @@ bool KRMaterial::bind(KRMaterial **prevBoundMaterial, char *szPrevShaderKey, KRC GLfloat *bone_mat_component = bone_mats; for(int bone_index=0; bone_index < bones.size(); bone_index++) { KRBone *bone = bones[bone_index]; + +// KRVector3 initialRotation = bone->getInitialLocalRotation(); +// KRVector3 rotation = bone->getLocalRotation(); +// KRVector3 initialTranslation = bone->getInitialLocalTranslation(); +// KRVector3 translation = bone->getLocalTranslation(); +// KRVector3 initialScale = bone->getInitialLocalScale(); +// KRVector3 scale = bone->getLocalScale(); +// + //printf("%s - delta rotation: %.4f %.4f %.4f\n", bone->getName().c_str(), (rotation.x - initialRotation.x) * 180.0 / M_PI, (rotation.y - initialRotation.y) * 180.0 / M_PI, (rotation.z - initialRotation.z) * 180.0 / M_PI); + //printf("%s - delta translation: %.4f %.4f %.4f\n", bone->getName().c_str(), translation.x - initialTranslation.x, translation.y - initialTranslation.y, translation.z - initialTranslation.z); +// printf("%s - delta scale: %.4f %.4f %.4f\n", bone->getName().c_str(), scale.x - initialScale.x, scale.y - initialScale.y, scale.z - initialScale.z); + KRMat4 t = bone->getInverseBindPoseMatrix() * bone->getModelMatrix(); for(int i=0; i < 16; i++) { *bone_mat_component++ = t[i]; diff --git a/KREngine/KREngine/Classes/KRModel.cpp b/KREngine/KREngine/Classes/KRModel.cpp index 55209dd..bd23f1c 100644 --- a/KREngine/KREngine/Classes/KRModel.cpp +++ b/KREngine/KREngine/Classes/KRModel.cpp @@ -261,7 +261,16 @@ void KRModel::renderSubmesh(int iSubmesh) { if(iVertex + cVertexes >= MAX_VBO_SIZE) { assert(iVertex + (MAX_VBO_SIZE - iVertex) <= cBufferVertexes); - GLDEBUG(glDrawArrays(GL_TRIANGLES, iVertex, (MAX_VBO_SIZE - iVertex))); + switch (getModelFormat()) { + case KRENGINE_MODEL_FORMAT_TRIANGLES: + GLDEBUG(glDrawArrays(GL_TRIANGLES, iVertex, (MAX_VBO_SIZE - iVertex))); + break; + case KRENGINE_MODEL_FORMAT_STRIP: + GLDEBUG(glDrawArrays(GL_TRIANGLE_STRIP, iVertex, (MAX_VBO_SIZE - iVertex))); + break; + default: + break; + } cVertexes -= (MAX_VBO_SIZE - iVertex); iVertex = 0; @@ -269,14 +278,24 @@ void KRModel::renderSubmesh(int iSubmesh) { } else { assert(iVertex + cVertexes <= cBufferVertexes); - GLDEBUG(glDrawArrays(GL_TRIANGLES, iVertex, cVertexes)); + switch (getModelFormat()) { + case KRENGINE_MODEL_FORMAT_TRIANGLES: + GLDEBUG(glDrawArrays(GL_TRIANGLES, iVertex, cVertexes)); + break; + case KRENGINE_MODEL_FORMAT_STRIP: + GLDEBUG(glDrawArrays(GL_TRIANGLE_STRIP, iVertex, cVertexes)); + break; + default: + break; + } + cVertexes = 0; } } } -void KRModel::LoadData(std::vector vertices, std::vector uva, std::vector uvb, std::vector normals, std::vector tangents, std::vector submesh_starts, std::vector submesh_lengths, std::vector material_names, std::vector bone_names, std::vector > bone_indexes, std::vector > bone_weights) { +void KRModel::LoadData(std::vector vertices, std::vector uva, std::vector uvb, std::vector normals, std::vector tangents, std::vector submesh_starts, std::vector submesh_lengths, std::vector material_names, std::vector bone_names, std::vector > bone_indexes, std::vector > bone_weights, KRModel::model_format_t model_format) { clearData(); @@ -317,6 +336,7 @@ void KRModel::LoadData(std::vector vertices, std::vector u pHeader->submesh_count = (__int32_t)submesh_count; pHeader->vertex_count = (__int32_t)vertex_count; pHeader->bone_count = (__int32_t)bone_count; + pHeader->model_format = model_format; strcpy(pHeader->szTag, "KROBJPACK1.1 "); updateAttributeOffsets(); @@ -435,6 +455,8 @@ void KRModel::LoadData(std::vector vertices, std::vector u } } } + + optimize(); } KRVector3 KRModel::getMinPoint() const { @@ -680,3 +702,13 @@ char *KRModel::getBoneName(int bone_index) { return getBone(bone_index)->szName; } + +void KRModel::optimize() +{ + // TODO - Add algorithm to convert model to indexed vertices, identying vertexes with identical attributes and optimizing order of trianges for best usage post-vertex-transform cache on GPU +} + +KRModel::model_format_t KRModel::getModelFormat() +{ + return (model_format_t)getHeader()->model_format; +} diff --git a/KREngine/KREngine/Classes/KRModel.h b/KREngine/KREngine/Classes/KRModel.h index c390138..8f6e552 100644 --- a/KREngine/KREngine/Classes/KRModel.h +++ b/KREngine/KREngine/Classes/KRModel.h @@ -70,28 +70,6 @@ public: bool hasTransparency(); -#if TARGET_OS_IPHONE - - void render(KRCamera *pCamera, std::vector &lights, const KRViewport &viewport, const KRMat4 &matModel, KRTexture *pLightMap, KRNode::RenderPass renderPass, const std::vector &bones); - -#endif - - std::string m_lodBaseName; - - virtual std::string getExtension(); - virtual bool save(const std::string& path); - - void LoadData(std::vector vertices, std::vector uva, std::vector uvb, std::vector normals, std::vector tangents, std::vector submesh_starts, std::vector submesh_lengths, std::vector material_names, std::vector bone_names, std::vector > bone_indexes, std::vector > bone_weights); - void loadPack(KRDataBlock *data); - - - void renderSubmesh(int iSubmesh); - - GLfloat getMaxDimension(); - - KRVector3 getMinPoint() const; - KRVector3 getMaxPoint() const; - typedef enum { KRENGINE_ATTRIB_VERTEX = 0, KRENGINE_ATTRIB_NORMAL, @@ -104,10 +82,36 @@ public: } vertex_attrib_t; typedef enum { - KRENGINE_MODEL_FORMAT_TRIANGLES = 1, - KRENGINE_MODEL_FORMAT_INDEXED + KRENGINE_MODEL_FORMAT_TRIANGLES = 0, + KRENGINE_MODEL_FORMAT_STRIP, + KRENGINE_MODEL_FORMAT_INDEXED_TRIANGLES, + KRENGINE_MODEL_FORMAT_INDEXED_STRIP } model_format_t; +#if TARGET_OS_IPHONE + + void render(KRCamera *pCamera, std::vector &lights, const KRViewport &viewport, const KRMat4 &matModel, KRTexture *pLightMap, KRNode::RenderPass renderPass, const std::vector &bones); + +#endif + + std::string m_lodBaseName; + + virtual std::string getExtension(); + virtual bool save(const std::string& path); + + void LoadData(std::vector vertices, std::vector uva, std::vector uvb, std::vector normals, std::vector tangents, std::vector submesh_starts, std::vector submesh_lengths, std::vector material_names, std::vector bone_names, std::vector > bone_indexes, std::vector > bone_weights, model_format_t model_format); + void loadPack(KRDataBlock *data); + + + void renderSubmesh(int iSubmesh); + + GLfloat getMaxDimension(); + + KRVector3 getMinPoint() const; + KRVector3 getMaxPoint() const; + + + typedef struct { GLint start_vertex; GLsizei vertex_count; @@ -176,7 +180,11 @@ public: int getBoneCount(); char *getBoneName(int bone_index); + + model_format_t getModelFormat(); private: + + int m_lodCoverage; // This LOD level is activated when the bounding box of the model will cover less than this percent of the screen (100 = highest detail model) vector m_materials; set m_uniqueMaterials; @@ -188,9 +196,11 @@ private: KRDataBlock *m_pData; + + typedef struct { char szTag[16]; - int32_t format_type; // 0 == Triangle list, 1 == Indexed triangle list, 2 == Indexed triangle strips, rest are reserved (model_format_t enum) + int32_t model_format; // 0 == Triangle list, 1 == Triangle strips, 2 == Indexed triangle list, 3 == Indexed triangle strips, rest are reserved (model_format_t enum) int32_t vertex_attrib_flags; int32_t vertex_count; int32_t submesh_count; @@ -209,6 +219,7 @@ private: void clearBuffers(); void setName(const std::string name); + void optimize(); diff --git a/KREngine/KREngine/Classes/KRModelCube.cpp b/KREngine/KREngine/Classes/KRModelCube.cpp new file mode 100644 index 0000000..d4ba8b8 --- /dev/null +++ b/KREngine/KREngine/Classes/KRModelCube.cpp @@ -0,0 +1,76 @@ +// +// KRModelCube.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 "KRModelCube.h" + + +KRModelCube::KRModelCube(KRContext &context) : KRModel(context, "__cube") +{ + std::vector vertices; + std::vector uva; + std::vector uvb; + std::vector normals; + std::vector tangents; + std::vector submesh_starts; + std::vector submesh_lengths; + std::vector material_names; + std::vector bone_names; + std::vector > bone_indexes; + std::vector > bone_weights; + + + vertices.push_back(KRVector3(1.0, 1.0, 1.0)); + vertices.push_back(KRVector3(-1.0, 1.0, 1.0)); + vertices.push_back(KRVector3(1.0,-1.0, 1.0)); + vertices.push_back(KRVector3(-1.0,-1.0, 1.0)); + vertices.push_back(KRVector3(-1.0,-1.0,-1.0)); + vertices.push_back(KRVector3(-1.0, 1.0, 1.0)); + vertices.push_back(KRVector3(-1.0, 1.0,-1.0)); + vertices.push_back(KRVector3(1.0, 1.0, 1.0)); + vertices.push_back(KRVector3(1.0, 1.0,-1.0)); + vertices.push_back(KRVector3(1.0,-1.0, 1.0)); + vertices.push_back(KRVector3(1.0,-1.0,-1.0)); + vertices.push_back(KRVector3(-1.0,-1.0,-1.0)); + vertices.push_back(KRVector3(1.0, 1.0,-1.0)); + vertices.push_back(KRVector3(-1.0, 1.0,-1.0)); + + + submesh_starts.push_back(0); + submesh_lengths.push_back(vertices.size()); + material_names.push_back(""); + + LoadData(vertices, uva, uvb, normals, tangents, submesh_starts, submesh_lengths, material_names, bone_names, bone_indexes, bone_weights, KRENGINE_MODEL_FORMAT_STRIP); +} + +KRModelCube::~KRModelCube() +{ + +} diff --git a/KREngine/KREngine/Classes/KRModelCube.h b/KREngine/KREngine/Classes/KRModelCube.h new file mode 100644 index 0000000..4d6ea11 --- /dev/null +++ b/KREngine/KREngine/Classes/KRModelCube.h @@ -0,0 +1,44 @@ +// +// KRModelCube.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. +// + +#ifndef KRMODELCUBE_H +#define KRMODELCUBE_H + +#include "KRModel.h" + +class KRModelCube : public KRModel { +public: + KRModelCube(KRContext &context); + virtual ~KRModelCube(); +private: +}; + +#endif diff --git a/KREngine/KREngine/Classes/KRModelManager.cpp b/KREngine/KREngine/Classes/KRModelManager.cpp index 9f059e7..146c6a3 100644 --- a/KREngine/KREngine/Classes/KRModelManager.cpp +++ b/KREngine/KREngine/Classes/KRModelManager.cpp @@ -33,6 +33,8 @@ #include #import "KRModel.h" +#import "KRModelCube.h" +#import "KRModelSphere.h" KRModelManager::KRModelManager(KRContext &context) : KRContextObject(context) { m_currentVBO.vbo_handle = 0; @@ -41,6 +43,9 @@ KRModelManager::KRModelManager(KRContext &context) : KRContextObject(context) { m_vboMemUsed = 0; m_randomParticleVertexData = NULL; m_volumetricLightingVertexData = NULL; + + addModel(new KRModelCube(context)); + addModel(new KRModelSphere(context)); } KRModelManager::~KRModelManager() { @@ -60,11 +65,14 @@ KRModel *KRModelManager::loadModel(const char *szName, KRDataBlock *pData) { KRModel *pModel = new KRModel(*m_pContext, lowerName, pData); - m_models.insert(std::pair(pModel->getLODBaseName(), pModel)); - + addModel(pModel); return pModel; } +void KRModelManager::addModel(KRModel *model) { + m_models.insert(std::pair(model->getLODBaseName(), model)); +} + std::vector KRModelManager::getModel(const char *szName) { std::string lowerName = szName; std::transform(lowerName.begin(), lowerName.end(), @@ -80,7 +88,7 @@ std::vector KRModelManager::getModel(const char *szName) { std::sort(matching_models.begin(), matching_models.end(), KRModel::lod_sort_predicate); - if(matching_models.size() == 0) { + if(matching_models.size() == 0) { fprintf(stderr, "ERROR: Model not found: %s\n", lowerName.c_str()); } diff --git a/KREngine/KREngine/Classes/KRModelManager.h b/KREngine/KREngine/Classes/KRModelManager.h index 4f18aea..0a4da21 100644 --- a/KREngine/KREngine/Classes/KRModelManager.h +++ b/KREngine/KREngine/Classes/KRModelManager.h @@ -55,6 +55,7 @@ public: KRModel *loadModel(const char *szName, KRDataBlock *pData); std::vector getModel(const char *szName); + void addModel(KRModel *model); std::vector getModelNames(); std::multimap getModels(); diff --git a/KREngine/KREngine/Classes/KRModelSphere.cpp b/KREngine/KREngine/Classes/KRModelSphere.cpp new file mode 100644 index 0000000..ff3178d --- /dev/null +++ b/KREngine/KREngine/Classes/KRModelSphere.cpp @@ -0,0 +1,143 @@ +// +// KRModelSphere.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 "KRModelSphere.h" + + +KRModelSphere::KRModelSphere(KRContext &context) : KRModel(context, "__sphere") +{ + std::vector vertices; + std::vector uva; + std::vector uvb; + std::vector normals; + std::vector tangents; + std::vector submesh_starts; + std::vector submesh_lengths; + std::vector material_names; + std::vector bone_names; + std::vector > bone_indexes; + std::vector > bone_weights; + + + + // Create a triangular facet approximation to a sphere + // Based on algorithm from Paul Bourke: http://paulbourke.net/miscellaneous/sphere_cylinder/ + + int iterations = 3; + int facet_count = pow(4, iterations) * 8; + + class Facet3 { + public: + Facet3() { + + } + ~Facet3() { + + } + KRVector3 p1; + KRVector3 p2; + KRVector3 p3; + }; + + std::vector f = std::vector(facet_count); + + int i,it; + float a; + KRVector3 p[6] = { + KRVector3(0,0,1), + KRVector3(0,0,-1), + KRVector3(-1,-1,0), + KRVector3(1,-1,0), + KRVector3(1,1,0), + KRVector3(-1,1,0) + }; + + KRVector3 pa,pb,pc; + int nt = 0,ntold; + + /* Create the level 0 object */ + a = 1 / sqrt(2.0); + for (i=0;i<6;i++) { + p[i].x *= a; + p[i].y *= a; + } + f[0].p1 = p[0]; f[0].p2 = p[3]; f[0].p3 = p[4]; + f[1].p1 = p[0]; f[1].p2 = p[4]; f[1].p3 = p[5]; + f[2].p1 = p[0]; f[2].p2 = p[5]; f[2].p3 = p[2]; + f[3].p1 = p[0]; f[3].p2 = p[2]; f[3].p3 = p[3]; + f[4].p1 = p[1]; f[4].p2 = p[4]; f[4].p3 = p[3]; + f[5].p1 = p[1]; f[5].p2 = p[5]; f[5].p3 = p[4]; + f[6].p1 = p[1]; f[6].p2 = p[2]; f[6].p3 = p[5]; + f[7].p1 = p[1]; f[7].p2 = p[3]; f[7].p3 = p[2]; + nt = 8; + + /* Bisect each edge and move to the surface of a unit sphere */ + for (it=0;it KRResource::LoadObj(KRContext &context, const std::str std::vector > bone_indexes; std::vector > bone_weights; - new_mesh->LoadData(vertices, uva, uvb, normals, tangents, submesh_starts, submesh_lengths, material_names, bone_names, bone_indexes, bone_weights); + new_mesh->LoadData(vertices, uva, uvb, normals, tangents, submesh_starts, submesh_lengths, material_names, bone_names, bone_indexes, bone_weights, KRModel::KRENGINE_MODEL_FORMAT_TRIANGLES); } if(pFaces) { diff --git a/KREngine/KREngine/Shaders/ObjectShader.vsh b/KREngine/KREngine/Shaders/ObjectShader.vsh index fc93872..fd08cb2 100644 --- a/KREngine/KREngine/Shaders/ObjectShader.vsh +++ b/KREngine/KREngine/Shaders/ObjectShader.vsh @@ -166,23 +166,27 @@ void main() { #if BONE_COUNT > 0 mediump vec4 scaled_bone_indexes = bone_indexes; + mediump vec4 scaled_bone_weights = bone_weights; + + // scaled_bone_indexes = vec4(1.0, 0.0, 0.0, 0.0); + // scaled_bone_weights = vec4(1.0, 0.0, 0.0, 0.0); highp vec3 vertex_position_skinned = - ((bone_transforms[ int(scaled_bone_indexes.x) ] * vec4(vertex_position, 1.0)).xyz * bone_weights.x) + - ((bone_transforms[ int(scaled_bone_indexes.y) ] * vec4(vertex_position, 1.0)).xyz * bone_weights.y) + - ((bone_transforms[ int(scaled_bone_indexes.z) ] * vec4(vertex_position, 1.0)).xyz * bone_weights.z) + - ((bone_transforms[ int(scaled_bone_indexes.w) ] * vec4(vertex_position, 1.0)).xyz * bone_weights.w); + ((bone_transforms[ int(scaled_bone_indexes.x) ] * vec4(vertex_position, 1.0)).xyz * scaled_bone_weights.x) + + ((bone_transforms[ int(scaled_bone_indexes.y) ] * vec4(vertex_position, 1.0)).xyz * scaled_bone_weights.y) + + ((bone_transforms[ int(scaled_bone_indexes.z) ] * vec4(vertex_position, 1.0)).xyz * scaled_bone_weights.z) + + ((bone_transforms[ int(scaled_bone_indexes.w) ] * vec4(vertex_position, 1.0)).xyz * scaled_bone_weights.w); highp vec3 vertex_normal_skinned = normalize( - ((bone_transforms[ int(scaled_bone_indexes.x) ] * vec4(vertex_normal, 1.0)).xyz * bone_weights.x) + - ((bone_transforms[ int(scaled_bone_indexes.y) ] * vec4(vertex_normal, 1.0)).xyz * bone_weights.y) + - ((bone_transforms[ int(scaled_bone_indexes.z) ] * vec4(vertex_normal, 1.0)).xyz * bone_weights.z) + - ((bone_transforms[ int(scaled_bone_indexes.w) ] * vec4(vertex_normal, 1.0)).xyz * bone_weights.w)); + ((bone_transforms[ int(scaled_bone_indexes.x) ] * vec4(vertex_normal, 1.0)).xyz * scaled_bone_weights.x) + + ((bone_transforms[ int(scaled_bone_indexes.y) ] * vec4(vertex_normal, 1.0)).xyz * scaled_bone_weights.y) + + ((bone_transforms[ int(scaled_bone_indexes.z) ] * vec4(vertex_normal, 1.0)).xyz * scaled_bone_weights.z) + + ((bone_transforms[ int(scaled_bone_indexes.w) ] * vec4(vertex_normal, 1.0)).xyz * scaled_bone_weights.w)); highp vec3 vertex_tangent_skinned = normalize( - ((bone_transforms[ int(scaled_bone_indexes.x) ] * vec4(vertex_tangent, 1.0)).xyz * bone_weights.x) + - ((bone_transforms[ int(scaled_bone_indexes.y) ] * vec4(vertex_tangent, 1.0)).xyz * bone_weights.y) + - ((bone_transforms[ int(scaled_bone_indexes.z) ] * vec4(vertex_tangent, 1.0)).xyz * bone_weights.z) + - ((bone_transforms[ int(scaled_bone_indexes.w) ] * vec4(vertex_tangent, 1.0)).xyz * bone_weights.w)); + ((bone_transforms[ int(scaled_bone_indexes.x) ] * vec4(vertex_tangent, 1.0)).xyz * scaled_bone_weights.x) + + ((bone_transforms[ int(scaled_bone_indexes.y) ] * vec4(vertex_tangent, 1.0)).xyz * scaled_bone_weights.y) + + ((bone_transforms[ int(scaled_bone_indexes.z) ] * vec4(vertex_tangent, 1.0)).xyz * scaled_bone_weights.z) + + ((bone_transforms[ int(scaled_bone_indexes.w) ] * vec4(vertex_tangent, 1.0)).xyz * scaled_bone_weights.w)); #endif