Created
July 12, 2012 11:22
-
-
Save gazliddon/3097550 to your computer and use it in GitHub Desktop.
Offscreen buffer stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* offscrbuffer.cpp | |
* gazotron | |
* | |
* Created by Gary Liddon on 23/08/2006. | |
* Copyright 2006 __MyCompanyName__. All rights reserved. | |
* | |
*/ | |
#include "cOffScreenBuffer.h" | |
#include <stdio.h> | |
#include <glib/include/gdebug.h> | |
namespace core | |
{ | |
cOffScreenBuffer::cOffScreenBuffer(int _width, int _height, bool _hasZ) | |
{ | |
// This routine creates a frame buffer object (FBO) | |
// and then binds a texture to it as a logical buffer for drawing to | |
// and if there's a Z buffer required | |
// creates a render buffer and then attaches it to the FBO as a depth | |
// buffer | |
glGenTextures(1, &mColorTex); | |
// Get an unused FB object | |
glGenFramebuffersEXT(1, &mFb); | |
/* Select this frame buffer as the current context */ | |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFb); | |
/* Now create a texture for the buffer to work with */ | |
glBindTexture(GL_TEXTURE_2D, mColorTex); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); | |
glTexImage2D(GL_TEXTURE_2D, 0, 4, _width, _height, 0, | |
GL_RGBA, GL_FLOAT, NULL); | |
/* Make this texture the logical drawing buffer for the current context */ | |
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, | |
GL_COLOR_ATTACHMENT0_EXT, | |
GL_TEXTURE_2D, mColorTex, 0); | |
// Create a render buffer for the depth buffer and the attach it | |
// to the current context if we want one | |
if (_hasZ) | |
{ | |
glGenRenderbuffersEXT(1, &mDepthRb); | |
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthRb); | |
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, | |
GL_DEPTH_COMPONENT24, _width, _height); | |
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, | |
GL_DEPTH_ATTACHMENT_EXT, | |
GL_RENDERBUFFER_EXT, mDepthRb); | |
} | |
// Check framebuffer completeness at the end of initialization. | |
GLenum status; | |
status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); | |
switch(status) | |
{ | |
case GL_FRAMEBUFFER_COMPLETE_EXT: | |
break; | |
case GL_FRAMEBUFFER_UNSUPPORTED_EXT: | |
ASSERT(0); | |
break; | |
default: | |
/* programming error; will fail on all hardware */ | |
ASSERT(0); | |
break; | |
} | |
mWidth = _width; | |
mHeight = _height; | |
useBuffer(); | |
glClearColor(0,0,0,1); | |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | |
discardBuffer(); | |
} | |
GLuint cOffScreenBuffer::texture(void) | |
{ | |
return (mColorTex); | |
} | |
void cOffScreenBuffer::useBuffer(void) | |
{ | |
glViewport(0,0,mWidth, mHeight); | |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFb); | |
} | |
void cOffScreenBuffer::discardBuffer() | |
{ | |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* offscrbuffer.h | |
* gazotron | |
* | |
* Created by Gary Liddon on 23/08/2006. | |
* Copyright 2006 __MyCompanyName__. All rights reserved. | |
* | |
*/ | |
#ifndef __OFFSCRBUFFER_H__ | |
#define __OFFSCRBUFFER_H__ | |
#include <core/glutils.h> | |
#include <core/render/cTexture.h> | |
namespace core | |
{ | |
class cOffScreenBuffer | |
{ | |
public: | |
cOffScreenBuffer(int _width, int _height, bool _hasZ = false); | |
virtual ~cOffScreenBuffer() | |
{ | |
} | |
GLuint texture(void); | |
virtual void useBuffer(); | |
void discardBuffer(); | |
unsigned int getWidth(void) const | |
{ return (mWidth); } | |
unsigned int getHeight(void) const | |
{ return (mHeight); } | |
GLuint getFrameBufferObject(void) const | |
{ | |
return mFb; | |
} | |
cTexture getcTexture(void) const | |
{ | |
return (cTexture(mColorTex, mWidth, mHeight)); | |
} | |
protected: | |
GLuint mFb; | |
GLuint mDepthRb; | |
GLuint mColorTex; | |
unsigned int mWidth, mHeight; | |
}; | |
} | |
#endif /* __OFFSCRBUFFER_H__ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment