Merge branch 'model3dview' into update-gfx-pipeline

This commit is contained in:
MihailRis
2025-05-30 21:37:25 +03:00
29 changed files with 473 additions and 130 deletions
+39 -10
View File
@@ -6,6 +6,11 @@
#include "typedefs.hpp"
#include "maths/UVRegion.hpp"
namespace {
const glm::vec3 SUN_VECTOR(0.528265f, 0.833149f, -0.163704f);
const float DIRECTIONAL_LIGHT_FACTOR = 0.3f;
}
Batch3D::Batch3D(size_t capacity)
: capacity(capacity) {
buffer = std::make_unique<Batch3DVertex[]>(capacity);
@@ -29,20 +34,28 @@ void Batch3D::begin(){
}
void Batch3D::vertex(
float x, float y, float z, float u, float v,
float r, float g, float b, float a
float x,
float y,
float z,
float u,
float v,
float r,
float g,
float b,
float a
) {
buffer[index].position = {x, y, z};
buffer[index].uv = {u, v};
buffer[index].uv = {
u * region.getWidth() + region.u1, v * region.getHeight() + region.v1};
buffer[index].color = {r, g, b, a};
index++;
}
void Batch3D::vertex(
glm::vec3 coord, float u, float v,
float r, float g, float b, float a
glm::vec3 coord, float u, float v, float r, float g, float b, float a
) {
buffer[index].position = coord;
buffer[index].uv = {u, v};
buffer[index].uv = {
u * region.getWidth() + region.u1, v * region.getHeight() + region.v1};
buffer[index].color = {r, g, b, a};
index++;
}
@@ -52,7 +65,9 @@ void Batch3D::vertex(
float r, float g, float b, float a
) {
buffer[index].position = point;
buffer[index].uv = uvpoint;
buffer[index].uv = {
uvpoint.x * region.getWidth() + region.u1,
uvpoint.y * region.getHeight() + region.v1};
buffer[index].color = {r, g, b, a};
index++;
}
@@ -84,14 +99,18 @@ void Batch3D::face(
}
void Batch3D::texture(const Texture* new_texture){
if (currentTexture == new_texture)
if (currentTexture == new_texture) {
return;
}
flush();
currentTexture = new_texture;
if (new_texture == nullptr)
if (new_texture == nullptr) {
blank->bind();
else
region = blank->getUVRegion();
} else {
new_texture->bind();
region = currentTexture->getUVRegion();
}
}
void Batch3D::sprite(
@@ -238,6 +257,16 @@ void Batch3D::blockCube(
cube((1.0f - size) * -0.5f, size, texfaces, tint, shading);
}
void Batch3D::setRegion(UVRegion region) {
this->region = region;
}
void Batch3D::vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec3& norm) {
float d = glm::dot(glm::normalize(norm), SUN_VECTOR);
d = (1.0f - DIRECTIONAL_LIGHT_FACTOR) + d * DIRECTIONAL_LIGHT_FACTOR;
vertex(pos, uv, glm::vec4(d, d, d, 1.0f));
}
void Batch3D::vertex(
const glm::vec3& coord, const glm::vec2& uv, const glm::vec4& tint
) {
+4 -1
View File
@@ -3,6 +3,7 @@
#include "typedefs.hpp"
#include "commons.hpp"
#include "MeshData.hpp"
#include "maths/UVRegion.hpp"
#include <memory>
#include <cstdlib>
@@ -32,8 +33,8 @@ class Batch3D : public Flushable {
std::unique_ptr<Texture> blank;
size_t index;
glm::vec4 tint {1.0f};
const Texture* currentTexture;
UVRegion region {0.0f, 0.0f, 1.0f, 1.0f};
void vertex(
float x, float y, float z,
@@ -102,6 +103,8 @@ public:
const glm::vec4& tint,
bool shading = true
);
void setRegion(UVRegion region);
void vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec3& norm);
void vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec4& tint);
void point(const glm::vec3& pos, const glm::vec4& tint);
void flush() override;
+1 -1
View File
@@ -34,7 +34,7 @@ public:
Batch2D* getBatch2D() const;
const glm::uvec2& getViewport() const;
DrawContext sub(Flushable* flushable=nullptr) const;
[[nodiscard]] DrawContext sub(Flushable* flushable=nullptr) const;
void setViewport(const glm::uvec2& viewport);
void setFramebuffer(Bindable* fbo);
+6
View File
@@ -16,6 +16,7 @@
namespace fs = std::filesystem;
GLSLExtension* Shader::preprocessor = new GLSLExtension();
Shader* Shader::used = nullptr;
Shader::Shader(uint id) : id(id){
}
@@ -25,6 +26,7 @@ Shader::~Shader(){
}
void Shader::use(){
used = this;
glUseProgram(id);
}
@@ -131,3 +133,7 @@ std::unique_ptr<Shader> Shader::create(
}
return std::make_unique<Shader>(id);
}
Shader& Shader::getUsed() {
return *used;
}
+3
View File
@@ -10,6 +10,7 @@
class GLSLExtension;
class Shader {
static Shader* used;
uint id;
std::unordered_map<std::string, uint> uniformLocations;
@@ -43,4 +44,6 @@ public:
const std::string& vertexSource,
const std::string& fragmentSource
);
static Shader& getUsed();
};