Skip to content

Commit

Permalink
various rendering fixes making it look closer to the actual game
Browse files Browse the repository at this point in the history
  • Loading branch information
aylaylay committed Aug 9, 2019
1 parent ab61d75 commit 4464622
Show file tree
Hide file tree
Showing 16 changed files with 1,761 additions and 41 deletions.
5 changes: 4 additions & 1 deletion assets/shaders/world.frag
@@ -1,6 +1,7 @@
#version 330

uniform sampler2D diffuseTex;
uniform float alphaMask;

in VertexData
{
Expand All @@ -13,5 +14,7 @@ out vec4 fragColor;
void main()
{
vec4 diffuseColor = texture2D(diffuseTex, inData.uv);
fragColor = vec4(diffuseColor.rgb * inData.color.rgb, diffuseColor.a);

if (diffuseColor.a < alphaMask) discard;
else fragColor = vec4(diffuseColor.rgb * inData.color.rgb, diffuseColor.a);
}
5 changes: 4 additions & 1 deletion assets/shaders/world_instanced.frag
@@ -1,6 +1,7 @@
#version 330

uniform sampler2D diffuseTex;
uniform float alphaMask;

in VertexData
{
Expand All @@ -13,5 +14,7 @@ out vec4 fragColor;
void main()
{
vec4 diffuseColor = texture2D(diffuseTex, inData.uv);
fragColor = vec4(diffuseColor.rgb * inData.color.rgb, diffuseColor.a);

if (diffuseColor.a < alphaMask) discard;
else fragColor = vec4(diffuseColor.rgb * inData.color.rgb, diffuseColor.a);
}
8 changes: 4 additions & 4 deletions src/Entity.cpp
Expand Up @@ -13,9 +13,9 @@ StaticEntity::StaticEntity(const P3D::StaticEntity& entity)
_mesh->Commit();
}

void StaticEntity::Draw(const GL::ShaderProgram& shader, bool opaque)
void StaticEntity::Draw(GL::ShaderProgram& shader, bool opaque)
{
_mesh->Draw(opaque);
_mesh->Draw(shader, opaque);
}


Expand All @@ -26,9 +26,9 @@ InstancedStaticEntity::InstancedStaticEntity(const P3D::Mesh& mesh, const std::v
_mesh->Commit();
}

void InstancedStaticEntity::Draw(const GL::ShaderProgram& shader, bool opaque)
void InstancedStaticEntity::Draw(GL::ShaderProgram& shader, bool opaque)
{
_mesh->Draw(opaque);
_mesh->Draw(shader, opaque);
}

} // namespace Donut
6 changes: 3 additions & 3 deletions src/Entity.h
Expand Up @@ -29,7 +29,7 @@ class Entity
Entity() = default;
virtual ~Entity() = default;

virtual void Draw(const GL::ShaderProgram&, bool opaque) {}
virtual void Draw(GL::ShaderProgram&, bool opaque) {}

const std::string& GetName() const { return _name; }
virtual const std::string GetClassName() const { return "Entity"; }
Expand All @@ -43,7 +43,7 @@ class StaticEntity: public Entity
public:
StaticEntity(const P3D::StaticEntity&);

void Draw(const GL::ShaderProgram&, bool opaque) override;
void Draw(GL::ShaderProgram&, bool opaque) override;

const std::string GetClassName() const override { return "StaticEntity"; }

Expand All @@ -56,7 +56,7 @@ class InstancedStaticEntity : public Entity
public:
InstancedStaticEntity(const P3D::Mesh&, const std::vector<glm::mat4>&);

void Draw(const GL::ShaderProgram&, bool opaque) override;
void Draw(GL::ShaderProgram&, bool opaque) override;

const std::string GetClassName() const override { return "InstancedStaticEntity"; }

Expand Down
22 changes: 12 additions & 10 deletions src/Game.cpp
Expand Up @@ -35,6 +35,7 @@
#include <iostream>
#include <sstream>
#include <string>
#include <Render/OpenGL/FrameBuffer.h>

namespace Donut
{
Expand Down Expand Up @@ -329,7 +330,6 @@ void Game::Run()
bool running = true;
while (running)
{

last = now;
now = SDL_GetPerformanceCounter();

Expand Down Expand Up @@ -412,17 +412,22 @@ void Game::Run()

ImGui::Render();

glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
int viewportWidth = 0;
int viewportHeight = 0;

viewportWidth = (int)io.DisplaySize.x;
viewportHeight = (int)io.DisplaySize.y;

glViewport(0, 0, viewportWidth, viewportHeight);

glEnable(GL_DEPTH_TEST);
// glEnable(GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
glEnable(GL_BLEND);

glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glm::mat4 projectionMatrix = glm::perspective(
glm::radians(70.0f), io.DisplaySize.x / io.DisplaySize.y, 0.1f, 10000.0f);
glm::radians(70.0f), (float)viewportWidth / (float)viewportHeight, 0.1f, 10000.0f);

glm::mat4 viewMatrix = _camera->GetViewMatrix();
glm::mat4 viewProjection = projectionMatrix * viewMatrix;
Expand All @@ -434,7 +439,7 @@ void Game::Run()

_lineRenderer->Flush(viewProjection);

glm::mat4 proj = glm::ortho(0.0f, io.DisplaySize.x, io.DisplaySize.y, 0.0f);
glm::mat4 proj = glm::ortho(0.0f, (float)viewportWidth, (float)viewportHeight, 0.0f);

if (_textureFontP3D != nullptr)
{
Expand All @@ -444,9 +449,6 @@ void Game::Run()
sprites.DrawText(font, fps, glm::vec2(32, 32), glm::vec4(1.0f, 1.0f, 0.0f, 1.0f));
}

// glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

sprites.Flush(proj);
frontend->Draw(proj);

Expand Down
21 changes: 11 additions & 10 deletions src/Level.cpp
Expand Up @@ -266,15 +266,21 @@ void Level::unloadRegion(const std::string& filename)

void Level::Draw(glm::mat4& viewProj)
{
glDisable(GL_BLEND);
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);

_worldShader->Bind();
_worldShader->SetUniformValue("viewProj", viewProj);

// draw opaque
glDisable(GL_DEPTH_TEST);

if (_worldSphere != nullptr)
_worldSphere->Draw(true);
{
glDisable(GL_BLEND);
_worldSphere->Draw(*_worldShader, true);
glEnable(GL_BLEND);
_worldSphere->Draw(*_worldShader, false);
}

glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);

for (const auto& ent : _entities)
ent->Draw(*_worldShader, true);
Expand All @@ -289,15 +295,10 @@ void Level::Draw(glm::mat4& viewProj)
ent->Draw(*_worldInstancedShader, true);

glEnable(GL_BLEND);
glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);

_worldShader->Bind();
_worldShader->SetUniformValue("viewProj", viewProj);

// transparent draw after
if (_worldSphere != nullptr)
_worldSphere->Draw(false);

for (const auto& ent : _entities)
ent->Draw(*_worldShader, false);

Expand Down
2 changes: 1 addition & 1 deletion src/Render/CompositeModel.cpp
Expand Up @@ -122,7 +122,7 @@ namespace Donut
for (const auto& prop : _props)
{
shader.SetUniformValue("viewProj", viewProj * modelMatrix * prop.transform);
_meshes[prop.meshIndex]->Draw(opaque);
_meshes[prop.meshIndex]->Draw(shader, opaque);
}
}
} // namespace Donut
28 changes: 26 additions & 2 deletions src/Render/Mesh.cpp
Expand Up @@ -102,7 +102,7 @@ void Mesh::CreateMeshBuffers(const P3D::Mesh& mesh)
std::make_shared<GL::IndexBuffer>(allIndices.data(), allIndices.size(), GL_UNSIGNED_INT);
}

void Mesh::Draw(bool opaque)
void Mesh::Draw(GL::ShaderProgram& shader, bool opaque)
{
_vertexBinding->Bind();

Expand All @@ -111,9 +111,33 @@ void Mesh::Draw(bool opaque)
if (prim.cacheShader == nullptr)
prim.cacheShader = Game::GetInstance().GetResourceManager().GetShader(prim.shaderName).get();

if ((prim.cacheShader->IsAlphaTested() || prim.cacheShader->IsTranslucent()) && opaque)
bool trans = (!prim.cacheShader->IsAlphaTested() && prim.cacheShader->IsTranslucent());

if (trans && opaque)
{
continue;
}

if (!trans && !opaque)
{
continue;
}

if (!opaque)
{
auto blendMode = prim.cacheShader->GetBlendMode();

if (blendMode == BlendMode::Alpha)
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
else if (blendMode == BlendMode::Additive)
{
glBlendFunc(GL_ONE, GL_ONE);
}
}

shader.SetUniformValue("alphaMask", (prim.cacheShader->IsAlphaTested()) ? 0.5f : 0.0f);
prim.cacheShader->Bind(0);

DrawPrimGroup(prim);
Expand Down
2 changes: 1 addition & 1 deletion src/Render/Mesh.h
Expand Up @@ -22,7 +22,7 @@ class Mesh
Mesh(const P3D::Mesh& mesh);

void Commit();
void Draw(bool opaque);
void Draw(GL::ShaderProgram&, bool opaque);

protected:

Expand Down
38 changes: 36 additions & 2 deletions src/Render/OpenGL/FrameBuffer.cpp
Expand Up @@ -3,6 +3,9 @@
#include "FrameBuffer.h"
#include <iostream>

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <ThirdParty/stb_image_write.h>

namespace Donut::GL
{
GLint FrameBuffer::MAX_ATTACHMENTS = -1;
Expand Down Expand Up @@ -188,7 +191,6 @@ namespace Donut::GL
glTexParameteri(_format._target, GL_TEXTURE_WRAP_T, _format._wrapT);
glTexParameteri(_format._target, GL_TEXTURE_MIN_FILTER, _format._filterMin);
glTexParameteri(_format._target, GL_TEXTURE_MAG_FILTER, _format._filterMag);
//glTexParameteri(_format._target, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, _format._target, _depthTextureHandle, 0);
glBindTexture(_format._target, 0);
}
Expand Down Expand Up @@ -337,6 +339,38 @@ namespace Donut::GL
return true;
}

void FrameBuffer::Save()
{
//glBindFramebuffer(GL_READ_FRAMEBUFFER, _handle);
//glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboID);
//glBlitFramebuffer(0, 0, _width, _height, 0, 0, _width, _height, GL_COLOR_BUFFER_BIT, GL_NEAREST);

auto w = _width;
auto h = _height;
uint32_t channels = 3;
std::vector<uint8_t> data(((w * h) * channels) + (w * channels));
uint8_t* row = &data[w * h * channels];

glReadBuffer((GLenum)GL_COLOR_ATTACHMENT0);

GLint alignment;
glGetIntegerv(GL_PACK_ALIGNMENT, &alignment);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, data.data());
glPixelStorei(GL_PACK_ALIGNMENT, alignment);

for (int y = 0; y < h / 2; ++y)
{
memcpy(row, &data[y * w * channels], w * channels);
memcpy(&data[y * w * channels], &data[(h - y - 1) * w * channels], w * channels);
memcpy(&data[(h - y - 1) * w * channels], row, w * channels);
}

stbi_write_png("screenshot.png", w, h, channels, data.data(), w * channels);

int i = 0;
}

void FrameBuffer::Bind()
{
glBindFramebuffer(GL_FRAMEBUFFER, _handle);
Expand All @@ -352,7 +386,7 @@ namespace Donut::GL
}
}

void FrameBuffer::BindTexture(int attachment)
void FrameBuffer::BindColorTexture(int attachment)
{
if (attachment >= (int)_colourTextureHandles.size())
{
Expand Down
5 changes: 4 additions & 1 deletion src/Render/OpenGL/FrameBuffer.h
Expand Up @@ -71,7 +71,7 @@ namespace Donut::GL
~FrameBuffer();

void Bind();
void BindTexture(int attachment);
void BindColorTexture(int attachment);
void BindDepthTexture();
static void Unbind();

Expand All @@ -82,9 +82,12 @@ namespace Donut::GL
inline GLint GetWidth() const { return _width; }
inline GLint GetHeight() const { return _height; }
inline const Format& GetFormat() const { return _format; }
inline GLint GetColorTexture(int attachment) { return _colourTextureHandles.at(attachment); }
static GLint GetMaxSamples();
static GLint GetMaxAttachments();

void Save();

private:

class BindingState
Expand Down
25 changes: 23 additions & 2 deletions src/Render/Shader.cpp
Expand Up @@ -3,6 +3,7 @@
#include <Render/Shader.h>
#include <P3D/p3d.generated.h>
#include <fmt/format.h>
#include <iostream>

namespace Donut
{
Expand All @@ -24,6 +25,18 @@ Shader::Shader(const P3D::Shader& shader):

glGenSamplers(1, &_glSampler);

for (const auto& param : shader.GetFloatParams())
{
if (param->GetKey() == "CBVV")
{
}
else if (param->GetKey() == "MSHP")
{
}
}

uint32_t uvmd = 0;

for (const auto& param : shader.GetIntegerParams())
{
if (param->GetKey() == "FIMD")
Expand Down Expand Up @@ -56,7 +69,8 @@ Shader::Shader(const P3D::Shader& shader):
}
else if (param->GetKey() == "UVMD")
{
GLint glparam = (param->GetValue() == 0) ? GL_REPEAT : GL_CLAMP_TO_EDGE;
uvmd = param->GetValue();
GLint glparam = (uvmd == 0) ? GL_REPEAT : GL_CLAMP_TO_EDGE;

glSamplerParameteri(_glSampler, GL_TEXTURE_WRAP_S, glparam);
glSamplerParameteri(_glSampler, GL_TEXTURE_WRAP_T, glparam);
Expand All @@ -69,8 +83,15 @@ Shader::Shader(const P3D::Shader& shader):
{
_alphaTested = param->GetValue() == 1;
}
else if (param->GetKey() == "BLMD")
{
_blendMode = (BlendMode)param->GetValue();
}
}

// ATST - alpha test
if (uvmd == 0 && _alphaTested)
{
glSamplerParameteri(_glSampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
}

Expand Down

0 comments on commit 4464622

Please sign in to comment.