144 lines
4.5 KiB
C++
144 lines
4.5 KiB
C++
/* ================================================================
|
|
RenderView
|
|
----------------
|
|
This file defines our RenderView object.
|
|
================================================================ */
|
|
#include "RenderView.hpp"
|
|
#include "Log.hpp"
|
|
/* ======== Constructors and Destructors ======== */
|
|
RenderView::RenderView(int width, int height) {
|
|
fbo = fbo_depth = fbo_tex = 0;
|
|
w = width;
|
|
h = height;
|
|
x = y = 0;
|
|
flags = 0;
|
|
mesh = NULL;
|
|
program = NULL;
|
|
createView(width, height);
|
|
}
|
|
RenderView::RenderView(GLuint fbo_, int width, int height) {
|
|
fbo = fbo_;
|
|
w = width;
|
|
h = height;
|
|
x = y = 0;
|
|
flags = STATIC_FBO;
|
|
flags |= ACTIVE;
|
|
mesh = NULL;
|
|
program = NULL;
|
|
}
|
|
RenderView::~RenderView() {
|
|
destroyView();
|
|
if (mesh != NULL) delete mesh;
|
|
}
|
|
/* ======== Getters ======== */
|
|
GLuint RenderView::getFBO() {
|
|
return fbo;
|
|
}
|
|
GLuint RenderView::getTex() {
|
|
return fbo_tex;
|
|
}
|
|
Mesh *RenderView::getMesh() {
|
|
return mesh;
|
|
}
|
|
Program* RenderView::getProgram() {
|
|
return program;
|
|
}
|
|
int RenderView::getWidth() {
|
|
return w;
|
|
}
|
|
int RenderView::getHeight() {
|
|
return h;
|
|
}
|
|
/* ======== Setters ======== */
|
|
int RenderView::createView(int width, int height) {
|
|
// TODO: call destroyView() and create new FBO at width and height
|
|
// ================ create depth buffer
|
|
glGenRenderbuffers(1, &fbo_depth);
|
|
glBindRenderbuffer(GL_RENDERBUFFER, fbo_depth);
|
|
#if defined(HAVE_OPENGLES)
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
|
|
#else
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
|
|
#endif
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo_depth);
|
|
// unbind the render buffer
|
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
|
// ================ create texture
|
|
glGenTextures(1, &fbo_tex);
|
|
glBindTexture(GL_TEXTURE_2D, fbo_tex);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
|
|
// add some filters (... move this)
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
// unbind texture
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
// ================ create framebuffers
|
|
glGenFramebuffers(1, &fbo);
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
|
// attach texture to framebuffer
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo_tex, 0);
|
|
// attach depth render buffer
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo_depth);
|
|
GLenum status;
|
|
if ((status = glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) {
|
|
GLenum error = glGetError();
|
|
LOG(LOG_ERROR) << FUNC_NAME << " " << status << " " << error << " " << "Failed to create Framebuffer!";
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
return 1;
|
|
}
|
|
flags |= ACTIVE;
|
|
// unbind our frame buffer
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
// ================ create quad mesh
|
|
// Load our default quad mesh
|
|
Vec3 quad[6] = {
|
|
Vec3(-1.0f, -1.0f, 0.0f),
|
|
Vec3(1.0f, -1.0f, 0.0f),
|
|
Vec3(-1.0f, 1.0f, 0.0f),
|
|
Vec3(-1.0f, 1.0f, 0.0f),
|
|
Vec3(1.0f, -1.0f, 0.0f),
|
|
Vec3(1.0f, 1.0f, 0.0f)
|
|
};
|
|
mesh = new Mesh(quad, 6, NULL, 0, NULL, 0);
|
|
mesh->buildMesh();
|
|
w = width; h = height;
|
|
return 0;
|
|
}
|
|
int RenderView::destroyView() {
|
|
if ((flags & ACTIVE) == 0) return 1;
|
|
flags &= ~ACTIVE;
|
|
if ((flags & STATIC_FBO)) return 2;
|
|
glDeleteFramebuffers(1, &fbo);
|
|
glDeleteTextures(1, &fbo_tex);
|
|
glDeleteRenderbuffers(1, &fbo_depth);
|
|
delete mesh;
|
|
mesh = NULL;
|
|
return 0;
|
|
}
|
|
int RenderView::setView(int width, int height) {
|
|
if ((flags & ACTIVE) == 0) return 1;
|
|
w = width;
|
|
h = height;
|
|
if ((flags & STATIC_FBO)) return 2;
|
|
// resize depth buffer
|
|
glBindRenderbuffer(GL_RENDERBUFFER, fbo_depth);
|
|
#if defined(HAVE_OPENGLES)
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
|
|
#else
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
|
|
#endif
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo_depth);
|
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
|
// resize texture
|
|
glBindTexture(GL_TEXTURE_2D, fbo_tex);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
return 0;
|
|
}
|
|
int RenderView::setProgram(Program *program_) {
|
|
program = program_;
|
|
return 0;
|
|
}
|