Browse Source

better error check (not tested)

fob has a new ctor (from context and stuff)
master
Joshua Moerman 14 years ago
parent
commit
3b94500617
  1. 4
      J.xcodeproj/project.pbxproj
  2. 14
      J/basic.cpp
  3. 18
      J/fbo.h
  4. 43
      J/fbo.mm

4
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 = "<group>"; };
425E9DF5140A774D00A81A65 /* basic.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = basic.cpp; sourceTree = "<group>"; };
425E9DF8140A7EB400A81A65 /* fbo.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = fbo.mm; sourceTree = "<group>"; };
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 = "<group>"; };
@ -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;
};

14
J/basic.cpp

@ -12,8 +12,18 @@ 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
}

18
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);
// 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();
@ -94,6 +103,7 @@ private:
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
private:
void create_attach_renderbuffer(GLenum format, GLenum attachment_point) {
GLuint buffer;
glGenRenderbuffers(1, &buffer);

43
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 <QuartzCore/QuartzCore.h>
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();
}
}