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);
}
+6 -1
View File
@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <glm/glm.hpp>
#include "commons.h"
#include "../../maths/UVRegion.h"
class Mesh;
@@ -18,6 +19,9 @@ class Batch2D {
size_t index;
glm::vec4 color;
Texture* _texture;
DrawPrimitive primitive = DrawPrimitive::triangle;
void setPrimitive(DrawPrimitive primitive);
void vertex(
float x, float y,
@@ -55,6 +59,8 @@ public:
float r, float g, float b, float a
);
void lineRect(float x, float y, float w, float h);
void rect(
float x, float y,
float w, float h,
@@ -81,7 +87,6 @@ public:
float r4, float g4, float b4, int sh
);
void flush(unsigned int gl_primitive);
void flush();
void lineWidth(float width);
+1 -1
View File
@@ -14,7 +14,7 @@ Cubemap::Cubemap(uint width, uint height, ImageFormat imageFormat)
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
uint format = gl::to_gl_format(imageFormat);
uint format = gl::to_glenum(imageFormat);
for (uint face = 0; face < 6; face++) {
glTexImage2D(
GL_TEXTURE_CUBE_MAP_POSITIVE_X + face,
+1 -1
View File
@@ -18,7 +18,7 @@ Texture::Texture(ubyte* data, uint width, uint height, ImageFormat imageFormat)
glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLenum format = gl::to_gl_format(imageFormat);
GLenum format = gl::to_glenum(imageFormat);
glTexImage2D(
GL_TEXTURE_2D, 0, format, width, height, 0,
format, GL_UNSIGNED_BYTE, (GLvoid *) data
+10
View File
@@ -0,0 +1,10 @@
#ifndef GRAPHICS_CORE_COMMONS_H_
#define GRAPHICS_CORE_COMMONS_H_
enum class DrawPrimitive {
point = 0,
line,
triangle,
};
#endif // GRAPHICS_CORE_COMMONS_H_
+13 -3
View File
@@ -1,12 +1,13 @@
#ifndef GRAPHICS_CORE_GL_UTIL_H_
#define GRAPHICS_CORE_GL_UTIL_H_
#include <GL/glew.h>
#include "commons.h"
#include "ImageData.h"
#include <GL/glew.h>
namespace gl {
inline GLenum to_gl_format(ImageFormat imageFormat) {
inline GLenum to_glenum(ImageFormat imageFormat) {
switch (imageFormat) {
case ImageFormat::rgb888: return GL_RGB;
case ImageFormat::rgba8888: return GL_RGBA;
@@ -14,6 +15,15 @@ namespace gl {
return 0;
}
}
inline GLenum to_glenum(DrawPrimitive primitive) {
static const GLenum primitives[]{
GL_POINTS,
GL_LINES,
GL_TRIANGLES
};
return primitives[static_cast<int>(primitive)];
}
}
#endif // GRAPHICS_CORE_GL_UTIL_H_