diff options
author | chai <chaifix@163.com> | 2018-10-05 08:07:22 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-05 08:07:22 +0800 |
commit | 95c088f3e48321c155eabc00fc4710692405855d (patch) | |
tree | 6b01c1a0d17cb7097f73158ec6bc2bd2c89aa14f | |
parent | f64bde8fe125393fa8a2a5de869c44aaed47b621 (diff) |
*update
-rw-r--r-- | build/.vs/libjin/v14/.suo | bin | 142848 -> 142848 bytes | |||
-rw-r--r-- | build/Debug/05Font.exe | bin | 1197568 -> 1194496 bytes | |||
-rw-r--r-- | libjin/Graphics/Drawable.cpp | 12 | ||||
-rw-r--r-- | libjin/Graphics/Font.cpp | 10 | ||||
-rw-r--r-- | libjin/Graphics/Shader.cpp | 65 | ||||
-rw-r--r-- | libjin/Graphics/Shader.h | 18 | ||||
-rw-r--r-- | libjin/Graphics/Shaders/base.shader.h | 4 | ||||
-rw-r--r-- | libjin/Graphics/Shapes.cpp | 51 | ||||
-rw-r--r-- | test/05Font/main.cpp | 4 |
9 files changed, 92 insertions, 72 deletions
diff --git a/build/.vs/libjin/v14/.suo b/build/.vs/libjin/v14/.suo Binary files differindex db34b56..fd9eecb 100644 --- a/build/.vs/libjin/v14/.suo +++ b/build/.vs/libjin/v14/.suo diff --git a/build/Debug/05Font.exe b/build/Debug/05Font.exe Binary files differindex 2ec8e30..cf80b7f 100644 --- a/build/Debug/05Font.exe +++ b/build/Debug/05Font.exe diff --git a/libjin/Graphics/Drawable.cpp b/libjin/Graphics/Drawable.cpp index f8ff131..9f52f0a 100644 --- a/libjin/Graphics/Drawable.cpp +++ b/libjin/Graphics/Drawable.cpp @@ -40,12 +40,14 @@ namespace graphics void Drawable::draw(int x, int y, float sx, float sy, float r) { - JSLProgram* jsl = JSLProgram::getCurrentJSL(); gl.ModelMatrix.setTransformation(x, y, r, sx, sy, anchor.x, anchor.y); - jsl->sendMatrix4("_modelMatrix_", &gl.ModelMatrix); - jsl->sendMatrix4("_projectionMatrix_", &gl.ProjectionMatrix); - jsl->bindVertexPointer(2, GL_FLOAT, 0, vertex_coords); - jsl->bindUVPointer(2, GL_FLOAT, 0, texture_coords); + + Shader* shader = Shader::getCurrentJSL(); + shader->sendMatrix4(Shader::MODEL_MATRIX, &gl.ModelMatrix); + shader->sendMatrix4(Shader::PROJECTION_MATRIX, &gl.ProjectionMatrix); + shader->bindVertexPointer(2, GL_FLOAT, 0, vertex_coords); + shader->bindUVPointer(2, GL_FLOAT, 0, texture_coords); + gl.bindTexture(texture); gl.drawArrays(GL_QUADS, 0, 4); gl.bindTexture(0); diff --git a/libjin/Graphics/Font.cpp b/libjin/Graphics/Font.cpp index 9e65dcc..dc7fb33 100644 --- a/libjin/Graphics/Font.cpp +++ b/libjin/Graphics/Font.cpp @@ -196,17 +196,17 @@ namespace graphics void Font::print(const Page* page, int x, int y) { - JSLProgram* jsl = JSLProgram::getCurrentJSL(); + Shader* shader = Shader::getCurrentJSL(); const vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist; const vector<GlyphVertex>& glyphvertices = page->glyphvertices; gl.ModelMatrix.setTransformation(x, y, 0, 1, 1, 0, 0); - jsl->sendMatrix4("_modelMatrix_", &gl.ModelMatrix); - jsl->sendMatrix4("_projectionMatrix_", &gl.ProjectionMatrix); + shader->sendMatrix4(Shader::MODEL_MATRIX, &gl.ModelMatrix); + shader->sendMatrix4(Shader::PROJECTION_MATRIX, &gl.ProjectionMatrix); for (int i = 0; i < glyphinfolist.size(); ++i) { const GlyphArrayDrawInfo& info = glyphinfolist[i]; - jsl->bindVertexPointer(2, GL_INT, sizeof(GlyphVertex), &glyphvertices[info.start].x); - jsl->bindUVPointer(2, GL_FLOAT, sizeof(GlyphVertex), &glyphvertices[info.start].u); + shader->bindVertexPointer(2, GL_INT, sizeof(GlyphVertex), &glyphvertices[info.start].x); + shader->bindUVPointer(2, GL_FLOAT, sizeof(GlyphVertex), &glyphvertices[info.start].u); gl.bindTexture(info.texture); gl.drawArrays(GL_QUADS, 0, info.count); gl.bindTexture(0); diff --git a/libjin/Graphics/Shader.cpp b/libjin/Graphics/Shader.cpp index e2aaf63..d484662 100644 --- a/libjin/Graphics/Shader.cpp +++ b/libjin/Graphics/Shader.cpp @@ -23,6 +23,12 @@ namespace graphics */ #include "Shaders/base.shader.h" + const char* Shader::PROJECTION_MATRIX = "_projectionMatrix_"; + const char* Shader::MODEL_MATRIX = "_modelMatrix_"; + const char* Shader::MAIN_TEXTURE = "_main_texture_"; + const char* Shader::VERTEX_COORDS = "_vert_coord_"; + const char* Shader::TEXTURE_COORDS = "_tex_coord_"; + /** * https://stackoverflow.com/questions/27941496/use-sampler-without-passing-through-value * The default value of a sampler variable is 0. From the GLSL 3.30 spec, @@ -48,36 +54,36 @@ namespace graphics */ const int DEFAULT_TEXTURE_UNIT = 0; - /*static*/ JSLProgram* JSLProgram::currentJSLProgram = nullptr; + /*static*/ Shader* Shader::currentShader = nullptr; - JSLProgram* JSLProgram::createJSLProgram(const string& program) + Shader* Shader::createShader(const string& program) { - JSLProgram* jsl = nullptr; + Shader* shader = nullptr; try { - jsl = new JSLProgram(program); + shader = new Shader(program); } catch(...) { return nullptr; } - return jsl; + return shader; } - JSLProgram::JSLProgram(const string& program) + Shader::Shader(const string& program) : currentTextureUnit(DEFAULT_TEXTURE_UNIT) { if (!compile(program)) throw 0; } - JSLProgram::~JSLProgram() + Shader::~Shader() { - if (currentJSLProgram == this) + if (currentShader == this) unuse(); } - bool JSLProgram::compile(const string& program) + bool Shader::compile(const string& program) { int loc_VERTEX_SHADER = program.find("#VERTEX_SHADER"); int loc_END_VERTEX_SHADER = program.find("#END_VERTEX_SHADER"); @@ -135,21 +141,20 @@ namespace graphics return maxTextureUnits; } - void JSLProgram::use() + void Shader::use() { glUseProgram(pid); - currentJSLProgram = this; - sendInt("_tex0_", DEFAULT_TEXTURE_UNIT); - //sendMatrix4("_projectionMatrix_", &projectionMatrix); + currentShader = this; + sendInt(Shader::MAIN_TEXTURE, DEFAULT_TEXTURE_UNIT); } - /*static*/ void JSLProgram::unuse() + /*static*/ void Shader::unuse() { glUseProgram(0); - currentJSLProgram = nullptr; + currentShader = nullptr; } - GLint JSLProgram::claimTextureUnit(const std::string& name) + GLint Shader::claimTextureUnit(const std::string& name) { std::map<std::string, GLint>::iterator unit = textureUnits.find(name); if (unit != textureUnits.end()) @@ -162,17 +167,17 @@ namespace graphics } #define checkJSL() \ - if (currentJSLProgram != this) \ + if (currentShader != this) \ return - void JSLProgram::sendInt(const char* name, int value) + void Shader::sendInt(const char* name, int value) { checkJSL(); int loc = glGetUniformLocation(pid, name); glUniform1i(loc, value); } - void JSLProgram::sendFloat(const char* variable, float number) + void Shader::sendFloat(const char* variable, float number) { checkJSL(); int loc = glGetUniformLocation(pid, variable); @@ -193,7 +198,7 @@ namespace graphics * TextureUnit textureUnits[GL_MAX_TEXTURE_IMAGE_UNITS] * GLuint currentTextureUnit = 0; */ - void JSLProgram::sendTexture(const char* variable, const Texture* tex) + void Shader::sendTexture(const char* variable, const Texture* tex) { checkJSL(); GLint location = glGetUniformLocation(pid, variable); @@ -211,7 +216,7 @@ namespace graphics gl.activeTexUnit(0); } - void JSLProgram::sendCanvas(const char* variable, const Canvas* canvas) + void Shader::sendCanvas(const char* variable, const Canvas* canvas) { checkJSL(); GLint location = glGetUniformLocation(pid, variable); @@ -230,28 +235,28 @@ namespace graphics glActiveTexture(GL_TEXTURE0); } - void JSLProgram::sendVec2(const char* name, float x, float y) + void Shader::sendVec2(const char* name, float x, float y) { checkJSL(); int loc = glGetUniformLocation(pid, name); glUniform2f(loc, x, y); } - void JSLProgram::sendVec3(const char* name, float x, float y, float z) + void Shader::sendVec3(const char* name, float x, float y, float z) { checkJSL(); int loc = glGetUniformLocation(pid, name); glUniform3f(loc, x, y, z); } - void JSLProgram::sendVec4(const char* name, float x, float y, float z, float w) + void Shader::sendVec4(const char* name, float x, float y, float z, float w) { checkJSL(); int loc = glGetUniformLocation(pid, name); glUniform4f(loc, x, y, z, w); } - void JSLProgram::sendColor(const char* name, const Color* col) + void Shader::sendColor(const char* name, const Color* col) { checkJSL(); int loc = glGetUniformLocation(pid, name); @@ -263,22 +268,22 @@ namespace graphics ); } - void JSLProgram::sendMatrix4(const char* name, const math::Matrix* mat4) + void Shader::sendMatrix4(const char* name, const math::Matrix* mat4) { int loc = glGetUniformLocation(pid, name); glUniformMatrix4fv(loc, 1, GL_FALSE, mat4->getElements()); } - void JSLProgram::bindVertexPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers) + void Shader::bindVertexPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers) { - GLint loc = glGetAttribLocation(pid, "_vert_coord_"); + GLint loc = glGetAttribLocation(pid, VERTEX_COORDS); glEnableVertexAttribArray(0); glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers); } - void JSLProgram::bindUVPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers) + void Shader::bindUVPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers) { - GLint loc = glGetAttribLocation(pid, "_tex_coord_"); + GLint loc = glGetAttribLocation(pid, TEXTURE_COORDS); glEnableVertexAttribArray(1); glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers); } diff --git a/libjin/Graphics/Shader.h b/libjin/Graphics/Shader.h index 6a92b88..2ea9e04 100644 --- a/libjin/Graphics/Shader.h +++ b/libjin/Graphics/Shader.h @@ -16,14 +16,20 @@ namespace graphics { /* Jin Shading Language Program*/ - class JSLProgram + class Shader { public: - static JSLProgram* createJSLProgram(const std::string& program); - static inline JSLProgram* getCurrentJSL() { return currentJSLProgram; } + static Shader* createShader(const std::string& program); + static inline Shader* getCurrentJSL() { return currentShader; } static void unuse(); - virtual ~JSLProgram(); + static const char* PROJECTION_MATRIX; + static const char* MODEL_MATRIX; + static const char* MAIN_TEXTURE; + static const char* VERTEX_COORDS; + static const char* TEXTURE_COORDS; + + virtual ~Shader(); void use(); void sendFloat(const char* name, float number); @@ -40,10 +46,10 @@ namespace graphics void bindUVPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers); protected: - static JSLProgram* currentJSLProgram; + static Shader* currentShader; GLint claimTextureUnit(const std::string& name); - JSLProgram(const std::string& program); + Shader(const std::string& program); bool compile(const std::string& program); GLuint pid; diff --git a/libjin/Graphics/Shaders/base.shader.h b/libjin/Graphics/Shaders/base.shader.h index 477d1b1..4efbab7 100644 --- a/libjin/Graphics/Shaders/base.shader.h +++ b/libjin/Graphics/Shaders/base.shader.h @@ -83,7 +83,7 @@ static const char* base_fragment = R"( %s -uniform Texture _tex0_; +uniform Texture _main_texture_; in vec4 _color; in vec2 _xy; @@ -95,7 +95,7 @@ out vec4 _outColor_; void main() { - _outColor_ = frag(_color, _tex0_, Vertex(_xy, _uv)); + _outColor_ = frag(_color, _main_texture_, Vertex(_xy, _uv)); } )"; diff --git a/libjin/Graphics/Shapes.cpp b/libjin/Graphics/Shapes.cpp index e054c96..f639695 100644 --- a/libjin/Graphics/Shapes.cpp +++ b/libjin/Graphics/Shapes.cpp @@ -16,34 +16,39 @@ namespace graphics void point(int x, int y) { - JSLProgram* jsl = JSLProgram::getCurrentJSL(); float verts[] = { x + 0.5f , y + 0.5f }; - jsl->bindVertexPointer(2, GL_FLOAT, 0, verts); - jsl->sendMatrix4("_modelMatrix_", &Matrix::Identity); - jsl->sendMatrix4("_projectionMatrix_", &gl.ProjectionMatrix); + + Shader* shader = Shader::getCurrentJSL(); + shader->bindVertexPointer(2, GL_FLOAT, 0, verts); + shader->sendMatrix4(Shader::MODEL_MATRIX, &Matrix::Identity); + shader->sendMatrix4(Shader::PROJECTION_MATRIX, &gl.ProjectionMatrix); + glDrawArrays(GL_POINTS, 0, 1); } void points(int n, GLshort* p) { - JSLProgram* jsl = JSLProgram::getCurrentJSL(); - jsl->bindVertexPointer(2, GL_SHORT, 0, p); - jsl->sendMatrix4("_modelMatrix_", &Matrix::Identity); - jsl->sendMatrix4("_projectionMatrix_", &gl.ProjectionMatrix); - glDrawArrays(GL_POINTS, 0, n); + Shader* shader = Shader::getCurrentJSL(); + shader->bindVertexPointer(2, GL_SHORT, 0, p); + shader->sendMatrix4(Shader::MODEL_MATRIX, &Matrix::Identity); + shader->sendMatrix4(Shader::PROJECTION_MATRIX, &gl.ProjectionMatrix); + + glDrawArrays(GL_POINTS, 0, n); } void line(int x1, int y1, int x2, int y2) { - JSLProgram* jsl = JSLProgram::getCurrentJSL(); float verts[] = { x1, y1, x2, y2 }; - jsl->bindVertexPointer(2, GL_FLOAT, 0, verts); - jsl->sendMatrix4("_modelMatrix_", &Matrix::Identity); - jsl->sendMatrix4("_projectionMatrix_", &gl.ProjectionMatrix); - glDrawArrays(GL_LINES, 0, 2); + + Shader* shader = Shader::getCurrentJSL(); + shader->bindVertexPointer(2, GL_FLOAT, 0, verts); + shader->sendMatrix4(Shader::MODEL_MATRIX, &Matrix::Identity); + shader->sendMatrix4(Shader::PROJECTION_MATRIX, &gl.ProjectionMatrix); + + glDrawArrays(GL_LINES, 0, 2); } void circle(RenderMode mode, int x, int y, int r) @@ -85,10 +90,11 @@ namespace graphics void polygon_line(float* p, int count) { - JSLProgram* jsl = JSLProgram::getCurrentJSL(); - jsl->sendMatrix4("_modelMatrix_", &Matrix::Identity); - jsl->sendMatrix4("_projectionMatrix_", &gl.ProjectionMatrix); - jsl->bindVertexPointer(2, GL_FLOAT, 0, p); + Shader* shader = Shader::getCurrentJSL(); + shader->sendMatrix4(Shader::MODEL_MATRIX, &Matrix::Identity); + shader->sendMatrix4(Shader::PROJECTION_MATRIX, &gl.ProjectionMatrix); + shader->bindVertexPointer(2, GL_FLOAT, 0, p); + glDrawArrays(GL_LINE_LOOP, 0, count); } @@ -100,10 +106,11 @@ namespace graphics } else if (mode == FILL) { - JSLProgram* jsl = JSLProgram::getCurrentJSL(); - jsl->sendMatrix4("_modelMatrix_", &Matrix::Identity); - jsl->sendMatrix4("_projectionMatrix_", &gl.ProjectionMatrix); - jsl->bindVertexPointer(2, GL_FLOAT, 0, p); + Shader* shader = Shader::getCurrentJSL(); + shader->sendMatrix4(Shader::MODEL_MATRIX, &Matrix::Identity); + shader->sendMatrix4(Shader::PROJECTION_MATRIX, &gl.ProjectionMatrix); + shader->bindVertexPointer(2, GL_FLOAT, 0, p); + glDrawArrays(GL_POLYGON, 0, count); } } diff --git a/test/05Font/main.cpp b/test/05Font/main.cpp index f6d9bce..3b58bae 100644 --- a/test/05Font/main.cpp +++ b/test/05Font/main.cpp @@ -9,7 +9,7 @@ using namespace jin::filesystem; Font* font = nullptr; Canvas* canvas; FontData* data = nullptr; -JSLProgram* shader = nullptr; +Shader* shader = nullptr; Page* page = nullptr; Texture* tex = nullptr; @@ -37,7 +37,7 @@ Color frag(Color col, Texture tex, Vertex v) #END_FRAGMENT_SHADER )"; - shader = JSLProgram::createJSLProgram(program); + shader = Shader::createShader(program); Filesystem* fs = Filesystem::get(); fs->mount("../Debug"); Buffer buffer; |