Completed implementation of skyboxes, enabled by setting the "skybox" attribute of the "scene" node. Once the camera is moved to the scene graph, this "skybox" attribute will be moved to the "camera" nodes.

--HG--
extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40125
This commit is contained in:
kearwood
2012-10-10 22:09:28 +00:00
parent fcd58bb373
commit 278c6d5926
21 changed files with 250 additions and 285 deletions

View File

@@ -7,8 +7,6 @@
objects = {
/* Begin PBXBuildFile section */
1000469D15E6EF550053B072 /* KRSkyBox.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1000469B15E6EF550053B072 /* KRSkyBox.cpp */; };
1000469E15E6EF550053B072 /* KRSkyBox.h in Headers */ = {isa = PBXBuildFile; fileRef = 1000469C15E6EF550053B072 /* KRSkyBox.h */; };
E4030E4C160A3CF000592648 /* KRStockGeometry.h in Headers */ = {isa = PBXBuildFile; fileRef = E4030E4B160A3CF000592648 /* KRStockGeometry.h */; };
E4030E4D160A3CF000592648 /* KRStockGeometry.h in Headers */ = {isa = PBXBuildFile; fileRef = E4030E4B160A3CF000592648 /* KRStockGeometry.h */; };
E40BA45415EFF79500D7C3DD /* KRAABB.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E40BA45215EFF79500D7C3DD /* KRAABB.cpp */; };
@@ -21,6 +19,8 @@
E414BAE91435585A00A668C4 /* KRScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E414BAE81435585A00A668C4 /* KRScene.cpp */; };
E414BAEB14355E5500A668C4 /* KRBoundingVolume.h in Headers */ = {isa = PBXBuildFile; fileRef = E414BAEA14355E5500A668C4 /* KRBoundingVolume.h */; };
E414BAED14355EFF00A668C4 /* KRBoundingVolume.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E414BAEC14355EFF00A668C4 /* KRBoundingVolume.cpp */; };
E41B841E16260C5600C7A771 /* sky_box.fsh in Sources */ = {isa = PBXBuildFile; fileRef = E41B841D16260C5600C7A771 /* sky_box.fsh */; };
E41B842116260C6500C7A771 /* sky_box.vsh in Sources */ = {isa = PBXBuildFile; fileRef = E41B842016260C6500C7A771 /* sky_box.vsh */; };
E42CB1EC158446940066E0D8 /* KRQuaternion.h in Headers */ = {isa = PBXBuildFile; fileRef = E42CB1EB158446940066E0D8 /* KRQuaternion.h */; };
E42CB1ED158446940066E0D8 /* KRQuaternion.h in Headers */ = {isa = PBXBuildFile; fileRef = E42CB1EB158446940066E0D8 /* KRQuaternion.h */; };
E42CB1F0158446AB0066E0D8 /* KRQuaternion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E42CB1EF158446AB0066E0D8 /* KRQuaternion.cpp */; };
@@ -125,7 +125,6 @@
E497B95F151BF09600D3DC67 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E497B95E151BF09600D3DC67 /* SystemConfiguration.framework */; };
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, ); }; };
E4AFC6BA15F7C7C300DDB4C8 /* KRSkyBox.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1000469B15E6EF550053B072 /* KRSkyBox.cpp */; };
E4AFC6BB15F7C7D600DDB4C8 /* KROctreeNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4924C2915EE96AA00B965C6 /* KROctreeNode.cpp */; };
E4AFC6BC15F7C95D00DDB4C8 /* KRSceneManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E46C214915364DDB009CABF3 /* KRSceneManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
E4AFC6BD15F7C9DA00DDB4C8 /* KROctree.h in Headers */ = {isa = PBXBuildFile; fileRef = E4924C2515EE95E800B965C6 /* KROctree.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -192,8 +191,6 @@
/* End PBXBuildRule section */
/* Begin PBXFileReference section */
1000469B15E6EF550053B072 /* KRSkyBox.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRSkyBox.cpp; path = Classes/KRSkyBox.cpp; sourceTree = "<group>"; };
1000469C15E6EF550053B072 /* KRSkyBox.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRSkyBox.h; path = Classes/KRSkyBox.h; sourceTree = "<group>"; };
E4030E4B160A3CF000592648 /* KRStockGeometry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRStockGeometry.h; path = Classes/KRStockGeometry.h; sourceTree = "<group>"; };
E40BA45215EFF79500D7C3DD /* KRAABB.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRAABB.cpp; path = Classes/KRAABB.cpp; sourceTree = "<group>"; };
E40BA45315EFF79500D7C3DD /* KRAABB.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRAABB.h; path = Classes/KRAABB.h; sourceTree = "<group>"; };
@@ -203,6 +200,8 @@
E414BAE81435585A00A668C4 /* KRScene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; name = KRScene.cpp; path = Classes/KRScene.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
E414BAEA14355E5500A668C4 /* KRBoundingVolume.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; name = KRBoundingVolume.h; path = Classes/KRBoundingVolume.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
E414BAEC14355EFF00A668C4 /* KRBoundingVolume.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; name = KRBoundingVolume.cpp; path = Classes/KRBoundingVolume.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
E41B841D16260C5600C7A771 /* sky_box.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = sky_box.fsh; path = Shaders/sky_box.fsh; sourceTree = "<group>"; };
E41B842016260C6500C7A771 /* sky_box.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = sky_box.vsh; path = Shaders/sky_box.vsh; sourceTree = "<group>"; };
E42CB1EB158446940066E0D8 /* KRQuaternion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRQuaternion.h; path = Classes/KRQuaternion.h; sourceTree = "<group>"; };
E42CB1EF158446AB0066E0D8 /* KRQuaternion.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRQuaternion.cpp; path = Classes/KRQuaternion.cpp; sourceTree = "<group>"; };
E430D08015F8882F0010558D /* occlusion_test.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = occlusion_test.fsh; path = Shaders/occlusion_test.fsh; sourceTree = "<group>"; };
@@ -361,6 +360,8 @@
E48839A015F92C2800BD66D5 /* visualize_overlay.fsh */,
E4CE184C15FF124600F80870 /* light_point_inside.fsh */,
E4CE184E15FF125700F80870 /* light_point_inside.vsh */,
E41B841D16260C5600C7A771 /* sky_box.fsh */,
E41B842016260C6500C7A771 /* sky_box.vsh */,
);
name = Shaders;
sourceTree = "<group>";
@@ -514,8 +515,6 @@
E461A171152E599E00F2044A /* Lights */,
E4F975311536220900FD60B2 /* KRNode.h */,
E4F975351536221C00FD60B2 /* KRNode.cpp */,
1000469C15E6EF550053B072 /* KRSkyBox.h */,
1000469B15E6EF550053B072 /* KRSkyBox.cpp */,
);
name = "Scene Graph Nodes";
sourceTree = "<group>";
@@ -684,7 +683,6 @@
E43B0AD815DDCA0F00A5CB9F /* KRContextObject.h in Headers */,
E4924C2715EE95E800B965C6 /* KROctree.h in Headers */,
E4924C2C15EE96AB00B965C6 /* KROctreeNode.h in Headers */,
1000469E15E6EF550053B072 /* KRSkyBox.h in Headers */,
E40BA45615EFF79500D7C3DD /* KRAABB.h in Headers */,
E488399615F928CA00BD66D5 /* KRBundle.h in Headers */,
E488399E15F92BE000BD66D5 /* KRBundleManager.h in Headers */,
@@ -857,7 +855,6 @@
E43B0AD615DDCA0F00A5CB9F /* KRContextObject.cpp in Sources */,
E4924C2615EE95E800B965C6 /* KROctree.cpp in Sources */,
E4924C2B15EE96AB00B965C6 /* KROctreeNode.cpp in Sources */,
1000469D15E6EF550053B072 /* KRSkyBox.cpp in Sources */,
E40BA45415EFF79500D7C3DD /* KRAABB.cpp in Sources */,
E430D08115F8882F0010558D /* occlusion_test.fsh in Sources */,
E430D08915F88AD10010558D /* occlusion_test.vsh in Sources */,
@@ -868,6 +865,8 @@
E4CE184F15FF125700F80870 /* light_point_inside.vsh in Sources */,
E4B175AC161F5A1000B8FB80 /* KRTexture.cpp in Sources */,
E4B175B2161F5FAF00B8FB80 /* KRTextureCube.cpp in Sources */,
E41B841E16260C5600C7A771 /* sky_box.fsh in Sources */,
E41B842116260C6500C7A771 /* sky_box.vsh in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -904,7 +903,6 @@
E48C697315374F7E00232E28 /* KRContext.cpp in Sources */,
E46A6B6D1559E97D000DBD37 /* KRResource+blend.cpp in Sources */,
E46F4A05155DF47C00CCF8B8 /* KRWorld.cpp in Sources */,
E4AFC6BA15F7C7C300DDB4C8 /* KRSkyBox.cpp in Sources */,
E46F4A0F155E003000CCF8B8 /* KRDataBlock.cpp in Sources */,
E42CB1F1158446AB0066E0D8 /* KRQuaternion.cpp in Sources */,
E4AFC6BB15F7C7D600DDB4C8 /* KROctreeNode.cpp in Sources */,

