diff options
Diffstat (limited to 'src/libjin/graphics')
-rw-r--r-- | src/libjin/graphics/animations/je_animation.h | 4 | ||||
-rw-r--r-- | src/libjin/graphics/animations/je_animator.h | 3 | ||||
-rw-r--r-- | src/libjin/graphics/fonts/je_decoder.h | 4 | ||||
-rw-r--r-- | src/libjin/graphics/fonts/je_font.h | 2 | ||||
-rw-r--r-- | src/libjin/graphics/fonts/je_text.h | 4 | ||||
-rw-r--r-- | src/libjin/graphics/fonts/je_ttf.h | 4 | ||||
-rw-r--r-- | src/libjin/graphics/je_bitmap.h | 2 | ||||
-rw-r--r-- | src/libjin/graphics/je_gl.cpp | 110 | ||||
-rw-r--r-- | src/libjin/graphics/je_gl.h | 127 | ||||
-rw-r--r-- | src/libjin/graphics/je_graphic.h | 3 | ||||
-rw-r--r-- | src/libjin/graphics/je_image.h | 3 | ||||
-rw-r--r-- | src/libjin/graphics/je_mesh.h | 1 | ||||
-rw-r--r-- | src/libjin/graphics/je_sprite.h | 2 | ||||
-rw-r--r-- | src/libjin/graphics/je_sprite_batch.h | 4 | ||||
-rw-r--r-- | src/libjin/graphics/je_window.cpp | 9 |
15 files changed, 254 insertions, 28 deletions
diff --git a/src/libjin/graphics/animations/je_animation.h b/src/libjin/graphics/animations/je_animation.h index 4037721..fd22049 100644 --- a/src/libjin/graphics/animations/je_animation.h +++ b/src/libjin/graphics/animations/je_animation.h @@ -4,6 +4,8 @@ #include <vector> #include <string> +#include "../../common/je_object.h" + #include "../je_sprite.h" namespace JinEngine @@ -16,7 +18,7 @@ namespace JinEngine /// /// Animation clip with key. /// - class Animation + class Animation : public Object { public: Animation(); diff --git a/src/libjin/graphics/animations/je_animator.h b/src/libjin/graphics/animations/je_animator.h index d3fdbae..bee3d7d 100644 --- a/src/libjin/graphics/animations/je_animator.h +++ b/src/libjin/graphics/animations/je_animator.h @@ -3,6 +3,7 @@ #include <string> +#include "../../common/je_object.h" #include "../../utils/je_log.h" #include "je_animation.h" @@ -14,7 +15,7 @@ namespace JinEngine namespace Animations { - class Animator : public IRenderable + class Animator : public Object, public IRenderable { public: Animator(); diff --git a/src/libjin/graphics/fonts/je_decoder.h b/src/libjin/graphics/fonts/je_decoder.h index 840cada..0c785af 100644 --- a/src/libjin/graphics/fonts/je_decoder.h +++ b/src/libjin/graphics/fonts/je_decoder.h @@ -3,6 +3,8 @@ #include <vector> +#include "../../common/je_object.h" + #include "je_text.h" namespace JinEngine @@ -15,7 +17,7 @@ namespace JinEngine /// /// Text decoder. /// - class Decoder + class Decoder : public Object { public: diff --git a/src/libjin/graphics/fonts/je_font.h b/src/libjin/graphics/fonts/je_font.h index e72ef6b..3f72a13 100644 --- a/src/libjin/graphics/fonts/je_font.h +++ b/src/libjin/graphics/fonts/je_font.h @@ -25,7 +25,7 @@ namespace JinEngine /// /// Base Font class. /// - class Font : public IRenderable + class Font : public Object, public IRenderable { public: /// diff --git a/src/libjin/graphics/fonts/je_text.h b/src/libjin/graphics/fonts/je_text.h index 6e6f8b0..319ee4d 100644 --- a/src/libjin/graphics/fonts/je_text.h +++ b/src/libjin/graphics/fonts/je_text.h @@ -3,6 +3,8 @@ #include <vector> +#include "../../common/je_object.h" + namespace JinEngine { namespace Graphics @@ -30,7 +32,7 @@ namespace JinEngine /// /// Decoded text. Saved as unicode codepoints. /// - class Text + class Text : public Object { public: /// diff --git a/src/libjin/graphics/fonts/je_ttf.h b/src/libjin/graphics/fonts/je_ttf.h index 28260f6..c2766b4 100644 --- a/src/libjin/graphics/fonts/je_ttf.h +++ b/src/libjin/graphics/fonts/je_ttf.h @@ -34,7 +34,7 @@ namespace JinEngine // . // . // - class TTFData + class TTFData : public Object { public: @@ -289,4 +289,4 @@ namespace JinEngine #endif // defined(jin_graphics) -#endif // __JE_FONT_H__ +#endif // __JE_FONT_H__
\ No newline at end of file diff --git a/src/libjin/graphics/je_bitmap.h b/src/libjin/graphics/je_bitmap.h index 5ab11ca..e9c214e 100644 --- a/src/libjin/graphics/je_bitmap.h +++ b/src/libjin/graphics/je_bitmap.h @@ -24,7 +24,7 @@ namespace JinEngine /// texture is a renderable hard ware side structure which could be handled with GPU. For instance, opengl /// create texture and store it in GPU memory for rendering them onto hdc. /// - class Bitmap + class Bitmap : public Object { public: /// diff --git a/src/libjin/graphics/je_gl.cpp b/src/libjin/graphics/je_gl.cpp index c58f0ac..68678e2 100644 --- a/src/libjin/graphics/je_gl.cpp +++ b/src/libjin/graphics/je_gl.cpp @@ -12,14 +12,80 @@ namespace JinEngine OpenGL gl; OpenGL::OpenGL() - : ogl2d::OpenGL() + : mBlendMode(BlendMode::NONE) { + memset(&mColor, 0xff, sizeof(mColor)); + memset(&mPrecolor, 0xff, sizeof(mPrecolor)); + // Set default modelview matrix. mModelViewMatrices.push_back(Matrix()); mModelViewMatrix.setIdentity(); for (Matrix& m : mModelViewMatrices) mModelViewMatrix *= m; } + OpenGL::~OpenGL() + { + } + + void OpenGL::pushColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a) + { + memcpy(&mPrecolor, &mColor, sizeof(mPrecolor)); + mColor.r = r; + mColor.g = g; + mColor.b = b; + mColor.a = a; + glColor4ub(r, g, b, a); + } + + void OpenGL::popColor() + { + memcpy(&mColor, &mPrecolor, sizeof(mPrecolor)); + glColor4ub(mColor.r, mColor.g, mColor.b, mColor.a); + } + + void OpenGL::flushError() + { + while (glGetError() != GL_NO_ERROR); + } + + GLuint OpenGL::genTexture() + { + GLuint t; + glGenTextures(1, &t); + return t; + } + + void OpenGL::bindTexture(GLuint texture) + { + glBindTexture(GL_TEXTURE_2D, texture); + } + + void OpenGL::deleteTexture(GLuint texture) + { + glDeleteTextures(1, &texture); + } + + void OpenGL::setTexParameter(GLenum pname, GLint param) + { + glTexParameteri(GL_TEXTURE_2D, pname, param); + } + + void OpenGL::texImage(GLint internalformat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels) + { + glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, type, pixels); + } + + void OpenGL::texSubImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels) + { + glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, yoffset, width, height, format, type, pixels); + } + + void OpenGL::activeTexUnit(unsigned int unit) + { + // glActiveTexture selects which texture unit subsequent texture state calls will affect. + glActiveTexture(GL_TEXTURE0 + unit); + } + void OpenGL::setColor(Channel r, Channel g, Channel b, Channel a) { setColor(Color(r, g, b, a)); @@ -43,12 +109,12 @@ namespace JinEngine mModelViewMatrix.setIdentity(); } - void OpenGL::push() + void OpenGL::pushMatrix() { mModelViewMatrices.push_back(Matrix()); } - void OpenGL::pop() + void OpenGL::popMatrix() { if (mModelViewMatrices.size() == 1) return; @@ -112,5 +178,43 @@ namespace JinEngine mProjectionMatrix.setOrtho(l, r, b, t, n, f); } + OpenGL::BlendMode OpenGL::getBlendMode() + { + return mBlendMode; + } + + void OpenGL::setBlendMode(BlendMode mode) + { + if (mBlendMode == mode) + return; + mBlendMode = mode; + + GLenum func = GL_FUNC_ADD; + GLenum srcRGB = GL_ONE; + GLenum srcA = GL_ONE; + GLenum dstRGB = GL_ZERO; + GLenum dstA = GL_ZERO; + + switch (mode) + { + case BlendMode::ADDITIVE: + srcRGB = GL_SRC_ALPHA; + dstRGB = GL_ONE; + break; + case BlendMode::PREMULTIPLIEDALPHA: + srcRGB = srcA = GL_ONE; + dstRGB = dstA = GL_ONE_MINUS_SRC_ALPHA; + break; + case BlendMode::ALPHA: + default: + srcRGB = srcA = GL_SRC_ALPHA; + dstRGB = dstA = GL_ONE_MINUS_SRC_ALPHA; + break; + } + + glBlendEquation(func); + glBlendFuncSeparate(srcRGB, dstRGB, srcA, dstA); + } + } // namespace Graphics } // namespace JinEngine
\ No newline at end of file diff --git a/src/libjin/graphics/je_gl.h b/src/libjin/graphics/je_gl.h index 134cfee..60b2396 100644 --- a/src/libjin/graphics/je_gl.h +++ b/src/libjin/graphics/je_gl.h @@ -7,7 +7,6 @@ #include "../math/je_transform.h" #include "GLee/GLee.h" -#include "ogl/OpenGL.h" #include "je_color.h" @@ -16,18 +15,112 @@ namespace JinEngine namespace Graphics { - /*class Canvas; + // Wrap OpenGL API. +/* + class Canvas; class Shader; class Font; */ - class OpenGL - : public ogl2d::OpenGL + + class OpenGL { public: /// + /// Blend mode. + /// https://www.andersriggelsen.dk/glblendfunc.php /// - /// + enum class BlendMode + { + NONE = 0, + ALPHA, + ADDITIVE, + PREMULTIPLIEDALPHA, + }; + OpenGL(); + ~OpenGL(); + + inline void enable(GLenum cap) + { + glEnable(cap); + } + + inline void disable(GLenum cap) + { + glDisable(cap); + } +/* + inline void setBlendFunc(GLenum sfactor, GLenum dfactor) + { + glBlendFunc(sfactor, dfactor); + } +*/ + inline void setClearColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a) + { + glClearColor(r / 255.f, g / 255.f, b / 255.f, a / 255.f); + } + + void pushColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a = 255); + void popColor(); + void flushError(); + GLuint genTexture(); + void deleteTexture(GLuint texture); + void bindTexture(GLuint texture = 0); + inline GLuint curTexture() + { + return mTexture; + } + void setTexParameter(GLenum pname, GLint param); + void texImage(GLint internalformat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels = NULL); + void texSubImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels); + void activeTexUnit(unsigned int unit = 0); + + inline void drawArrays(GLenum mode, GLint first, GLsizei count) + { + glDrawArrays(mode, first, count); + } + + inline void drawBuffer(GLenum mode) + { + + } + + inline void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices) + { + + } + + inline void enableClientState(GLenum arr) + { + glEnableClientState(arr); + } + + inline void disableClientState(GLenum arr) + { + glDisableClientState(arr); + } + + inline GLuint genFrameBuffer() + { + GLuint fbo; + glGenFramebuffers(1, &fbo); + return fbo; + } + + inline void bindFrameBuffer(GLuint fbo) + { + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + } + + inline void ortho(int w, float radio) + { + glOrtho(0, w, w*radio, 0, -1, 1); + } + + inline void orthox(int w, int h) + { + glOrtho(0, w, h, 0, -1, 1); + } void setColor(Channel r, Channel g, Channel b, Channel a); @@ -37,7 +130,7 @@ namespace JinEngine void clearMatrix(); - void push(); + void pushMatrix(); void translate(float x, float y); @@ -45,7 +138,7 @@ namespace JinEngine void rotate(float r); - void pop(); + void popMatrix(); /// /// @@ -92,6 +185,16 @@ namespace JinEngine /// void unUseShader(); + /// + /// + /// + void setBlendMode(BlendMode mode); + + /// + /// + /// + BlendMode getBlendMode(); + private: /// @@ -113,6 +216,16 @@ namespace JinEngine /// /// Color mCurrentColor; + + /// + /// + /// + BlendMode mBlendMode; + + struct { GLubyte r, g, b, a; } mColor; // current draw color + struct { GLubyte r, g, b, a; } mPrecolor; // previous draw color + GLuint mTexture; // current binded texture + /* /// /// diff --git a/src/libjin/graphics/je_graphic.h b/src/libjin/graphics/je_graphic.h index 58de7ec..d48ba3c 100644 --- a/src/libjin/graphics/je_graphic.h +++ b/src/libjin/graphics/je_graphic.h @@ -3,6 +3,7 @@ #include "../core/je_configuration.h" #if defined(jin_graphics) +#include "../common/je_object.h" #include "../math/je_quad.h" #include "../math/je_vector2.hpp" #include "../math/je_transform.h" @@ -20,7 +21,7 @@ namespace JinEngine /// Class inherites Graphic doesn't keep any state such as origin, scale and other properties. Very low /// level visualized resources. /// - class Graphic : public IRenderable + class Graphic : public Object, public IRenderable { public: /// diff --git a/src/libjin/graphics/je_image.h b/src/libjin/graphics/je_image.h index 971ac18..15baed3 100644 --- a/src/libjin/graphics/je_image.h +++ b/src/libjin/graphics/je_image.h @@ -13,8 +13,7 @@ namespace JinEngine /// /// Just like bitmap but only from image file. The pixels data is readonly. /// - class Image - : public Bitmap + class Image : public Bitmap { public: /// diff --git a/src/libjin/graphics/je_mesh.h b/src/libjin/graphics/je_mesh.h index 27c0cb7..4327424 100644 --- a/src/libjin/graphics/je_mesh.h +++ b/src/libjin/graphics/je_mesh.h @@ -20,7 +20,6 @@ namespace JinEngine private: const Graphic* mGraphic; - }; } // namespace Graphics diff --git a/src/libjin/graphics/je_sprite.h b/src/libjin/graphics/je_sprite.h index c7c5a8b..de2117e 100644 --- a/src/libjin/graphics/je_sprite.h +++ b/src/libjin/graphics/je_sprite.h @@ -16,7 +16,7 @@ namespace JinEngine /// /// A sprite is unit of rendering. Animation is based on sprite, but not texture or other graphic stuff. /// - class Sprite : public IRenderable + class Sprite : public Object, public IRenderable { public: diff --git a/src/libjin/graphics/je_sprite_batch.h b/src/libjin/graphics/je_sprite_batch.h index 64f9805..d58489c 100644 --- a/src/libjin/graphics/je_sprite_batch.h +++ b/src/libjin/graphics/je_sprite_batch.h @@ -1,12 +1,14 @@ #ifndef __JE_GRAPHICS_SPRITE_BATCH_H__ #define __JE_GRAPHICS_SPRITE_BATCH_H__ +#include "../common/je_object.h" + namespace JinEngine { namespace Graphics { - class SpriteBatch + class SpriteBatch : public Object { public: diff --git a/src/libjin/graphics/je_window.cpp b/src/libjin/graphics/je_window.cpp index c14d290..f08e816 100644 --- a/src/libjin/graphics/je_window.cpp +++ b/src/libjin/graphics/je_window.cpp @@ -86,20 +86,21 @@ namespace JinEngine return false; SDL_GL_SetSwapInterval(vsync ? 1 : 0); SDL_GL_MakeCurrent(mWnd, ctx); - // Default configuration + // Default configuration. gl.setClearColor(0, 0, 0, 0xff); glClear(GL_COLOR_BUFFER_BIT); gl.pushColor(0xff, 0xff, 0xff, 0xff); gl.enable(GL_BLEND); gl.enable(GL_TEXTURE_2D); - gl.setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - // Bind to default canvas + // Default blend function. + gl.setBlendMode(OpenGL::BlendMode::ALPHA); + // Bind to default canvas. Canvas::unbind(); Shader::unuse(); // Avoid white blinnk. swapBuffers(); - return true; + return true; } void Window::quitSystem() |