diff options
Diffstat (limited to 'src/libjin')
-rw-r--r-- | src/libjin/graphics/fonts/je_texture_font.cpp | 2 | ||||
-rw-r--r-- | src/libjin/graphics/fonts/je_ttf.cpp | 2 | ||||
-rw-r--r-- | src/libjin/graphics/je_graphic.cpp | 4 | ||||
-rw-r--r-- | src/libjin/graphics/je_graphics.h | 1 | ||||
-rw-r--r-- | src/libjin/graphics/je_mesh.cpp | 68 | ||||
-rw-r--r-- | src/libjin/graphics/je_mesh.h | 28 | ||||
-rw-r--r-- | src/libjin/graphics/je_shapes.cpp | 10 | ||||
-rw-r--r-- | src/libjin/graphics/je_vertex.h | 21 | ||||
-rw-r--r-- | src/libjin/graphics/shaders/je_jsl_compiler.cpp | 14 | ||||
-rw-r--r-- | src/libjin/graphics/shaders/je_jsl_compiler.h | 1 | ||||
-rw-r--r-- | src/libjin/graphics/shaders/je_shader.cpp | 33 | ||||
-rw-r--r-- | src/libjin/graphics/shaders/je_shader.h | 22 | ||||
-rw-r--r-- | src/libjin/math/je_bbox.h | 30 | ||||
-rw-r--r-- | src/libjin/math/je_matrix.cpp | 4 | ||||
-rw-r--r-- | src/libjin/math/je_matrix.h | 18 | ||||
-rw-r--r-- | src/libjin/math/je_quad.h | 1 |
16 files changed, 228 insertions, 31 deletions
diff --git a/src/libjin/graphics/fonts/je_texture_font.cpp b/src/libjin/graphics/fonts/je_texture_font.cpp index 4fd1af1..7737f90 100644 --- a/src/libjin/graphics/fonts/je_texture_font.cpp +++ b/src/libjin/graphics/fonts/je_texture_font.cpp @@ -281,8 +281,10 @@ namespace JinEngine for (int i = 0; i < glyphinfolist.size(); ++i) { const GlyphArrayDrawInfo& info = glyphinfolist[i]; + shader->beginUploadAttributes(); shader->uploadVertices(2, GL_INT, sizeof(GlyphVertex), &glyphvertices[info.start].x); shader->uploadUV(2, GL_FLOAT, sizeof(GlyphVertex), &glyphvertices[info.start].u); + shader->endUploadAttributes(); gl.bindTexture(info.texture); gl.drawArrays(GL_QUADS, 0, info.count); gl.bindTexture(0); diff --git a/src/libjin/graphics/fonts/je_ttf.cpp b/src/libjin/graphics/fonts/je_ttf.cpp index 29bc834..904617a 100644 --- a/src/libjin/graphics/fonts/je_ttf.cpp +++ b/src/libjin/graphics/fonts/je_ttf.cpp @@ -300,8 +300,10 @@ namespace JinEngine for (int i = 0; i < glyphinfolist.size(); ++i) { const GlyphArrayDrawInfo& info = glyphinfolist[i]; + shader->beginUploadAttributes(); shader->uploadVertices(2, GL_INT, sizeof(GlyphVertex), &glyphvertices[info.start].x); shader->uploadUV(2, GL_FLOAT, sizeof(GlyphVertex), &glyphvertices[info.start].u); + shader->endUploadAttributes(); gl.bindTexture(info.texture); gl.drawArrays(GL_QUADS, 0, info.count); gl.bindTexture(0); diff --git a/src/libjin/graphics/je_graphic.cpp b/src/libjin/graphics/je_graphic.cpp index a0c2257..1e18995 100644 --- a/src/libjin/graphics/je_graphic.cpp +++ b/src/libjin/graphics/je_graphic.cpp @@ -73,8 +73,10 @@ namespace JinEngine Shader* shader = gl.getShader(); shader->sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelViewMatrix); shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()); + shader->beginUploadAttributes(); shader->uploadVertices(2, GL_FLOAT, 0, vertexCoords); shader->uploadUV(2, GL_FLOAT, 0, textureCoords); + shader->endUploadAttributes(); gl.bindTexture(getGLTexture()); gl.drawArrays(GL_QUADS, 0, 4); @@ -106,8 +108,10 @@ namespace JinEngine Shader* shader = gl.getShader(); shader->sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelViewMatrix); shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()); + shader->beginUploadAttributes(); shader->uploadVertices(2, GL_FLOAT, 0, vertexCoords); shader->uploadUV(2, GL_FLOAT, 0, textureCoords); + shader->endUploadAttributes(); gl.bindTexture(getGLTexture()); gl.drawArrays(GL_QUADS, 0, 4); diff --git a/src/libjin/graphics/je_graphics.h b/src/libjin/graphics/je_graphics.h index 64f92a4..e8f06c1 100644 --- a/src/libjin/graphics/je_graphics.h +++ b/src/libjin/graphics/je_graphics.h @@ -12,6 +12,7 @@ #include "je_image.h" #include "je_sprite.h" #include "je_sprite_sheet.h" +#include "je_mesh.h" #include "shaders/je_shader.h" diff --git a/src/libjin/graphics/je_mesh.cpp b/src/libjin/graphics/je_mesh.cpp index a88abbd..4c27433 100644 --- a/src/libjin/graphics/je_mesh.cpp +++ b/src/libjin/graphics/je_mesh.cpp @@ -1,11 +1,77 @@ +#include "../math/je_math.h" + #include "je_mesh.h" +#include "shaders/je_shader.h" + +using namespace JinEngine::Math; +using namespace JinEngine::Graphics::Shaders; namespace JinEngine { namespace Graphics { - + Mesh::Mesh() + : mGraphic(nullptr) + { + } + + void Mesh::setGraphic(const Graphic* graphic) + { + mGraphic = graphic; + } + + void Mesh::pushVertex(float x, float y, float u, float v, Color color) + { + Vertex vert; + vert.x = x; vert.y = y; + vert.u = u; vert.v = v; + vert.color = color; + pushVertex(vert); + } + + void Mesh::pushVertex(const Vertex& vert) + { + mVertices.push_back(vert); + // Update bound + if (mVertices.size() == 2) + { + const Vertex& v0 = mVertices[0]; + mBound.l = min(v0.x, vert.x); + mBound.r = max(v0.x, vert.x); + mBound.t = min(v0.y, vert.y); + mBound.b = max(v0.y, vert.y); + } + else + { + float x = vert.x, y = vert.y; + mBound.l = x < mBound.l ? x : mBound.l; + mBound.r = x > mBound.r ? x : mBound.r; + mBound.t = y < mBound.t ? y : mBound.t; + mBound.b = y > mBound.b ? y : mBound.b; + } + } + + void Mesh::render(float x, float y, float sx, float sy, float r, float ox, float oy) const + { + if (mGraphic == nullptr || mVertices.size() == 0) + return; + + Math::Matrix modelViewMatrix = gl.getModelViewMatrix(x, y, sx, sy, r, ox, oy); + + Shader* shader = gl.getShader(); + shader->sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelViewMatrix); + shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()); + shader->beginUploadAttributes(); + shader->uploadVertices(2, GL_FLOAT, sizeof(Vertex), &(mVertices[0].x)); + shader->uploadUV(2, GL_FLOAT, sizeof(Vertex), &(mVertices[0].u)); + shader->uploadColor(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &(mVertices[0].color), GL_TRUE); + shader->endUploadAttributes(); + + gl.bindTexture(mGraphic->getGLTexture()); + gl.drawArrays(GL_POLYGON, 0, mVertices.size()); + gl.bindTexture(0); + }; } // namespace Graphics } // namespace JinEngine
\ No newline at end of file diff --git a/src/libjin/graphics/je_mesh.h b/src/libjin/graphics/je_mesh.h index 5bcca2e..1dbf3a4 100644 --- a/src/libjin/graphics/je_mesh.h +++ b/src/libjin/graphics/je_mesh.h @@ -1,6 +1,11 @@ #ifndef __JE_MESH_H__ #define __JE_MESH_H__ +#include <vector> + +#include "../math/je_bbox.h" + +#include "je_vertex.h" #include "je_graphic.h" namespace JinEngine @@ -11,15 +16,34 @@ namespace JinEngine /// /// A 2D mesh. /// - class Mesh + class Mesh : public Renderable, public Object { public: + Mesh(); + void setGraphic(const Graphic* graphic); - void pushVertex(float x, float y, float u, float v); + void pushVertex(float x, float y, float u, float v, Color color = Color::WHITE); + void pushVertex(const Vertex& vertex); + inline Math::BBox getBound() { return mBound; } + + void render(float x, float y, float sx, float sy, float r, float ox = 0, float oy = 0) const; private: + /// + /// Graphic binded. + /// const Graphic* mGraphic; + /// + /// Bound box of mesh. + /// + Math::BBox mBound; + + /// + /// + /// + std::vector<Vertex> mVertices; + }; } // namespace Graphics diff --git a/src/libjin/graphics/je_shapes.cpp b/src/libjin/graphics/je_shapes.cpp index 96a4f76..9af031d 100644 --- a/src/libjin/graphics/je_shapes.cpp +++ b/src/libjin/graphics/je_shapes.cpp @@ -23,7 +23,9 @@ namespace JinEngine float verts[] = { x + 0.5f , y + 0.5f }; Shader* shader = gl.getShader(); + shader->beginUploadAttributes(); shader->uploadVertices(2, GL_FLOAT, 0, verts); + shader->endUploadAttributes(); Matrix modelMatrix = gl.getModelViewMatrix(); shader->sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix); shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()); @@ -34,7 +36,9 @@ namespace JinEngine void points(int n, GLshort* p) { Shader* shader = gl.getShader(); + shader->beginUploadAttributes(); shader->uploadVertices(2, GL_SHORT, 0, p); + shader->endUploadAttributes(); Matrix modelMatrix = gl.getModelViewMatrix(); shader->sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix); shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()); @@ -50,7 +54,9 @@ namespace JinEngine }; Shader* shader = gl.getShader(); + shader->beginUploadAttributes(); shader->uploadVertices(2, GL_FLOAT, 0, verts); + shader->endUploadAttributes(); Matrix modelMatrix = gl.getModelViewMatrix(); shader->sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix); shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()); @@ -101,7 +107,9 @@ namespace JinEngine Matrix modelMatrix = gl.getModelViewMatrix(); shader->sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix); shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()); + shader->beginUploadAttributes(); shader->uploadVertices(2, GL_FLOAT, 0, p); + shader->endUploadAttributes(); glDrawArrays(GL_LINE_LOOP, 0, count); } @@ -118,7 +126,9 @@ namespace JinEngine Matrix modelMatrix = gl.getModelViewMatrix(); shader->sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix); shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()); + shader->beginUploadAttributes(); shader->uploadVertices(2, GL_FLOAT, 0, p); + shader->endUploadAttributes(); glDrawArrays(GL_POLYGON, 0, count); } diff --git a/src/libjin/graphics/je_vertex.h b/src/libjin/graphics/je_vertex.h new file mode 100644 index 0000000..d149cfc --- /dev/null +++ b/src/libjin/graphics/je_vertex.h @@ -0,0 +1,21 @@ +#ifndef __JE_MATH_VERTEX_H__ +#define __JE_MATH_VERTEX_H__ + +#include "je_color.h" + +namespace JinEngine +{ + namespace Graphics + { + + struct Vertex + { + float x, y; // Coordinates + float u, v; // UV + Color color; // Color + }; + + } +} + +#endif
\ No newline at end of file diff --git a/src/libjin/graphics/shaders/je_jsl_compiler.cpp b/src/libjin/graphics/shaders/je_jsl_compiler.cpp index e04958c..74613ad 100644 --- a/src/libjin/graphics/shaders/je_jsl_compiler.cpp +++ b/src/libjin/graphics/shaders/je_jsl_compiler.cpp @@ -21,6 +21,7 @@ namespace JinEngine JinEngine::String SHADER_MAIN_TEXTURE = "jin_MainTexture"; JinEngine::String SHADER_VERTEX_COORDS = "jin_VertexCoords"; JinEngine::String SHADER_TEXTURE_COORDS = "jin_TextureCoords"; + JinEngine::String SHADER_VERTEX_COLOR = "jin_VertexColor"; JinEngine::String SHADER_VERSION = "#version 130 core \n"; JinEngine::String SHADER_DEFINITIONS = // Types @@ -38,6 +39,7 @@ namespace JinEngine "{ \n" " vec2 xy; \n" " vec2 uv; \n" + " vec4 color; \n" "}; \n" "\n"; JinEngine::String SHADER_UNIFORMS = @@ -52,20 +54,23 @@ namespace JinEngine SHADER_DEFINITIONS + SHADER_UNIFORMS + "in vec2 " + SHADER_VERTEX_COORDS + "; \n" - "in vec2 " + SHADER_TEXTURE_COORDS + "; \n"; + "in vec2 " + SHADER_TEXTURE_COORDS + "; \n" + "in vec4 " + SHADER_VERTEX_COLOR + "; \n"; // Color data in unsigned byte. static JinEngine::String vert_part2 = "\n" "out vec4 jin_Color; \n" "out vec2 jin_XY; \n" "out vec2 jin_UV; \n" + "out vec4 jin_COLOR; \n" "void main()\n" "{\n" " vec4 v = " + SHADER_MODELVIEW_MATRIX + " * vec4(" + SHADER_VERTEX_COORDS + ", 0, 1.0); \n" - " Vertex _v = vert(Vertex(v.xy, " + SHADER_TEXTURE_COORDS + ")); \n" + " Vertex _v = vert(Vertex(v.xy, " + SHADER_TEXTURE_COORDS + ", " + SHADER_VERTEX_COLOR + ")); \n" " gl_Position = " + SHADER_PROJECTION_MATRIX + " * vec4(_v.xy, 0, 1.0f); \n" " jin_Color = gl_Color; \n" " jin_XY = _v.xy; \n" " jin_UV = _v.uv; \n" + " jin_COLOR = _v.color; \n" "}"; return vert_part1 + vert + vert_part2; } @@ -78,13 +83,14 @@ namespace JinEngine SHADER_UNIFORMS + "in vec4 jin_Color; \n" "in vec2 jin_XY; \n" - "in vec2 jin_UV; \n"; + "in vec2 jin_UV; \n" + "in vec4 jin_COLOR; \n"; static JinEngine::String frag_part2 = "\n" "out vec4 jin_OutColor; \n" "void main() \n" "{ \n" - " jin_OutColor = frag(jin_Color, " + SHADER_MAIN_TEXTURE + ", Vertex(jin_XY, jin_UV)); \n" + " jin_OutColor = frag(jin_Color, " + SHADER_MAIN_TEXTURE + ", Vertex(jin_XY, jin_UV, jin_COLOR)); \n" "} \n"; return frag_part1 + frag + frag_part2; } diff --git a/src/libjin/graphics/shaders/je_jsl_compiler.h b/src/libjin/graphics/shaders/je_jsl_compiler.h index ed6775a..c34f8ec 100644 --- a/src/libjin/graphics/shaders/je_jsl_compiler.h +++ b/src/libjin/graphics/shaders/je_jsl_compiler.h @@ -21,6 +21,7 @@ namespace JinEngine extern JinEngine::String SHADER_MAIN_TEXTURE; extern JinEngine::String SHADER_VERTEX_COORDS; extern JinEngine::String SHADER_TEXTURE_COORDS; + extern JinEngine::String SHADER_VERTEX_COLOR; /// /// Compile JSL into GLSL. diff --git a/src/libjin/graphics/shaders/je_shader.cpp b/src/libjin/graphics/shaders/je_shader.cpp index f752e99..16627d5 100644 --- a/src/libjin/graphics/shaders/je_shader.cpp +++ b/src/libjin/graphics/shaders/je_shader.cpp @@ -240,25 +240,36 @@ namespace JinEngine glUniformMatrix4fv(loc, 1, GL_FALSE, mat4->getElements()); } - void Shader::uploadVertices(int n, GLenum type, GLsizei stride, const GLvoid * pointers) + void Shader::uploadVertices(int n, GLenum type, GLsizei stride, const GLvoid * pointers, GLboolean normalized) { - GLint loc = glGetAttribLocation(mPID, SHADER_VERTEX_COORDS); - glEnableVertexAttribArray(0); - glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers); + uploadAttribute(SHADER_VERTEX_COORDS, n, type, stride, pointers, normalized); } - void Shader::uploadUV(int n, GLenum type, GLsizei stride, const GLvoid * pointers) + void Shader::uploadUV(int n, GLenum type, GLsizei stride, const GLvoid * pointers, GLboolean normalized) { - GLint loc = glGetAttribLocation(mPID, SHADER_TEXTURE_COORDS); - glEnableVertexAttribArray(1); - glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers); + uploadAttribute(SHADER_TEXTURE_COORDS, n, type, stride, pointers, normalized); } - void Shader::uploadAttribute(const String& name, int n, GLenum type, GLsizei stride, const GLvoid * pointers) + void Shader::uploadColor(int n, GLenum type, GLsizei stride, const GLvoid * pointers, GLboolean normalized) + { + uploadAttribute(SHADER_VERTEX_COLOR, n, type, stride, pointers, normalized); + } + + void Shader::beginUploadAttributes() + { + mAttributeIndex = 0; + } + + void Shader::endUploadAttributes() + { + mAttributeIndex = 0; + } + + void Shader::uploadAttribute(const String& name, int n, GLenum type, GLsizei stride, const GLvoid * pointers, GLboolean normalized) { GLint loc = glGetAttribLocation(mPID, name); - glEnableVertexAttribArray(1); - glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers); + glEnableVertexAttribArray(mAttributeIndex++); + glVertexAttribPointer(loc, n, type, normalized, stride, pointers); } } // namespace Shaders diff --git a/src/libjin/graphics/shaders/je_shader.h b/src/libjin/graphics/shaders/je_shader.h index 9056d51..6a2db9c 100644 --- a/src/libjin/graphics/shaders/je_shader.h +++ b/src/libjin/graphics/shaders/je_shader.h @@ -132,7 +132,7 @@ namespace JinEngine /// @param stride Byte offset between consecutive generic vertex attributes. /// @param pointers Pointer to the first component of the first generic vertex attribute in the array. /// - void uploadVertices(int n, GLenum type, GLsizei stride, const GLvoid * pointers); + void uploadVertices(int n, GLenum type, GLsizei stride, const GLvoid * pointers, GLboolean normalized = GL_FALSE); /// /// Set texture UV coordinates. @@ -142,12 +142,27 @@ namespace JinEngine /// @param stride Byte offset between consecutive generic vertex attributes. /// @param pointers Pointer to the first component of the first generic vertex attribute in the array. /// - void uploadUV(int n, GLenum type, GLsizei stride, const GLvoid * pointers); + void uploadUV(int n, GLenum type, GLsizei stride, const GLvoid * pointers, GLboolean normalized = GL_FALSE); + + /// + /// Upload vertex color array. + /// + void uploadColor(int n, GLenum type, GLsizei stride, const GLvoid * pointers, GLboolean normalized = GL_FALSE); /// /// Set attribute. /// - void uploadAttribute(const String& name, int n, GLenum type, GLsizei stride, const GLvoid * pointers); + void uploadAttribute(const String& name, int n, GLenum type, GLsizei stride, const GLvoid * pointers, GLboolean normalized = GL_FALSE); + + /// + /// Reset attribute index. + /// + void beginUploadAttributes(); + + /// + /// Reset attribute index. + /// + void endUploadAttributes(); /// /// Program ID. @@ -174,6 +189,7 @@ namespace JinEngine GLuint mPID; GLint mCurrentTextureUnit; std::map<std::string, GLint> mTextureUnits; + GLint mAttributeIndex; }; diff --git a/src/libjin/math/je_bbox.h b/src/libjin/math/je_bbox.h new file mode 100644 index 0000000..ce88080 --- /dev/null +++ b/src/libjin/math/je_bbox.h @@ -0,0 +1,30 @@ +#ifndef __JE_BBOX_H__ +#define __JE_BBOX_H__ + +namespace JinEngine +{ + namespace Math + { + + /// + /// + /// + struct BBox + { + BBox() + : l(0), r(0), t(0), b(0) + { + } + + BBox(float _l, float _r, float _t, float _b) + : l(+l), r(_r), t(_t), b(_b) + { + } + + float l, r, t, b; + }; + + } // namespace Math +} // namespace JinEngine + +#endif // __JE_BBOX_H__
\ No newline at end of file diff --git a/src/libjin/math/je_matrix.cpp b/src/libjin/math/je_matrix.cpp index f51ef5d..f422002 100644 --- a/src/libjin/math/je_matrix.cpp +++ b/src/libjin/math/je_matrix.cpp @@ -1,3 +1,5 @@ +#include "../graphics/je_vertex.h" + #include "je_matrix.h" #include <cstring> // memcpy @@ -177,7 +179,7 @@ namespace JinEngine // | e2 e6 e10 e14 | // | e3 e7 e11 e15 | - void Matrix::transform(vertex * dst, const vertex * src, int size) const + void Matrix::transform(Graphics::Vertex* dst, const Graphics::Vertex* src, int size) const { for (int i = 0; i<size; ++i) { diff --git a/src/libjin/math/je_matrix.h b/src/libjin/math/je_matrix.h index 65f080b..7955de1 100644 --- a/src/libjin/math/je_matrix.h +++ b/src/libjin/math/je_matrix.h @@ -3,21 +3,21 @@ namespace JinEngine { - namespace Math + + // Forward declarations. + namespace Graphics { + struct Vertex; + } - struct vertex - { - unsigned char r, g, b, a; - float x, y; - float s, t; - }; + namespace Math + { /// /// This class is the basis for all transformations in LOVE. Althought not /// really needed for 2D, it contains 4x4 elements to be compatible with /// OpenGL without conversions. - /// Ҫתõľ + /// ҪתõOpenGL /// https://blog.csdn.net/candycat1992/article/details/8830894 /// class Matrix @@ -150,7 +150,7 @@ namespace JinEngine /// @param src The source vertices. /// @param size The number of vertices. /// - void transform(vertex * dst, const vertex * src, int size) const; + void transform(Graphics::Vertex* dst, const Graphics::Vertex * src, int size) const; }; diff --git a/src/libjin/math/je_quad.h b/src/libjin/math/je_quad.h index e62f370..5bda125 100644 --- a/src/libjin/math/je_quad.h +++ b/src/libjin/math/je_quad.h @@ -22,6 +22,7 @@ namespace JinEngine } float x, y, w, h; + }; } // namespace Math |