From 3eeea52f5e515c07d0461ee3bba8322c3fa29e8b Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 7 Mar 2015 22:48:44 +0100 Subject: [PATCH] Gave flames their own class again. The size of the flames are now respected. --- data/levels/Compositions.xml | 4 +-- flame.cc | 50 ++++++++++++++++++++++++++++++++ flame.hh | 18 ++++++++++++ graphics.cc | 56 +++++++----------------------------- graphics.hh | 3 +- light.cc | 20 +++++++++---- light.hh | 9 +++--- loader.cc | 6 ++-- 8 files changed, 106 insertions(+), 60 deletions(-) create mode 100644 flame.cc create mode 100644 flame.hh diff --git a/data/levels/Compositions.xml b/data/levels/Compositions.xml index b805836..758be76 100644 --- a/data/levels/Compositions.xml +++ b/data/levels/Compositions.xml @@ -76,8 +76,8 @@ 0.3 4.0 -1.5 - 3.0 - 0.8 + 1.0 + 1.0 diff --git a/flame.cc b/flame.cc new file mode 100644 index 0000000..1fafe2d --- /dev/null +++ b/flame.cc @@ -0,0 +1,50 @@ +#include "flame.hh" + +Flame::Flame(glm::vec3 position, glm::vec3 color, glm::vec3 size) : + Entity(position, glm::vec3(0.0f, 0.0f, 0.0f)) { + this->color = color; + this->size = size; + float flameData[6] = {}; + flameData[0] = position.x; + flameData[1] = position.y; + flameData[2] = position.z; + flameData[3] = color.r; + flameData[4] = color.g; + flameData[5] = color.b; + ab = SharedArrayBuffer(new ArrayBuffer()); + ab->defineAttribute("aPosition", GL_FLOAT, 3); + ab->defineAttribute("aColor", GL_FLOAT, 3); + vao = SharedVertexArrayObject(new VertexArrayObject()); + vao->setMode(GL_POINTS); + vao->attachAllAttributes(ab); + ab->setDataElements(1, flameData); +} + +Flame::Flame() { +} + +void Flame::render(SharedShaderProgram shader, glm::mat4 viewProjectionMatrix, float time, + bool withColor) { + glm::mat4 modelMatrix; + if (!withColor) { + modelMatrix = glm::scale(size * glm::vec3(1.1f)); + } + else { + modelMatrix = glm::scale(size); + } + glm::mat4 modelViewProjectionMatrix = viewProjectionMatrix * modelMatrix; + shader->setUniform("modelViewProjectionMatrix", modelViewProjectionMatrix); + shader->setUniform("viewProjectionMatrix", viewProjectionMatrix); + shader->setUniform("withColor", withColor); + shader->setUniform("time", time); + shader->setUniform("bottom", true); + shader->setUniform("left", true); + vao->render(); + shader->setUniform("left", false); + vao->render(); + shader->setUniform("bottom", false); + shader->setUniform("left", true); + vao->render(); + shader->setUniform("left", false); + vao->render(); +} diff --git a/flame.hh b/flame.hh new file mode 100644 index 0000000..b9372a3 --- /dev/null +++ b/flame.hh @@ -0,0 +1,18 @@ +#pragma once +#include "entity.hh" +#include + +using namespace ACGL::OpenGL; + +class Flame : public Entity { + public: + Flame(glm::vec3 position, glm::vec3 color, glm::vec3 size); + Flame(); + void render(SharedShaderProgram shader, glm::mat4 viewProjectionMatrix, + float time, bool withColor); + private: + glm::vec3 color; + glm::vec3 size; + SharedVertexArrayObject vao; + SharedArrayBuffer ab; +}; diff --git a/graphics.cc b/graphics.cc index 7a00fef..e21ea4b 100644 --- a/graphics.cc +++ b/graphics.cc @@ -68,11 +68,10 @@ void Graphics::init(Level* level) { depthCubeShader = ShaderProgramCreator("depth_cube") .attributeLocations(vao->getAttributeLocations()).create(); - flame_positions_ab = SharedArrayBuffer(new ArrayBuffer()); + SharedArrayBuffer flame_positions_ab = SharedArrayBuffer(new ArrayBuffer()); flame_positions_ab->defineAttribute("aPosition", GL_FLOAT, 3); flame_positions_ab->defineAttribute("aColor", GL_FLOAT, 3); - flame_positions = SharedVertexArrayObject(new VertexArrayObject()); - flame_positions->setMode(GL_POINTS); + SharedVertexArrayObject flame_positions = SharedVertexArrayObject(new VertexArrayObject()); flame_positions->attachAllAttributes(flame_positions_ab); flameShader = ShaderProgramCreator("flame") @@ -346,45 +345,21 @@ void Graphics::render(double time) glCullFace(GL_BACK); // draw with colors - flameShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix); - flameShader->setUniform("modelViewProjectionMatrix", lightingViewProjectionMatrix); - flameShader->setUniform("withColor", true); - flameShader->setUniform("time", (float) time); - flameShader->setUniform("bottom", true); - flameShader->setUniform("left", true); - flame_positions->render(); - flameShader->setUniform("left", false); - flame_positions->render(); - flameShader->setUniform("bottom", false); - flameShader->setUniform("left", true); - flame_positions->render(); - flameShader->setUniform("left", false); - flame_positions->render(); + for(unsigned int i = 0; irender(flameShader, lightingViewProjectionMatrix, float(time), true); + } glDisable(GL_CULL_FACE); // draw slightly larger only for stencil buffer to blur edges - flameShader->use(); glEnable(GL_STENCIL_TEST); glStencilFunc(GL_ALWAYS, 1, 0xFF); //Set any stencil to 1 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); glStencilMask(0xFF);//write to stencil buffer glClear(GL_STENCIL_BUFFER_BIT);//clear stencil buffer - glm::mat4 modelMatrix = glm::scale(glm::vec3(1.1f)); - flameShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix); - flameShader->setUniform("modelViewProjectionMatrix", lightingViewProjectionMatrix * modelMatrix); - flameShader->setUniform("withColor", false); - flameShader->setUniform("time", (float) time); - flameShader->setUniform("bottom", true); - flameShader->setUniform("left", true); - flame_positions->render(); - flameShader->setUniform("left", false); - flame_positions->render(); - flameShader->setUniform("bottom", false); - flameShader->setUniform("left", true); - flame_positions->render(); - flameShader->setUniform("left", false); - flame_positions->render(); + for(unsigned int i = 0; irender(flameShader, lightingViewProjectionMatrix, float(time), false); + } glStencilFunc(GL_EQUAL, 1, 0xFF); //Pass test if stencil value is 1 glStencilMask(0x00);// don't write to stencil buffer @@ -462,21 +437,12 @@ void Graphics::updateLights() { lightingShader->setUniform("directionalIntensity", level->getDirectionalLight()->getIntensity()); } - float* flameData = new float[closestLights.size() * 6]; - int flameIndex = 0; + closestFlames = std::vector(); for (unsigned int i = 0; isetDataElements(flameIndex, flameData); } void Graphics::resize(glm::uvec2 windowSize) { diff --git a/graphics.hh b/graphics.hh index eb22ae5..085b15c 100644 --- a/graphics.hh +++ b/graphics.hh @@ -39,6 +39,7 @@ class Graphics { SharedTexture2D loadingScreen; SharedTexture2D loadingContinueScreen; std::vector closestLights; + std::vector closestFlames; SharedShaderProgram loadingShader; SharedShaderProgram lightingShader; SharedShaderProgram skydomeShader; @@ -53,8 +54,6 @@ class Graphics { SharedFrameBufferObject framebuffer_light; SharedTexture2D light_fbo_color_texture; SharedTexture2D light_fbo_depth_texture; - SharedVertexArrayObject flame_positions; - SharedArrayBuffer flame_positions_ab; SharedVertexArrayObject fullscreen_quad; SharedArrayBuffer fullscreen_quad_ab; int cube_size; diff --git a/light.cc b/light.cc index e523ba5..00193f8 100644 --- a/light.cc +++ b/light.cc @@ -4,9 +4,15 @@ Light::Light(glm::vec3 position, glm::vec3 colour, float intensity, float flameY : Entity(position, glm::vec3(0.0f, 0.0f, 0.0f)) { this->colour = colour; this->intensity = intensity; - this->flameYOffset = flameYOffset; - this->flameHeight = flameHeight; - this->flameWidth = flameWidth; + if (flameYOffset != 0.0f) { + this->withFlame = true; + glm::vec3 flamePosition = glm::vec3(position.x, position.y + flameYOffset, position.z); + glm::vec3 flameSize = glm::vec3(flameWidth, flameHeight, flameWidth); + this->flame = Flame(flamePosition, colour, flameSize); + } + else { + this->withFlame = false; + } } Light::Light() { @@ -23,6 +29,10 @@ float Light::getIntensity() { return intensity; } -float Light::getFlameYOffset() { - return flameYOffset; +Flame* Light::getFlame() { + return &flame; +} + +bool Light::isFlame() { + return withFlame; } diff --git a/light.hh b/light.hh index 0ad997c..6870c37 100644 --- a/light.hh +++ b/light.hh @@ -2,6 +2,7 @@ #define LIGHT_HH_INCLUDED #include "entity.hh" +#include "flame.hh" #include class Light : public Entity { @@ -10,12 +11,12 @@ class Light : public Entity { Light(); glm::vec3 getColour(); float getIntensity(); - float getFlameYOffset(); + Flame* getFlame(); ~Light(); + bool isFlame(); private: - float flameYOffset; - float flameHeight; - float flameWidth; + bool withFlame; + Flame flame; float intensity; glm::vec3 colour; }; diff --git a/loader.cc b/loader.cc index 36ca918..82ff424 100644 --- a/loader.cc +++ b/loader.cc @@ -364,8 +364,10 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa if (flameOffset != NULL){ float offset = 0; errorCheck(flameOffset->QueryFloatText(&offset)); - float flameHeight = queryBool(xmlLight, "flameHeight"); - float flameWidth = queryBool(xmlLight, "flameWidth"); + float flameHeight = queryFloat(xmlLight, "flameHeight"); + printf("loading: flameHeight: %2.2f\n", flameHeight); + float flameWidth = queryFloat(xmlLight, "flameWidth"); + printf("loading: flameWidth: %2.2f\n", flameWidth); Light light = Light(lightPosition, lightColour, lightIntensity, offset, flameHeight, flameWidth); level->addLight(light); }