View File

@@ -101,6 +101,9 @@ KRCamera::KRCamera(KRContext &context, GLint width, GLint height) : KRContextObj
m_iFrame = 0;
m_skyBoxName = "";
m_pSkyBoxTexture = NULL;
createBuffers();
}
@@ -368,7 +371,7 @@ void KRCamera::renderFrame(KRScene &scene, KRMat4 &viewMatrix, KRVector3 &lightD
scene.render(this, frontToBackOrder, m_visibleBounds, m_pContext, viewMatrix, lightDirection, shadowmvpmatrix, shadowDepthTexture, m_cShadowBuffers, KRNode::RENDER_PASS_FORWARD_OPAQUE, newVisibleBounds);
}
// ----====---- Transparent Geometry, Forward Rendering ----====----
// ----====---- Sky Box ----====----
// Set render target
GLDEBUG(glBindFramebuffer(GL_FRAMEBUFFER, compositeFramebuffer));
@@ -385,6 +388,42 @@ void KRCamera::renderFrame(KRScene &scene, KRMat4 &viewMatrix, KRVector3 &lightD
GLDEBUG(glDepthFunc(GL_LEQUAL));
GLDEBUG(glDepthRangef(0.0, 1.0));
if(!m_pSkyBoxTexture && m_skyBoxName.length()) {
m_pSkyBoxTexture = getContext().getTextureManager()->getTextureCube(m_skyBoxName.c_str());
}
if(m_pSkyBoxTexture) {
KRMat4 mvpMatrix = viewMatrix * getProjectionMatrix();
KRShader *pShader = getContext().getShaderManager()->getShader("sky_box", this, false, false, false, 0, false, false, false, false, false, false, false, false, false, KRNode::RENDER_PASS_FORWARD_OPAQUE);
pShader->bind(this, viewMatrix, mvpMatrix, lightDirection, NULL, NULL, 0, KRNode::RENDER_PASS_FORWARD_OPAQUE);
getContext().getTextureManager()->selectTexture(0, m_pSkyBoxTexture, 2048);
// Render a full screen quad
m_pContext->getModelManager()->bindVBO((void *)KRENGINE_VBO_2D_SQUARE, KRENGINE_VBO_2D_SQUARE_SIZE, true, false, false, true, false);
GLDEBUG(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4));
}
// ----====---- Transparent Geometry, Forward Rendering ----====----
// Note: These parameters have already been set up by the skybox render above
//
// // Set render target
// GLDEBUG(glBindFramebuffer(GL_FRAMEBUFFER, compositeFramebuffer));
// GLDEBUG(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, compositeDepthTexture, 0));
//
// // Disable backface culling
// GLDEBUG(glDisable(GL_CULL_FACE));
//
// // 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));
// Enable alpha blending
GLDEBUG(glEnable(GL_BLEND));
GLDEBUG(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
@@ -779,3 +818,8 @@ void KRCamera::invalidateShadowBuffers() {
shadowValid[i] = false;
}
}
void KRCamera::setSkyBox(const std::string &skyBoxName) {
m_pSkyBoxTexture = NULL;
m_skyBoxName = skyBoxName;
}

