Gave flames their own class again. The size of the flames are now respected.

This commit is contained in:
Faerbit 2015-03-07 22:48:44 +01:00
parent d3c812bba3
commit 3eeea52f5e
8 changed files with 106 additions and 60 deletions

View File

@ -76,8 +76,8 @@
<bColour>0.3</bColour> <bColour>0.3</bColour>
<intensity>4.0</intensity> <intensity>4.0</intensity>
<flameOffset>-1.5</flameOffset> <flameOffset>-1.5</flameOffset>
<flameHeight>3.0</flameHeight> <flameHeight>1.0</flameHeight>
<flameWidth>0.8</flameWidth> <flameWidth>1.0</flameWidth>
</light> </light>
</composition> </composition>

50
flame.cc Normal file
View File

@ -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<float>(size * glm::vec3(1.1f));
}
else {
modelMatrix = glm::scale<float>(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();
}

18
flame.hh Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include "entity.hh"
#include <ACGL/OpenGL/Objects.hh>
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;
};

View File

@ -68,11 +68,10 @@ void Graphics::init(Level* level) {
depthCubeShader = ShaderProgramCreator("depth_cube") depthCubeShader = ShaderProgramCreator("depth_cube")
.attributeLocations(vao->getAttributeLocations()).create(); .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("aPosition", GL_FLOAT, 3);
flame_positions_ab->defineAttribute("aColor", GL_FLOAT, 3); flame_positions_ab->defineAttribute("aColor", GL_FLOAT, 3);
flame_positions = SharedVertexArrayObject(new VertexArrayObject()); SharedVertexArrayObject flame_positions = SharedVertexArrayObject(new VertexArrayObject());
flame_positions->setMode(GL_POINTS);
flame_positions->attachAllAttributes(flame_positions_ab); flame_positions->attachAllAttributes(flame_positions_ab);
flameShader = ShaderProgramCreator("flame") flameShader = ShaderProgramCreator("flame")
@ -346,45 +345,21 @@ void Graphics::render(double time)
glCullFace(GL_BACK); glCullFace(GL_BACK);
// draw with colors // draw with colors
flameShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix); for(unsigned int i = 0; i<closestFlames.size(); i++) {
flameShader->setUniform("modelViewProjectionMatrix", lightingViewProjectionMatrix); closestFlames.at(i)->render(flameShader, lightingViewProjectionMatrix, float(time), true);
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();
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
// draw slightly larger only for stencil buffer to blur edges // draw slightly larger only for stencil buffer to blur edges
flameShader->use();
glEnable(GL_STENCIL_TEST); glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 0xFF); //Set any stencil to 1 glStencilFunc(GL_ALWAYS, 1, 0xFF); //Set any stencil to 1
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glStencilMask(0xFF);//write to stencil buffer glStencilMask(0xFF);//write to stencil buffer
glClear(GL_STENCIL_BUFFER_BIT);//clear stencil buffer glClear(GL_STENCIL_BUFFER_BIT);//clear stencil buffer
glm::mat4 modelMatrix = glm::scale(glm::vec3(1.1f)); for(unsigned int i = 0; i<closestFlames.size(); i++) {
flameShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix); closestFlames.at(i)->render(flameShader, lightingViewProjectionMatrix, float(time), false);
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();
glStencilFunc(GL_EQUAL, 1, 0xFF); //Pass test if stencil value is 1 glStencilFunc(GL_EQUAL, 1, 0xFF); //Pass test if stencil value is 1
glStencilMask(0x00);// don't write to stencil buffer glStencilMask(0x00);// don't write to stencil buffer
@ -462,21 +437,12 @@ void Graphics::updateLights() {
lightingShader->setUniform("directionalIntensity", lightingShader->setUniform("directionalIntensity",
level->getDirectionalLight()->getIntensity()); level->getDirectionalLight()->getIntensity());
} }
float* flameData = new float[closestLights.size() * 6]; closestFlames = std::vector<Flame*>();
int flameIndex = 0;
for (unsigned int i = 0; i<closestLights.size(); i++) { for (unsigned int i = 0; i<closestLights.size(); i++) {
if (closestLights.at(i).getFlameYOffset() != 0.0f) { if (closestLights.at(i).isFlame()) {
flameData[flameIndex + 0] = closestLights.at(i).getPosition().x; closestFlames.push_back(closestLights.at(i).getFlame());
flameData[flameIndex + 1] = closestLights.at(i).getPosition().y + closestLights.at(i).getFlameYOffset();
flameData[flameIndex + 2] = closestLights.at(i).getPosition().z;
flameData[flameIndex + 3] = closestLights.at(i).getColour().r;
flameData[flameIndex + 3] = closestLights.at(i).getColour().r;
flameData[flameIndex + 4] = closestLights.at(i).getColour().g;
flameData[flameIndex + 5] = closestLights.at(i).getColour().b;
flameIndex+=6;
} }
} }
flame_positions_ab->setDataElements(flameIndex, flameData);
} }
void Graphics::resize(glm::uvec2 windowSize) { void Graphics::resize(glm::uvec2 windowSize) {

View File

@ -39,6 +39,7 @@ class Graphics {
SharedTexture2D loadingScreen; SharedTexture2D loadingScreen;
SharedTexture2D loadingContinueScreen; SharedTexture2D loadingContinueScreen;
std::vector<Light> closestLights; std::vector<Light> closestLights;
std::vector<Flame*> closestFlames;
SharedShaderProgram loadingShader; SharedShaderProgram loadingShader;
SharedShaderProgram lightingShader; SharedShaderProgram lightingShader;
SharedShaderProgram skydomeShader; SharedShaderProgram skydomeShader;
@ -53,8 +54,6 @@ class Graphics {
SharedFrameBufferObject framebuffer_light; SharedFrameBufferObject framebuffer_light;
SharedTexture2D light_fbo_color_texture; SharedTexture2D light_fbo_color_texture;
SharedTexture2D light_fbo_depth_texture; SharedTexture2D light_fbo_depth_texture;
SharedVertexArrayObject flame_positions;
SharedArrayBuffer flame_positions_ab;
SharedVertexArrayObject fullscreen_quad; SharedVertexArrayObject fullscreen_quad;
SharedArrayBuffer fullscreen_quad_ab; SharedArrayBuffer fullscreen_quad_ab;
int cube_size; int cube_size;

View File

@ -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)) { : Entity(position, glm::vec3(0.0f, 0.0f, 0.0f)) {
this->colour = colour; this->colour = colour;
this->intensity = intensity; this->intensity = intensity;
this->flameYOffset = flameYOffset; if (flameYOffset != 0.0f) {
this->flameHeight = flameHeight; this->withFlame = true;
this->flameWidth = flameWidth; 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() { Light::Light() {
@ -23,6 +29,10 @@ float Light::getIntensity() {
return intensity; return intensity;
} }
float Light::getFlameYOffset() { Flame* Light::getFlame() {
return flameYOffset; return &flame;
}
bool Light::isFlame() {
return withFlame;
} }

View File

@ -2,6 +2,7 @@
#define LIGHT_HH_INCLUDED #define LIGHT_HH_INCLUDED
#include "entity.hh" #include "entity.hh"
#include "flame.hh"
#include <ACGL/Math/Math.hh> #include <ACGL/Math/Math.hh>
class Light : public Entity { class Light : public Entity {
@ -10,12 +11,12 @@ class Light : public Entity {
Light(); Light();
glm::vec3 getColour(); glm::vec3 getColour();
float getIntensity(); float getIntensity();
float getFlameYOffset(); Flame* getFlame();
~Light(); ~Light();
bool isFlame();
private: private:
float flameYOffset; bool withFlame;
float flameHeight; Flame flame;
float flameWidth;
float intensity; float intensity;
glm::vec3 colour; glm::vec3 colour;
}; };

View File

@ -364,8 +364,10 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
if (flameOffset != NULL){ if (flameOffset != NULL){
float offset = 0; float offset = 0;
errorCheck(flameOffset->QueryFloatText(&offset)); errorCheck(flameOffset->QueryFloatText(&offset));
float flameHeight = queryBool(xmlLight, "flameHeight"); float flameHeight = queryFloat(xmlLight, "flameHeight");
float flameWidth = queryBool(xmlLight, "flameWidth"); 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); Light light = Light(lightPosition, lightColour, lightIntensity, offset, flameHeight, flameWidth);
level->addLight(light); level->addLight(light);
} }