Skip to content

Commit

Permalink
Merge pull request #433 from NFSMONSTR/add_transparent_pickups2
Browse files Browse the repository at this point in the history
Added transparency for pickups
  • Loading branch information
danhedron committed May 14, 2018
2 parents 9831314 + 45ee3ad commit a8cafe7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
16 changes: 11 additions & 5 deletions rwengine/src/render/GameRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,18 @@ void GameRenderer::renderWorld(GameWorld* world, const ViewCamera& camera,
culled += objectRenderer.culled;
renderer->pushDebugGroup("Objects");
renderer->pushDebugGroup("RenderList");

// Also parallelizable
RW_PROFILE_BEGIN("Sort");
// Earlier position in the array means earlier object's rendering
// Transparent objects should be sorted and rendered after opaque
std::sort(renderList.begin(), renderList.end(),
[](const Renderer::RenderInstruction& a,
const Renderer::RenderInstruction& b) {
if (!a.drawInfo.blend && b.drawInfo.blend) return true;
if (a.drawInfo.blend && !b.drawInfo.blend) return false;
if (a.drawInfo.blendMode==BlendMode::BLEND_NONE && b.drawInfo.blendMode!=BlendMode::BLEND_NONE) return true;
if (a.drawInfo.blendMode!=BlendMode::BLEND_NONE && b.drawInfo.blendMode==BlendMode::BLEND_NONE) return false;
return (a.sortKey > b.sortKey);
});
RW_PROFILE_END();

RW_PROFILE_BEGIN("Draw");
renderer->drawBatched(renderList);
RW_PROFILE_END();
Expand Down Expand Up @@ -410,6 +408,8 @@ void GameRenderer::renderWorld(GameWorld* world, const ViewCamera& camera,

float fadeTimer = world->getGameTime() - world->state->fadeStart;
if (fadeTimer < world->state->fadeTime || !world->state->fadeOut) {
/// @todo rewrite this render code to use renderer class
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(ssRectProgram);
glUniform2f(ssRectOffset, 0.f, 0.f);
glUniform2f(ssRectSize, 1.f, 1.f);
Expand Down Expand Up @@ -519,7 +519,7 @@ void GameRenderer::renderEffects(GameWorld* world) {
dp.colour = glm::u8vec4(particle.colour * 255.f);
dp.start = 0;
dp.count = 4;
dp.blend = true;
dp.blendMode = BlendMode::BLEND_ADDITIVE;
dp.diffuse = 1.f;

renderer->drawArrays(transformMat, &particleDraw, dp);
Expand All @@ -544,7 +544,9 @@ void GameRenderer::drawTexture(TextureData* texture, glm::vec4 extents) {
extents.y -= .5f;
extents *= glm::vec4(2.f, -2.f, 1.f, 1.f);

/// @todo rewrite this render code to use renderer class
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUniform2f(ssRectOffset, extents.x, extents.y);
glUniform2f(ssRectSize, extents.z, extents.w);

Expand Down Expand Up @@ -574,7 +576,9 @@ void GameRenderer::drawColour(const glm::vec4& colour, glm::vec4 extents) {
extents.y -= .5f;
extents *= glm::vec4(2.f, -2.f, 1.f, 1.f);

/// @todo rewrite this render code to use renderer class
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUniform2f(ssRectOffset, extents.x, extents.y);
glUniform2f(ssRectSize, extents.z, extents.w);

Expand Down Expand Up @@ -674,7 +678,9 @@ void GameRenderer::renderPaths() {
}

void GameRenderer::renderLetterbox() {
/// @todo rewrite this render code to use renderer class
glUseProgram(ssRectProgram);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
const float cinematicExperienceSize = 0.15f;
glUniform2f(ssRectOffset, 0.f, -1.f * (1.f - cinematicExperienceSize));
glUniform2f(ssRectSize, 1.f, cinematicExperienceSize);
Expand Down
2 changes: 1 addition & 1 deletion rwengine/src/render/MapRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void MapRenderer::draw(GameWorld* world, const MapInfo& mi) {

Renderer::DrawParameters dp { };
dp.start = 0;
dp.blend = true;
dp.blendMode = BlendMode::BLEND_ALPHA;
dp.depthWrite = false;

// World out the number of units per tile
Expand Down
2 changes: 1 addition & 1 deletion rwengine/src/render/ObjectRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void ObjectRenderer::renderGeometry(Geometry* geom,
dp.ambient = mat.ambientIntensity;
}

dp.blend = isTransparent;
dp.blendMode = isTransparent ? BlendMode::BLEND_ALPHA : BlendMode::BLEND_NONE;

glm::vec3 position(modelMatrix[3]);
float distance = glm::length(m_camera.position - position);
Expand Down
5 changes: 2 additions & 3 deletions rwengine/src/render/OpenGLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void OpenGLRenderer::setDrawState(const glm::mat4& model, DrawBuffer* draw,
useTexture(u, p.textures[u]);
}

setBlend(p.blend);
setBlend(p.blendMode);
setDepthWrite(p.depthWrite);

ObjectUniformData objectData{model,
Expand Down Expand Up @@ -368,8 +368,7 @@ void OpenGLRenderer::invalidate() {
currentProgram = nullptr;
currentTextures.clear();
currentUBO = 0;
blendEnabled = false;
glDisable(GL_BLEND);
setBlend(BlendMode::BLEND_NONE);
}

bool OpenGLRenderer::createUBO(Buffer &out, GLsizei size, GLsizei entrySize)
Expand Down
45 changes: 34 additions & 11 deletions rwengine/src/render/OpenGLRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ struct VertexP3 {
VertexP3() = default;
};

/**
* Enum used to determine which blending mode to use
*/
enum class BlendMode {
BLEND_NONE,
BLEND_ALPHA,
BLEND_ADDITIVE
};

class Renderer {
public:
typedef std::vector<GLuint> Textures;
Expand All @@ -73,8 +82,8 @@ class Renderer {
unsigned int start;
/// Textures to use
Textures textures;
/// Alpha blending state
bool blend;
/// Blending mode
BlendMode blendMode;
// Depth writing state
bool depthWrite;
/// Material
Expand All @@ -88,7 +97,7 @@ class Renderer {

// Default state -- should be moved to materials
DrawParameters()
: blend(false)
: blendMode(BlendMode::BLEND_NONE)
, depthWrite(true)
, ambient(1.f)
, diffuse(1.f)
Expand Down Expand Up @@ -338,22 +347,36 @@ class OpenGLRenderer : public Renderer {
// State Cache
DrawBuffer* currentDbuff = nullptr;
OpenGLShaderProgram* currentProgram = nullptr;
bool blendEnabled = false;
BlendMode blendMode = BlendMode::BLEND_NONE;
bool depthWriteEnabled = false;
GLuint currentUBO = 0;
GLuint currentUnit = 0;
std::map<GLuint, GLuint> currentTextures;

// Set state
void setBlend(bool enable) {
if (enable && !blendEnabled) {
void setBlend(BlendMode mode) {
if (mode!=BlendMode::BLEND_NONE && blendMode==BlendMode::BLEND_NONE)//To don't call glEnable again when it already enabled
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
blendEnabled = enable;
} else if(!enable && blendEnabled) {
glDisable(GL_BLEND);
blendEnabled = enable;

if (mode!=blendMode) {
switch (mode) {
default:
assert(false);
break;
case BlendMode::BLEND_NONE:
glDisable(GL_BLEND);
break;
case BlendMode::BLEND_ALPHA:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
break;
case BlendMode::BLEND_ADDITIVE:
glBlendFunc(GL_ONE, GL_ONE);
break;

}
}

blendMode = mode;
}

void setDepthWrite(bool enable) {
Expand Down
2 changes: 1 addition & 1 deletion rwengine/src/render/TextRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ void TextRenderer::renderText(const TextRenderer::TextInfo& ti,

Renderer::DrawParameters dp;
dp.start = 0;
dp.blend = true;
dp.blendMode = BlendMode::BLEND_ALPHA;
dp.count = gb.getCount();
auto ftexture = renderer->getData()->findSlotTexture("fonts", fonts[ti.font]);
dp.textures = {ftexture->getName()};
Expand Down

0 comments on commit a8cafe7

Please sign in to comment.