From 3b94500617d99bc7dfb53e0dda44bcff12431385 Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Sun, 28 Aug 2011 16:12:24 +0200 Subject: [PATCH] better error check (not tested) fob has a new ctor (from context and stuff) --- J.xcodeproj/project.pbxproj | 4 ++++ J/basic.cpp | 16 +++++++++++--- J/fbo.h | 24 +++++++++++++++------ J/fbo.mm | 43 +++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 J/fbo.mm diff --git a/J.xcodeproj/project.pbxproj b/J.xcodeproj/project.pbxproj index 85ec6d5..4f3813f 100644 --- a/J.xcodeproj/project.pbxproj +++ b/J.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 425E9DF2140A74DA00A81A65 /* basic.h in Headers */ = {isa = PBXBuildFile; fileRef = 425E9DF1140A74DA00A81A65 /* basic.h */; }; 425E9DF6140A774D00A81A65 /* basic.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 425E9DF5140A774D00A81A65 /* basic.cpp */; }; + 425E9DF9140A7EB400A81A65 /* fbo.mm in Sources */ = {isa = PBXBuildFile; fileRef = 425E9DF8140A7EB400A81A65 /* fbo.mm */; }; 42681369140A321800CBF943 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 42681368140A321800CBF943 /* Foundation.framework */; }; 4268136F140A321800CBF943 /* J.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4268136E140A321800CBF943 /* J.mm */; }; 42ED6A6B140A380000402F76 /* fbo.h in Headers */ = {isa = PBXBuildFile; fileRef = 42ED6A68140A380000402F76 /* fbo.h */; }; @@ -19,6 +20,7 @@ /* Begin PBXFileReference section */ 425E9DF1140A74DA00A81A65 /* basic.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = basic.h; sourceTree = ""; }; 425E9DF5140A774D00A81A65 /* basic.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = basic.cpp; sourceTree = ""; }; + 425E9DF8140A7EB400A81A65 /* fbo.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = fbo.mm; sourceTree = ""; }; 42681365140A321800CBF943 /* libJ.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libJ.a; sourceTree = BUILT_PRODUCTS_DIR; }; 42681368140A321800CBF943 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 4268136C140A321800CBF943 /* J-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "J-Prefix.pch"; sourceTree = ""; }; @@ -82,6 +84,7 @@ 425E9DF1140A74DA00A81A65 /* basic.h */, 425E9DF5140A774D00A81A65 /* basic.cpp */, 42ED6A68140A380000402F76 /* fbo.h */, + 425E9DF8140A7EB400A81A65 /* fbo.mm */, 42ED6A69140A380000402F76 /* shader.h */, 4268136D140A321800CBF943 /* J.h */, 4268136E140A321800CBF943 /* J.mm */, @@ -164,6 +167,7 @@ files = ( 4268136F140A321800CBF943 /* J.mm in Sources */, 425E9DF6140A774D00A81A65 /* basic.cpp in Sources */, + 425E9DF9140A7EB400A81A65 /* fbo.mm in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/J/basic.cpp b/J/basic.cpp index 18514d2..fd8f63f 100644 --- a/J/basic.cpp +++ b/J/basic.cpp @@ -12,9 +12,19 @@ namespace J { void check_error(){ #if defined(DEBUG) GLenum status = glGetError(); - if(status != GL_NO_ERROR){ - throw std::runtime_error("GL Error: " + std::to_string(status)); - } + switch (status) { + case GL_NO_ERROR: + return; + #define c(x) case x: throw std::runtime_error("GL Error: "#x); + c(GL_INVALID_ENUM) + c(GL_INVALID_VALUE) + c(GL_INVALID_OPERATION) + c(GL_INVALID_FRAMEBUFFER_OPERATION) + c(GL_OUT_OF_MEMORY) + #undef c + default: + throw std::runtime_error("GL Error: UNKNOWN"); + } #endif } } \ No newline at end of file diff --git a/J/fbo.h b/J/fbo.h index 6f9d661..a37a763 100644 --- a/J/fbo.h +++ b/J/fbo.h @@ -16,6 +16,10 @@ #include "basic.h" +@class EAGLContext; +@class CAEAGLLayer; + + // TODO: more error checking in debug build. // TODO: make texture class? for easier switching between linear/nearest interpolation for example // NOTE: stencil attachment is not supported @@ -32,6 +36,7 @@ public: GLuint texture_id; public: + // standard ctor, makes use of textures fbo(int width, int height) : width(width), height(height), fbo_number(0), renderbuffers(), texture_id(0) { glGenFramebuffers(1, &fbo_number); bind(); @@ -53,6 +58,10 @@ public: unbind(); } + + // ctor from EAGLContext, not using texture, so you cannot use it as lookup, only as renderer. + fbo(EAGLContext* context, CAEAGLLayer* layer); + ~fbo() { end(); @@ -60,17 +69,18 @@ public: for(RenderbuffersMap::const_iterator i = renderbuffers.begin(); i != renderbuffers.end(); ++i){ RenderbuffersMap::value_type const & it = *i; - glDeleteFramebuffers(1, &it.second); + glDeleteRenderbuffers(1, &it.second); } - - glDeleteTextures(1, &texture_id); + + glDeleteTextures(1, &texture_id); + // NOTE: glDeleteTextures silently ignores 0's and names that do not correspond to existing textures. } void begin(){ bind(); // FIXME: this should be done, but also switching back should be done... //glPushAttrib(GL_VIEWPORT); - //glViewport(0, 0, width, height); + glViewport(0, 0, width, height); // NOTE: commented out, because we don't want fbo's to nest. /*GLint savedFramebuffer = -1; @@ -80,11 +90,10 @@ public: void end(){ unbind(); - // FIXME: switch back to previous viewport. + // FIXME: switch back to previous viewport. See begin() //glPopAttrib(); } -private: void bind(){ glBindFramebuffer(GL_FRAMEBUFFER, fbo_number); check_error(); @@ -93,7 +102,8 @@ private: void unbind(){ glBindFramebuffer(GL_FRAMEBUFFER, 0); } - + +private: void create_attach_renderbuffer(GLenum format, GLenum attachment_point) { GLuint buffer; glGenRenderbuffers(1, &buffer); diff --git a/J/fbo.mm b/J/fbo.mm new file mode 100644 index 0000000..a33c8cd --- /dev/null +++ b/J/fbo.mm @@ -0,0 +1,43 @@ +// +// fbo.mm +// J +// +// Created by Joshua Moerman on 8/28/11. +// Copyright 2011 Vadovas. All rights reserved. +// + +#import "fbo.h" + +#import + +namespace J { + fbo::fbo(EAGLContext* context, CAEAGLLayer* layer) : width(0), height(0), fbo_number(0), renderbuffers(), texture_id(0){ + glGenFramebuffers(1, &fbo_number); + bind(); + + // much like create_attach_renderbuffer(), but it's not created, but given by the context. + { + GLuint buffer; + glGenRenderbuffers(1, &buffer); + glBindRenderbuffer(GL_RENDERBUFFER, buffer); + [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer]; + glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width); + glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height); + + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, buffer); + + renderbuffers[GL_COLOR_ATTACHMENT0] = buffer; + } + + //this will cause a INVALID_OPERATION in bind(). + //create_attach_renderbuffer(GL_DEPTH_COMPONENT16, GL_DEPTH_ATTACHMENT); + + check_error(); + check_status(); + + glClearColor(0.0, 0.0, 0.0, 0.0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + unbind(); + } +} \ No newline at end of file