View File

@@ -39,6 +39,7 @@
#import "KRAABB.h"
#import "KRShader.h"
#import "KRContextObject.h"
#import "KRTexture.h"
#define KRENGINE_MAX_SHADOW_BUFFERS 3
@@ -111,6 +112,8 @@ public:
int m_cShadowBuffers;
std::string m_debug_text;
void setSkyBox(const std::string &skyBoxName);
private:
KRVector3 m_position;
@@ -158,6 +161,9 @@ private:
std::set<KRAABB> m_visibleBounds; // AABB's that output fragments in the last frame
std::set<KRAABB> m_shadowVisibleBounds[KRENGINE_MAX_SHADOW_BUFFERS]; // AABB's that output fragments in the last frame for each shadow map
std::string m_skyBoxName;
KRTexture *m_pSkyBoxTexture;
};
#endif

View File

@@ -15,7 +15,6 @@
#import "KRSpotLight.h"
#import "KRDirectionalLight.h"
#import "KRInstance.h"
#import "KRSkyBox.h"
#import "KRAABB.h"
@@ -143,8 +142,6 @@ KRNode *KRNode::LoadXML(KRScene &scene, tinyxml2::XMLElement *e) {
lod_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
}
new_node = new KRInstance(scene, szName, szName, e->Attribute("light_map"), lod_min_coverage);
} else if(strcmp(szElementName, "sky_box") == 0) {
new_node = new KRSkyBox(scene, szName);
}
if(new_node) {

View File

@@ -126,12 +126,6 @@ void KRPointLight::render(KRCamera *pCamera, KRContext *pContext, KRMat4 &viewMa
GLDEBUG(glUniformMatrix4fv(pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_M2V], 1, GL_FALSE, matModelToView2.getPointer()));
KRMat4 matInvProjection;
matInvProjection = pCamera->getProjectionMatrix();
matInvProjection.invert();
GLDEBUG(glUniformMatrix4fv(pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_INVP], 1, GL_FALSE, matInvProjection.getPointer()));
if(bVisualize) {
// Enable additive blending
GLDEBUG(glEnable(GL_BLEND));

View File

@@ -48,6 +48,7 @@ KRScene::KRScene(KRContext &context, std::string name) : KRResource(context, nam
sun_yaw = 4.333; // TODO - Remove temporary testing code
sun_pitch = 0.55;
m_skyBoxName = "";
}
KRScene::~KRScene() {
delete m_pRootNode;
@@ -59,6 +60,7 @@ KRScene::~KRScene() {
void KRScene::render(KRCamera *pCamera, int childOrder[], std::set<KRAABB> &visibleBounds, KRContext *pContext, KRMat4 &viewMatrix, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRNode::RenderPass renderPass, std::set<KRAABB> &newVisibleBounds) {
updateOctree();
pCamera->setSkyBox(m_skyBoxName); // This is temporary until the camera is moved into the scene graph
if(renderPass != KRNode::RENDER_PASS_SHADOWMAP) {
@@ -309,8 +311,10 @@ KRNode *KRScene::getRootNode() {
bool KRScene::save(const std::string& path) {
tinyxml2::XMLDocument doc;
tinyxml2::XMLNode *scene_node = doc.InsertEndChild( doc.NewElement( "scene" ));
tinyxml2::XMLElement *scene_node = doc.NewElement( "scene" );
doc.InsertEndChild(scene_node);
m_pRootNode->saveXML(scene_node);
scene_node->SetAttribute("skybox", m_skyBoxName.c_str()); // This is temporary until the camera is moved into the scene graph
doc.SaveFile(path.c_str());
return true;
}
@@ -339,7 +343,10 @@ KRScene *KRScene::Load(KRContext &context, const std::string &name, KRDataBlock
doc.Parse((char *)data->getStart());
KRScene *new_scene = new KRScene(context, name);
KRNode *n = KRNode::LoadXML(*new_scene, doc.RootElement()->FirstChildElement());
tinyxml2::XMLElement *scene_element = doc.RootElement();
new_scene->m_skyBoxName = scene_element->Attribute("skybox"); // This is temporary until the camera is moved into the scene graph
KRNode *n = KRNode::LoadXML(*new_scene, scene_element->FirstChildElement());
if(n) {
new_scene->getRootNode()->addChild(n);
}

View File

@@ -87,7 +87,8 @@ private:
KROctree m_nodeTree;
void updateOctree();
std::string m_skyBoxName;
};

View File

@@ -130,6 +130,8 @@ KRShader::KRShader(char *szKey, std::string options, std::string vertShaderSourc
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_MATERIAL_SHININESS] = glGetUniformLocation(m_iProgram, "material_shininess"));
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_MVP] = glGetUniformLocation(m_iProgram, "mvp_matrix"));
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_INVP] = glGetUniformLocation(m_iProgram, "inv_projection_matrix"));
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_INVMVP] = glGetUniformLocation(m_iProgram, "inv_mvp_matrix"));
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_INVMVP_NO_TRANSLATE] = glGetUniformLocation(m_iProgram, "inv_mvp_matrix_no_translate"));
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_MN2V] = glGetUniformLocation(m_iProgram, "model_normal_to_view_matrix"));
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_M2V] = glGetUniformLocation(m_iProgram, "model_to_view_matrix"));
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_V2M] = glGetUniformLocation(m_iProgram, "view_to_model_matrix"));
@@ -210,6 +212,30 @@ bool KRShader::bind(KRCamera *pCamera, KRMat4 &matModelToView, KRMat4 &mvpMatrix
// Bind our modelmatrix variable to be a uniform called mvpmatrix in our shaderprogram
GLDEBUG(glUniformMatrix4fv(m_uniforms[KRENGINE_UNIFORM_MVP], 1, GL_FALSE, mvpMatrix.getPointer()));
GLDEBUG(glUniformMatrix4fv(m_uniforms[KRENGINE_UNIFORM_MN2V], 1, GL_FALSE, matModelToView.getPointer()));
KRMat4 matInvProjection;
matInvProjection = pCamera->getProjectionMatrix();
matInvProjection.invert();
GLDEBUG(glUniformMatrix4fv(m_uniforms[KRShader::KRENGINE_UNIFORM_INVP], 1, GL_FALSE, matInvProjection.getPointer()));
KRMat4 matInvMVP = mvpMatrix;
matInvMVP.invert();
GLDEBUG(glUniformMatrix4fv(m_uniforms[KRShader::KRENGINE_UNIFORM_INVMVP], 1, GL_FALSE, matInvMVP.getPointer()));
KRMat4 matInvMVPNoTranslate = matModelToView;
// Remove the translation
matInvMVPNoTranslate.getPointer()[3] = 0;
matInvMVPNoTranslate.getPointer()[7] = 0;
matInvMVPNoTranslate.getPointer()[11] = 0;
matInvMVPNoTranslate.getPointer()[12] = 0;
matInvMVPNoTranslate.getPointer()[13] = 0;
matInvMVPNoTranslate.getPointer()[14] = 0;
matInvMVPNoTranslate.getPointer()[15] = 1.0;
matInvMVPNoTranslate = matInvMVPNoTranslate * pCamera->getProjectionMatrix();
matInvMVPNoTranslate.invert();
GLDEBUG(glUniformMatrix4fv(m_uniforms[KRShader::KRENGINE_UNIFORM_INVMVP_NO_TRANSLATE], 1, GL_FALSE, matInvMVPNoTranslate.getPointer()));
KRVector3 nLightDir = lightDirection;

View File

@@ -89,6 +89,8 @@ public:
KRENGINE_UNIFORM_MVP,
KRENGINE_UNIFORM_INVP,
KRENGINE_UNIFORM_INVMVP,
KRENGINE_UNIFORM_INVMVP_NO_TRANSLATE,
KRENGINE_UNIFORM_MN2V,
KRENGINE_UNIFORM_M2V,
KRENGINE_UNIFORM_V2M,

View File

@@ -1,163 +0,0 @@
//
// KRSkyBox.cpp
// KREngine
//
// Created by Michael Ilich on 2012-08-23.
// Copyright (c) 2012 Kearwood Software. All rights reserved.
//
#include <iostream>
#include "KRSkyBox.h"
#import "KRShader.h"
#import "KRContext.h"
#import "KRMat4.h"
#import "KRResource.h"
#import "KRContext.h"
#import "KRStockGeometry.h"
KRSkyBox::KRSkyBox(KRScene &scene, std::string name) : KRNode(scene, name)
{
m_frontTexture = "";
m_backTexture = "";
m_topTexture = "";
m_bottomTexture = "";
m_leftTexture = "";
m_rightTexture = "";
m_pFrontTexture = NULL;
m_pBackTexture = NULL;
m_pTopTexture = NULL;
m_pBottomTexture = NULL;
m_pLeftTexture = NULL;
m_pRightTexture = NULL;
}
KRSkyBox::~KRSkyBox()
{
}
std::string KRSkyBox::getElementName() {
return "sky_box";
}
void KRSkyBox::loadXML(tinyxml2::XMLElement *e) {
KRNode::loadXML(e);
const char *szFrontTexture = e->Attribute("front_texture");
if(szFrontTexture) {
m_frontTexture = szFrontTexture;
} else {
m_frontTexture = "";
}
m_pFrontTexture = NULL;
const char *szBackTexture = e->Attribute("back_texture");
if(szBackTexture) {
m_backTexture = szBackTexture;
} else {
m_backTexture = "";
}
m_pBackTexture = NULL;
const char *szTopTexture = e->Attribute("top_texture");
if(szTopTexture) {
m_topTexture = szTopTexture;
} else {
m_topTexture = "";
}
m_pTopTexture = NULL;
const char *szBottomTexture = e->Attribute("bottom_texture");
if(szBottomTexture) {
m_bottomTexture = szBottomTexture;
} else {
m_bottomTexture = "";
}
m_pBottomTexture = NULL;
const char *szLeftTexture = e->Attribute("left_texture");
if(szLeftTexture) {
m_leftTexture = szLeftTexture;
} else {
m_leftTexture = "";
}
m_pLeftTexture = NULL;
const char *szRightTexture = e->Attribute("right_texture");
if(szRightTexture) {
m_rightTexture = szRightTexture;
} else {
m_rightTexture = "";
}
m_pRightTexture = NULL;
}
#if TARGET_OS_IPHONE
void KRSkyBox::render(KRCamera *pCamera, KRContext *pContext, KRMat4 &viewMatrix, KRNode::RenderPass renderPass) {
// KRNode::render(pCamera, pContext, frustrumVolume, viewMatrix, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, renderPass);
if(renderPass == KRNode::RENDER_PASS_SKYBOX) {
// Skybox is rendered on the final pass of the deferred renderer
if(m_frontTexture.size() && !m_pFrontTexture) {
m_pFrontTexture = pContext->getTextureManager()->getTexture(m_frontTexture.c_str());
}
if(m_backTexture.size() && !m_pBackTexture) {
m_pBackTexture = pContext->getTextureManager()->getTexture(m_backTexture.c_str());
}
if(m_topTexture.size() && !m_pTopTexture) {
m_pTopTexture = pContext->getTextureManager()->getTexture(m_topTexture.c_str());
}
if(m_bottomTexture.size() && !m_pBottomTexture) {
m_pBottomTexture = pContext->getTextureManager()->getTexture(m_bottomTexture.c_str());
}
if(m_leftTexture.size() && !m_pLeftTexture) {
m_pLeftTexture = pContext->getTextureManager()->getTexture(m_leftTexture.c_str());
}
if(m_rightTexture.size() && !m_pRightTexture) {
m_pRightTexture = pContext->getTextureManager()->getTexture(m_rightTexture.c_str());
}
KRMat4 projectionMatrix = pCamera->getProjectionMatrix();
KRMat4 mvpmatrix = m_modelMatrix * viewMatrix * projectionMatrix;
KRMat4 matModelToView = viewMatrix * m_modelMatrix;
matModelToView.transpose();
matModelToView.invert();
// KRShader *pShader = pContext->getShaderManager()->getShader("sky_box", pCamera, false, false, false, 0, false, false, false, false, false, false, false, false, renderPass);
// pShader->bind(pCamera, matModelToView, mvpmatrix, NULL, NULL, NULL, 0, renderPass);
m_pContext->getTextureManager()->selectTexture(0, m_pFrontTexture, 2048);
m_pContext->getTextureManager()->selectTexture(1, m_pBackTexture, 2048);
m_pContext->getTextureManager()->selectTexture(2, m_pTopTexture, 2048);
m_pContext->getTextureManager()->selectTexture(3, m_pBottomTexture, 2048);
m_pContext->getTextureManager()->selectTexture(4, m_pLeftTexture, 2048);
m_pContext->getTextureManager()->selectTexture(5, m_pRightTexture, 2048);
// Disable z-buffer write
GLDEBUG(glDepthMask(GL_FALSE));
// Disable z-buffer test
GLDEBUG(glDisable(GL_DEPTH_TEST));
// Render a full screen quad
m_pContext->getModelManager()->bindVBO((void *)KRENGINE_VBO_2D_SQUARE, KRENGINE_VBO_2D_SQUARE_SIZE, true, false, false, true, false);
GLDEBUG(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4));
}
}
#endif

