add 'modelviewer' ui element
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -31,20 +36,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++;
|
||||
}
|
||||
@@ -54,7 +67,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++;
|
||||
}
|
||||
@@ -86,14 +101,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(
|
||||
@@ -240,6 +259,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
|
||||
) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user