diff options
author | chai <chaifix@163.com> | 2018-09-09 21:05:28 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-09-09 21:05:28 +0800 |
commit | 6decddfd8470b44609e8c3aa144380f198b7b54c (patch) | |
tree | 619de00d3d7ba058c2597c55c6ae570cc018c2cf /src | |
parent | 4cae436252d108f9bffe30009b754df00137bb1c (diff) |
*update
Diffstat (limited to 'src')
-rw-r--r-- | src/libjin/Graphics/Canvas.cpp | 59 | ||||
-rw-r--r-- | src/libjin/Graphics/Canvas.h | 10 | ||||
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 2 |
3 files changed, 44 insertions, 27 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 diff --git a/src/libjin/Graphics/Canvas.h b/src/libjin/Graphics/Canvas.h index 2522d32..a6a52ea 100644 --- a/src/libjin/Graphics/Canvas.h +++ b/src/libjin/Graphics/Canvas.h @@ -13,17 +13,21 @@ namespace graphics { public: static Canvas* createCanvas(int w, int h); + + static void bind(Canvas*); static void unbind(); - static bool hasbind(GLint fbo); + static bool isBinded(const Canvas*); ~Canvas(); - void bind(); protected: + static const Canvas* const DEFAULT_CANVAS; + static const Canvas* current; + Canvas(int w, int h); + Canvas(GLuint n); GLuint fbo; - static GLint cur; }; diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index d662cc7..92bc7d5 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -280,7 +280,7 @@ namespace lua } Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); Ref<Canvas>& ref = proxy->getRef<Canvas>(); - ref->bind(); + Canvas::bind(ref.getObject()); return 0; } |