View File

@@ -1,53 +0,0 @@
//
// KRSkyBox.h
// KREngine
//
// Created by Michael Ilich on 2012-08-23.
// Copyright (c) 2012 Kearwood Software. All rights reserved.
//
#ifndef __KREngine__KRSkyBox__
#define __KREngine__KRSkyBox__
#include <iostream>
#import "KRMat4.h"
#import "KRNode.h"
#import "KRTexture.h"
class KRSkyBox : public KRNode {
public:
KRSkyBox(KRScene &scene, std::string name);
virtual ~KRSkyBox();
virtual std::string getElementName();
virtual void loadXML(tinyxml2::XMLElement *e);
#if TARGET_OS_IPHONE
virtual void render(KRCamera *pCamera, KRContext *pContext, KRMat4 &viewMatrix, KRNode::RenderPass renderPass);
#endif
private:
KRMat4 m_modelMatrix;
protected:
std::string m_frontTexture;
std::string m_backTexture;
std::string m_topTexture;
std::string m_bottomTexture;
std::string m_leftTexture;
std::string m_rightTexture;
KRTexture *m_pFrontTexture;
KRTexture *m_pBackTexture;
KRTexture *m_pTopTexture;
KRTexture *m_pBottomTexture;
KRTexture *m_pLeftTexture;
KRTexture *m_pRightTexture;
};
#endif

