From dfdc8c69b8f164aa00c94db109a8ca6e1fe01f88 Mon Sep 17 00:00:00 2001 From: kearwood Date: Wed, 9 May 2012 01:56:04 +0000 Subject: [PATCH] Native Blender file format support in progress Changed material colour parameters from separate float values to KRVector3's --HG-- extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%4056 --- KREngine/KREngine.xcodeproj/project.pbxproj | 8 + KREngine/KREngine/Classes/KRPointLight.cpp | 47 ++--- .../KREngine/Classes/KRResource+blend.cpp | 175 ++++++++++++++++++ KREngine/KREngine/Classes/KRResource+blend.h | 50 +++++ KREngine/KREngine/Classes/KRResource+fbx.cpp | 10 +- KREngine/KREngine/Classes/KRResource.cpp | 2 + KREngine/KREngine/Classes/KRResource.h | 1 + KREngine/KREngine/Classes/KRShader.cpp | 4 +- 8 files changed, 254 insertions(+), 43 deletions(-) create mode 100644 KREngine/KREngine/Classes/KRResource+blend.cpp create mode 100644 KREngine/KREngine/Classes/KRResource+blend.h diff --git a/KREngine/KREngine.xcodeproj/project.pbxproj b/KREngine/KREngine.xcodeproj/project.pbxproj index f064bfb..4d15e02 100644 --- a/KREngine/KREngine.xcodeproj/project.pbxproj +++ b/KREngine/KREngine.xcodeproj/project.pbxproj @@ -30,6 +30,8 @@ E461A176152E5C5600F2044A /* KRPointLight.h in Headers */ = {isa = PBXBuildFile; fileRef = E461A157152E555400F2044A /* KRPointLight.h */; settings = {ATTRIBUTES = (Public, ); }; }; E461A177152E5C6600F2044A /* KRMat4.h in Headers */ = {isa = PBXBuildFile; fileRef = E491017613C99BDC0098455B /* KRMat4.h */; settings = {ATTRIBUTES = (Public, ); }; }; E461A17A152E5C9100F2044A /* KRMat4.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017713C99BDC0098455B /* KRMat4.cpp */; }; + E46A6B6D1559E97D000DBD37 /* KRResource+blend.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E46A6B6C1559E97D000DBD37 /* KRResource+blend.cpp */; }; + E46A6B701559EF0A000DBD37 /* KRResource+blend.h in Headers */ = {isa = PBXBuildFile; fileRef = E46A6B6F1559EF0A000DBD37 /* KRResource+blend.h */; }; E46C214515364BC8009CABF3 /* tinyxml2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E46C214215364BC8009CABF3 /* tinyxml2.cpp */; }; E46C214615364BC8009CABF3 /* tinyxml2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E46C214215364BC8009CABF3 /* tinyxml2.cpp */; }; E46C214715364BC8009CABF3 /* tinyxml2.h in Headers */ = {isa = PBXBuildFile; fileRef = E46C214315364BC8009CABF3 /* tinyxml2.h */; }; @@ -159,6 +161,8 @@ E461A15E152E565700F2044A /* KRDirectionalLight.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRDirectionalLight.cpp; path = Classes/KRDirectionalLight.cpp; sourceTree = ""; }; E461A164152E56C000F2044A /* KRSpotLight.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRSpotLight.cpp; path = Classes/KRSpotLight.cpp; sourceTree = ""; }; E461A167152E570500F2044A /* KRSpotLight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRSpotLight.h; path = Classes/KRSpotLight.h; sourceTree = ""; }; + E46A6B6C1559E97D000DBD37 /* KRResource+blend.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = "KRResource+blend.cpp"; path = "Classes/KRResource+blend.cpp"; sourceTree = ""; }; + E46A6B6F1559EF0A000DBD37 /* KRResource+blend.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "KRResource+blend.h"; path = "Classes/KRResource+blend.h"; sourceTree = ""; }; E46C214115364BC8009CABF3 /* tinyxml2_readme.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = tinyxml2_readme.txt; path = KREngine/Classes/tinyxml2_readme.txt; sourceTree = ""; }; E46C214215364BC8009CABF3 /* tinyxml2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = tinyxml2.cpp; path = KREngine/Classes/tinyxml2.cpp; sourceTree = ""; }; E46C214315364BC8009CABF3 /* tinyxml2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = tinyxml2.h; path = KREngine/Classes/tinyxml2.h; sourceTree = ""; }; @@ -279,6 +283,8 @@ E497B949151BCEE900D3DC67 /* KRResource.h */, E497B94C151BCF2500D3DC67 /* KRResource.cpp */, E497B94F151BD2CE00D3DC67 /* KRResource+obj.cpp */, + E46A6B6C1559E97D000DBD37 /* KRResource+blend.cpp */, + E46A6B6F1559EF0A000DBD37 /* KRResource+blend.h */, E497B952151BEDA600D3DC67 /* KRResource+fbx.cpp */, ); name = Resources; @@ -542,6 +548,7 @@ E461A15D152E563100F2044A /* KRDirectionalLight.h in Headers */, E461A169152E570700F2044A /* KRSpotLight.h in Headers */, E46C214815364BC8009CABF3 /* tinyxml2.h in Headers */, + E46A6B701559EF0A000DBD37 /* KRResource+blend.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -692,6 +699,7 @@ E4D13364153767ED0070068C /* KRShaderManager.cpp in Sources */, E46C214C15364DEC009CABF3 /* KRSceneManager.cpp in Sources */, E48C697315374F7E00232E28 /* KRContext.cpp in Sources */, + E46A6B6D1559E97D000DBD37 /* KRResource+blend.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/KREngine/KREngine/Classes/KRPointLight.cpp b/KREngine/KREngine/Classes/KRPointLight.cpp index b0c4516..341b1cb 100644 --- a/KREngine/KREngine/Classes/KRPointLight.cpp +++ b/KREngine/KREngine/Classes/KRPointLight.cpp @@ -121,43 +121,18 @@ void KRPointLight::render(KRCamera *pCamera, KRContext *pContext, KRBoundingVolu // Disable z-buffer write glDepthMask(GL_FALSE); - //if(view_space_light_position.GetMagnitude() > influence_radius) { - // Render sphere of light's influence - generateMesh(); + // Render sphere of light's influence + generateMesh(); + + // Enable z-buffer test + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glDepthRangef(0.0, 1.0); - // Enable z-buffer test - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LEQUAL); - glDepthRangef(0.0, 1.0); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_VERTEX, 3, GL_FLOAT, 0, 0, m_sphereVertices); - glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_VERTEX); - glDrawArrays(GL_TRIANGLES, 0, m_cVertices); - /* - } else { - // Sphere would be clipped against view frustrum. Fall back to a full screen quad - - // Disable z-buffer test - glDisable(GL_DEPTH_TEST); - - // Render a full screen quad - static const GLfloat squareVertices[] = { - -1.0f, -1.0f, 0.0f, - 1.0f, -1.0f, 0.0f, - -1.0f, 1.0f, 0.0f, - 1.0f, 1.0f, 0.0f, - }; - - KRMat4 matIdentity; - glUniformMatrix4fv(pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_MVP], 1, GL_FALSE, matIdentity.getPointer()); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_VERTEX, 3, GL_FLOAT, 0, 0, squareVertices); - glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_VERTEX); - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - } - */ + glBindBuffer(GL_ARRAY_BUFFER, 0); + glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_VERTEX, 3, GL_FLOAT, 0, 0, m_sphereVertices); + glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_VERTEX); + glDrawArrays(GL_TRIANGLES, 0, m_cVertices); } } diff --git a/KREngine/KREngine/Classes/KRResource+blend.cpp b/KREngine/KREngine/Classes/KRResource+blend.cpp new file mode 100644 index 0000000..5218411 --- /dev/null +++ b/KREngine/KREngine/Classes/KRResource+blend.cpp @@ -0,0 +1,175 @@ +// +// KRResource+blend.cpp +// KREngine +// +// Created by Kearwood Gilbert on 12-05-08. +// Copyright (c) 2012 Kearwood Software. All rights reserved. +// + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "KRResource.h" +#include "KRMesh.h" +#include "KRScene.h" +#include "KRResource+blend.h" + + +std::vector KRResource::LoadBlenderScene(const std::string& path) { + std::vector resources; + KRScene *pScene = new KRScene(KRResource::GetFileBase(path)); + resources.push_back(pScene); + + int fdFile = 0; + int fileSize = 0; + void *pFile = NULL; + + struct stat statbuf; + fdFile = open(path.c_str(), O_RDONLY); + if(fdFile >= 0) { + if(fstat(fdFile, &statbuf) >= 0) { + if ((pFile = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fdFile, 0)) == (caddr_t) -1) { + } else { + fileSize = statbuf.st_size; + + + KRBlendFile blend_file = KRBlendFile(pFile); + } + } + } + + if(pFile != NULL) { + munmap(pFile, fileSize); + } + + if(fdFile != 0) { + close(fdFile); + } + + + return resources; +} + + +KRBlendFile::KRBlendFile(const void *pFile) { + unsigned char *scan = (unsigned char *)pFile; + readHeader(scan); + std::string block_code = ""; + while(block_code != "ENDB") { + Block b = Block(this, scan); + block_code = b.getCode(); + m_blocks.push_back(b); + + printf("Loaded block: %s - %i bytes\n", b.getCode().c_str(), b.getDataSize()); + } +} + +void KRBlendFile::readHeader(unsigned char *&scan) { + if(strncmp((char *)scan, "BLENDER", 7) != 0) { + // TODO throw exception + } + scan += 7; + if(scan[0] == '_' && scan[1] == 'v') { + // 32-bit, little-endian + m_file_type = KRBLEND_LITTLEENDIAN_32BIT; + } else if(scan[0] == '_' && scan[1] == 'V') { + // 32-bit, bit-endian + m_file_type = KRBLEND_BIGENDIAN_32BIT; + } else if(scan[0] == '-' && scan[1] == 'v') { + // 64-bit, little-endian + m_file_type = KRBLEND_LITTLEENDIAN_64BIT; + } else if(scan[0] == '-' && scan[1] == 'V') { + // 64-bit, big-endian + m_file_type = KRBLEND_BIGENDIAN_64BIT; + } else { + // TODO - throw exception + } + scan += 5; // Skip and ignore version +} + +__int32_t KRBlendFile::readInt(unsigned char *&scan) { + __int32_t ret = 0; + // read a 32-bit integer and increment scan + + switch (m_file_type) { + case KRBLEND_BIGENDIAN_32BIT: + case KRBLEND_BIGENDIAN_64BIT: + ret = (__int32_t)scan[3] + (__int32_t)scan[2] * 0x100 + (__int32_t)scan[1] * 0x10000 + (__int32_t)scan[0] * 0x1000000; + break; + case KRBLEND_LITTLEENDIAN_32BIT: + case KRBLEND_LITTLEENDIAN_64BIT: + ret = (__int32_t)scan[0] + (__int32_t)scan[1] * 0x100 + (__int32_t)scan[2] * 0x10000 + (__int32_t)scan[3] * 0x1000000; + break; + } + + scan += 4; + return ret; +} + +__int64_t KRBlendFile::readPointer(unsigned char *&scan) { + __int64_t ret = 0; + // read a 32-bit integer and increment scan + switch (m_file_type) { + case KRBLEND_BIGENDIAN_32BIT: + ret = scan[3] + scan[2] * 0x100 + scan[1] * 0x10000 + scan[0] * 0x1000000; + scan += 4; + break; + case KRBLEND_LITTLEENDIAN_32BIT: + ret = scan[0] + scan[1] * 0x100 + scan[2] * 0x10000 + scan[3] * 0x1000000; + scan += 4; + break; + + case KRBLEND_BIGENDIAN_64BIT: + ret = scan[7] + scan[6] * 0x100 + scan[5] * 0x10000 + scan[4] * 0x1000000 + scan[3] * 0x100000000 + scan[2] * 0x10000000000 + scan[1] * 0x1000000000000 + scan[0] * 0x100000000000000; + scan += 8; + break; + case KRBLEND_LITTLEENDIAN_64BIT: + ret = scan[0] + scan[1] * 0x100 + scan[2] * 0x10000 + scan[3] * 0x1000000 + scan[4] * 0x100000000 + scan[5] * 0x10000000000 + scan[6] * 0x1000000000000 + scan[7] * 0x100000000000000; + scan += 8; + break; + } + + + return ret; +} + +KRBlendFile::~KRBlendFile() { + +} + + +KRBlendFile::Block::Block(KRBlendFile *blendFile, unsigned char *&scan) { + scan += (__int64_t)scan % 4; // Scan forward until the next 4-byte boundary + char szBlock[5]; + szBlock[0] = *scan++; + szBlock[1] = *scan++; + szBlock[2] = *scan++; + szBlock[3] = *scan++; + szBlock[4] = '\0'; + m_code = szBlock; + m_dataSize = blendFile->readInt(scan); + m_prev_pointer = blendFile->readPointer(scan); + m_sdna_index = blendFile->readInt(scan); + m_structure_count = blendFile->readInt(scan); + m_data = scan; + scan += m_dataSize; +} +KRBlendFile::Block::~Block() { + +} + +std::string KRBlendFile::Block::getCode() { + return m_code; +} + +int KRBlendFile::Block::getDataSize() { + return m_dataSize; +} + diff --git a/KREngine/KREngine/Classes/KRResource+blend.h b/KREngine/KREngine/Classes/KRResource+blend.h new file mode 100644 index 0000000..a6e8882 --- /dev/null +++ b/KREngine/KREngine/Classes/KRResource+blend.h @@ -0,0 +1,50 @@ +// +// KRResource+blend.h +// KREngine +// +// Created by Kearwood Gilbert on 12-05-08. +// Copyright (c) 2012 Kearwood Software. All rights reserved. +// + +#ifndef KREngine_KRResource_blend_h +#define KREngine_KRResource_blend_h + +class KRBlendFile { +public: + KRBlendFile(const void *pFile); + ~KRBlendFile(); + + class Block { + public: + Block(KRBlendFile *blendFile, unsigned char *&scan); + ~Block(); + + std::string getCode(); + int getDataSize(); + private: + std::string m_code; + __int32_t m_dataSize; + __int32_t m_sdna_index; + __int32_t m_structure_count; + __int64_t m_prev_pointer; + unsigned char *m_data; + }; + +private: + enum file_type { + KRBLEND_LITTLEENDIAN_32BIT, + KRBLEND_LITTLEENDIAN_64BIT, + KRBLEND_BIGENDIAN_32BIT, + KRBLEND_BIGENDIAN_64BIT + } m_file_type; + void readHeader(unsigned char *&scan); + + __int32_t readInt(unsigned char *&scan); + __int64_t readPointer(unsigned char *&scan); + + std::vector m_blocks; +}; + + + +#endif diff --git a/KREngine/KREngine/Classes/KRResource+fbx.cpp b/KREngine/KREngine/Classes/KRResource+fbx.cpp index be88e48..5971109 100644 --- a/KREngine/KREngine/Classes/KRResource+fbx.cpp +++ b/KREngine/KREngine/Classes/KRResource+fbx.cpp @@ -433,15 +433,15 @@ void LoadMesh(KRNode *parent_node, std::vector &resources, KFbxGeo // Ambient Color lKFbxDouble3 =((KFbxSurfacePhong *) pMaterial)->Ambient; - new_material->setAmbient(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2]); + new_material->setAmbient(KRVector3(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2])); // Diffuse Color lKFbxDouble3 =((KFbxSurfacePhong *) pMaterial)->Diffuse; - new_material->setDiffuse(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2]); + new_material->setDiffuse(KRVector3(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2])); // Specular Color (unique to Phong materials) lKFbxDouble3 =((KFbxSurfacePhong *) pMaterial)->Specular; - new_material->setSpecular(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2]); + new_material->setSpecular(KRVector3(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2])); // Emissive Color //lKFbxDouble3 =((KFbxSurfacePhong *) pMaterial)->Emissive; @@ -468,11 +468,11 @@ void LoadMesh(KRNode *parent_node, std::vector &resources, KFbxGeo // Ambient Color lKFbxDouble3=((KFbxSurfaceLambert *)pMaterial)->Ambient; - new_material->setAmbient(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2]); + new_material->setAmbient(KRVector3(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2])); // Diffuse Color lKFbxDouble3 =((KFbxSurfaceLambert *)pMaterial)->Diffuse; - new_material->setDiffuse(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2]); + new_material->setDiffuse(KRVector3(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2])); // Emissive //lKFbxDouble3 =((KFbxSurfaceLambert *)pMaterial)->Emissive; diff --git a/KREngine/KREngine/Classes/KRResource.cpp b/KREngine/KREngine/Classes/KRResource.cpp index ef54580..9aafdb5 100644 --- a/KREngine/KREngine/Classes/KRResource.cpp +++ b/KREngine/KREngine/Classes/KRResource.cpp @@ -64,6 +64,8 @@ std::vector KRResource::Load(const std::string& path) return LoadObj(path); } else if(extension.compare("fbx") == 0) { return LoadFbx(path); + } else if(extension.compare("blend") == 0) { + return LoadBlenderScene(path); } return resources; diff --git a/KREngine/KREngine/Classes/KRResource.h b/KREngine/KREngine/Classes/KRResource.h index 49d3820..878bc23 100644 --- a/KREngine/KREngine/Classes/KRResource.h +++ b/KREngine/KREngine/Classes/KRResource.h @@ -40,6 +40,7 @@ private: static std::vector LoadObj(const std::string& path); static std::vector LoadFbx(const std::string& path); + static std::vector LoadBlenderScene(const std::string& path); }; #endif diff --git a/KREngine/KREngine/Classes/KRShader.cpp b/KREngine/KREngine/Classes/KRShader.cpp index 1c4d113..20dd2c0 100644 --- a/KREngine/KREngine/Classes/KRShader.cpp +++ b/KREngine/KREngine/Classes/KRShader.cpp @@ -246,8 +246,8 @@ void KRShader::bind(KRCamera *pCamera, KRMat4 &matModelToView, KRMat4 &mvpMatrix glUniform1i(m_uniforms[KRENGINE_UNIFORM_GBUFFER_FRAME], 6); - glUniform1i(m_uniforms[KRENGINE_UNIFORM_GBUFFER_DEPTH], 7); - glUniform1i(m_uniforms[KRENGINE_UNIFORM_REFLECTIONTEXTURE], 7); + glUniform1i(m_uniforms[KRENGINE_UNIFORM_GBUFFER_DEPTH], 7); // Texture unit 7 is used for reading the depth buffer in gBuffer pass #2 and in post-processing pass + glUniform1i(m_uniforms[KRENGINE_UNIFORM_REFLECTIONTEXTURE], 7); // Texture unit 7 is used for the reflection map textures in gBuffer pass #3 and when using forward rendering #if defined(DEBUG) GLint logLength;