+ GLuint type;
+ if (framebuffer_format == GL_RGBA8) {
+ type = GL_UNSIGNED_BYTE;
+ } else if (framebuffer_format == GL_RGBA16F || framebuffer_format == GL_RGBA32F) {
+ type = GL_FLOAT;
+ } else {
+ // Add more here as needed.
+ assert(false);
+ }
+
+ unsigned num_outputs;
+ if (out_data3 != NULL) {
+ num_outputs = 3;
+ } else if (out_data2 != NULL) {
+ num_outputs = 2;
+ } else {
+ num_outputs = 1;
+ }
+
+ GLuint fbo, texnum[3];
+
+ glGenTextures(num_outputs, texnum);
+ check_error();
+ for (unsigned i = 0; i < num_outputs; ++i) {
+ glBindTexture(GL_TEXTURE_2D, texnum[i]);
+ check_error();
+ glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, type, NULL);
+ check_error();
+ }
+
+ glGenFramebuffers(1, &fbo);
+ check_error();
+ glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+ check_error();
+ for (unsigned i = 0; i < num_outputs; ++i) {
+ glFramebufferTexture2D(
+ GL_FRAMEBUFFER,
+ GL_COLOR_ATTACHMENT0 + i,
+ GL_TEXTURE_2D,
+ texnum[i],
+ 0);
+ check_error();
+ }
+
+ GLenum bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
+ glDrawBuffers(num_outputs, bufs);
+