aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/graphics/je_gl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/graphics/je_gl.cpp')
-rw-r--r--src/libjin/graphics/je_gl.cpp110
1 files changed, 107 insertions, 3 deletions
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