From: <axl...@us...> - 2009-06-13 01:10:49
|
Revision: 312 http://hgengine.svn.sourceforge.net/hgengine/?rev=312&view=rev Author: axlecrusher Date: 2009-06-13 01:10:48 +0000 (Sat, 13 Jun 2009) Log Message: ----------- broken FBO Added Paths: ----------- Mercury2/src/MercuryFBO.cpp Mercury2/src/MercuryFBO.h Mercury2/src/RenderBuffer.cpp Mercury2/src/RenderBuffer.h Added: Mercury2/src/MercuryFBO.cpp =================================================================== --- Mercury2/src/MercuryFBO.cpp (rev 0) +++ Mercury2/src/MercuryFBO.cpp 2009-06-13 01:10:48 UTC (rev 312) @@ -0,0 +1,87 @@ +#include <MercuryFBO.h> +#include <GLHeaders.h> + +REGISTER_NODE_TYPE(MercuryFBO); + +MercuryFBO::MercuryFBO() + :m_fboID(0), m_initiated(false) +{ +} + +MercuryFBO::~MercuryFBO() +{ + if (m_fboID != 0) glDeleteFramebuffersEXT(1, &m_fboID); +} + +void MercuryFBO::InitFBOBeforeRender() +{ + m_initiated = true; + glGenFramebuffersEXT(1, &m_fboID); +} + +void MercuryFBO::PreRender(const MercuryMatrix& matrix) +{ + if ( !m_initiated ) InitFBOBeforeRender(); + RenderableNode::PreRender(matrix); +} + +void MercuryFBO::Render(const MercuryMatrix& matrix) +{ + if (m_lastRendered != m_fboID) + { + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fboID); + m_lastRendered = m_fboID; +// m_lastInStask = m_lastRendered; + } + +// glPushAttrib(GL_VIEWPORT_BIT); + // glViewport(0,0,width, height); + + RenderableNode::Render(matrix); +} + +void MercuryFBO::PostRender(const MercuryMatrix& matrix) +{ +// glPopAttrib(); + +// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_lastInStask); +// m_lastRendered = m_lastInStask; + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); //unbind + m_lastRendered = 0; + + RenderableNode::PostRender(matrix); +} + +uint32_t MercuryFBO::m_lastRendered = NULL; + +/**************************************************************************** + * Copyright (C) 2009 by Joshua Allen * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * 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. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "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 THE COPYRIGHT * + * OWNER 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. * + ***************************************************************************/ Added: Mercury2/src/MercuryFBO.h =================================================================== --- Mercury2/src/MercuryFBO.h (rev 0) +++ Mercury2/src/MercuryFBO.h 2009-06-13 01:10:48 UTC (rev 312) @@ -0,0 +1,64 @@ +#ifndef MERCURYFBO_H +#define MERCURYFBO_H + +#include <RenderableNode.h> + +class MercuryFBO : public RenderableNode +{ + public: + MercuryFBO(); + virtual ~MercuryFBO(); + + virtual void PreRender(const MercuryMatrix& matrix); + virtual void Render(const MercuryMatrix& matrix); + virtual void PostRender(const MercuryMatrix& matrix); + + GENRTTI(MercuryFBO); + + private: + void InitFBOBeforeRender(); + + uint32_t m_fboID; + bool m_initiated; + static uint32_t m_lastRendered; + + uint32_t m_lastInStask; + + protected: +// AlignedBuffer<float> m_vertexData; +// AlignedBuffer<uint16_t> m_indexData; +}; + +#endif + +/**************************************************************************** + * Copyright (C) 2009 by Joshua Allen * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * 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. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "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 THE COPYRIGHT * + * OWNER 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. * + ***************************************************************************/ Added: Mercury2/src/RenderBuffer.cpp =================================================================== --- Mercury2/src/RenderBuffer.cpp (rev 0) +++ Mercury2/src/RenderBuffer.cpp 2009-06-13 01:10:48 UTC (rev 312) @@ -0,0 +1,228 @@ +#include <RenderBuffer.h> +#include <GLHeaders.h> +#include <MercuryWindow.h> +#include <RenderableNode.h> + +#include <Texture.h> + +REGISTER_ASSET_TYPE(RenderBuffer); + +RenderBuffer::RenderBuffer() + :MercuryAsset(), m_bufferID(0), m_textureID(0), m_initiated(false), m_width(0), m_height(0), m_type(NONE), m_bound(false) +{ +} + +RenderBuffer::~RenderBuffer() +{ + if (m_bufferID != 0) glDeleteRenderbuffersEXT(1, &m_bufferID); +} + +void RenderBuffer::Init(MercuryNode* node) +{ + MercuryAsset::Init( node ); + + RenderableNode* rn = RenderableNode::Cast( node ); + if ( rn ) + { + rn->AddPreRender( this ); + rn->AddPostRender( this ); + } +} + +void RenderBuffer::PreRender(const MercuryNode* node) +{ + if ( !m_initiated ) InitRenderBuffer(); +} + +void RenderBuffer::Render(const MercuryNode* node) +{ + glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_bufferID); + if ( NeedResize() ) AllocateSpace(); + + //attach to FBO +// if ( !m_bound ) + { + m_bound = true; + if ( m_type == TEXTURE ) + glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GLAttachPoint(), GL_TEXTURE_2D, m_textureID, 0); + else + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GLAttachPoint(), GL_RENDERBUFFER_EXT, m_bufferID); + } + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +} + +void RenderBuffer::PostRender(const MercuryNode* node) +{ + static uint32_t t = time(NULL); + if ( (m_type == TEXTURE) && (time(NULL) > (t+3))) +// if (false) + { + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + +// printf("active %d\n", Texture::NumberActiveTextures() ); + + //this works with a "normal" texture, FBO texture is still white + glActiveTexture( GL_TEXTURE0 ); + glClientActiveTextureARB(GL_TEXTURE0); + glEnable( GL_TEXTURE_2D ); + glBindTexture(GL_TEXTURE_2D, m_textureID); + glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); + + + glBegin(GL_QUADS); + glTexCoord2d(0,1); + glVertex3i(-1, -1, -1); + + glTexCoord2d(1,1); + glVertex3i(1, -1, -1); + + glTexCoord2d(1,0); + glVertex3i(1, 1, -1); + + glTexCoord2d(0,0); + glVertex3i(-1, 1, -1); + glEnd(); + + + glBindTexture(GL_TEXTURE_2D, 0); +// glActiveTexture( GL_TEXTURE0 ); +// glClientActiveTextureARB(GL_TEXTURE0); +// glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisable( GL_TEXTURE_2D ); + + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + } +} + +void RenderBuffer::InitRenderBuffer() +{ + m_initiated = true; + glGenRenderbuffersEXT(1, &m_bufferID); + + if (m_type == TEXTURE) + { + glGenTextures(1, &m_textureID); + printf("texture rb %d\n", m_textureID); + } +} + +void RenderBuffer::AllocateSpace() +{ + m_width = MercuryWindow::GetCurrentWindow()->Width(); + m_height = MercuryWindow::GetCurrentWindow()->Height(); + + if (m_type == TEXTURE) + { + glBindTexture(GL_TEXTURE_2D, m_textureID); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + } + else + glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GLType(), m_width, m_height); +} + +bool RenderBuffer::NeedResize() +{ + int width = MercuryWindow::GetCurrentWindow()->Width(); + int height = MercuryWindow::GetCurrentWindow()->Height(); + return (width != m_width)||(height != m_height); +} + +int RenderBuffer::GLType() +{ + switch( m_type ) + { + case TEXTURE: + case COLOR: + return GL_RGBA; + case DEPTH: + return GL_DEPTH_COMPONENT; + case STENCIL: + return GL_STENCIL_INDEX; + default: + return 0; + } +} + +int RenderBuffer::GLAttachPoint() +{ + switch( m_type ) + { + case TEXTURE: + case COLOR: + return GL_COLOR_ATTACHMENT0_EXT; + case DEPTH: + return GL_DEPTH_ATTACHMENT_EXT; + case STENCIL: + return GL_STENCIL_ATTACHMENT_EXT; + default: + return 0; + } +} + +void RenderBuffer::LoadFromXML(const XMLNode& node) +{ + MString t = node.Attribute("buffertype"); + + if ( t == "color" ) + { + m_type = COLOR; + } + else if ( t == "depth" ) + { + m_type = DEPTH; + } + else if ( t == "stencil" ) + { + m_type = STENCIL; + } + else if ( t == "texture" ) + { + m_type = TEXTURE; + } +} + +RenderBuffer* RenderBuffer::Generate() +{ + return new RenderBuffer(); +} + +/**************************************************************************** + * Copyright (C) 2009 by Joshua Allen * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * 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. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "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 THE COPYRIGHT * + * OWNER 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. * + ***************************************************************************/ Added: Mercury2/src/RenderBuffer.h =================================================================== --- Mercury2/src/RenderBuffer.h (rev 0) +++ Mercury2/src/RenderBuffer.h 2009-06-13 01:10:48 UTC (rev 312) @@ -0,0 +1,85 @@ +#ifndef RENDERBUFFER_H +#define RENDERBUFFER_H + +#include <MercuryAsset.h> + +class RenderBuffer : public MercuryAsset +{ + public: + enum RBType + { + NONE, + COLOR, + DEPTH, + STENCIL, + TEXTURE + }; + + RenderBuffer(); + virtual ~RenderBuffer(); + + virtual void Init(MercuryNode* node); + + virtual void PreRender(const MercuryNode* node); + virtual void Render(const MercuryNode* node); + virtual void PostRender(const MercuryNode* node); + virtual void LoadFromXML(const XMLNode& node); + + static RenderBuffer* Generate(); + + private: + virtual void InitRenderBuffer(); + void AllocateSpace(); + bool NeedResize(); + + int GLType(); + int GLAttachPoint(); + + uint32_t m_bufferID; + uint32_t m_textureID; + bool m_initiated; + int m_width, m_height; + RBType m_type; + + bool m_bound; + +// static void* m_lastRendered; + + protected: +// AlignedBuffer<float> m_vertexData; +// AlignedBuffer<uint16_t> m_indexData; +}; + +#endif + +/**************************************************************************** + * Copyright (C) 2009 by Joshua Allen * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * 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. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "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 THE COPYRIGHT * + * OWNER 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. * + ***************************************************************************/ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |