add entities renderer tint support
This commit is contained in:
parent
fba0bca0dc
commit
0c4cdeaa87
@ -3,8 +3,7 @@
|
|||||||
layout (location = 0) in vec3 v_position;
|
layout (location = 0) in vec3 v_position;
|
||||||
layout (location = 1) in vec2 v_texCoord;
|
layout (location = 1) in vec2 v_texCoord;
|
||||||
layout (location = 2) in vec3 v_color;
|
layout (location = 2) in vec3 v_color;
|
||||||
layout (location = 3) in vec3 v_normal;
|
layout (location = 3) in float v_light;
|
||||||
layout (location = 4) in float v_light;
|
|
||||||
|
|
||||||
out vec4 a_color;
|
out vec4 a_color;
|
||||||
out vec2 a_texCoord;
|
out vec2 a_texCoord;
|
||||||
@ -31,12 +30,12 @@ void main() {
|
|||||||
float torchlight = max(0.0, 1.0-distance(u_cameraPos, modelpos.xyz) /
|
float torchlight = max(0.0, 1.0-distance(u_cameraPos, modelpos.xyz) /
|
||||||
u_torchlightDistance);
|
u_torchlightDistance);
|
||||||
light += torchlight * u_torchlightColor;
|
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_texCoord = v_texCoord;
|
||||||
|
|
||||||
a_dir = modelpos.xyz - u_cameraPos;
|
a_dir = modelpos.xyz - u_cameraPos;
|
||||||
vec3 skyLightColor = pick_sky_color(u_cubemap);
|
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));
|
a_distance = length(u_view * u_model * vec4(pos3d * FOG_POS_SCALE, 0.0));
|
||||||
gl_Position = u_proj * u_view * modelpos;
|
gl_Position = u_proj * u_view * modelpos;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,11 +16,11 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
/// xyz, uv, compressed rgba
|
/// xyz, uv, color, compressed lights
|
||||||
inline constexpr uint VERTEX_SIZE = 6;
|
inline constexpr uint VERTEX_SIZE = 9;
|
||||||
|
|
||||||
static const vattr attrs[] = {
|
static const vattr attrs[] = {
|
||||||
{3}, {2}, {1}, {0}
|
{3}, {2}, {3}, {1}, {0}
|
||||||
};
|
};
|
||||||
|
|
||||||
inline constexpr glm::vec3 X(1, 0, 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,
|
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) {
|
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));
|
light_t light = chunks->getLight(floor(gpos.x), floor(gpos.y), floor(gpos.z));
|
||||||
glm::vec4 lights (
|
glm::vec4 lights (
|
||||||
Lightmap::extract(light, 0) / 15.0f,
|
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;
|
auto norm = rotation * vert.normal;
|
||||||
float d = glm::dot(norm, SUN_VECTOR);
|
float d = glm::dot(norm, SUN_VECTOR);
|
||||||
d = 0.8f + d * 0.2f;
|
d = 0.8f + d * 0.2f;
|
||||||
|
vertex(matrix * glm::vec4(vert.coord, 1.0f), vert.uv, lights*d, tint);
|
||||||
auto color = lights * d;
|
|
||||||
vertex(matrix * glm::vec4(vert.coord, 1.0f), vert.uv, color);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModelBatch::draw(glm::mat4 matrix,
|
void ModelBatch::draw(glm::mat4 matrix,
|
||||||
|
glm::vec3 tint,
|
||||||
const model::Model* model,
|
const model::Model* model,
|
||||||
const texture_names_map* varTextures) {
|
const texture_names_map* varTextures) {
|
||||||
for (const auto& mesh : model->meshes) {
|
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) {
|
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();
|
flush();
|
||||||
entries.clear();
|
entries.clear();
|
||||||
|
|||||||
@ -37,7 +37,7 @@ class ModelBatch {
|
|||||||
static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f};
|
static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f};
|
||||||
|
|
||||||
inline void vertex(
|
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();
|
float* buffer = this->buffer.get();
|
||||||
buffer[index++] = pos.x;
|
buffer[index++] = pos.x;
|
||||||
@ -45,6 +45,9 @@ class ModelBatch {
|
|||||||
buffer[index++] = pos.z;
|
buffer[index++] = pos.z;
|
||||||
buffer[index++] = uv.x * region.getWidth() + region.u1;
|
buffer[index++] = uv.x * region.getWidth() + region.u1;
|
||||||
buffer[index++] = uv.y * region.getHeight() + region.v1;
|
buffer[index++] = uv.y * region.getHeight() + region.v1;
|
||||||
|
buffer[index++] = tint.x;
|
||||||
|
buffer[index++] = tint.y;
|
||||||
|
buffer[index++] = tint.z;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
float floating;
|
float floating;
|
||||||
@ -59,8 +62,11 @@ class ModelBatch {
|
|||||||
buffer[index++] = compressed.floating;
|
buffer[index++] = compressed.floating;
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw(const model::Mesh& mesh, const glm::mat4& matrix,
|
void draw(const model::Mesh& mesh,
|
||||||
const glm::mat3& rotation, const texture_names_map* varTextures);
|
const glm::mat4& matrix,
|
||||||
|
const glm::mat3& rotation,
|
||||||
|
glm::vec3 tint,
|
||||||
|
const texture_names_map* varTextures);
|
||||||
void setTexture(const std::string& name,
|
void setTexture(const std::string& name,
|
||||||
const texture_names_map* varTextures);
|
const texture_names_map* varTextures);
|
||||||
void setTexture(Texture* texture);
|
void setTexture(Texture* texture);
|
||||||
@ -69,6 +75,7 @@ class ModelBatch {
|
|||||||
struct DrawEntry {
|
struct DrawEntry {
|
||||||
glm::mat4 matrix;
|
glm::mat4 matrix;
|
||||||
glm::mat3 rotation;
|
glm::mat3 rotation;
|
||||||
|
glm::vec3 tint;
|
||||||
const model::Mesh* mesh;
|
const model::Mesh* mesh;
|
||||||
const texture_names_map* varTextures;
|
const texture_names_map* varTextures;
|
||||||
};
|
};
|
||||||
@ -77,12 +84,10 @@ public:
|
|||||||
ModelBatch(size_t capacity, Assets* assets, Chunks* chunks);
|
ModelBatch(size_t capacity, Assets* assets, Chunks* chunks);
|
||||||
~ModelBatch();
|
~ModelBatch();
|
||||||
|
|
||||||
void pushMatrix(glm::mat4 matrix);
|
|
||||||
void popMatrix();
|
|
||||||
void draw(glm::mat4 matrix,
|
void draw(glm::mat4 matrix,
|
||||||
|
glm::vec3 tint,
|
||||||
const model::Model* model,
|
const model::Model* model,
|
||||||
const texture_names_map* varTextures);
|
const texture_names_map* varTextures);
|
||||||
|
|
||||||
void render();
|
void render();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -152,22 +152,12 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldRenderer::renderLevel(
|
void WorldRenderer::setupWorldShader(
|
||||||
const DrawContext&,
|
Shader* shader, Camera* camera, const EngineSettings& settings,
|
||||||
Camera* camera,
|
float fogFactor
|
||||||
const EngineSettings& settings,
|
|
||||||
bool pause
|
|
||||||
) {
|
) {
|
||||||
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->use();
|
||||||
|
shader->uniformMatrix("u_model", glm::mat4(1.0f));
|
||||||
shader->uniformMatrix("u_proj", camera->getProjection());
|
shader->uniformMatrix("u_proj", camera->getProjection());
|
||||||
shader->uniformMatrix("u_view", camera->getView());
|
shader->uniformMatrix("u_view", camera->getView());
|
||||||
shader->uniform1f("u_timer", timer);
|
shader->uniform1f("u_timer", timer);
|
||||||
@ -178,6 +168,7 @@ void WorldRenderer::renderLevel(
|
|||||||
shader->uniform3f("u_cameraPos", camera->position);
|
shader->uniform3f("u_cameraPos", camera->position);
|
||||||
shader->uniform1i("u_cubemap", 1);
|
shader->uniform1i("u_cubemap", 1);
|
||||||
|
|
||||||
|
auto indices = level->content->getIndices();
|
||||||
// Light emission when an emissive item is chosen
|
// Light emission when an emissive item is chosen
|
||||||
{
|
{
|
||||||
auto inventory = player->getInventory();
|
auto inventory = player->getInventory();
|
||||||
@ -191,15 +182,32 @@ void WorldRenderer::renderLevel(
|
|||||||
);
|
);
|
||||||
shader->uniform1f("u_torchlightDistance", 6.0f);
|
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();
|
skybox->bind();
|
||||||
atlas->getTexture()->bind();
|
atlas->getTexture()->bind();
|
||||||
|
|
||||||
drawChunks(level->chunks.get(), camera, shader);
|
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);
|
level->entities->render(assets, *modelBatch, *frustumCulling, pause);
|
||||||
|
|
||||||
if (!pause) {
|
if (!pause) {
|
||||||
scripting::on_frontend_render();
|
scripting::on_frontend_render();
|
||||||
}
|
}
|
||||||
@ -263,8 +271,8 @@ void WorldRenderer::renderDebugLines(
|
|||||||
glm::vec3 coord = player->camera->position;
|
glm::vec3 coord = player->camera->position;
|
||||||
if (coord.x < 0) coord.x--;
|
if (coord.x < 0) coord.x--;
|
||||||
if (coord.z < 0) coord.z--;
|
if (coord.z < 0) coord.z--;
|
||||||
int cx = floordiv((int)coord.x, CHUNK_W);
|
int cx = floordiv(static_cast<int>(coord.x), CHUNK_W);
|
||||||
int cz = floordiv((int)coord.z, CHUNK_D);
|
int cz = floordiv(static_cast<int>(coord.z), CHUNK_D);
|
||||||
|
|
||||||
drawBorders(
|
drawBorders(
|
||||||
cx * CHUNK_W, 0, cz * CHUNK_D,
|
cx * CHUNK_W, 0, cz * CHUNK_D,
|
||||||
@ -276,8 +284,8 @@ void WorldRenderer::renderDebugLines(
|
|||||||
glm::vec3 tsl(displayWidth/2, displayHeight/2, 0.f);
|
glm::vec3 tsl(displayWidth/2, displayHeight/2, 0.f);
|
||||||
glm::mat4 model(glm::translate(glm::mat4(1.f), tsl));
|
glm::mat4 model(glm::translate(glm::mat4(1.f), tsl));
|
||||||
linesShader->uniformMatrix("u_projview", glm::ortho(
|
linesShader->uniformMatrix("u_projview", glm::ortho(
|
||||||
0.f, (float)displayWidth,
|
0.f, static_cast<float>(displayWidth),
|
||||||
0.f, (float)displayHeight,
|
0.f, static_cast<float>(displayHeight),
|
||||||
-length, length) * model * glm::inverse(camera->rotation)
|
-length, length) * model * glm::inverse(camera->rotation)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -62,6 +62,13 @@ class WorldRenderer {
|
|||||||
Camera* camera,
|
Camera* camera,
|
||||||
Shader* linesShader
|
Shader* linesShader
|
||||||
);
|
);
|
||||||
|
|
||||||
|
void setupWorldShader(
|
||||||
|
Shader* shader,
|
||||||
|
Camera* camera,
|
||||||
|
const EngineSettings& settings,
|
||||||
|
float fogFactor
|
||||||
|
);
|
||||||
public:
|
public:
|
||||||
static bool showChunkBorders;
|
static bool showChunkBorders;
|
||||||
static bool showEntitiesDebug;
|
static bool showEntitiesDebug;
|
||||||
|
|||||||
@ -99,8 +99,8 @@ void SkeletonConfig::render(
|
|||||||
}
|
}
|
||||||
model = modelOverride.model ? modelOverride.model : model;
|
model = modelOverride.model ? modelOverride.model : model;
|
||||||
if (model) {
|
if (model) {
|
||||||
batch.draw(
|
batch.draw(skeleton.calculated.matrices[i], glm::vec3(1.0f), model,
|
||||||
skeleton.calculated.matrices[i], model, &skeleton.textures);
|
&skeleton.textures);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user