From 87b7877958ea7efc3ff2cc3887f7c8b1460e0aad Mon Sep 17 00:00:00 2001 From: kearwood Date: Thu, 1 Nov 2012 22:16:59 +0000 Subject: [PATCH] Fixed LOD mesh selection bugs --HG-- extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40149 --- KREngine/KREngine.xcodeproj/project.pbxproj | 24 ++++++++ KREngine/KREngine/Classes/KRAABB.cpp | 4 +- KREngine/KREngine/Classes/KRCamera.cpp | 58 +++++++------------ KREngine/KREngine/Classes/KRContext.cpp | 10 ++++ KREngine/KREngine/Classes/KRContext.h | 11 ++++ KREngine/KREngine/Classes/KRInstance.cpp | 10 +--- KREngine/KREngine/Classes/KRLight.cpp | 1 - KREngine/KREngine/Classes/KRModel.cpp | 30 ++++++---- KREngine/KREngine/Classes/KRModel.h | 2 + KREngine/KREngine/Classes/KRModelManager.cpp | 2 +- KREngine/KREngine/Classes/KRPointLight.cpp | 1 - KREngine/KREngine/Classes/KRScene.cpp | 10 +--- KREngine/KREngine/Classes/KRShader.cpp | 4 +- KREngine/KREngine/Classes/KRShader.h | 1 + KREngine/KREngine/Classes/KRShaderManager.cpp | 1 - KREngine/KREngine/Classes/KRTexture2D.cpp | 1 + KREngine/KREngine/Shaders/ObjectShader.fsh | 4 +- KREngine/KREngine/Shaders/ShadowShader.fsh | 4 +- KREngine/KREngine/Shaders/ShadowShader.vsh | 8 +-- KREngine/KREngine/Shaders/debug_font.fsh | 35 +++++++++++ KREngine/KREngine/Shaders/debug_font.vsh | 38 ++++++++++++ KREngine/KREngine/Shaders/simple_blit.fsh | 35 +++++++++++ KREngine/KREngine/Shaders/simple_blit.vsh | 38 ++++++++++++ 23 files changed, 250 insertions(+), 82 deletions(-) create mode 100644 KREngine/KREngine/Shaders/debug_font.fsh create mode 100644 KREngine/KREngine/Shaders/debug_font.vsh create mode 100644 KREngine/KREngine/Shaders/simple_blit.fsh create mode 100644 KREngine/KREngine/Shaders/simple_blit.vsh diff --git a/KREngine/KREngine.xcodeproj/project.pbxproj b/KREngine/KREngine.xcodeproj/project.pbxproj index 3441eda..9c77caf 100644 --- a/KREngine/KREngine.xcodeproj/project.pbxproj +++ b/KREngine/KREngine.xcodeproj/project.pbxproj @@ -27,6 +27,14 @@ E43B0AD715DDCA0F00A5CB9F /* KRContextObject.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E43B0AD415DDCA0C00A5CB9F /* KRContextObject.cpp */; }; E43B0AD815DDCA0F00A5CB9F /* KRContextObject.h in Headers */ = {isa = PBXBuildFile; fileRef = E43B0AD515DDCA0D00A5CB9F /* KRContextObject.h */; }; E43B0AD915DDCA0F00A5CB9F /* KRContextObject.h in Headers */ = {isa = PBXBuildFile; fileRef = E43B0AD515DDCA0D00A5CB9F /* KRContextObject.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E45AC0301641D66300DC3C3B /* simple_blit.vsh in Sources */ = {isa = PBXBuildFile; fileRef = E45AC02F1641D66300DC3C3B /* simple_blit.vsh */; }; + E45AC0311641D66300DC3C3B /* simple_blit.vsh in Sources */ = {isa = PBXBuildFile; fileRef = E45AC02F1641D66300DC3C3B /* simple_blit.vsh */; }; + E45AC0341641D67300DC3C3B /* simple_blit.fsh in Sources */ = {isa = PBXBuildFile; fileRef = E45AC0331641D67300DC3C3B /* simple_blit.fsh */; }; + E45AC0351641D67300DC3C3B /* simple_blit.fsh in Sources */ = {isa = PBXBuildFile; fileRef = E45AC0331641D67300DC3C3B /* simple_blit.fsh */; }; + E45AC03F1641DE5D00DC3C3B /* debug_font.vsh in Sources */ = {isa = PBXBuildFile; fileRef = E45AC03E1641DE5D00DC3C3B /* debug_font.vsh */; }; + E45AC0401641DE5D00DC3C3B /* debug_font.vsh in Sources */ = {isa = PBXBuildFile; fileRef = E45AC03E1641DE5D00DC3C3B /* debug_font.vsh */; }; + E45AC0421641DE6D00DC3C3B /* debug_font.fsh in Sources */ = {isa = PBXBuildFile; fileRef = E45AC0411641DE6D00DC3C3B /* debug_font.fsh */; }; + E45AC0431641DE6D00DC3C3B /* debug_font.fsh in Sources */ = {isa = PBXBuildFile; fileRef = E45AC0411641DE6D00DC3C3B /* debug_font.fsh */; }; E461A152152E54B500F2044A /* KRLight.h in Headers */ = {isa = PBXBuildFile; fileRef = E461A151152E54B500F2044A /* KRLight.h */; }; E461A153152E54B500F2044A /* KRLight.h in Headers */ = {isa = PBXBuildFile; fileRef = E461A151152E54B500F2044A /* KRLight.h */; settings = {ATTRIBUTES = (Public, ); }; }; E461A156152E54F800F2044A /* KRLight.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E461A155152E54F700F2044A /* KRLight.cpp */; }; @@ -221,6 +229,10 @@ E45772F413C9A13C0037BEEA /* ObjectShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; lineEnding = 0; name = ObjectShader.fsh; path = Shaders/ObjectShader.fsh; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.glsl; }; E45772F513C9A13C0037BEEA /* ObjectShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; lineEnding = 0; name = ObjectShader.vsh; path = Shaders/ObjectShader.vsh; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.glsl; }; E45772F613C9A13C0037BEEA /* PostShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.vsh; path = Shaders/PostShader.vsh; sourceTree = ""; }; + E45AC02F1641D66300DC3C3B /* simple_blit.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = simple_blit.vsh; path = Shaders/simple_blit.vsh; sourceTree = ""; }; + E45AC0331641D67300DC3C3B /* simple_blit.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = simple_blit.fsh; path = Shaders/simple_blit.fsh; sourceTree = ""; }; + E45AC03E1641DE5D00DC3C3B /* debug_font.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = debug_font.vsh; path = Shaders/debug_font.vsh; sourceTree = ""; }; + E45AC0411641DE6D00DC3C3B /* debug_font.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = debug_font.fsh; path = Shaders/debug_font.fsh; sourceTree = ""; }; E461A151152E54B500F2044A /* KRLight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRLight.h; path = Classes/KRLight.h; sourceTree = ""; }; E461A155152E54F700F2044A /* KRLight.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; name = KRLight.cpp; path = Classes/KRLight.cpp; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.cpp; }; E461A157152E555400F2044A /* KRPointLight.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; lineEnding = 0; name = KRPointLight.h; path = Classes/KRPointLight.h; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; @@ -379,6 +391,10 @@ E4CE184E15FF125700F80870 /* light_point_inside.vsh */, E41B841D16260C5600C7A771 /* sky_box.fsh */, E41B842016260C6500C7A771 /* sky_box.vsh */, + E45AC02F1641D66300DC3C3B /* simple_blit.vsh */, + E45AC0331641D67300DC3C3B /* simple_blit.fsh */, + E45AC03E1641DE5D00DC3C3B /* debug_font.vsh */, + E45AC0411641DE6D00DC3C3B /* debug_font.fsh */, ); name = Shaders; sourceTree = ""; @@ -894,6 +910,10 @@ E4CA10E91637BD2B005D9400 /* KRTexturePVR.cpp in Sources */, E4CA10EF1637BD58005D9400 /* KRTextureTGA.cpp in Sources */, E4CA11781639CC90005D9400 /* KRViewport.cpp in Sources */, + E45AC0301641D66300DC3C3B /* simple_blit.vsh in Sources */, + E45AC0341641D67300DC3C3B /* simple_blit.fsh in Sources */, + E45AC03F1641DE5D00DC3C3B /* debug_font.vsh in Sources */, + E45AC0421641DE6D00DC3C3B /* debug_font.fsh in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -942,6 +962,10 @@ E4CA10EA1637BD2B005D9400 /* KRTexturePVR.cpp in Sources */, E4CA10F01637BD58005D9400 /* KRTextureTGA.cpp in Sources */, E4CA11791639CC90005D9400 /* KRViewport.cpp in Sources */, + E45AC0311641D66300DC3C3B /* simple_blit.vsh in Sources */, + E45AC0351641D67300DC3C3B /* simple_blit.fsh in Sources */, + E45AC0401641DE5D00DC3C3B /* debug_font.vsh in Sources */, + E45AC0431641DE6D00DC3C3B /* debug_font.fsh in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/KREngine/KREngine/Classes/KRAABB.cpp b/KREngine/KREngine/Classes/KRAABB.cpp index f2c288a..56897e3 100644 --- a/KREngine/KREngine/Classes/KRAABB.cpp +++ b/KREngine/KREngine/Classes/KRAABB.cpp @@ -162,7 +162,7 @@ float KRAABB::coverage(const KRMat4 &matMVP, const KRVector2 viewportSize) const KRVector2 screen_max; // Loop through all corners and transform them to screen space for(int i=0; i<8; i++) { - KRVector3 screen_pos = KRMat4::Dot(matMVP, KRVector3(i & 1 ? min.x : max.x, i & 2 ? min.y : max.y, i & 4 ? min.z :max.z)); + KRVector3 screen_pos = KRMat4::DotWDiv(matMVP, KRVector3(i & 1 ? min.x : max.x, i & 2 ? min.y : max.y, i & 4 ? min.z : max.z)); if(i==0) { screen_min.x = screen_pos.x; screen_min.y = screen_pos.y; @@ -176,6 +176,6 @@ float KRAABB::coverage(const KRMat4 &matMVP, const KRVector2 viewportSize) const } } - return (screen_max.x - screen_min.x) / viewportSize.x * (screen_max.y - screen_min.y) / viewportSize.y; + return (screen_max.x - screen_min.x) * (screen_max.y - screen_min.y); } } \ No newline at end of file diff --git a/KREngine/KREngine/Classes/KRCamera.cpp b/KREngine/KREngine/Classes/KRCamera.cpp index 68bc058..140effb 100644 --- a/KREngine/KREngine/Classes/KRCamera.cpp +++ b/KREngine/KREngine/Classes/KRCamera.cpp @@ -169,14 +169,18 @@ void KRCamera::renderFrame(KRScene &scene, KRMat4 &viewMatrix) KRMat4 newShadowMVP; - if(shadowMaxDepths[m_cShadowBuffers - 1][iShadow] == 0.0) { - KRBoundingVolume ext = KRBoundingVolume(-KRVector3::One(), KRVector3::One(), KRMat4()); // HACK - Temporary workaround to compile until this logic is updated to use information from the Octree - - newShadowMVP = ext.calcShadowProj(&scene, m_pContext, scene.sun_yaw, scene.sun_pitch); - } else { - KRBoundingVolume frustrumSliceVolume = KRBoundingVolume(viewMatrix, perspective_fov, getViewportSize().x / getViewportSize().y, perspective_nearz + (perspective_farz - perspective_nearz) * shadowMinDepths[m_cShadowBuffers - 1][iShadow], perspective_nearz + (perspective_farz - perspective_nearz) * shadowMaxDepths[m_cShadowBuffers - 1][iShadow]); - newShadowMVP = frustrumSliceVolume.calcShadowProj(&scene, m_pContext, scene.sun_yaw, scene.sun_pitch); - } +// if(shadowMaxDepths[m_cShadowBuffers - 1][iShadow] == 0.0) { +// KRBoundingVolume ext = KRBoundingVolume(-KRVector3::One(), KRVector3::One(), KRMat4()); // HACK - Temporary workaround to compile until this logic is updated to use information from the Octree +// +// newShadowMVP = ext.calcShadowProj(&scene, m_pContext, scene.sun_yaw, scene.sun_pitch); +// } else { +// KRBoundingVolume frustrumSliceVolume = KRBoundingVolume(viewMatrix, perspective_fov, getViewportSize().x / getViewportSize().y, perspective_nearz + (perspective_farz - perspective_nearz) * shadowMinDepths[m_cShadowBuffers - 1][iShadow], perspective_nearz + (perspective_farz - perspective_nearz) * shadowMaxDepths[m_cShadowBuffers - 1][iShadow]); +// newShadowMVP = frustrumSliceVolume.calcShadowProj(&scene, m_pContext, scene.sun_yaw, scene.sun_pitch); +// } + + KRBoundingVolume frustrumSliceVolume = KRBoundingVolume(viewMatrix, perspective_fov, getViewportSize().x / getViewportSize().y, perspective_nearz + (perspective_farz - perspective_nearz) * shadowMinDepths[m_cShadowBuffers - 1][iShadow], perspective_nearz + (perspective_farz - perspective_nearz) * shadowMaxDepths[m_cShadowBuffers - 1][iShadow]); + newShadowMVP = frustrumSliceVolume.calcShadowProj(&scene, m_pContext, scene.sun_yaw, scene.sun_pitch); + if(!(shadowmvpmatrix[iShadow] == newShadowMVP)) { shadowValid[iShadow] = false; @@ -694,50 +698,32 @@ void KRCamera::renderPost() if(bShowShadowBuffer) { - GLDEBUG(glDisable(GL_DEPTH_TEST)); - - m_pContext->getTextureManager()->selectTexture(0, NULL, 0); - GLDEBUG(glActiveTexture(GL_TEXTURE0)); - GLDEBUG(glBindTexture(GL_TEXTURE_2D, compositeDepthTexture)); - - // Update attribute values. -#if GL_OES_vertex_array_object - GLDEBUG(glBindVertexArrayOES(0)); -#endif - m_pContext->getModelManager()->configureAttribs(true, false, false, true, false); - GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TEXUVA, 2, GL_FLOAT, 0, 0, KRENGINE_VERTICES_2D_SQUARE_UV)); + KRShader *blitShader = m_pContext->getShaderManager()->getShader("simple_blit", this, false, false, false, 0, false, false, false, false, false, false, false, false, false, false, false, false, false, KRNode::RENDER_PASS_FORWARD_TRANSPARENT); for(int iShadow=0; iShadow < m_cShadowBuffers; iShadow++) { + KRMat4 viewMatrix = KRMat4(); + viewMatrix.scale(0.20, 0.20, 0.20); + viewMatrix.translate(-0.70, 0.70 - 0.45 * iShadow, 0.0); + blitShader->bind(KRViewport(getViewportSize(), viewMatrix, KRMat4()), KRMat4(), KRVector3(), NULL, NULL, 0, KRNode::RENDER_PASS_FORWARD_TRANSPARENT); m_pContext->getTextureManager()->selectTexture(1, NULL, 0); - GLDEBUG(glActiveTexture(GL_TEXTURE1)); + m_pContext->getModelManager()->bindVBO((void *)KRENGINE_VBO_2D_SQUARE, KRENGINE_VBO_2D_SQUARE_SIZE, true, false, false, true, false); + GLDEBUG(glActiveTexture(GL_TEXTURE0)); GLDEBUG(glBindTexture(GL_TEXTURE_2D, shadowDepthTexture[iShadow])); - GLDEBUG(glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVerticesShadow[iShadow])); - GLDEBUG(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)); } m_pContext->getTextureManager()->selectTexture(0, NULL, 0); GLDEBUG(glActiveTexture(GL_TEXTURE0)); GLDEBUG(glBindTexture(GL_TEXTURE_2D, 0)); - - m_pContext->getTextureManager()->selectTexture(1, NULL, 0); - GLDEBUG(glActiveTexture(GL_TEXTURE1)); - GLDEBUG(glBindTexture(GL_TEXTURE_2D, 0)); } const char *szText = m_debug_text.c_str(); if(*szText) { - KRTexture *pFontTexture = m_pContext->getTextureManager()->getTexture("font"); - - GLDEBUG(glDisable(GL_DEPTH_TEST)); - - m_pContext->getTextureManager()->selectTexture(0, NULL, 0); - GLDEBUG(glActiveTexture(GL_TEXTURE0)); - GLDEBUG(glBindTexture(GL_TEXTURE_2D, compositeDepthTexture)); - - m_pContext->getTextureManager()->selectTexture(1, pFontTexture, 2048); + KRShader *fontShader = m_pContext->getShaderManager()->getShader("debug_font", this, false, false, false, 0, false, false, false, false, false, false, false, false, false, false, false, false, false, KRNode::RENDER_PASS_FORWARD_TRANSPARENT); + + m_pContext->getTextureManager()->selectTexture(0, m_pContext->getTextureManager()->getTexture("font"), 2048); const char *pChar = szText; int iPos=0; diff --git a/KREngine/KREngine/Classes/KRContext.cpp b/KREngine/KREngine/Classes/KRContext.cpp index 0263d8e..60ef956 100644 --- a/KREngine/KREngine/Classes/KRContext.cpp +++ b/KREngine/KREngine/Classes/KRContext.cpp @@ -21,6 +21,10 @@ int KRContext::KRENGINE_TARGET_TEXTURE_MEM_MIN; int KRContext::KRENGINE_MAX_TEXTURE_DIM; int KRContext::KRENGINE_MIN_TEXTURE_DIM; +const char *KRContext::extension_names[KRENGINE_NUM_EXTENSIONS] = { + "GL_EXT_texture_storage" +}; + KRContext::KRContext() { m_pBundleManager = new KRBundleManager(*this); m_pShaderManager = new KRShaderManager(*this); @@ -28,6 +32,7 @@ KRContext::KRContext() { m_pMaterialManager = new KRMaterialManager(*this, m_pTextureManager, m_pShaderManager); m_pModelManager = new KRModelManager(*this); m_pSceneManager = new KRSceneManager(*this); + m_bDetectedExtensions = false; } KRContext::~KRContext() { @@ -129,4 +134,9 @@ void KRContext::rotateBuffers(bool new_frame) { m_pModelManager->rotateBuffers(new_frame); m_pTextureManager->rotateBuffers(new_frame); +} + +void KRContext::detectExtensions() { + m_bDetectedExtensions = true; + } \ No newline at end of file diff --git a/KREngine/KREngine/Classes/KRContext.h b/KREngine/KREngine/Classes/KRContext.h index 028bda5..d38d2e7 100644 --- a/KREngine/KREngine/Classes/KRContext.h +++ b/KREngine/KREngine/Classes/KRContext.h @@ -47,6 +47,14 @@ public: void rotateBuffers(bool new_frame); + enum { + KRENGINE_GL_EXT_texture_storage, + KRENGINE_NUM_EXTENSIONS + }; + + static const char * extension_names[KRENGINE_NUM_EXTENSIONS]; + static bool extension_available[KRENGINE_NUM_EXTENSIONS]; + private: KRBundleManager *m_pBundleManager; KRSceneManager *m_pSceneManager; @@ -54,6 +62,9 @@ private: KRMaterialManager *m_pMaterialManager; KRShaderManager *m_pShaderManager; KRModelManager *m_pModelManager; + + void detectExtensions(); + bool m_bDetectedExtensions; }; #endif diff --git a/KREngine/KREngine/Classes/KRInstance.cpp b/KREngine/KREngine/Classes/KRInstance.cpp index 1d614ce..9b4e315 100644 --- a/KREngine/KREngine/Classes/KRInstance.cpp +++ b/KREngine/KREngine/Classes/KRInstance.cpp @@ -91,12 +91,8 @@ void KRInstance::render(KRCamera *pCamera, KRContext *pContext, const KRViewport loadModel(); if(m_models.size() > 0) { - KRMat4 projectionMatrix; - if(renderPass != KRNode::RENDER_PASS_SHADOWMAP) { - projectionMatrix = pCamera->getProjectionMatrix(); - } KRMat4 matMVP = m_modelMatrix * viewport.getViewProjectionMatrix(); - float lod_coverage = getBounds().coverage(matMVP, pCamera->getViewportSize()); // This also checks the view frustrum culling + float lod_coverage = getBounds().coverage(matMVP, viewport.getSize()); // This also checks the view frustrum culling if(lod_coverage > m_min_lod_coverage) { // ---===--- Select the best LOD model based on screen coverage ---===--- @@ -105,7 +101,7 @@ void KRInstance::render(KRCamera *pCamera, KRContext *pContext, const KRViewport while(itr != m_models.end()) { KRModel *pLODModel = *itr++; - if((float)pLODModel->getLODCoverage() / 100.0f > lod_coverage) { + if((float)pLODModel->getLODCoverage() / 100.0f > lod_coverage && pLODModel->getLODCoverage() < pModel->getLODCoverage()) { pModel = pLODModel; } else { break; @@ -117,7 +113,7 @@ void KRInstance::render(KRCamera *pCamera, KRContext *pContext, const KRViewport } if(cShadowBuffers == 0 && m_pLightMap && pCamera->bEnableLightMap && renderPass != RENDER_PASS_SHADOWMAP) { - m_pContext->getTextureManager()->selectTexture(3, m_pLightMap, 2048); + m_pContext->getTextureManager()->selectTexture(5, m_pLightMap, 2048); } pModel->render(pCamera, pContext, viewport, m_modelMatrix, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, m_pLightMap, renderPass); diff --git a/KREngine/KREngine/Classes/KRLight.cpp b/KREngine/KREngine/Classes/KRLight.cpp index 9e95a29..86c650a 100644 --- a/KREngine/KREngine/Classes/KRLight.cpp +++ b/KREngine/KREngine/Classes/KRLight.cpp @@ -130,7 +130,6 @@ void KRLight::render(KRCamera *pCamera, KRContext *pContext, const KRViewport &v if(m_pFlareTexture) { - KRMat4 projectionMatrix = pCamera->getProjectionMatrix(); KRVector3 light_position = getLocalTranslation(); KRMat4 m_modelMatrix = KRMat4(); diff --git a/KREngine/KREngine/Classes/KRModel.cpp b/KREngine/KREngine/Classes/KRModel.cpp index 51c82ac..e917d98 100644 --- a/KREngine/KREngine/Classes/KRModel.cpp +++ b/KREngine/KREngine/Classes/KRModel.cpp @@ -52,7 +52,21 @@ KRModel::KRModel(KRContext &context, std::string name) : KRResource(context, nam m_materials.clear(); m_uniqueMaterials.clear(); m_pData = new KRDataBlock(); - m_lodCoverage = 100; // Default to being the full detail mesh; not an LOD level + setName(name); +} + +KRModel::KRModel(KRContext &context, std::string name, KRDataBlock *data) : KRResource(context, name) { + m_hasTransparency = false; + m_materials.clear(); + m_uniqueMaterials.clear(); + m_pData = new KRDataBlock(); + setName(name); + + loadPack(data); +} + +void KRModel::setName(const std::string name) { + m_lodCoverage = 100; m_lodBaseName = name; size_t last_underscore_pos = name.find_last_of('_'); @@ -62,22 +76,14 @@ KRModel::KRModel(KRContext &context, std::string name) : KRResource(context, nam if(suffix.find_first_of("lod") == 0) { std::string lod_level_string = suffix.substr(3); char *end = NULL; - int c = (int)strtol(suffix.c_str(), &end, 10); - if(m_lodCoverage < 0 || m_lodCoverage > 100 || *end != '\0') { + int c = (int)strtol(lod_level_string.c_str(), &end, 10); + if(c >= 0 && c <= 100 && *end == '\0') { m_lodCoverage = c; - m_lodBaseName = name.substr(0, last_underscore_pos - 1); + m_lodBaseName = name.substr(0, last_underscore_pos); } } } -} -KRModel::KRModel(KRContext &context, std::string name, KRDataBlock *data) : KRResource(context, name) { - m_hasTransparency = false; - m_materials.clear(); - m_uniqueMaterials.clear(); - m_pData = new KRDataBlock(); - m_lodCoverage = 100; - loadPack(data); } KRModel::~KRModel() { diff --git a/KREngine/KREngine/Classes/KRModel.h b/KREngine/KREngine/Classes/KRModel.h index 67d91e7..d7988a6 100644 --- a/KREngine/KREngine/Classes/KRModel.h +++ b/KREngine/KREngine/Classes/KRModel.h @@ -153,6 +153,8 @@ private: void clearData(); void clearBuffers(); + + void setName(const std::string name); }; diff --git a/KREngine/KREngine/Classes/KRModelManager.cpp b/KREngine/KREngine/Classes/KRModelManager.cpp index d539896..53a964a 100644 --- a/KREngine/KREngine/Classes/KRModelManager.cpp +++ b/KREngine/KREngine/Classes/KRModelManager.cpp @@ -56,7 +56,7 @@ KRModel *KRModelManager::loadModel(const char *szName, KRDataBlock *pData) { KRModel *pModel = new KRModel(*m_pContext, lowerName, pData); - m_models.insert(std::pair(pModel->getName(), pModel)); + m_models.insert(std::pair(pModel->getLODBaseName(), pModel)); return pModel; } diff --git a/KREngine/KREngine/Classes/KRPointLight.cpp b/KREngine/KREngine/Classes/KRPointLight.cpp index 6fb3907..99c3e2c 100644 --- a/KREngine/KREngine/Classes/KRPointLight.cpp +++ b/KREngine/KREngine/Classes/KRPointLight.cpp @@ -47,7 +47,6 @@ void KRPointLight::render(KRCamera *pCamera, KRContext *pContext, const KRViewpo if(renderPass == KRNode::RENDER_PASS_DEFERRED_LIGHTS || bVisualize) { // Lights are rendered on the second pass of the deferred renderer - KRMat4 projectionMatrix = pCamera->getProjectionMatrix(); KRVector3 light_position = getLocalTranslation(); float influence_radius = sqrt((m_intensity / 100.0) / KRLIGHT_MIN_INFLUENCE - 1.0) + m_decayStart; diff --git a/KREngine/KREngine/Classes/KRScene.cpp b/KREngine/KREngine/Classes/KRScene.cpp index 855ff34..54d8f16 100644 --- a/KREngine/KREngine/Classes/KRScene.cpp +++ b/KREngine/KREngine/Classes/KRScene.cpp @@ -164,16 +164,10 @@ void KRScene::render(KROctreeNode *pOctreeNode, std::set &visibleBounds, pOctreeNode->m_occlusionQuery = 0; } } else { - - - KRMat4 projectionMatrix; - if(renderPass != KRNode::RENDER_PASS_SHADOWMAP) { - projectionMatrix = pCamera->getProjectionMatrix(); - } float min_coverage = 0.0f; // 1.0f / 1024.0f / 768.0f; // FINDME - HACK - Need to dynamically select the absolute minimum based on the render buffer size - float lod_coverage = pOctreeNode->getBounds().coverage(viewport.getViewProjectionMatrix(), pCamera->getViewportSize()); // This also checks the view frustrum culling + float lod_coverage = pOctreeNode->getBounds().coverage(viewport.getViewProjectionMatrix(), viewport.getSize()); // This also checks the view frustrum culling if(lod_coverage > min_coverage) { // ----====---- Rendering and occlusion test pass ----====---- @@ -223,8 +217,6 @@ void KRScene::render(KROctreeNode *pOctreeNode, std::set &visibleBounds, KRShader *pVisShader = m_pContext->getShaderManager()->getShader("occlusion_test", pCamera, false, false, false, 0, false, false, false, false, false, false, false, false, false, false, false, false, false, KRNode::RENDER_PASS_FORWARD_TRANSPARENT); - KRMat4 projectionMatrix = pCamera->getProjectionMatrix(); - m_pContext->getModelManager()->bindVBO((void *)KRENGINE_VBO_3D_CUBE, KRENGINE_VBO_3D_CUBE_SIZE, true, false, false, false, false); KRMat4 matModel = KRMat4(); matModel.scale(octreeBounds.size() / 2.0f); diff --git a/KREngine/KREngine/Classes/KRShader.cpp b/KREngine/KREngine/Classes/KRShader.cpp index 44d5f15..e2c0cae 100644 --- a/KREngine/KREngine/Classes/KRShader.cpp +++ b/KREngine/KREngine/Classes/KRShader.cpp @@ -159,6 +159,7 @@ KRShader::KRShader(char *szKey, std::string options, std::string vertShaderSourc GLDEBUG(m_uniforms[KRENGINE_UNIFORM_REFLECTIONTEXTURE_OFFSET] = glGetUniformLocation(m_iProgram, "reflectionTexture_Offset")); GLDEBUG(m_uniforms[KRENGINE_UNIFORM_NORMALTEXTURE_OFFSET] = glGetUniformLocation(m_iProgram, "normalTexture_Offset")); GLDEBUG(m_uniforms[KRENGINE_UNIFORM_AMBIENTTEXTURE_OFFSET] = glGetUniformLocation(m_iProgram, "ambientTexture_Offset")); + GLDEBUG(m_uniforms[KRENGINE_UNIFORM_LIGHTMAPTEXTURE] = glGetUniformLocation(m_iProgram, "lightmapTexture")); GLDEBUG(m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE1] = glGetUniformLocation(m_iProgram, "shadowTexture1")); GLDEBUG(m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE2] = glGetUniformLocation(m_iProgram, "shadowTexture2")); GLDEBUG(m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE3] = glGetUniformLocation(m_iProgram, "shadowTexture3")); @@ -310,9 +311,8 @@ bool KRShader::bind(const KRViewport &viewport, const KRMat4 &matModel, const KR GLDEBUG(glUniform1i(m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE3], 5)); GLDEBUG(glUniform1i(m_uniforms[KRENGINE_UNIFORM_REFLECTIONCUBETEXTURE], 4)); - + GLDEBUG(glUniform1i(m_uniforms[KRENGINE_UNIFORM_LIGHTMAPTEXTURE], 5)); GLDEBUG(glUniform1i(m_uniforms[KRENGINE_UNIFORM_GBUFFER_FRAME], 6)); - GLDEBUG(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 GLDEBUG(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 diff --git a/KREngine/KREngine/Classes/KRShader.h b/KREngine/KREngine/Classes/KRShader.h index 640fee6..f005f99 100644 --- a/KREngine/KREngine/Classes/KRShader.h +++ b/KREngine/KREngine/Classes/KRShader.h @@ -120,6 +120,7 @@ public: KRENGINE_UNIFORM_SHADOWTEXTURE1, KRENGINE_UNIFORM_SHADOWTEXTURE2, KRENGINE_UNIFORM_SHADOWTEXTURE3, + KRENGINE_UNIFORM_LIGHTMAPTEXTURE, KRENGINE_UNIFORM_GBUFFER_FRAME, KRENGINE_UNIFORM_GBUFFER_DEPTH, KRENGINE_UNIFORM_DEPTH_FRAME, diff --git a/KREngine/KREngine/Classes/KRShaderManager.cpp b/KREngine/KREngine/Classes/KRShaderManager.cpp index 1323c52..58e3e1a 100644 --- a/KREngine/KREngine/Classes/KRShaderManager.cpp +++ b/KREngine/KREngine/Classes/KRShaderManager.cpp @@ -128,7 +128,6 @@ KRShader *KRShaderManager::getShader(std::string shader_name, KRCamera *pCamera, stream << "\n"; std::string options = stream.str(); - //fprintf(stderr, "Shader Options:\n%s\n\n", options.c_str()); pShader = new KRShader(szKey, options, m_vertShaderSource[shader_name], m_fragShaderSource[shader_name]); diff --git a/KREngine/KREngine/Classes/KRTexture2D.cpp b/KREngine/KREngine/Classes/KRTexture2D.cpp index 5893d4c..0ed465e 100644 --- a/KREngine/KREngine/Classes/KRTexture2D.cpp +++ b/KREngine/KREngine/Classes/KRTexture2D.cpp @@ -56,6 +56,7 @@ bool KRTexture2D::createGLTexture(int lod_max_dim) { if(m_iHandle == 0) { return false; } + GLDEBUG(glBindTexture(GL_TEXTURE_2D, m_iHandle)); if (hasMipmaps()) { GLDEBUG(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)); diff --git a/KREngine/KREngine/Shaders/ObjectShader.fsh b/KREngine/KREngine/Shaders/ObjectShader.fsh index 0c2c7bc..9d434ea 100644 --- a/KREngine/KREngine/Shaders/ObjectShader.fsh +++ b/KREngine/KREngine/Shaders/ObjectShader.fsh @@ -104,7 +104,7 @@ #endif #if HAS_LIGHT_MAP == 1 - uniform sampler2D shadowTexture1; + uniform sampler2D lightmapTexture; varying mediump vec2 lightmap_uv; #endif @@ -316,7 +316,7 @@ void main() // -------------------- Multiply light map -------------------- #if HAS_LIGHT_MAP == 1 - mediump vec3 lightMapColor = vec3(texture2D(shadowTexture1, lightmap_uv)); + mediump vec3 lightMapColor = vec3(texture2D(lightmapTexture, lightmap_uv)); gl_FragColor = vec4(gl_FragColor.r * lightMapColor.r, gl_FragColor.g * lightMapColor.g, gl_FragColor.b * lightMapColor.b, gl_FragColor.a); #endif #endif diff --git a/KREngine/KREngine/Shaders/ShadowShader.fsh b/KREngine/KREngine/Shaders/ShadowShader.fsh index 61c18cb..81802b9 100644 --- a/KREngine/KREngine/Shaders/ShadowShader.fsh +++ b/KREngine/KREngine/Shaders/ShadowShader.fsh @@ -25,9 +25,7 @@ // or implied, of Kearwood Gilbert. // -uniform sampler2D diffuseTexture; -varying mediump vec2 texCoord; - +//varying mediump vec2 texCoord; void main() { diff --git a/KREngine/KREngine/Shaders/ShadowShader.vsh b/KREngine/KREngine/Shaders/ShadowShader.vsh index 759aced..b8241a0 100644 --- a/KREngine/KREngine/Shaders/ShadowShader.vsh +++ b/KREngine/KREngine/Shaders/ShadowShader.vsh @@ -28,10 +28,8 @@ #define SHADOW_BIAS 0.01 -attribute highp vec3 vertex_position; -uniform highp mat4 shadow_mvp1; // Shadowmvpmatrix is the result of multiplying the model, view, and projection matrices - - +attribute highp vec4 vertex_position; +uniform highp mat4 mvp_matrix; // mvp_matrix is the result of multiplying the model, view, and projection matrices void main() { @@ -39,7 +37,7 @@ void main() /* position = shadow_mvp1 * vec4(vertex_position,1.0); */ - gl_Position = shadow_mvp1 * vec4(vertex_position,1.0); + gl_Position = mvp_matrix * vertex_position; gl_Position.z += SHADOW_BIAS; /* // Pass UV co-ordinates diff --git a/KREngine/KREngine/Shaders/debug_font.fsh b/KREngine/KREngine/Shaders/debug_font.fsh new file mode 100644 index 0000000..abc6fe4 --- /dev/null +++ b/KREngine/KREngine/Shaders/debug_font.fsh @@ -0,0 +1,35 @@ +// 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. +// + +varying mediump vec2 textureCoordinate; + +uniform sampler2D diffuseTexture; + +void main() +{ + gl_FragColor = texture2D(diffuseTexture, textureCoordinate); +} diff --git a/KREngine/KREngine/Shaders/debug_font.vsh b/KREngine/KREngine/Shaders/debug_font.vsh new file mode 100644 index 0000000..28f2d46 --- /dev/null +++ b/KREngine/KREngine/Shaders/debug_font.vsh @@ -0,0 +1,38 @@ +// 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. +// + +varying mediump vec2 textureCoordinate; + +attribute vec4 vertex_position; +attribute lowp vec4 vertex_uv; +uniform highp mat4 mvp_matrix; // mvp_matrix is the result of multiplying the model, view, and projection matrices + +void main() +{ + gl_Position = mvp_matrix * vertex_position; + textureCoordinate = vertex_uv.xy; +} diff --git a/KREngine/KREngine/Shaders/simple_blit.fsh b/KREngine/KREngine/Shaders/simple_blit.fsh new file mode 100644 index 0000000..abc6fe4 --- /dev/null +++ b/KREngine/KREngine/Shaders/simple_blit.fsh @@ -0,0 +1,35 @@ +// 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. +// + +varying mediump vec2 textureCoordinate; + +uniform sampler2D diffuseTexture; + +void main() +{ + gl_FragColor = texture2D(diffuseTexture, textureCoordinate); +} diff --git a/KREngine/KREngine/Shaders/simple_blit.vsh b/KREngine/KREngine/Shaders/simple_blit.vsh new file mode 100644 index 0000000..fbc9319 --- /dev/null +++ b/KREngine/KREngine/Shaders/simple_blit.vsh @@ -0,0 +1,38 @@ +// 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. +// + +varying mediump vec2 textureCoordinate; + +attribute vec4 vertex_position; +attribute lowp vec4 vertex_uv; +uniform highp mat4 mvp_matrix; // mvp_matrix is the result of multiplying the model, view, and projection matrices + +void main() +{ + gl_Position = mvp_matrix * vertex_position; + textureCoordinate = vertex_uv.xy; +}