FBO with GLObject

This commit is contained in:
gazer 2015-12-12 01:24:39 +03:00
parent 3f2b14f4e3
commit 9896492018
1 changed files with 26 additions and 45 deletions

View File

@ -737,7 +737,7 @@ class GLSupport {
float y1 = cast(float)(bufferDy-p2.y); float y1 = cast(float)(bufferDy-p2.y);
// don't flip for framebuffer // don't flip for framebuffer
if (currentFramebufferId) { if (currentFBO) {
y0 = cast(float)(p1.y); y0 = cast(float)(p1.y);
y1 = cast(float)(p2.y); y1 = cast(float)(p2.y);
} }
@ -768,7 +768,7 @@ class GLSupport {
float y1 = cast(float)(bufferDy-rc.bottom); float y1 = cast(float)(bufferDy-rc.bottom);
// don't flip for framebuffer // don't flip for framebuffer
if (currentFramebufferId) { if (currentFBO) {
y0 = cast(float)(rc.top); y0 = cast(float)(rc.top);
y1 = cast(float)(rc.bottom); y1 = cast(float)(rc.bottom);
} }
@ -827,7 +827,7 @@ class GLSupport {
float dsty1 = cast(float)(bufferDy - (yy + dy)); float dsty1 = cast(float)(bufferDy - (yy + dy));
// don't flip for framebuffer // don't flip for framebuffer
if (currentFramebufferId) { if (currentFBO) {
dsty0 = cast(float)((yy)); dsty0 = cast(float)((yy));
dsty1 = cast(float)((yy + dy)); dsty1 = cast(float)((yy + dy));
} }
@ -908,7 +908,7 @@ class GLSupport {
float dsty1 = cast(float)(bufferDy - (yy + dy)); float dsty1 = cast(float)(bufferDy - (yy + dy));
// don't flip for framebuffer // don't flip for framebuffer
if (currentFramebufferId) { if (currentFBO) {
dsty0 = cast(float)((yy)); dsty0 = cast(float)((yy));
dsty1 = cast(float)((yy + dy)); dsty1 = cast(float)((yy + dy));
} }
@ -1067,37 +1067,27 @@ class GLSupport {
return true; return true;
} }
private uint currentFramebufferId; private FBO currentFBO;
/// returns texture ID for buffer, 0 if failed /// returns texture for buffer, null if failed
bool createFramebuffer(out uint textureId, out uint framebufferId, int dx, int dy) { bool createFramebuffer(out Tex2D texture, out FBO fbo, int dx, int dy) {
checkError("before createFramebuffer"); checkError("before createFramebuffer");
bool res = true; bool res = true;
textureId = genTexture(); texture = new Tex2D();
if (!textureId) if (!texture.ID)
return false; return false;
GLuint fid = 0; checkError("glBindTexture GL_TEXTURE_2D");
glGenFramebuffers(1, &fid); FBO f = new FBO();
if (checkError("createFramebuffer glGenFramebuffersOES")) return false; if (!f.ID)
framebufferId = fid; return false;
glBindFramebuffer(GL_FRAMEBUFFER, framebufferId); fbo = f;
if (checkError("createFramebuffer glBindFramebuffer")) return false;
glBindTexture(GL_TEXTURE_2D, textureId);
checkError("glBindTexture(GL_TEXTURE_2D, _textureId)");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, dx, dy, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, null); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, dx, dy, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, null);
checkError("glTexImage2D"); checkError("glTexImage2D");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); texture.setSamplerParams(true, true);
checkError("texParameter");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
checkError("texParameter");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
checkError("texParameter");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
checkError("texParameter");
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.ID, 0);
checkError("glFramebufferTexture2D"); checkError("glFramebufferTexture2D");
// Always check that our framebuffer is ok // Always check that our framebuffer is ok
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
@ -1112,35 +1102,26 @@ class GLSupport {
checkError("glClear"); checkError("glClear");
checkError("after createFramebuffer"); checkError("after createFramebuffer");
//CRLog::trace("CRGLSupportImpl::createFramebuffer %d,%d texture=%d, buffer=%d", dx, dy, textureId, framebufferId); //CRLog::trace("CRGLSupportImpl::createFramebuffer %d,%d texture=%d, buffer=%d", dx, dy, textureId, framebufferId);
currentFramebufferId = framebufferId; currentFBO = fbo;
glBindTexture(GL_TEXTURE_2D, 0); texture.unbind();
checkError("createFramebuffer - glBindTexture(0)"); fbo.unbind();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
checkError("createFramebuffer - glBindFramebuffer(0)");
return res; return res;
} }
void deleteFramebuffer(ref uint framebufferId) { void deleteFramebuffer(ref FBO fbo) {
//CRLog::debug("GLDrawBuf::deleteFramebuffer"); //CRLog::debug("GLDrawBuf::deleteFramebuffer");
if (framebufferId != 0) { if (fbo.ID != 0) {
glBindFramebuffer(GL_FRAMEBUFFER, 0); destroy(fbo);
checkError("deleteFramebuffer - glBindFramebuffer");
GLuint fid = framebufferId;
glDeleteFramebuffers(1, &fid);
checkError("deleteFramebuffer - glDeleteFramebuffer");
} }
//CRLog::trace("CRGLSupportImpl::deleteFramebuffer(%d)", framebufferId); currentFBO = null;
framebufferId = 0;
checkError("after deleteFramebuffer");
currentFramebufferId = 0;
} }
bool bindFramebuffer(uint framebufferId) { bool bindFramebuffer(FBO fbo) {
//CRLog::trace("CRGLSupportImpl::bindFramebuffer(%d)", framebufferId); //CRLog::trace("CRGLSupportImpl::bindFramebuffer(%d)", framebufferId);
glBindFramebuffer(GL_FRAMEBUFFER, framebufferId); fbo.bind();
currentFramebufferId = framebufferId; currentFBO = fbo;
return !checkError("glBindFramebuffer"); return !checkError("glBindFramebuffer");
} }