Ver Fonte

Allow rendering to floating point depth buffer

Jim Duchek há 10 anos atrás
pai
commit
d30407948b

+ 20 - 8
gameplay/src/FrameBuffer.cpp

@@ -76,13 +76,13 @@ FrameBuffer* FrameBuffer::create(const char* id)
     return create(id, 0, 0);
 }
 
-FrameBuffer* FrameBuffer::create(const char* id, unsigned int width, unsigned int height)
+FrameBuffer* FrameBuffer::create(const char* id, unsigned int width, unsigned int height, Texture::Format format)
 {
     RenderTarget* renderTarget = NULL;
     if (width > 0 && height > 0)
     {
         // Create a default RenderTarget with same ID.
-        renderTarget = RenderTarget::create(id, width, height);
+        renderTarget = RenderTarget::create(id, width, height, format);
         if (renderTarget == NULL)
         {
             GP_ERROR("Failed to create render target for frame buffer.");
@@ -195,8 +195,20 @@ void FrameBuffer::setRenderTarget(RenderTarget* target, unsigned int index, GLen
 
         // Now set this target as the color attachment corresponding to index.
         GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
-        GLenum attachment = GL_COLOR_ATTACHMENT0 + index;
-        GL_ASSERT( glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, textureTarget, _renderTargets[index]->getTexture()->getHandle(), 0) );
+        GLenum attachment;
+        if (target->getTexture()->getFormat() == Texture::DEPTH)
+        {
+        	attachment = GL_DEPTH_ATTACHMENT;
+        	GL_ASSERT( glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, textureTarget, _renderTargets[index]->getTexture()->getHandle(), 0));
+        	glDrawBuffer(GL_NONE);
+        	glReadBuffer(GL_NONE);
+        }
+        else
+        {
+            attachment = GL_COLOR_ATTACHMENT0 + index;
+        	GL_ASSERT( glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, textureTarget, _renderTargets[index]->getTexture()->getHandle(), 0) );
+        }
+
         GLenum fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
         if (fboStatus != GL_FRAMEBUFFER_COMPLETE)
         {
@@ -274,9 +286,9 @@ bool FrameBuffer::isDefault() const
     return (this == _defaultFrameBuffer);
 }
 
-FrameBuffer* FrameBuffer::bind()
+FrameBuffer* FrameBuffer::bind(GLenum type)
 {
-    GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _handle) );
+    GL_ASSERT( glBindFramebuffer(type, _handle) );
     FrameBuffer* previousFrameBuffer = _currentFrameBuffer;
     _currentFrameBuffer = this;
     return previousFrameBuffer;
@@ -303,9 +315,9 @@ Image* FrameBuffer::createScreenshot(Image::Format format)
     return screenshot;
 }
 
-FrameBuffer* FrameBuffer::bindDefault()
+FrameBuffer* FrameBuffer::bindDefault(GLenum type)
 {
-    GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, _defaultFrameBuffer->_handle) );
+    GL_ASSERT( glBindFramebuffer(type, _defaultFrameBuffer->_handle) );
     _currentFrameBuffer = _defaultFrameBuffer;
     return _defaultFrameBuffer;
 }

+ 3 - 3
gameplay/src/FrameBuffer.h

@@ -59,7 +59,7 @@ public:
      * @return A newly created FrameBuffer.
      * @script{create}
      */
-    static FrameBuffer* create(const char* id, unsigned int width, unsigned int height);
+    static FrameBuffer* create(const char* id, unsigned int width, unsigned int height, Texture::Format format = Texture::RGBA);
 
     /**
      * Get a named FrameBuffer from its ID.
@@ -159,7 +159,7 @@ public:
      *
      * @ return The currently bound framebuffer.
      */
-    FrameBuffer* bind();
+    FrameBuffer* bind(GLenum type = GL_FRAMEBUFFER);
 
     /**
      * Records a screenshot of what is stored on the current FrameBuffer.
@@ -183,7 +183,7 @@ public:
      *
      * @ return The default framebuffer.
      */
