From 49aba83fa6f3393214919166c42fb55c5a9a7796 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 8 Jun 2014 23:04:53 +0300 Subject: Add basic shaders in modernopengl. Add draw/fill rectangle functions. --- src/render/graphics.h | 3 +- src/render/mgltypes.h | 2 +- src/render/mobileopenglgraphics.h | 2 + src/render/modernopenglgraphics.cpp | 116 ++++++++++++++++++++++++++++++++++-- src/render/modernopenglgraphics.h | 11 ++++ src/render/normalopenglgraphics.h | 2 + src/render/nullopenglgraphics.h | 2 + src/render/openglgraphicsdef.hpp | 14 ----- src/render/openglgraphicsdef1.hpp | 37 ++++++++++++ src/render/safeopenglgraphics.h | 2 + src/render/shaders/shaderprogram.h | 3 + 11 files changed, 173 insertions(+), 21 deletions(-) create mode 100644 src/render/openglgraphicsdef1.hpp (limited to 'src') diff --git a/src/render/graphics.h b/src/render/graphics.h index a61c47437..9b63ba6c6 100644 --- a/src/render/graphics.h +++ b/src/render/graphics.h @@ -308,7 +308,8 @@ class Graphics notfinal void setColor2(const Color &color) { mColor2 = color; } - void setColorAll(const Color &color, const Color &color2) + virtual void setColorAll(const Color &color, + const Color &color2) { mColor = color; mColor2 = color2; diff --git a/src/render/mgltypes.h b/src/render/mgltypes.h index 7e56941a9..321739845 100644 --- a/src/render/mgltypes.h +++ b/src/render/mgltypes.h @@ -101,7 +101,7 @@ typedef void (APIENTRY *glAttachShader_t) (GLuint program, GLuint shader); typedef void (APIENTRY *glDetachShader_t) (GLuint program, GLuint shader); typedef void (APIENTRY *glGetAttachedShaders_t) (GLuint program, GLsizei maxCount, GLsizei *count, GLuint *shaders); -typedef void (APIENTRY *glGetUniformLocation_t) (GLuint program, +typedef GLint (APIENTRY *glGetUniformLocation_t) (GLuint program, const GLchar *name); typedef void (APIENTRY *glGetActiveUniform_t) (GLuint program, GLuint index, GLsizei bufSize, GLsizei *length, GLint *size, GLenum *type, GLchar *name); diff --git a/src/render/mobileopenglgraphics.h b/src/render/mobileopenglgraphics.h index 6161c7f29..813bcc987 100644 --- a/src/render/mobileopenglgraphics.h +++ b/src/render/mobileopenglgraphics.h @@ -68,6 +68,8 @@ class MobileOpenGLGraphics final : public Graphics #include "render/graphicsdef.hpp" + #include "render/openglgraphicsdef1.hpp" + #include "render/openglgraphicsdef.hpp" #include "render/openglgraphicsdefadvanced.hpp" diff --git a/src/render/modernopenglgraphics.cpp b/src/render/modernopenglgraphics.cpp index 566dec714..141b6ccc7 100644 --- a/src/render/modernopenglgraphics.cpp +++ b/src/render/modernopenglgraphics.cpp @@ -64,6 +64,10 @@ ModernOpenGLGraphics::ModernOpenGLGraphics() : mImageCached(0), mFloatColor(1.0F), mMaxVertices(500), + mSimpleId(0U), + mSimpleColor(0U), + mSimplePos(0), + mScreenUniform(0U), mColorAlpha(false), #ifdef DEBUG_BIND_TEXTURE mOldTexture(), @@ -78,6 +82,8 @@ ModernOpenGLGraphics::ModernOpenGLGraphics() : ModernOpenGLGraphics::~ModernOpenGLGraphics() { deleteArraysInternal(); + if (mSimpleProgram) + mSimpleProgram->decRef(); } void ModernOpenGLGraphics::initArrays(const int vertCount) @@ -105,10 +111,28 @@ void ModernOpenGLGraphics::postInit() { logger->log("Compiling shaders"); mSimpleProgram = shaders.getSimpleProgram(); + mSimpleId = mSimpleProgram->getProgramId(); if (mSimpleProgram) + { logger->log("Shaders compilation done."); + mglUseProgram(mSimpleId); + mSimplePos = mglGetAttribLocation(mSimpleId, "simplePos"); + mglEnableVertexAttribArray(mSimplePos); + mSimpleColor = mglGetUniformLocation(mSimpleId, "simpleColor"); + mScreenUniform = mglGetUniformLocation(mSimpleId, "screen"); + screenResized(); + } else + { logger->error("Shaders compilation error."); + } +} + +void ModernOpenGLGraphics::screenResized() +{ + mglUniform2f(mScreenUniform, + static_cast(mWidth) / 2.0f, + static_cast(mHeight) / 2.0f); } void ModernOpenGLGraphics::deleteArrays() @@ -141,6 +165,35 @@ bool ModernOpenGLGraphics::setVideoMode(const int w, const int h, return setOpenGLMode(); } +void ModernOpenGLGraphics::setColor(const Color &color) +{ + setColorAll(color, color); +} + +void ModernOpenGLGraphics::setColorAll(const Color &color, + const Color &color2) +{ + mColor = color; + mColor2 = color2; + mColorAlpha = (color.a != 255); + if (mColorAlpha) + { + mglUniform4f(mSimpleColor, + static_cast(color.r) / 255.0F, + static_cast(color.g) / 255.0F, + static_cast(color.b) / 255.0F, + static_cast(color.a) / 255.0F); + } + else + { + mglUniform4f(mSimpleColor, + static_cast(color.r) / 255.0F, + static_cast(color.g) / 255.0F, + static_cast(color.b) / 255.0F, + 1.0F); + } +} + static inline void drawQuad(const Image *const image, const int srcX, const int srcY, const int dstX, const int dstY, @@ -471,12 +524,60 @@ void ModernOpenGLGraphics::drawLine(int x1, int y1, int x2, int y2) void ModernOpenGLGraphics::drawRectangle(const Rect& rect) { - drawRectangle(rect, false); + setTexturingAndBlending(false); + const ClipRect &clipArea = mClipStack.top(); + const int x1 = rect.x + clipArea.xOffset; + const int y1 = rect.y + clipArea.yOffset; + const int x2 = x1 + rect.width; + const int y2 = y1 + rect.height; + GLfloat vertices[] = + { + x1, y1, + x1, y2, + x2, y2, + x2, y1 + }; + GLuint vbo; + mglGenBuffers(1, &vbo); + mglBindBuffer(GL_ARRAY_BUFFER, vbo); + mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices), + vertices, GL_DYNAMIC_DRAW); +// mglEnableVertexAttribArray(mSimplePos); + mglVertexAttribPointer(mSimplePos, 2, GL_FLOAT, GL_FALSE, 0, 0); + + glDrawArrays(GL_LINE_LOOP, 0, 4); + updateScreen(); + + mglDeleteBuffers(1, &vbo); } void ModernOpenGLGraphics::fillRectangle(const Rect& rect) { - drawRectangle(rect, true); + setTexturingAndBlending(false); + const ClipRect &clipArea = mClipStack.top(); + const int x1 = rect.x + clipArea.xOffset; + const int y1 = rect.y + clipArea.yOffset; + const int x2 = x1 + rect.width; + const int y2 = y1 + rect.height; + GLfloat vertices[] = + { + x1, y1, + x2, y1, + x1, y2, + x2, y2 + }; + GLuint vbo; + mglGenBuffers(1, &vbo); + mglBindBuffer(GL_ARRAY_BUFFER, vbo); + mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices), + vertices, GL_DYNAMIC_DRAW); +// mglEnableVertexAttribArray(mSimplePos); + mglVertexAttribPointer(mSimplePos, 2, GL_FLOAT, GL_FALSE, 0, 0); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + updateScreen(); + + mglDeleteBuffers(1, &vbo); } void ModernOpenGLGraphics::setTexturingAndBlending(const bool enable) @@ -491,16 +592,21 @@ void ModernOpenGLGraphics::setTexturingAndBlending(const bool enable) } else { - if (mAlpha) + if (mAlpha && !mColorAlpha) { glDisable(GL_BLEND); mAlpha = false; } + else if (!mAlpha && mColorAlpha) + { + glEnable(GL_BLEND); + mAlpha = true; + } } } -void ModernOpenGLGraphics::drawRectangle(const Rect& rect, - const bool filled) +void ModernOpenGLGraphics::drawRectangle(const Rect& rect A_UNUSED, + const bool filled A_UNUSED) { } diff --git a/src/render/modernopenglgraphics.h b/src/render/modernopenglgraphics.h index 75165031f..2e80fb56a 100644 --- a/src/render/modernopenglgraphics.h +++ b/src/render/modernopenglgraphics.h @@ -59,6 +59,13 @@ class ModernOpenGLGraphics final : public Graphics void postInit() override final; + void setColor(const Color &color) override final; + + void setColorAll(const Color &color, + const Color &color2) override final; + + void screenResized(); + #include "render/graphicsdef.hpp" #include "render/openglgraphicsdef.hpp" @@ -80,6 +87,10 @@ class ModernOpenGLGraphics final : public Graphics GLuint mImageCached; float mFloatColor; int mMaxVertices; + GLuint mSimpleId; + GLuint mSimpleColor; + GLint mSimplePos; + GLuint mScreenUniform; bool mColorAlpha; #ifdef DEBUG_BIND_TEXTURE std::string mOldTexture; diff --git a/src/render/normalopenglgraphics.h b/src/render/normalopenglgraphics.h index 6de8d9397..bc77b6563 100644 --- a/src/render/normalopenglgraphics.h +++ b/src/render/normalopenglgraphics.h @@ -80,6 +80,8 @@ class NormalOpenGLGraphics final : public Graphics #include "render/openglgraphicsdef.hpp" + #include "render/openglgraphicsdef1.hpp" + #include "render/openglgraphicsdefadvanced.hpp" #ifdef DEBUG_BIND_TEXTURE diff --git a/src/render/nullopenglgraphics.h b/src/render/nullopenglgraphics.h index ebecbe7dd..e01e3b2ef 100644 --- a/src/render/nullopenglgraphics.h +++ b/src/render/nullopenglgraphics.h @@ -76,6 +76,8 @@ class NullOpenGLGraphics final : public Graphics #include "render/openglgraphicsdef.hpp" + #include "render/openglgraphicsdef1.hpp" + #include "render/openglgraphicsdefadvanced.hpp" private: diff --git a/src/render/openglgraphicsdef.hpp b/src/render/openglgraphicsdef.hpp index 4a48d919a..016c2762c 100644 --- a/src/render/openglgraphicsdef.hpp +++ b/src/render/openglgraphicsdef.hpp @@ -21,20 +21,6 @@ */ public: - void setColor(const Color &color) override final - { - mColor = color; - mColor2 = color; - mColorAlpha = (color.a != 255); - } - - void setColorAll(const Color &color, const Color &color2) - { - mColor = color; - mColor2 = color2; - mColorAlpha = (color.a != 255); - } - void drawRectangle(const Rect &rect, const bool filled); diff --git a/src/render/openglgraphicsdef1.hpp b/src/render/openglgraphicsdef1.hpp new file mode 100644 index 000000000..c37b4331f --- /dev/null +++ b/src/render/openglgraphicsdef1.hpp @@ -0,0 +1,37 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +public: + void setColor(const Color &color) override final + { + mColor = color; + mColor2 = color; + mColorAlpha = (color.a != 255); + } + + void setColorAll(const Color &color, + const Color &color2) override final + { + mColor = color; + mColor2 = color2; + mColorAlpha = (color.a != 255); + } diff --git a/src/render/safeopenglgraphics.h b/src/render/safeopenglgraphics.h index 80a10a5eb..d8b37a0d6 100644 --- a/src/render/safeopenglgraphics.h +++ b/src/render/safeopenglgraphics.h @@ -54,6 +54,8 @@ class SafeOpenGLGraphics final : public Graphics #include "render/openglgraphicsdef.hpp" + #include "render/openglgraphicsdef1.hpp" + private: bool mTexture; bool mIsByteColor; diff --git a/src/render/shaders/shaderprogram.h b/src/render/shaders/shaderprogram.h index bb01a4802..a0781d684 100644 --- a/src/render/shaders/shaderprogram.h +++ b/src/render/shaders/shaderprogram.h @@ -38,6 +38,9 @@ class ShaderProgram final : public Resource A_DELETE_COPY(ShaderProgram) + unsigned int getProgramId() const + { return mProgramId; } + protected: unsigned int mProgramId; Shader *mVertex; -- cgit v1.2.3-60-g2f50