add entities renderer tint support

This commit is contained in:
MihailRis 2024-07-20 19:35:43 +03:00
parent fba0bca0dc
commit 0c4cdeaa87
6 changed files with 62 additions and 42 deletions

View File

@ -3,8 +3,7 @@
layout (location = 0) in vec3 v_position;
layout (location = 1) in vec2 v_texCoord;
layout (location = 2) in vec3 v_color;
layout (location = 3) in vec3 v_normal;
layout (location = 4) in float v_light;
layout (location = 3) in float v_light;
out vec4 a_color;
out vec2 a_texCoord;
@ -31,12 +30,12 @@ void main() {
float torchlight = max(0.0, 1.0-distance(u_cameraPos, modelpos.xyz) /
u_torchlightDistance);
light += torchlight * u_torchlightColor;
a_color = vec4(pow(light, vec3(u_gamma)) * v_normal,1.0f);
a_color = vec4(pow(light, vec3(u_gamma)),1.0f);
a_texCoord = v_texCoord;
a_dir = modelpos.xyz - u_cameraPos;
vec3 skyLightColor = pick_sky_color(u_cubemap);
a_color.rgb = max(a_color.rgb, skyLightColor.rgb*decomp_light.a);
a_color.rgb = max(a_color.rgb, skyLightColor.rgb*decomp_light.a) * v_color;
a_distance = length(u_view * u_model * vec4(pos3d * FOG_POS_SCALE, 0.0));
gl_Position = u_proj * u_view * modelpos;
}

View File

@ -16,11 +16,11 @@
#include <algorithm>
/// xyz, uv, compressed rgba
inline constexpr uint VERTEX_SIZE = 6;
/// xyz, uv, color, compressed lights
inline constexpr uint VERTEX_SIZE = 9;
static const vattr attrs[] = {
{3}, {2}, {1}, {0}
{3}, {2}, {3}, {1}, {0}
};
inline constexpr glm::vec3 X(1, 0, 0);
@ -68,9 +68,9 @@ ModelBatch::~ModelBatch() {
}
void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix,
const glm::mat3& rotation,
const glm::mat3& rotation, glm::vec3 tint,
const texture_names_map* varTextures) {
glm::vec3 gpos = matrix * glm::vec4(glm::vec3(), 1.0f);
glm::vec3 gpos = matrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
light_t light = chunks->getLight(floor(gpos.x), floor(gpos.y), floor(gpos.z));
glm::vec4 lights (
Lightmap::extract(light, 0) / 15.0f,
@ -90,18 +90,19 @@ void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix,
auto norm = rotation * vert.normal;
float d = glm::dot(norm, SUN_VECTOR);
d = 0.8f + d * 0.2f;
auto color = lights * d;
vertex(matrix * glm::vec4(vert.coord, 1.0f), vert.uv, color);
vertex(matrix * glm::vec4(vert.coord, 1.0f), vert.uv, lights*d, tint);
}
}
}
void ModelBatch::draw(glm::mat4 matrix,
glm::vec3 tint,
const model::Model* model,
const texture_names_map* varTextures) {
for (const auto& mesh : model->meshes) {
entries.push_back({matrix, extract_rotation(matrix), &mesh, varTextures});
entries.push_back({
matrix, extract_rotation(matrix), tint, &mesh, varTextures
});
}
}
@ -112,7 +113,7 @@ void ModelBatch::render() {
}
);
for (auto& entry : entries) {
draw(*entry.mesh, entry.matrix, entry.rotation, entry.varTextures);
draw(*entry.mesh, entry.matrix, entry.rotation, entry.tint, entry.varTextures);
}
flush();
entries.clear();

View File