-    static FrameBuffer* bindDefault();
+    static FrameBuffer* bindDefault(GLenum type = GL_FRAMEBUFFER);
 
     /**
      * Gets the currently bound FrameBuffer.

+ 2 - 2
gameplay/src/RenderTarget.cpp

@@ -23,10 +23,10 @@ RenderTarget::~RenderTarget()
     }
 }
 
-RenderTarget* RenderTarget::create(const char* id, unsigned int width, unsigned int height)
+RenderTarget* RenderTarget::create(const char* id, unsigned int width, unsigned int height, Texture::Format format)
 {
     // Create a new texture with the given width.
-    Texture* texture = Texture::create(Texture::RGBA, width, height, NULL, false);
+    Texture* texture = Texture::create(format, width, height, NULL, false);
     if (texture == NULL)
     {
         GP_ERROR("Failed to create texture for render target.");

+ 1 - 1
gameplay/src/RenderTarget.h

@@ -29,7 +29,7 @@ public:
      * @return A newly created RenderTarget.
      * @script{create}
      */
-    static RenderTarget* create(const char* id, unsigned int width, unsigned int height);
+    static RenderTarget* create(const char* id, unsigned int width, unsigned int height, Texture::Format format = Texture::RGBA);
 
     /**
      * Create a RenderTarget from the given Texture and add it to the list of

+ 24 - 5
gameplay/src/Texture.cpp

@@ -183,7 +183,14 @@ Texture* Texture::create(Format format, unsigned int width, unsigned int height,
     if (type == Texture::TEXTURE_2D)
     {
         // Texture 2D
-        GL_ASSERT( glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, width, height, 0, (GLenum)format, GL_UNSIGNED_BYTE, data) );
+        if (format == Texture::DEPTH)
+        {
+    		GL_ASSERT( glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, data) );
+        }
+        else
+        {
+    		GL_ASSERT( glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, width, height, 0, (GLenum)format, GL_UNSIGNED_BYTE, data) );
+    	}
     }
     else
     {
@@ -217,8 +224,21 @@ Texture* Texture::create(Format format, unsigned int width, unsigned int height,
     }
 
     // Set initial minification filter based on whether or not mipmaping was enabled.
-    Filter minFilter = generateMipmaps ? NEAREST_MIPMAP_LINEAR : LINEAR;
-    GL_ASSERT( glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter) );
+    Filter minFilter;
+    if (format == Texture::DEPTH)
+    {
+    	minFilter = NEAREST;
+    	GL_ASSERT( glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST) );
+    	GL_ASSERT( glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST) );
+    	GL_ASSERT( glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) );
+    	GL_ASSERT( glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) );
+    	GL_ASSERT( glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_NONE) );
+    }
+    else
+    {
+    	minFilter = generateMipmaps ? NEAREST_MIPMAP_LINEAR : LINEAR;
+    	GL_ASSERT( glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter) );
+    }
 
     Texture* texture = new Texture();
     texture->_handle = textureId;
@@ -228,9 +248,8 @@ Texture* Texture::create(Format format, unsigned int width, unsigned int height,
     texture->_height = height;
     texture->_minFilter = minFilter;
     if (generateMipmaps)
-    {
         texture->generateMipmaps();
-    }
+
 
     // Restore the texture id
     GL_ASSERT( glBindTexture((GLenum)__currentTextureType, __currentTextureId) );

+ 2 - 1
gameplay/src/Texture.h

@@ -26,7 +26,8 @@ public:
         UNKNOWN = 0,
         RGB     = GL_RGB,
         RGBA    = GL_RGBA,
-        ALPHA   = GL_ALPHA
+        ALPHA   = GL_ALPHA,
+        DEPTH	= GL_DEPTH_COMPONENT,
     };
 
     /**