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 7d0062f270
commit 25f7800c86
8 changed files with 106 additions and 60 deletions

View File

@ -76,8 +76,8 @@
<bColour>0.3</bColour>
<intensity>4.0</intensity>
<flameOffset>-1.5</flameOffset>
<flameHeight>3.0</flameHeight>
<flameWidth>0.8</flameWidth>
<flameHeight>1.0</flameHeight>
<flameWidth>1.0</flameWidth>
</light>
</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")
.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; i<closestFlames.size(); i++) {
closestFlames.at(i)->render(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; i<closestFlames.size(); i++) {
closestFlames.at(i)->render(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<Flame*>();
for (unsigned int i = 0; i<closestLights.size(); i++) {
if (closestLights.at(i).getFlameYOffset() != 0.0f) {
flameData[flameIndex + 0] = closestLights.at(i).getPosition().x;
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;
if (closestLights.at(i).isFlame()) {
closestFlames.push_back(closestLights.at(i).getFlame());
}
}
flame_positions_ab->setDataElements(flameIndex, flameData);
}
void Graphics::resize(glm::uvec2 windowSize) {

View File

@ -39,6 +39,7 @@ class Graphics {
SharedTexture2D loadingScreen;
SharedTexture2D loadingContinueScreen;
std::vector<Light> closestLights;
std::vector<Flame*> 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;

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)) {
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;
}

View File

@ -2,6 +2,7 @@
#define LIGHT_HH_INCLUDED
#include "entity.hh"
#include "flame.hh"
#include <ACGL/Math/Math.hh>
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;
};

View File

@ -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);
}