aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/Canvas.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Graphics/Canvas.cpp')
-rw-r--r--src/libjin/Graphics/Canvas.cpp59
1 files changed, 36 insertions, 23 deletions
diff --git a/src/libjin/Graphics/Canvas.cpp b/src/libjin/Graphics/Canvas.cpp
index 4def58d..efcd12d 100644
--- a/src/libjin/Graphics/Canvas.cpp
+++ b/src/libjin/Graphics/Canvas.cpp
@@ -10,13 +10,19 @@ namespace jin
namespace graphics
{
- /*class member*/ GLint Canvas::cur = -1;
+ /*class member*/ const Canvas* Canvas::current = nullptr;
+ /*class member*/ const Canvas* const Canvas::DEFAULT_CANVAS = new Canvas(0);
/*class member*/ Canvas* Canvas::createCanvas(int w, int h)
{
return new Canvas(w, h);
}
+ Canvas::Canvas(GLuint n)
+ : fbo(n)
+ {
+ }
+
Canvas::Canvas(int w, int h)
: Drawable(w, h)
{
@@ -56,63 +62,70 @@ namespace graphics
{
}
- bool Canvas::hasbind(GLint fbo)
+ /*class member*/ bool Canvas::isBinded(const Canvas* cvs)
{
- return cur == fbo;
+ return current == cvs;
}
/**
* bind to canvas
*/
- void Canvas::bind()
+ /*class member*/ void Canvas::bind(Canvas* canvas)
{
- if (hasbind(fbo)) return;
-
- cur = fbo;
+ if (isBinded(canvas)) return;
+ current = canvas;
+ glBindFramebuffer(GL_FRAMEBUFFER, canvas->fbo);
- glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+ int w = canvas->size.w;
+ int h = canvas->size.h;
+ /* set view port to canvas */
+ glViewport(0, 0, w, h);
+ /* set projection matrix */
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
+ glOrtho(0, w, h, 0, -1, 1);
- glViewport(0, 0, size.w, size.h);
- glOrtho(0, size.w, size.h, 0, -1, 1);
-
- /* Switch back to modelview matrix */
+ /* set (model*view) matrix */
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
+
+ /* ready to draw */
}
/**
* bind to default screen render buffer.
+ * do some coordinates transform work
+ * https://blog.csdn.net/liji_digital/article/details/79370841
+ * https://blog.csdn.net/lyx2007825/article/details/8792475
*/
/*class member*/ void Canvas::unbind()
{
- if (hasbind(0)) return;
-
- cur = 0;
-
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ if (isBinded(DEFAULT_CANVAS)) return;
+ current = DEFAULT_CANVAS;
+ /* get window size as viewport */
Window* wnd = Window::get();
int ww = wnd->getW();
int wh = wnd->getH();
- glViewport(0, 0, ww, wh);
+ glBindFramebuffer(GL_FRAMEBUFFER, DEFAULT_CANVAS->fbo);
- /* load back to normal */
+ glViewport(0, 0, ww, wh);
+
+ /* set projection matrix */
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
-
- /* set viewport matrix */
glOrtho(0, ww, wh, 0, -1, 1);
-
- /* switch to model matrix */
+
+ /* set (model*view) matrix */
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
+
+ /* ready to draw */
}
} // render