[colobot] 371/390: Added proper render to texture support

Didier Raboud odyx at moszumanska.debian.org
Fri Jun 12 14:22:06 UTC 2015


This is an automated email from the git hooks/post-receive script.

odyx pushed a commit to branch upstream/latest
in repository colobot.

commit f67c01e5e77735535c6631494f7af0ff69072d68
Author: Tomasz Kapuściński <tomaszkax86 at gmail.com>
Date:   Thu May 21 18:03:17 2015 +0200

    Added proper render to texture support
---
 src/graphics/core/device.h         | 14 ++++++++++
 src/graphics/core/nulldevice.cpp   |  4 +++
 src/graphics/core/nulldevice.h     |  2 ++
 src/graphics/opengl/gl33device.cpp | 56 +++++++++++++++++++++++++++++++++++++-
 src/graphics/opengl/gl33device.h   |  4 +++
 src/graphics/opengl/gldevice.cpp   | 40 +++++++++++++++++++++++++++
 src/graphics/opengl/gldevice.h     |  4 +++
 7 files changed, 123 insertions(+), 1 deletion(-)

diff --git a/src/graphics/core/device.h b/src/graphics/core/device.h
index 8db8e33..f99127a 100644
--- a/src/graphics/core/device.h
+++ b/src/graphics/core/device.h
@@ -227,6 +227,17 @@ enum FrustumPlane
 };
 
 /**
+ * \enum RenderTarget
+ * \brief Render targets for rendering to textures
+ */
+enum RenderTarget
+{
+    RENDER_TARGET_COLOR,
+    RENDER_TARGET_DEPTH,
+    RENDER_TARGET_STENCIL
+};
+
+/**
  * \class CDevice
  * \brief Abstract interface of graphics device
  *
@@ -382,6 +393,9 @@ public:
     //! Initializes offscreen buffer
     virtual void InitOffscreenBuffer(int width, int height) = 0;
 
+    //! Sets render target to texture
+    virtual void SetRenderTexture(RenderTarget target, int texture) = 0;
+
     //! Copies content of framebuffer to texture
     virtual void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) = 0;
 
diff --git a/src/graphics/core/nulldevice.cpp b/src/graphics/core/nulldevice.cpp
index 26956ab..acac78d 100644
--- a/src/graphics/core/nulldevice.cpp
+++ b/src/graphics/core/nulldevice.cpp
@@ -355,6 +355,10 @@ void CNullDevice::InitOffscreenBuffer(int width, int height)
 {
 }
 
+void CNullDevice::SetRenderTexture(RenderTarget target, int texture)
+{
+}
+
 void CNullDevice::CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height)
 {
 }
diff --git a/src/graphics/core/nulldevice.h b/src/graphics/core/nulldevice.h
index e68b982..07abf3a 100644
--- a/src/graphics/core/nulldevice.h
+++ b/src/graphics/core/nulldevice.h
@@ -140,6 +140,8 @@ public:
 
     virtual void InitOffscreenBuffer(int width, int height);
 
+    virtual void SetRenderTexture(RenderTarget target, int texture);
+
     virtual void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height);
     
     virtual void* GetFrameBufferPixels() const;
diff --git a/src/graphics/opengl/gl33device.cpp b/src/graphics/opengl/gl33device.cpp
index 193440c..0401edc 100644
--- a/src/graphics/opengl/gl33device.cpp
+++ b/src/graphics/opengl/gl33device.cpp
@@ -49,6 +49,7 @@ CGL33Device::CGL33Device(const GLDeviceConfig &config)
     m_framebuffer = 0;
     m_colorBuffer = 0;
     m_depthBuffer = 0;
+    m_offscreenRenderingEnabled = false;
 }
 
 
@@ -336,6 +337,9 @@ bool CGL33Device::Create()
         assert(false);
     }
 
+    glDeleteShader(vertexShader);
+    glDeleteShader(fragmentShader);
+
     glUseProgram(m_shaderProgram);
 
     // Obtain uniform locations
@@ -428,6 +432,18 @@ bool CGL33Device::Create()
 
 void CGL33Device::Destroy()
 {
+    glUseProgram(0);
+    glDeleteProgram(m_shaderProgram);
+
+    if (m_framebuffer != 0)
+    {
+        glDeleteFramebuffers(1, &m_framebuffer);
+        glDeleteRenderbuffers(1, &m_colorBuffer);
+        glDeleteRenderbuffers(1, &m_depthBuffer);
+
+        m_framebuffer = 0;
+    }
+
     // Delete the remaining textures
     // Should not be strictly necessary, but just in case
     DestroyAllTextures();
@@ -827,7 +843,7 @@ Texture CGL33Device::CreateDepthTexture(int width, int height, int depth)
     }
 
     glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, GL_DEPTH_COMPONENT, GL_INT, nullptr);
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
 
     float color[] = { 1.0f, 1.0f, 1.0f, 1.0f };
@@ -1767,6 +1783,8 @@ void CGL33Device::SetRenderState(RenderState state, bool enabled)
         if (m_framebuffer == 0)
             InitOffscreenBuffer(2048, 2048);
 
+        m_offscreenRenderingEnabled = true;
+
         GLuint toBind = (enabled ? m_framebuffer : 0);
 
         glBindFramebuffer(GL_FRAMEBUFFER, toBind);
@@ -1914,6 +1932,42 @@ void CGL33Device::InitOffscreenBuffer(int width, int height)
     GetLogger()->Info("Initialized offscreen buffer %dx%d\n", width, height);
 }
 
+void CGL33Device::SetRenderTexture(RenderTarget target, int texture)
+{
+    if (!m_offscreenRenderingEnabled) return;
+
+    GLenum attachment;
+    GLuint defaultBuffer;
+
+    switch (target)
+    {
+    case RENDER_TARGET_COLOR:
+        attachment = GL_COLOR_ATTACHMENT0;
+        defaultBuffer = m_colorBuffer;
+        break;
+    case RENDER_TARGET_DEPTH:
+        attachment = GL_DEPTH_ATTACHMENT;
+        defaultBuffer = m_depthBuffer;
+        break;
+    case RENDER_TARGET_STENCIL:
+        attachment = GL_STENCIL_ATTACHMENT;
+        defaultBuffer = 0;
+        break;
+    default: assert(false); break;
+    }
+
+    if (texture == 0)       // unbind texture and bind default buffer
+    {
+        glFramebufferTexture2DEXT(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, 0, 0);
+        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, defaultBuffer);
+    }
+    else            // unbind default buffer and bind texture
+    {
+        glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, 0);
+        glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, texture, 0);
+    }
+}
+
 void CGL33Device::CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height)
 {
     if (texture.id == 0) return;
diff --git a/src/graphics/opengl/gl33device.h b/src/graphics/opengl/gl33device.h
index 9c14e38..d83bf20 100644
--- a/src/graphics/opengl/gl33device.h
+++ b/src/graphics/opengl/gl33device.h
@@ -135,6 +135,8 @@ public:
 
     virtual void InitOffscreenBuffer(int width, int height) OVERRIDE;
 
+    virtual void SetRenderTexture(RenderTarget target, int texture) OVERRIDE;
+
     virtual void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) OVERRIDE;
 
     virtual void* GetFrameBufferPixels() const OVERRIDE;
@@ -220,6 +222,8 @@ private:
     GLuint m_depthBuffer;
     //! Maximum available renderbuffer size
     int m_maxRenderbufferSize;
+    //! true if offscreen rendering is enabled
+    bool m_offscreenRenderingEnabled;
 
     //! Shader program
     GLuint m_shaderProgram;
diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp
index f8fc2fa..811ae5b 100644
--- a/src/graphics/opengl/gldevice.cpp
+++ b/src/graphics/opengl/gldevice.cpp
@@ -54,6 +54,7 @@ CGLDevice::CGLDevice(const GLDeviceConfig &config)
     m_framebuffer = 0;
     m_colorBuffer = 0;
     m_depthBuffer = 0;
+    m_offscreenRenderingEnabled = false;
 }
 
 
@@ -1741,6 +1742,8 @@ void CGLDevice::SetRenderState(RenderState state, bool enabled)
             return;
         }
 
+        m_offscreenRenderingEnabled = enabled;
+
         if (m_framebuffer == 0)
             InitOffscreenBuffer(2048, 2048);
 
@@ -1878,6 +1881,43 @@ void CGLDevice::InitOffscreenBuffer(int width, int height)
     GetLogger()->Info("Initialized offscreen buffer %dx%d\n", width, height);
 }
 
+void CGLDevice::SetRenderTexture(RenderTarget target, int texture)
+{
+    if (!m_framebufferObject) return;
+    if (!m_offscreenRenderingEnabled) return;
+
+    GLenum attachment;
+    GLuint defaultBuffer;
+
+    switch (target)
+    {
+    case RENDER_TARGET_COLOR:
+        attachment = GL_COLOR_ATTACHMENT0_EXT;
+        defaultBuffer = m_colorBuffer;
+        break;
+    case RENDER_TARGET_DEPTH:
+        attachment = GL_DEPTH_ATTACHMENT_EXT;
+        defaultBuffer = m_depthBuffer;
+        break;
+    case RENDER_TARGET_STENCIL:
+        attachment = GL_STENCIL_ATTACHMENT_EXT;
+        defaultBuffer = 0;
+        break;
+    default: assert(false); break;
+    }
+
+    if (texture == 0)       // unbind texture and bind default buffer
+    {
+        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attachment, GL_TEXTURE_2D, 0, 0);
+        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attachment, GL_RENDERBUFFER_EXT, defaultBuffer);
+    }
+    else            // unbind default buffer and bind texture
+    {
+        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attachment, GL_RENDERBUFFER_EXT, 0);
+        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attachment, GL_TEXTURE_2D, texture, 0);
+    }
+}
+
 void CGLDevice::CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height)
 {
     if (texture.id == 0) return;
diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h
index d1f085a..2ffefdf 100644
--- a/src/graphics/opengl/gldevice.h
+++ b/src/graphics/opengl/gldevice.h
@@ -158,6 +158,8 @@ public:
 
     virtual void InitOffscreenBuffer(int width, int height) OVERRIDE;
 
+    virtual void SetRenderTexture(RenderTarget target, int texture) OVERRIDE;
+
     virtual void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) OVERRIDE;
 
     virtual void* GetFrameBufferPixels() const OVERRIDE;
@@ -253,6 +255,8 @@ private:
     GLuint m_depthBuffer;
     //! Maximum available renderbuffer size
     int m_maxRenderbufferSize;
+    //! true if offscreen rendering enabled
+    bool m_offscreenRenderingEnabled;
 };
 
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git



More information about the Pkg-games-commits mailing list