View File

@@ -19,36 +19,36 @@ KRTexture::KRTexture(KRContext &context) : KRContextObject(context)
KRTexture::~KRTexture()
{
long textureMemFreed = 0;
size_t textureMemFreed = 0;
releaseHandle(textureMemFreed);
}
void KRTexture::releaseHandle(long &textureMemUsed) {
void KRTexture::releaseHandle(size_t &textureMemUsed) {
textureMemUsed -= getMemSize();
if(m_iHandle != 0) {
textureMemUsed -= getMemSize();
GLDEBUG(glDeleteTextures(1, &m_iHandle));
m_iHandle = 0;
m_textureMemUsed = 0;
}
textureMemUsed += getMemSize();
}
long KRTexture::getMemSize() {
return m_textureMemUsed; // TODO - This is not 100% accurate, as loaded format may differ in size while in GPU memory
}
GLuint KRTexture::getHandle(long &textureMemUsed, int max_dim, bool can_resize) {
GLuint KRTexture::getHandle(int max_dim, bool can_resize) {
// Constrain target LOD to be within mipmap levels of texture
int target_dim = max_dim;
if(target_dim < m_min_lod_max_dim) target_dim = m_min_lod_max_dim;
if(target_dim > m_max_lod_max_dim) target_dim = m_max_lod_max_dim;
if(can_resize && m_current_lod_max_dim != target_dim) {
releaseHandle(textureMemUsed);
size_t memFreed = 0;
releaseHandle(memFreed);
}
if(m_iHandle == 0) {
if(createGLTexture(target_dim, m_textureMemUsed)) {
textureMemUsed += getMemSize();
} else {
if(!createGLTexture(target_dim)) {
assert(false);
}
}

View File

@@ -45,18 +45,19 @@ public:
KRTexture(KRContext &context);
virtual ~KRTexture();
GLuint getHandle(long &textureMemUsed, int max_dim, bool can_resize);
void releaseHandle(long &textureMemUsed);
virtual void bind(size_t &textureMemUsed, int max_dim, bool can_resize) = 0;
void releaseHandle(size_t &textureMemUsed);
long getMemSize();
protected:
virtual bool createGLTexture(int lod_max_dim, uint32_t &textureMemUsed) = 0;
virtual bool createGLTexture(int lod_max_dim) = 0;
GLuint getHandle(int max_dim, bool can_resize);
GLuint m_iHandle;
uint32_t m_textureMemUsed;
GLuint m_iHandle;
size_t m_textureMemUsed;
int m_current_lod_max_dim;

View File

@@ -171,7 +171,7 @@ bool KRTexture2D::load() {
bool KRTexture2D::createGLTexture(int lod_max_dim, uint32_t &textureMemUsed) {
bool KRTexture2D::createGLTexture(int lod_max_dim) {
m_current_lod_max_dim = 0;
GLDEBUG(glGenTextures(1, &m_iHandle));
if(m_iHandle == 0) {
@@ -183,9 +183,8 @@ bool KRTexture2D::createGLTexture(int lod_max_dim, uint32_t &textureMemUsed) {
} else {
GLDEBUG(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
}
if(!uploadTexture(GL_TEXTURE_2D, lod_max_dim, m_current_lod_max_dim, textureMemUsed)) {
if(!uploadTexture(GL_TEXTURE_2D, lod_max_dim, m_current_lod_max_dim, m_textureMemUsed)) {
GLDEBUG(glDeleteTextures(1, &m_iHandle));
textureMemUsed = 0;
m_iHandle = 0;
m_current_lod_max_dim = 0;
return false;
@@ -194,7 +193,7 @@ bool KRTexture2D::createGLTexture(int lod_max_dim, uint32_t &textureMemUsed) {
return true;
}
bool KRTexture2D::uploadTexture(GLenum target, int lod_max_dim, int &current_lod_max_dim, uint32_t &textureMemUsed)
bool KRTexture2D::uploadTexture(GLenum target, int lod_max_dim, int &current_lod_max_dim, size_t &textureMemUsed)
{
int width = m_iWidth;
int height = m_iHeight;
@@ -214,11 +213,9 @@ bool KRTexture2D::uploadTexture(GLenum target, int lod_max_dim, int &current_lod
if(height > current_lod_max_dim) {
current_lod_max_dim = height;
}
GLDEBUG(glCompressedTexImage2D(target, i, m_internalFormat, width, height, 0, block.length, block.start));
glCompressedTexImage2D(target, i, m_internalFormat, width, height, 0, block.length, block.start);
err = glGetError();
if (err != GL_NO_ERROR) {
return false;
}
textureMemUsed += block.length;
@@ -239,5 +236,15 @@ bool KRTexture2D::uploadTexture(GLenum target, int lod_max_dim, int &current_lod
}
void KRTexture2D::bind(size_t &textureMemUsed, int max_dim, bool can_resize) {
textureMemUsed -= getMemSize();
GLDEBUG(glBindTexture(GL_TEXTURE_2D, getHandle(max_dim, can_resize)));
// TODO - These texture parameters should be assigned by the material or texture parameters
GLDEBUG(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f));
GLDEBUG(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT));
GLDEBUG(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT));
textureMemUsed += getMemSize();
}

View File

@@ -52,12 +52,13 @@ public:
int getMaxMipMap();
int getMinMipMap();
bool uploadTexture(GLenum target, int lod_max_dim, int &current_lod_max_dim, uint32_t &textureMemUsed);
bool uploadTexture(GLenum target, int lod_max_dim, int &current_lod_max_dim, size_t &textureMemUsed);
virtual void bind(size_t &textureMemUsed, int max_dim, bool can_resize);
private:
KRDataBlock *m_pData;
virtual bool createGLTexture(int lod_max_dim, uint32_t &textureMemUsed);
virtual bool createGLTexture(int lod_max_dim);
uint32_t m_iWidth;

View File

@@ -54,7 +54,7 @@ KRTextureCube::~KRTextureCube()
{
}
bool KRTextureCube::createGLTexture(int lod_max_dim, uint32_t &textureMemUsed)
bool KRTextureCube::createGLTexture(int lod_max_dim)
{
m_current_lod_max_dim = 0;
GLDEBUG(glGenTextures(1, &m_iHandle));
@@ -71,7 +71,7 @@ bool KRTextureCube::createGLTexture(int lod_max_dim, uint32_t &textureMemUsed)
KRTexture2D *faceTexture = (KRTexture2D *)getContext().getTextureManager()->getTexture(faceName.c_str());
if(faceTexture) {
if(faceTexture->hasMipmaps()) bMipMaps = true;
faceTexture->uploadTexture(TARGETS[i], lod_max_dim, m_current_lod_max_dim, textureMemUsed);
faceTexture->uploadTexture(TARGETS[i], lod_max_dim, m_current_lod_max_dim, m_textureMemUsed);
}
}
@@ -82,3 +82,15 @@ bool KRTextureCube::createGLTexture(int lod_max_dim, uint32_t &textureMemUsed)
}
return true;
}
void KRTextureCube::bind(size_t &textureMemUsed, int max_dim, bool can_resize)
{
textureMemUsed -= getMemSize();
GLDEBUG(glBindTexture(GL_TEXTURE_CUBE_MAP, getHandle(max_dim, can_resize)));
GLDEBUG(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GLDEBUG(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
textureMemUsed += getMemSize();
}

View File

@@ -39,15 +39,13 @@ public:
KRTextureCube(KRContext &context, std::string name);
virtual ~KRTextureCube();
virtual void bind(size_t &textureMemUsed, int max_dim, bool can_resize);
private:
virtual bool createGLTexture(int lod_max_dim, uint32_t &textureMemUsed);
virtual bool createGLTexture(int lod_max_dim);
std::string m_name;
GLuint m_iHandle;
uint32_t m_textureMemUsed;
const GLenum TARGETS[6] = {
GL_TEXTURE_CUBE_MAP_POSITIVE_X,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,

View File

@@ -106,18 +106,15 @@ void KRTextureManager::selectTexture(int iTextureUnit, KRTexture *pTexture, int
bActive = false;
m_activeTextures.insert(pTexture);
}
long textureMemChange = 0;
GLDEBUG(glBindTexture(GL_TEXTURE_2D, pTexture->getHandle(textureMemChange, lod_max_dim < m_lod_max_dim_cap ? lod_max_dim : m_lod_max_dim_cap, !bActive)));
size_t textureMemChange = 0;
pTexture->bind(textureMemChange, lod_max_dim < m_lod_max_dim_cap ? lod_max_dim : m_lod_max_dim_cap, !bActive);
m_textureMemUsed += textureMemChange;
if(bActive) {
m_activeTextureMemUsed += textureMemChange;
} else {
m_activeTextureMemUsed += pTexture->getMemSize();
}
// TODO - These texture parameters should be assigned by the material or texture parameters
GLDEBUG(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f));
GLDEBUG(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT));
GLDEBUG(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT));
} else {
GLDEBUG(glBindTexture(GL_TEXTURE_2D, 0));
}
@@ -130,20 +127,23 @@ void KRTextureManager::selectTexture(int iTextureUnit, KRTexture *pTexture, int
}
// Keep texture size within limits
KRTexture *droppedTexture = (*m_poolTextures.begin());
droppedTexture->releaseHandle(m_textureMemUsed);
m_poolTextures.erase(droppedTexture);
//fprintf(stderr, "Texture Swapping...\n");
if(droppedTexture == NULL) {
break;
} else {
droppedTexture->releaseHandle(m_textureMemUsed);
m_poolTextures.erase(droppedTexture);
}
}
}
// fprintf(stderr, "VBO Mem: %i Kbyte Texture Mem: %i Kbyte\n", (int)m_pContext->getModelManager()->getMemUsed() / 1024, (int)m_pContext->getTextureManager()->getMemUsed() / 1024);
}
long KRTextureManager::getMemUsed() {
size_t KRTextureManager::getMemUsed() {
return m_textureMemUsed;
}
long KRTextureManager::getActiveMemUsed() {
size_t KRTextureManager::getActiveMemUsed() {
return m_activeTextureMemUsed;
}

View File

@@ -64,8 +64,8 @@ public:
KRTexture *getTextureCube(const char *szName);
KRTexture *getTexture(const char *szFile);
long getMemUsed();
long getActiveMemUsed();
size_t getMemUsed();
size_t getActiveMemUsed();
int getLODDimCap();
@@ -79,8 +79,8 @@ private:
std::set<KRTexture *> m_activeTextures;
std::set<KRTexture *> m_poolTextures;
long m_textureMemUsed;
long m_activeTextureMemUsed;
size_t m_textureMemUsed;
size_t m_activeTextureMemUsed;
int m_lod_max_dim_cap;
};

View File

@@ -0,0 +1,38 @@
//
// sky_box.fsh
// 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.
//
uniform samplerCube diffuseTexture;
varying mediump vec3 texCoord;
void main() {
gl_FragColor = textureCube(diffuseTexture, normalize(texCoord));
}

View File

@@ -0,0 +1,49 @@
//
// sky_box.vsh
// 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.
//
//
attribute vec4 vertex_position;
//attribute lowp vec4 vertex_uv;
//
//varying mediump vec2 textureCoordinate;
varying mediump vec3 texCoord;
uniform highp mat4 inv_mvp_matrix_no_translate; // Inverse of the model-view-projection matrix, without view translation component
uniform mediump vec4 viewport;
void main()
{
gl_Position = vec4(vertex_position.xy, 1.0, 1.0);
vec4 t = inv_mvp_matrix_no_translate * vec4(vertex_position.xy, 1.0, 1.0);
t /= t.w;
texCoord = vec3(t);
}