From 8b00d67febf133e89f6a0bfabc41feed555dc4a9 Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 12 Jan 2019 21:48:33 +0800 Subject: =?UTF-8?q?*=E5=8E=BB=E6=8E=89=E6=96=87=E4=BB=B6=E5=89=8D=E7=BC=80?= =?UTF-8?q?je=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/graphics/shapes.cpp | 148 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 src/libjin/graphics/shapes.cpp (limited to 'src/libjin/graphics/shapes.cpp') diff --git a/src/libjin/graphics/shapes.cpp b/src/libjin/graphics/shapes.cpp new file mode 100644 index 0000000..23fc888 --- /dev/null +++ b/src/libjin/graphics/shapes.cpp @@ -0,0 +1,148 @@ +#include "../core/configuration.h" +#if defined(jin_graphics) + +#include + +#include "../math/matrix.h" +#include "../math/constant.h" + +#include "shaders/shader.h" +#include "shapes.h" + +using namespace JinEngine::Graphics::Shaders; + +namespace JinEngine +{ + namespace Graphics + { + + using namespace Math; + + void point(int x, int y) + { + float verts[] = { x + 0.5f , y + 0.5f }; + + Matrix modelMatrix = gl.getModelViewMatrix(); + + Shader* shader = gl.getShader(); + shader->begin() + .uploadVertices(2, GL_FLOAT, 0, verts) + .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix) + .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()); + + gl.drawArrays(GL_POINTS, 0, 1); + + shader->end(); + } + + void points(int n, GLshort* p) + { + Matrix modelMatrix = gl.getModelViewMatrix(); + + Shader* shader = gl.getShader(); + shader->begin() + .uploadVertices(2, GL_SHORT, 0, p) + .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix) + .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()); + + gl.drawArrays(GL_POINTS, 0, n); + + shader->end(); + } + + void line(int x1, int y1, int x2, int y2) + { + float verts[] = { + x1 + 0.5f, y1 + 0.5f, + x2 + 0.5f, y2 + 0.5f + }; + + Matrix modelMatrix = gl.getModelViewMatrix(); + + Shader* shader = gl.getShader(); + shader->begin() + .uploadVertices(2, GL_FLOAT, 0, verts) + .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix) + .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()); + + gl.drawArrays(GL_LINES, 0, 2); + + shader->end(); + } + + void circle(RenderMode mode, int x, int y, int r) + { + r = r < 0 ? 0 : r; + + int points = 40; + float two_pi = static_cast(PI * 2); + if (points <= 0) points = 1; + float angle_shift = (two_pi / points); + float phi = .0f; + + float *coords = new float[2 * (points + 1)]; + for (int i = 0; i < points; ++i, phi += angle_shift) + { + coords[2 * i] = x + r * cos(phi); + coords[2 * i + 1] = y + r * sin(phi); + } + + coords[2 * points] = coords[0]; + coords[2 * points + 1] = coords[1]; + + polygon(mode, coords, points); + + delete[] coords; + } + + void rect(RenderMode mode, int x, int y, int w, int h) + { + float coords[] = { x + 0.5f, y + 0.5f, x + w + 0.5f, y + 0.5f, x + w + 0.5f, y + h + 0.5f, x + 0.5f, y + h + 0.5f }; + polygon(mode, coords, 4); + } + + void triangle(RenderMode mode, int x1, int y1, int x2, int y2, int x3, int y3) + { + float coords[] = { x1 + 0.5f, y1 + 0.5f, x2 + 0.5f, y2 + 0.5f, x3 + 0.5f, y3 + 0.5f }; + polygon(mode, coords, 3); + } + + void polygon_line(float* p, int count) + { + Shader* shader = gl.getShader(); + Matrix modelMatrix = gl.getModelViewMatrix(); + shader->begin() + .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix) + .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()) + .uploadVertices(2, GL_FLOAT, 0, p); + + gl.drawArrays(GL_LINE_LOOP, 0, count); + + shader->end(); + } + + void polygon(RenderMode mode, float* p, int count) + { + if (mode == LINE) + { + polygon_line(p, count); + } + else if (mode == FILL) + { + Shader* shader = gl.getShader(); + Matrix modelMatrix = gl.getModelViewMatrix(); + shader->begin() + .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix) + .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix()) + .uploadVertices(2, GL_FLOAT, 0, p); + + gl.drawArrays(GL_POLYGON, 0, count); + + shader->end(); + } + } + + } // namespace Graphics +} // namespace JinEngine + +#endif // defined(jin_graphics) \ No newline at end of file -- cgit v1.1-26-g67d0