@ -37,7 +37,7 @@ class ModelBatch {
static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f};
inline void vertex(
glm::vec3 pos, glm::vec2 uv, glm::vec4 light
glm::vec3 pos, glm::vec2 uv, glm::vec4 light, glm::vec3 tint
) {
float* buffer = this->buffer.get();
buffer[index++] = pos.x;
@ -45,6 +45,9 @@ class ModelBatch {
buffer[index++] = pos.z;
buffer[index++] = uv.x * region.getWidth() + region.u1;
buffer[index++] = uv.y * region.getHeight() + region.v1;
buffer[index++] = tint.x;
buffer[index++] = tint.y;
buffer[index++] = tint.z;
union {
float floating;
@ -59,8 +62,11 @@ class ModelBatch {
buffer[index++] = compressed.floating;
}
void draw(const model::Mesh& mesh, const glm::mat4& matrix,
const glm::mat3& rotation, const texture_names_map* varTextures);
void draw(const model::Mesh& mesh,
const glm::mat4& matrix,
const glm::mat3& rotation,
glm::vec3 tint,
const texture_names_map* varTextures);
void setTexture(const std::string& name,
const texture_names_map* varTextures);
void setTexture(Texture* texture);
@ -69,6 +75,7 @@ class ModelBatch {
struct DrawEntry {
glm::mat4 matrix;
glm::mat3 rotation;
glm::vec3 tint;
const model::Mesh* mesh;
const texture_names_map* varTextures;
};
@ -77,12 +84,10 @@ public:
ModelBatch(size_t capacity, Assets* assets, Chunks* chunks);
~ModelBatch();
void pushMatrix(glm::mat4 matrix);
void popMatrix();
void draw(glm::mat4 matrix,
glm::vec3 tint,
const model::Model* model,
const texture_names_map* varTextures);
void render();
};

View File

@ -152,22 +152,12 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) {
}
}
void WorldRenderer::renderLevel(
const DrawContext&,
Camera* camera,
const EngineSettings& settings,
bool pause
void WorldRenderer::setupWorldShader(
Shader* shader, Camera* camera, const EngineSettings& settings,
float fogFactor
) {
auto assets = engine->getAssets();
auto atlas = assets->get<Atlas>("blocks");
auto shader = assets->get<Shader>("main");
auto indices = level->content->getIndices();
float fogFactor = 15.0f / ((float)settings.chunks.loadDistance.get()-2);
// Setting up main shader
shader->use();
shader->uniformMatrix("u_model", glm::mat4(1.0f));
shader->uniformMatrix("u_proj", camera->getProjection());
shader->uniformMatrix("u_view", camera->getView());
shader->uniform1f("u_timer", timer);
@ -178,6 +168,7 @@ void WorldRenderer::renderLevel(
shader->uniform3f("u_cameraPos", camera->position);
shader->uniform1i("u_cubemap", 1);
auto indices = level->content->getIndices();
// Light emission when an emissive item is chosen
{
auto inventory = player->getInventory();
@ -191,15 +182,32 @@ void WorldRenderer::renderLevel(
);
shader->uniform1f("u_torchlightDistance", 6.0f);
}
}
void WorldRenderer::renderLevel(
const DrawContext&,
Camera* camera,
const EngineSettings& settings,
bool pause
) {
auto assets = engine->getAssets();
auto atlas = assets->get<Atlas>("blocks");
float fogFactor = 15.0f / ((float)settings.chunks.loadDistance.get()-2);
auto shader = assets->get<Shader>("main");
setupWorldShader(shader, camera, settings, fogFactor);
// Binding main shader textures
skybox->bind();
atlas->getTexture()->bind();
drawChunks(level->chunks.get(), camera, shader);
shader->uniformMatrix("u_model", glm::mat4(1.0f));
auto entityShader = assets->get<Shader>("entity");
setupWorldShader(entityShader, camera, settings, fogFactor);
level->entities->render(assets, *modelBatch, *frustumCulling, pause);
if (!pause) {
scripting::on_frontend_render();
}
@ -263,8 +271,8 @@ void WorldRenderer::renderDebugLines(
glm::vec3 coord = player->camera->position;
if (coord.x < 0) coord.x--;
if (coord.z < 0) coord.z--;
int cx = floordiv((int)coord.x, CHUNK_W);
int cz = floordiv((int)coord.z, CHUNK_D);
int cx = floordiv(static_cast<int>(coord.x), CHUNK_W);
int cz = floordiv(static_cast<int>(coord.z), CHUNK_D);
drawBorders(
cx * CHUNK_W, 0, cz * CHUNK_D,
@ -276,8 +284,8 @@ void WorldRenderer::renderDebugLines(
glm::vec3 tsl(displayWidth/2, displayHeight/2, 0.f);
glm::mat4 model(glm::translate(glm::mat4(1.f), tsl));
linesShader->uniformMatrix("u_projview", glm::ortho(
0.f, (float)displayWidth,
0.f, (float)displayHeight,
0.f, static_cast<float>(displayWidth),
0.f, static_cast<float>(displayHeight),
-length, length) * model * glm::inverse(camera->rotation)
);

View File

@ -62,6 +62,13 @@ class WorldRenderer {
Camera* camera,
Shader* linesShader
);
void setupWorldShader(
Shader* shader,
Camera* camera,
const EngineSettings& settings,
float fogFactor
);
public:
static bool showChunkBorders;
static bool showEntitiesDebug;

View File

@ -99,8 +99,8 @@ void SkeletonConfig::render(
}
model = modelOverride.model ? modelOverride.model : model;
if (model) {
batch.draw(
skeleton.calculated.matrices[i], model, &skeleton.textures);
batch.draw(skeleton.calculated.matrices[i], glm::vec3(1.0f), model,
&skeleton.textures);
}
}
}