aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Render/Canvas.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Render/Canvas.cpp')
-rw-r--r--src/libjin/Render/Canvas.cpp134
1 files changed, 134 insertions, 0 deletions
diff --git a/src/libjin/Render/Canvas.cpp b/src/libjin/Render/Canvas.cpp
new file mode 100644
index 0000000..8cb34ca
--- /dev/null
+++ b/src/libjin/Render/Canvas.cpp
@@ -0,0 +1,134 @@
+#include "../modules.h"
+#if JIN_MODULES_RENDER
+
+#include "../utils/macros.h"
+#include "canvas.h"
+#include "window.h"
+
+namespace jin
+{
+namespace render
+{
+
+ shared Canvas* Canvas::createCanvas(int w, int h)
+ {
+ return new Canvas(w, h);
+ }
+
+ Canvas::Canvas(int w, int h)
+ : Drawable(w, h)
+ {
+ init();
+ }
+
+ Canvas::~Canvas()
+ {
+ }
+
+ shared GLint Canvas::cur = -1;
+
+ bool Canvas::init()
+ {
+ setVertices(
+ new float [DRAWABLE_V_SIZE] {
+ 0, 0,
+ 0, (float)height,
+ (float)width, (float)height,
+ (float)width, 0,
+ },
+ new float [DRAWABLE_V_SIZE] {
+ 0, 1,
+ 0, 0,
+ 1, 0,
+ 1, 1
+ }
+ );
+
+ GLint current_fbo;
+ glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &current_fbo);
+
+ // generate a new render buffer object
+ glGenFramebuffers(1, &fbo);
+ glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+
+ // generate texture save target
+ glGenTextures(1, &texture);
+ glBindTexture(GL_TEXTURE_2D, texture);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+ glBindTexture(GL_TEXTURE_2D, 0);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
+
+ GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
+
+ // unbind framebuffer
+ glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
+
+ if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
+ return false;
+ return true;
+ }
+
+ bool Canvas::hasbind(GLint fbo)
+ {
+ return cur == fbo;
+ }
+
+ /**
+ * bind to canvas
+ */
+ void Canvas::bind()
+ {
+ if (hasbind(fbo)) return;
+
+ cur = fbo;
+
+ glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+
+ glMatrixMode(GL_PROJECTION);
+ glPushMatrix();
+ glLoadIdentity();
+
+ glViewport(0, 0, width, height);
+ glOrtho(0, width, height, 0, -1, 1);
+
+ // Switch back to modelview matrix
+ glMatrixMode(GL_MODELVIEW);
+ glPushMatrix();
+ glLoadIdentity();
+ }
+
+ /**
+ * bind to default screen render buffer.
+ */
+ shared void Canvas::unbind()
+ {
+ if (hasbind(0)) return;
+
+ cur = 0;
+
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ WindowSystem* wnd = WindowSystem::get();
+ int ww = wnd->getW(),
+ wh = wnd->getH();
+
+ glViewport(0, 0, ww, wh);
+
+ // load back to normal
+ glMatrixMode(GL_PROJECTION);
+ glPushMatrix();
+ glLoadIdentity();
+
+ // set viewport matrix
+ glOrtho(0, ww, wh, 0, -1, 1);
+
+ // switch to model matrix
+ glMatrixMode(GL_MODELVIEW);
+ glPushMatrix();
+ glLoadIdentity();
+ }
+
+}
+}
+#endif // JIN_MODULES_RENDER \ No newline at end of file