Plotter element

This commit is contained in:
MihailRis
2024-04-20 21:23:00 +03:00
parent c92cb52e2a
commit c82d95b97d
11 changed files with 176 additions and 71 deletions
+49 -24
View File
@@ -1,6 +1,7 @@
#include "Batch2D.h"
#include "Mesh.h"
#include "Texture.h"
#include "gl_util.h"
#include <GL/glew.h>
@@ -26,10 +27,19 @@ Batch2D::~Batch2D(){
delete[] buffer;
}
void Batch2D::setPrimitive(DrawPrimitive primitive) {
if (primitive == this->primitive) {
return;
}
flush();
this->primitive = primitive;
}
void Batch2D::begin(){
_texture = nullptr;
blank->bind();
color = glm::vec4(1.0f);
primitive = DrawPrimitive::triangle;
}
void Batch2D::vertex(
@@ -64,7 +74,7 @@ void Batch2D::vertex(
void Batch2D::texture(Texture* new_texture){
if (_texture == new_texture)
return;
flush(GL_TRIANGLES);
flush();
_texture = new_texture;
if (new_texture == nullptr)
blank->bind();
@@ -78,19 +88,18 @@ void Batch2D::untexture() {
void Batch2D::point(float x, float y, float r, float g, float b, float a){
if (index + 6*B2D_VERTEX_SIZE >= capacity)
flush(GL_TRIANGLES);
flush();
setPrimitive(DrawPrimitive::point);
vertex(x, y, 0, 0, r,g,b,a);
flush(GL_POINTS);
}
void Batch2D::line(float x1, float y1, float x2, float y2, float r, float g, float b, float a){
if (index + 6*B2D_VERTEX_SIZE >= capacity)
flush(GL_TRIANGLES);
if (index + 6*B2D_VERTEX_SIZE >= capacity) {
flush();
}
setPrimitive(DrawPrimitive::line);
vertex(x1, y1, 0, 0, r,g,b,a);
vertex(x2, y2, 1, 1, r,g,b,a);
flush(GL_LINES);
}
void Batch2D::rect(float x, float y, float w, float h){
@@ -98,9 +107,10 @@ void Batch2D::rect(float x, float y, float w, float h){
const float g = color.g;
const float b = color.b;
const float a = color.a;
if (index + 6*B2D_VERTEX_SIZE >= capacity)
flush(GL_TRIANGLES);
if (index + 6*B2D_VERTEX_SIZE >= capacity) {
flush();
}
setPrimitive(DrawPrimitive::triangle);
vertex(x, y, 0, 0, r,g,b,a);
vertex(x, y+h, 0, 1, r,g,b,a);
vertex(x+w, y+h, 1, 1, r,g,b,a);
@@ -120,9 +130,10 @@ void Batch2D::rect(
bool flippedY,
glm::vec4 tint
) {
if (index + 6*B2D_VERTEX_SIZE >= capacity)
flush(GL_TRIANGLES);
if (index + 6*B2D_VERTEX_SIZE >= capacity) {
flush();
}
setPrimitive(DrawPrimitive::triangle);
float centerX = w*ox;
float centerY = h*oy;
float acenterX = w-centerX;
@@ -205,13 +216,29 @@ void Batch2D::rect(
vertex(x4, y4, u4, v4, tint.r, tint.g, tint.b, tint.a);
}
void Batch2D::lineRect(float x, float y, float w, float h) {
vertex(x, y, 0.0f, 0.0f, color.r, color.g, color.b, color.a);
vertex(x, y+h, 0.0f, 1.0f, color.r, color.g, color.b, color.a);
vertex(x, y+h, 0.0f, 1.0f, color.r, color.g, color.b, color.a);
vertex(x+w, y+h, 1.0f, 1.0f, color.r, color.g, color.b, color.a);
vertex(x+w, y+h, 1.0f, 1.0f, color.r, color.g, color.b, color.a);
vertex(x+w, y, 1.0f, 0.0f, color.r, color.g, color.b, color.a);
vertex(x+w, y, 1.0f, 0.0f, color.r, color.g, color.b, color.a);
vertex(x, y, 0.0f, 0.0f, color.r, color.g, color.b, color.a);
}
void Batch2D::rect(
float x, float y, float w, float h,
float u, float v, float tx, float ty,
float r, float g, float b, float a
){
if (index + 6*B2D_VERTEX_SIZE >= capacity)
flush(GL_TRIANGLES);
if (index + 6*B2D_VERTEX_SIZE >= capacity) {
flush();
}
setPrimitive(DrawPrimitive::triangle);
vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v, r,g,b,a);
vertex(x, y+h, u, v, r,g,b,a);
@@ -229,8 +256,10 @@ void Batch2D::rect(
float r3, float g3, float b3,
float r4, float g4, float b4, int sh
){
if (index + 30*B2D_VERTEX_SIZE >= capacity)
flush(GL_TRIANGLES);
if (index + 30*B2D_VERTEX_SIZE >= capacity) {
flush();
}
setPrimitive(DrawPrimitive::triangle);
glm::vec2 v0(x+h/2,y+h/2);
glm::vec2 v1(x+w-sh,y);
glm::vec2 v2(x+sh,y);
@@ -294,18 +323,14 @@ void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index
rect(x, y, w, h, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a);
}
void Batch2D::flush(unsigned int gl_primitive) {
void Batch2D::flush() {
if (index == 0)
return;
mesh->reload(buffer, index / B2D_VERTEX_SIZE);
mesh->draw(gl_primitive);
mesh->draw(gl::to_glenum(primitive));
index = 0;
}
void Batch2D::flush() {
flush(GL_TRIANGLES);
}
void Batch2D::lineWidth(float width) {
glLineWidth(width);
}