2014-10-20 16:49:10 +00:00
|
|
|
#include "graphics.hh"
|
2015-03-12 10:41:19 +00:00
|
|
|
#include <lodepng/lodepng.h>
|
2014-10-20 15:31:26 +00:00
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2015-02-12 00:13:26 +00:00
|
|
|
#include <functional>
|
2014-11-14 15:47:47 +00:00
|
|
|
|
2014-12-01 16:49:59 +00:00
|
|
|
#include <ACGL/OpenGL/Creator/ShaderProgramCreator.hh>
|
2015-03-13 14:30:21 +00:00
|
|
|
#include "LinearMath/btIDebugDraw.h"
|
2014-12-01 16:49:59 +00:00
|
|
|
|
2014-12-04 00:13:59 +00:00
|
|
|
using namespace ACGL::OpenGL;
|
|
|
|
|
2015-01-25 23:18:09 +00:00
|
|
|
const double lightUpdateDelay = 0.5f;
|
2015-03-09 13:14:17 +00:00
|
|
|
const double windUpdateDelay = 0.5f;
|
2015-06-02 19:14:22 +00:00
|
|
|
const int maxShadowSampleCount = 26;
|
2014-12-15 01:09:33 +00:00
|
|
|
|
2015-02-13 15:20:22 +00:00
|
|
|
Graphics::Graphics(glm::uvec2 windowSize, float nearPlane,
|
|
|
|
float farPlane, int cube_size,
|
2015-03-07 18:59:52 +00:00
|
|
|
unsigned int maxShadowRenderCount,
|
|
|
|
std::string screenPath,
|
|
|
|
std::string screenContinuePath) {
|
2014-12-01 16:49:59 +00:00
|
|
|
this->windowSize = windowSize;
|
|
|
|
this->nearPlane = nearPlane;
|
2015-06-02 16:39:17 +00:00
|
|
|
if (farPlane > 0) {
|
|
|
|
this->farPlane = farPlane;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->farPlane = 0;
|
|
|
|
}
|
|
|
|
if (cube_size > 0) {
|
|
|
|
this->cube_size = cube_size;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->cube_size = 0;
|
|
|
|
}
|
2015-06-02 19:14:22 +00:00
|
|
|
if (maxShadowRenderCount < maxShadowSampleCount) {
|
2015-06-02 16:39:17 +00:00
|
|
|
this->maxShadowRenderCount = maxShadowRenderCount;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->maxShadowRenderCount = 0;
|
|
|
|
}
|
2015-03-07 18:59:52 +00:00
|
|
|
this->loadingScreenPath = screenPath;
|
|
|
|
this->loadingScreenContinuePath = screenContinuePath;
|
|
|
|
gameStart = false;
|
2015-03-12 18:24:10 +00:00
|
|
|
renderShadows = true;
|
|
|
|
renderFlames = true;
|
2015-03-13 14:43:10 +00:00
|
|
|
renderWorld = true;
|
2015-03-13 14:30:21 +00:00
|
|
|
renderDebug = false;
|
2014-12-01 16:49:59 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
Graphics::Graphics() {
|
|
|
|
}
|
2014-10-20 15:31:26 +00:00
|
|
|
|
2014-12-15 00:05:46 +00:00
|
|
|
void Graphics::init(Level* level) {
|
|
|
|
// save Level
|
|
|
|
this->level = level;
|
2015-03-01 15:41:46 +00:00
|
|
|
|
|
|
|
// OpenGL state:
|
|
|
|
glClearColor( 0.0, 0.0, 0.0, 1.0 );
|
|
|
|
glEnable( GL_DEPTH_TEST );
|
|
|
|
glEnable(GL_BLEND);
|
2015-03-06 08:16:29 +00:00
|
|
|
glBlendEquation(GL_FUNC_ADD);
|
2015-03-01 15:41:46 +00:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
|
2015-03-09 07:21:44 +00:00
|
|
|
glEnable(GL_MULTISAMPLE);
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2015-03-18 07:58:24 +00:00
|
|
|
lastLightUpdate = 0;
|
2015-03-09 13:14:17 +00:00
|
|
|
|
|
|
|
lastWindUpdate = - windUpdateDelay;
|
|
|
|
windTarget = 0.0f;
|
|
|
|
wind = glm::vec2(0.0f, 0.0f);
|
2015-05-31 15:36:58 +00:00
|
|
|
windDirection = glm::vec2(1.0f, 1.0f);
|
|
|
|
windDirectionTarget = glm::vec2(1.0f, 1.0f);
|
2015-03-09 13:14:17 +00:00
|
|
|
textureMovementPosition = glm::vec2(0.0, 0.0);
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-12-01 16:49:59 +00:00
|
|
|
// construct VAO to give shader correct Attribute locations
|
2014-12-04 00:13:59 +00:00
|
|
|
SharedArrayBuffer ab = SharedArrayBuffer(new ArrayBuffer());
|
2014-12-01 16:49:59 +00:00
|
|
|
ab->defineAttribute("aPosition", GL_FLOAT, 3);
|
|
|
|
ab->defineAttribute("aTexCoord", GL_FLOAT, 2);
|
|
|
|
ab->defineAttribute("aNormal", GL_FLOAT, 3);
|
2014-12-04 00:13:59 +00:00
|
|
|
SharedVertexArrayObject vao = SharedVertexArrayObject(new VertexArrayObject());
|
2014-12-01 16:49:59 +00:00
|
|
|
vao->attachAllAttributes(ab);
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-12-01 16:49:59 +00:00
|
|
|
// look up all shader files starting with 'phong' and build a ShaderProgram from it:
|
2014-12-04 00:13:59 +00:00
|
|
|
lightingShader = ShaderProgramCreator("phong").attributeLocations(
|
2014-12-01 16:49:59 +00:00
|
|
|
vao->getAttributeLocations()).create();
|
2015-03-04 15:08:03 +00:00
|
|
|
|
|
|
|
skydomeShader = ShaderProgramCreator("skydome").attributeLocations(
|
|
|
|
vao->getAttributeLocations()).create();
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-12-04 00:13:59 +00:00
|
|
|
depthShader = ShaderProgramCreator("depth")
|
2014-12-04 16:19:58 +00:00
|
|
|
.attributeLocations(vao->getAttributeLocations()).create();
|
|
|
|
|
2015-02-09 23:27:13 +00:00
|
|
|
depthCubeShader = ShaderProgramCreator("depth_cube")
|
|
|
|
.attributeLocations(vao->getAttributeLocations()).create();
|
|
|
|
|
2015-03-07 21:48:44 +00:00
|
|
|
SharedArrayBuffer flame_positions_ab = SharedArrayBuffer(new ArrayBuffer());
|
2015-02-24 22:30:59 +00:00
|
|
|
flame_positions_ab->defineAttribute("aPosition", GL_FLOAT, 3);
|
2015-03-01 17:45:31 +00:00
|
|
|
flame_positions_ab->defineAttribute("aColor", GL_FLOAT, 3);
|
2015-03-07 21:48:44 +00:00
|
|
|
SharedVertexArrayObject flame_positions = SharedVertexArrayObject(new VertexArrayObject());
|
2015-02-24 22:30:59 +00:00
|
|
|
flame_positions->attachAllAttributes(flame_positions_ab);
|
|
|
|
|
|
|
|
flameShader = ShaderProgramCreator("flame")
|
|
|
|
.attributeLocations(flame_positions->getAttributeLocations()).create();
|
|
|
|
|
2015-03-08 16:05:52 +00:00
|
|
|
fullscreen_quad_ab = SharedArrayBuffer(new ArrayBuffer());
|
|
|
|
fullscreen_quad_ab->defineAttribute("aPosition", GL_FLOAT, 2);
|
|
|
|
fullscreen_quad_ab->defineAttribute("aTexCoord", GL_FLOAT, 2);
|
|
|
|
|
|
|
|
float quadData[] = {
|
|
|
|
-1.0f, 1.0f, 0.0f, 1.0f,
|
|
|
|
1.0f, 1.0f, 1.0f, 1.0f,
|
|
|
|
1.0f, -1.0f, 1.0f, 0.0f,
|
|
|
|
|
|
|
|
1.0f, -1.0f, 1.0f, 0.0f,
|
|
|
|
-1.0f, -1.0f, 0.0f, 0.0f,
|
|
|
|
-1.0f, 1.0f, 0.0f, 1.0f
|
|
|
|
};
|
|
|
|
|
|
|
|
fullscreen_quad_ab->setDataElements(6, quadData);
|
|
|
|
|
|
|
|
fullscreen_quad = SharedVertexArrayObject(new VertexArrayObject);
|
|
|
|
fullscreen_quad->attachAllAttributes(fullscreen_quad_ab);
|
|
|
|
|
2015-03-03 22:27:41 +00:00
|
|
|
flamePostShader = ShaderProgramCreator("flame_post")
|
|
|
|
.attributeLocations(fullscreen_quad->getAttributeLocations()).create();
|
|
|
|
|
2015-03-13 14:30:21 +00:00
|
|
|
debug_ab = SharedArrayBuffer(new ArrayBuffer());
|
|
|
|
debug_ab->defineAttribute("aPosition", GL_FLOAT, 3);
|
|
|
|
debug_ab->defineAttribute("aColor", GL_FLOAT, 3);
|
|
|
|
debug_vao = SharedVertexArrayObject(new VertexArrayObject());
|
|
|
|
debug_vao->attachAllAttributes(debug_ab);
|
|
|
|
debug_vao->setMode(GL_LINES);
|
|
|
|
|
|
|
|
debugShader = ShaderProgramCreator("debug")
|
|
|
|
.attributeLocations(debug_vao->getAttributeLocations()).create();
|
|
|
|
|
|
|
|
debugDrawer = DebugDraw();
|
|
|
|
|
|
|
|
level->getPhysics()->getWorld()->setDebugDrawer(&debugDrawer);
|
|
|
|
|
2015-02-12 00:13:26 +00:00
|
|
|
|
2015-05-31 15:36:21 +00:00
|
|
|
depth_directionalMaps = std::vector<SharedTexture2D>(5);
|
|
|
|
framebuffer_directional = std::vector<SharedFrameBufferObject>(5);
|
2015-02-28 13:40:54 +00:00
|
|
|
for (unsigned int i = 0; i<depth_directionalMaps.size(); i++) {
|
|
|
|
depth_directionalMaps.at(i) = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT24));
|
|
|
|
depth_directionalMaps.at(i)->setMinFilter(GL_NEAREST);
|
|
|
|
depth_directionalMaps.at(i)->setMagFilter(GL_NEAREST);
|
|
|
|
depth_directionalMaps.at(i)->setWrapS(GL_CLAMP_TO_EDGE);
|
|
|
|
depth_directionalMaps.at(i)->setWrapT(GL_CLAMP_TO_EDGE);
|
|
|
|
depth_directionalMaps.at(i)->setCompareMode(GL_COMPARE_REF_TO_TEXTURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i<framebuffer_directional.size(); i++) {
|
|
|
|
framebuffer_directional.at(i) = SharedFrameBufferObject(new FrameBufferObject());
|
|
|
|
framebuffer_directional.at(i)->setDepthTexture(depth_directionalMaps.at(i));
|
|
|
|
framebuffer_directional.at(i)->validate();
|
|
|
|
}
|
|
|
|
|
2015-06-01 08:45:15 +00:00
|
|
|
// always generate and bind all cube maps, because otherwise the shader won't work
|
|
|
|
depth_cubeMaps = std::vector<ACGL::OpenGL::SharedTextureCubeMap>(maxShadowSampleCount);
|
2015-02-09 20:29:44 +00:00
|
|
|
for (unsigned int i = 0; i<depth_cubeMaps.size(); i++) {
|
2015-02-04 22:10:51 +00:00
|
|
|
depth_cubeMaps.at(i) = SharedTextureCubeMap(new TextureCubeMap(glm::vec2(cube_size, cube_size), GL_DEPTH_COMPONENT24));
|
2014-12-15 01:09:33 +00:00
|
|
|
depth_cubeMaps.at(i)->setMinFilter(GL_NEAREST);
|
|
|
|
depth_cubeMaps.at(i)->setMagFilter(GL_NEAREST);
|
|
|
|
depth_cubeMaps.at(i)->setWrapS(GL_CLAMP_TO_EDGE);
|
|
|
|
depth_cubeMaps.at(i)->setWrapT(GL_CLAMP_TO_EDGE);
|
|
|
|
depth_cubeMaps.at(i)->setCompareMode(GL_COMPARE_REF_TO_TEXTURE);
|
2014-12-15 21:00:23 +00:00
|
|
|
}
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-12-15 01:09:33 +00:00
|
|
|
framebuffer_cube = SharedFrameBufferObject(new FrameBufferObject());
|
2015-02-09 13:26:04 +00:00
|
|
|
|
2015-03-03 22:27:41 +00:00
|
|
|
|
2015-03-03 23:55:01 +00:00
|
|
|
light_fbo_color_texture = SharedTexture2D(new Texture2D(windowSize, GL_RGBA8));
|
|
|
|
light_fbo_color_texture->setMinFilter(GL_NEAREST);
|
|
|
|
light_fbo_color_texture->setMagFilter(GL_NEAREST);
|
|
|
|
light_fbo_color_texture->setWrapS(GL_CLAMP_TO_BORDER);
|
|
|
|
light_fbo_color_texture->setWrapT(GL_CLAMP_TO_BORDER);
|
2015-03-04 10:25:02 +00:00
|
|
|
light_fbo_depth_texture = SharedTexture2D(new Texture2D(windowSize, GL_DEPTH24_STENCIL8));
|
2015-03-03 23:55:01 +00:00
|
|
|
light_fbo_depth_texture->setMinFilter(GL_NEAREST);
|
|
|
|
light_fbo_depth_texture->setMagFilter(GL_NEAREST);
|
|
|
|
light_fbo_depth_texture->setWrapS(GL_CLAMP_TO_BORDER);
|
|
|
|
light_fbo_depth_texture->setWrapT(GL_CLAMP_TO_BORDER);
|
|
|
|
framebuffer_light = SharedFrameBufferObject(new FrameBufferObject());
|
|
|
|
framebuffer_light->attachColorTexture("oColor", light_fbo_color_texture);
|
|
|
|
framebuffer_light->setDepthTexture(light_fbo_depth_texture);
|
|
|
|
framebuffer_light->setClearColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
|
|
|
framebuffer_light->validate();
|
|
|
|
|
2015-03-03 22:27:41 +00:00
|
|
|
flamePostShader->use();
|
2015-03-03 23:55:01 +00:00
|
|
|
flamePostShader->setUniform("windowSizeX", int(windowSize.x));
|
|
|
|
flamePostShader->setUniform("windowSizeY", int(windowSize.y));
|
2015-03-03 22:27:41 +00:00
|
|
|
|
2015-03-07 18:59:52 +00:00
|
|
|
bindTextureUnits();
|
|
|
|
|
2015-03-18 08:14:09 +00:00
|
|
|
// set shader variables that stay the same across the runtime of the application
|
|
|
|
skydomeShader->use();
|
|
|
|
skydomeShader->setUniform("farPlane", farPlane);
|
|
|
|
skydomeShader->setUniform("skydomeSize", level->getSkydomeSize());
|
|
|
|
skydomeShader->setUniform("fogColorDay", level->getFogColourDay());
|
|
|
|
skydomeShader->setUniform("fogColorRise", level->getFogColourRise());
|
|
|
|
skydomeShader->setUniform("fogColorNight", level->getFogColourNight());
|
|
|
|
skydomeShader->setUniform("sunColor", level->getDirectionalLight()->getColour());
|
|
|
|
|
|
|
|
lightingShader->use();
|
|
|
|
lightingShader->setUniform("farPlane", farPlane);
|
|
|
|
lightingShader->setUniform("fogColorDay", level->getFogColourDay());
|
|
|
|
lightingShader->setUniform("fogColorRise", level->getFogColourRise());
|
|
|
|
lightingShader->setUniform("fogColorNight", level->getFogColourNight());
|
|
|
|
lightingShader->setUniform("ambientColor", level->getAmbientLight());
|
2015-06-01 08:47:26 +00:00
|
|
|
|
2015-03-21 20:46:58 +00:00
|
|
|
if(level->getDirectionalLight()) {
|
|
|
|
lightingShader->setUniform("directionalLightVector",
|
|
|
|
level->getDirectionalLight()->getPosition());
|
|
|
|
lightingShader->setUniform("directionalColor",
|
|
|
|
level->getDirectionalLight()->getColour());
|
|
|
|
lightingShader->setUniform("targetDirectionalIntensity",
|
|
|
|
level->getDirectionalLight()->getIntensity());
|
|
|
|
}
|
2015-03-20 22:45:28 +00:00
|
|
|
|
2015-06-01 08:45:15 +00:00
|
|
|
depthCubeShader->use();
|
|
|
|
depthCubeShader->setUniform("farPlane", farPlane);
|
|
|
|
|
2015-03-21 17:44:08 +00:00
|
|
|
level->sortObjects(Material::getAllTextures()->size());
|
2015-06-01 22:18:22 +00:00
|
|
|
|
2015-03-21 14:05:22 +00:00
|
|
|
#ifdef SAXUM_DEBUG
|
2015-03-21 17:44:08 +00:00
|
|
|
std::cout << "There were " << Material::getAllTextures()->size()
|
2015-03-21 14:05:22 +00:00
|
|
|
<< " materials used in this level." << std::endl;
|
2015-06-02 19:14:54 +00:00
|
|
|
cout << "There are " << level->checkMaxSurroundingLights() << " max surrounding lights." << endl;
|
2015-03-21 14:05:22 +00:00
|
|
|
#endif
|
2015-06-01 08:45:15 +00:00
|
|
|
|
|
|
|
initShadowRenderQueue();
|
2015-06-01 22:18:22 +00:00
|
|
|
updateLights();
|
2015-03-07 18:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::bindTextureUnits(){
|
2015-03-18 15:26:20 +00:00
|
|
|
unsigned int textureCount = Material::getAllTextures()->size();
|
|
|
|
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &number_of_texture_units);
|
|
|
|
printf("Your graphics card supports %d texture units.\n", number_of_texture_units);
|
|
|
|
// Exit if we need more texture units
|
2015-06-01 08:45:15 +00:00
|
|
|
if (number_of_texture_units < (int)textureCount + maxShadowSampleCount + 9) {
|
2015-06-01 11:29:08 +00:00
|
|
|
printf("You need at least %d texture units to run this application. Exiting\n", textureCount + maxShadowSampleCount + 9);
|
2015-03-18 15:26:20 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
2015-06-01 08:45:15 +00:00
|
|
|
|
|
|
|
loadingShader->use();
|
|
|
|
loadingShader->setTexture("screen", loadingScreen, 0);
|
|
|
|
loadingShader->setTexture("screenContinue", loadingContinueScreen, 1);
|
|
|
|
|
|
|
|
lightingShader->use();
|
2015-03-18 15:26:20 +00:00
|
|
|
for(unsigned int i = 0; i<Material::getAllTextures()->size(); i++) {
|
|
|
|
lightingShader->setTexture("uTexture", Material::getAllTextures()->at(i), i+2);
|
|
|
|
}
|
2015-03-07 18:59:52 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i<depth_directionalMaps.size(); i++) {
|
2015-03-18 15:26:20 +00:00
|
|
|
lightingShader->setTexture("shadowMap_directional" + std::to_string(i), depth_directionalMaps.at(i), textureCount + i + 2);
|
2015-03-07 18:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (level->getLights()->size() > 0) {
|
|
|
|
for(unsigned int i = 0; i<depth_cubeMaps.size(); i++){
|
2015-05-31 15:36:21 +00:00
|
|
|
lightingShader->setTexture("shadowMap_cube" + std::to_string(i), depth_cubeMaps.at(i), textureCount + i + 7);
|
2015-03-07 18:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
flamePostShader->use();
|
2015-06-01 08:45:15 +00:00
|
|
|
flamePostShader->setTexture("light_fbo", light_fbo_color_texture, textureCount + maxShadowSampleCount + 7);
|
2015-03-07 18:59:52 +00:00
|
|
|
|
2015-03-07 17:25:14 +00:00
|
|
|
skydomeShader->use();
|
2015-06-01 08:45:15 +00:00
|
|
|
skydomeShader->setTexture("dayTexture", level->getSkydome()->getDayTexture(), textureCount + maxShadowSampleCount + 8);
|
|
|
|
skydomeShader->setTexture("nightTexture", level->getSkydome()->getNightTexture(), textureCount + maxShadowSampleCount + 9);
|
2015-03-06 08:21:38 +00:00
|
|
|
|
2015-06-01 08:45:15 +00:00
|
|
|
printf("This application used %d texture units.\n", textureCount + maxShadowSampleCount + 9);
|
2015-03-07 18:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::renderLoadingScreen() {
|
|
|
|
loadingScreen = Texture2DFileManager::the()->get(Texture2DCreator(loadingScreenPath));
|
|
|
|
loadingScreen->generateMipmaps();
|
2015-03-15 14:39:24 +00:00
|
|
|
loadingScreen->setMinFilter(GL_NEAREST_MIPMAP_LINEAR);
|
|
|
|
loadingScreen->setMagFilter(GL_LINEAR);
|
2015-03-07 18:59:52 +00:00
|
|
|
loadingContinueScreen = Texture2DFileManager::the()->get(Texture2DCreator(loadingScreenContinuePath));
|
|
|
|
loadingContinueScreen->generateMipmaps();
|
2015-03-15 14:39:24 +00:00
|
|
|
loadingContinueScreen->setMinFilter(GL_NEAREST_MIPMAP_LINEAR);
|
|
|
|
loadingContinueScreen->setMagFilter(GL_LINEAR);
|
2015-03-09 12:41:34 +00:00
|
|
|
loadingScreenWidth = (float)loadingScreen->getWidth();
|
|
|
|
loadingScreenHeight = (float)loadingScreen->getHeight();
|
2015-03-08 16:05:52 +00:00
|
|
|
|
|
|
|
fullscreen_quad_ab_loading = SharedArrayBuffer(new ArrayBuffer());
|
|
|
|
fullscreen_quad_ab_loading->defineAttribute("aPosition", GL_FLOAT, 2);
|
|
|
|
fullscreen_quad_ab_loading->defineAttribute("aTexCoord", GL_FLOAT, 2);
|
|
|
|
|
|
|
|
float quadData[24];
|
2015-03-09 12:41:34 +00:00
|
|
|
if (loadingScreenWidth/loadingScreenHeight < ((float)windowSize.x)/((float)windowSize.y)) {
|
2015-03-08 16:05:52 +00:00
|
|
|
float quadTemp[24] ={
|
2015-03-09 12:41:34 +00:00
|
|
|
-(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), 1.0f, 0.0f, 1.0f,
|
|
|
|
(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), 1.0f, 1.0f, 1.0f,
|
|
|
|
(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), -1.0f, 1.0f, 0.0f,
|
2015-03-08 16:05:52 +00:00
|
|
|
|
2015-03-09 12:41:34 +00:00
|
|
|
(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), -1.0f, 1.0f, 0.0f,
|
|
|
|
-(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), -1.0f, 0.0f, 0.0f,
|
|
|
|
-(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), 1.0f, 0.0f, 1.0f
|
2015-03-08 16:05:52 +00:00
|
|
|
};
|
|
|
|
for(int i = 0; i<24; i++) {
|
|
|
|
quadData[i] = quadTemp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
float quadTemp[24] = {
|
2015-03-09 12:41:34 +00:00
|
|
|
-1.0f, ((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 0.0f, 1.0f,
|
|
|
|
1.0f, ((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 1.0f, 1.0f,
|
|
|
|
1.0f, -((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 1.0f, 0.0f,
|
2015-03-08 16:05:52 +00:00
|
|
|
|
2015-03-09 12:41:34 +00:00
|
|
|
1.0f, -((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 1.0f, 0.0f,
|
|
|
|
-1.0f, -((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 0.0f, 0.0f,
|
|
|
|
-1.0f, ((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 0.0f, 1.0f
|
2015-03-08 16:05:52 +00:00
|
|
|
};
|
|
|
|
for(int i = 0; i<24; i++) {
|
|
|
|
quadData[i] = quadTemp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fullscreen_quad_ab_loading->setDataElements(6, quadData);
|
|
|
|
|
|
|
|
fullscreen_quad_loading = SharedVertexArrayObject(new VertexArrayObject);
|
|
|
|
fullscreen_quad_loading->attachAllAttributes(fullscreen_quad_ab_loading);
|
2015-03-07 18:59:52 +00:00
|
|
|
loadingShader = ShaderProgramCreator("loading")
|
2015-03-08 16:05:52 +00:00
|
|
|
.attributeLocations(fullscreen_quad_loading->getAttributeLocations()).create();
|
2015-03-07 18:59:52 +00:00
|
|
|
loadingShader->use();
|
|
|
|
loadingShader->setUniform("time", 0.0f);
|
2015-03-18 15:26:20 +00:00
|
|
|
loadingShader->setTexture("screen", loadingScreen, 0);
|
|
|
|
loadingShader->setTexture("screenContinue", loadingContinueScreen, 1);
|
2015-03-08 16:05:52 +00:00
|
|
|
fullscreen_quad_loading->render();
|
2014-12-01 16:49:59 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 17:33:42 +00:00
|
|
|
glm::uvec2 Graphics::getWindowSize() {
|
|
|
|
return windowSize;
|
|
|
|
}
|
|
|
|
|
2015-01-25 23:06:31 +00:00
|
|
|
void Graphics::render(double time)
|
2014-10-20 15:31:26 +00:00
|
|
|
{
|
2015-03-07 18:59:52 +00:00
|
|
|
if (!gameStart) {
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2015-06-01 08:45:15 +00:00
|
|
|
glViewport(0, 0, windowSize.x, windowSize.y);
|
2015-03-07 18:59:52 +00:00
|
|
|
loadingShader->use();
|
|
|
|
loadingShader->setUniform("time", float(time));
|
2015-03-08 16:05:52 +00:00
|
|
|
fullscreen_quad_loading->render();
|
2015-03-07 18:59:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-03-09 13:14:17 +00:00
|
|
|
double nextLightUpdate = lastLightUpdate + lightUpdateDelay;
|
|
|
|
if (time >= nextLightUpdate)
|
2015-03-08 14:59:35 +00:00
|
|
|
{
|
|
|
|
updateLights();
|
2015-03-09 13:14:17 +00:00
|
|
|
lastLightUpdate = time;
|
2015-03-08 14:59:35 +00:00
|
|
|
}
|
2015-03-12 18:24:10 +00:00
|
|
|
|
2015-03-07 18:59:52 +00:00
|
|
|
// At first render shadows
|
2015-03-12 18:24:10 +00:00
|
|
|
std::vector<glm::mat4> depthViewProjectionMatrices = std::vector<glm::mat4>(framebuffer_directional.size());
|
|
|
|
if (renderShadows) {
|
2015-06-01 13:47:30 +00:00
|
|
|
// update priorities
|
|
|
|
for(unsigned int i = 0; i<shadowRenderQueue.size(); i++) {
|
|
|
|
shadowRenderQueue.at(i).currentPriority += shadowRenderQueue.at(i).priority;
|
|
|
|
}
|
|
|
|
// schedule lights with highest priority
|
|
|
|
// tuple : Light, currentPriority, slot
|
|
|
|
std::vector<std::tuple<std::shared_ptr<Light>, int, int>> renderQueue =
|
|
|
|
std::vector<std::tuple<std::shared_ptr<Light>, int, int>>(maxShadowRenderCount);
|
|
|
|
for(unsigned int i = 0; i<shadowRenderQueue.size(); i++) {
|
|
|
|
for(unsigned int j = 0; j<renderQueue.size(); j++){
|
2015-06-01 14:17:37 +00:00
|
|
|
if (shadowRenderQueue.at(i).currentPriority > std::get<1>(renderQueue.at(j))){
|
2015-06-03 12:05:57 +00:00
|
|
|
if (renderQueue.at(j) != renderQueue.back()) {
|
2015-06-01 22:33:55 +00:00
|
|
|
renderQueue.at(j+1) = renderQueue.at(j);
|
|
|
|
}
|
2015-06-01 13:47:30 +00:00
|
|
|
renderQueue.at(j) = std::make_tuple(shadowRenderQueue.at(i).light, shadowRenderQueue.at(i).currentPriority, i);
|
2015-06-01 14:17:37 +00:00
|
|
|
break;
|
2015-06-01 13:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-03 00:18:31 +00:00
|
|
|
}
|
|
|
|
// reset currentPriority
|
|
|
|
for(unsigned int i = 0; i<renderQueue.size(); i++) {
|
|
|
|
shadowRenderQueue.at(std::get<2>(renderQueue.at(i))).currentPriority = 0;
|
2015-06-01 13:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
depthCubeShader->use();
|
2015-03-12 18:24:10 +00:00
|
|
|
// render depth textures for point lights
|
|
|
|
glViewport(0, 0, cube_size, cube_size);
|
2015-06-01 11:35:23 +00:00
|
|
|
depthCubeShader->use();
|
2015-03-21 17:57:14 +00:00
|
|
|
glm::mat4 depthProjectionMatrix_pointlights = glm::perspective(1.571f, (float)cube_size/(float)cube_size, 0.1f, 50.0f);
|
2015-03-12 18:24:10 +00:00
|
|
|
glm::vec3 looking_directions[6] = {glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f),
|
|
|
|
glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f)};
|
|
|
|
glm::vec3 upvectors[6] = {glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, 0.0f, -1.0f),
|
|
|
|
glm::vec3(0.0f, 0.0f, -1.0f),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f)};
|
|
|
|
|
|
|
|
|
|
|
|
framebuffer_cube->bind();
|
2015-06-01 13:47:30 +00:00
|
|
|
for (unsigned int i_pointlight = 0; i_pointlight < renderQueue.size(); i_pointlight++) {
|
2015-06-01 22:18:22 +00:00
|
|
|
// check if queue points to a existing light
|
|
|
|
if (std::get<0>(renderQueue.at(i_pointlight))) {
|
|
|
|
// render each side of the cube
|
|
|
|
glm::vec3 position = glm::vec3(0.0f);
|
|
|
|
if (std::get<0>(renderQueue.at(i_pointlight))->isFlame()) {
|
|
|
|
position = std::get<0>(renderQueue.at(i_pointlight))->getPosition();
|
2015-06-02 18:15:48 +00:00
|
|
|
position = glm::vec3(position.x - 0.75f*wind.y, position.y, position.z + 0.75f*wind.x);
|
2015-06-01 22:18:22 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
position = std::get<0>(renderQueue.at(i_pointlight))->getPosition();
|
|
|
|
}
|
|
|
|
for (int i_face = 0; i_face<6; i_face++) {
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i_face,
|
|
|
|
depth_cubeMaps.at(std::get<2>(renderQueue.at(i_pointlight)))->getObjectName(), 0);
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
|
|
|
glm::mat4 viewMatrix = glm::lookAt(position, position + looking_directions[i_face], upvectors[i_face]);
|
|
|
|
glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix;
|
|
|
|
std::vector<glm::mat4> viewMatrixVector = std::vector<glm::mat4>(1);
|
|
|
|
viewMatrixVector.at(0) = viewMatrix;
|
|
|
|
level->render(depthCubeShader, false, std::get<0>(renderQueue.at(i_pointlight))->getPosition(), 1, &depthViewProjectionMatrix_face, &viewMatrixVector);
|
|
|
|
if (!framebuffer_cube->isFrameBufferObjectComplete()) {
|
|
|
|
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
|
|
|
}
|
2015-03-12 18:24:10 +00:00
|
|
|
}
|
2015-03-07 18:59:52 +00:00
|
|
|
}
|
2014-12-15 01:09:33 +00:00
|
|
|
}
|
2015-03-04 16:08:53 +00:00
|
|
|
|
2015-03-12 18:24:10 +00:00
|
|
|
glViewport(0, 0, windowSize.x, windowSize.y);
|
2015-05-28 13:03:08 +00:00
|
|
|
|
|
|
|
// render depth textures for sun
|
2015-03-12 18:24:10 +00:00
|
|
|
float sunAngle = glm::dot(glm::vec3(0.0f, 1.0f, 0.0f), glm::normalize(level->getDirectionalLight()->getPosition()));
|
|
|
|
glm::vec3 sunVector = (level->getCameraCenter()->getPosition() + level->getDirectionalLight()->getPosition());
|
2015-02-28 13:40:54 +00:00
|
|
|
|
2015-05-28 13:03:08 +00:00
|
|
|
if (sunAngle > 0.0f) {
|
|
|
|
depthShader->use();
|
|
|
|
for (unsigned int i = 0; i<framebuffer_directional.size(); i++) {
|
|
|
|
framebuffer_directional.at(i)->bind();
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
|
|
|
float projection_size = 0.0f;
|
|
|
|
switch(i) {
|
|
|
|
case 0:
|
|
|
|
projection_size = 10.0f;
|
|
|
|
break;
|
|
|
|
case 1:
|
2015-05-31 15:36:21 +00:00
|
|
|
projection_size = 20.0f;
|
2015-05-28 13:03:08 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2015-05-31 15:36:21 +00:00
|
|
|
projection_size = 40.0f;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
projection_size = 60.0f;
|
|
|
|
break;
|
|
|
|
case 4:
|
2015-05-28 13:03:08 +00:00
|
|
|
projection_size = farPlane/1.5f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
depthViewProjectionMatrices.at(i) = glm::ortho<float>(-projection_size, projection_size, -projection_size, projection_size, -farPlane/1.5f, farPlane/1.5f) *
|
|
|
|
glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0));
|
2015-05-31 22:57:36 +00:00
|
|
|
level->render(depthShader, false, level->getCameraCenter()->getPosition(), -1, &depthViewProjectionMatrices.at(i));
|
2015-05-28 13:03:08 +00:00
|
|
|
if (!framebuffer_directional.at(i)->isFrameBufferObjectComplete()) {
|
|
|
|
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
|
|
|
}
|
2015-03-08 09:36:45 +00:00
|
|
|
}
|
2015-03-07 18:59:52 +00:00
|
|
|
}
|
2015-02-28 13:40:54 +00:00
|
|
|
}
|
2015-03-07 18:59:52 +00:00
|
|
|
|
|
|
|
// lighting render pass
|
2015-03-09 07:21:44 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2015-03-07 18:59:52 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2015-03-08 00:28:34 +00:00
|
|
|
//wind
|
2015-03-09 13:14:17 +00:00
|
|
|
double nextWindUpdate = lastWindUpdate + lightUpdateDelay;
|
|
|
|
if (time >= nextWindUpdate)
|
|
|
|
{
|
|
|
|
const float windTargetEnd = 0.7f;
|
|
|
|
windTarget = static_cast<float>(rand()) / static_cast<float>(RAND_MAX/pow(windTargetEnd, 2));
|
|
|
|
windTarget = sqrt(windTarget);
|
|
|
|
windTarget *= 0.8f*pow(sin(0.1f*time), 2) +0.2f;
|
|
|
|
const float windDirectionXEnd = 0.5f;
|
|
|
|
float windDirectionX = static_cast<float>(rand()) / static_cast<float>(RAND_MAX/pow(windDirectionXEnd, 2));
|
|
|
|
windDirectionX = sqrt(windDirectionX);
|
|
|
|
const float windDirectionYEnd = 0.5f;
|
|
|
|
float windDirectionY = static_cast<float>(rand()) / static_cast<float>(RAND_MAX/pow(windDirectionYEnd, 2));
|
|
|
|
windDirectionY = sqrt(windDirectionY);
|
|
|
|
windDirectionTarget = glm::vec2(windDirectionX, windDirectionY);
|
|
|
|
lastWindUpdate = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
const float windApproachSpeed= 0.0005f;
|
|
|
|
|
|
|
|
if (windApproachSpeed*static_cast<float>(time)>1.0f) {
|
|
|
|
wind = glm::normalize(windDirection)*windTarget;
|
|
|
|
windDirection = windDirectionTarget;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
windDirection.x = windDirection.x + windApproachSpeed*static_cast<float>(time)*windDirectionTarget.x - windDirection.x;
|
|
|
|
windDirection.y = windDirection.y + windApproachSpeed*static_cast<float>(time)*windDirectionTarget.y - windDirection.x;
|
|
|
|
wind = wind + (windApproachSpeed*static_cast<float>(time)) * (glm::normalize(windDirection)*windTarget - wind);
|
|
|
|
}
|
2015-03-08 00:28:34 +00:00
|
|
|
|
2015-03-07 18:59:52 +00:00
|
|
|
//set view and projection matrix
|
|
|
|
glm::mat4 lightingViewProjectionMatrix = glm::perspective(1.571f, (float)windowSize.x/(float)windowSize.y, 0.1f, farPlane) * buildViewMatrix(level);
|
|
|
|
|
|
|
|
//render skydome
|
|
|
|
skydomeShader->use();
|
|
|
|
// set fog Parameters
|
|
|
|
skydomeShader->setUniform("cameraCenter", level->getCameraCenter()->getPosition());
|
|
|
|
skydomeShader->setUniform("directionalVector", level->getDirectionalLight()->getPosition());
|
2015-04-12 19:06:59 +00:00
|
|
|
level->getSkydome()->render(skydomeShader, false, &lightingViewProjectionMatrix);
|
2015-03-07 18:59:52 +00:00
|
|
|
|
|
|
|
lightingShader->use();
|
|
|
|
|
|
|
|
// convert texture to homogenouse coordinates
|
|
|
|
glm::mat4 biasMatrix(
|
|
|
|
0.5, 0.0, 0.0, 0.0,
|
|
|
|
0.0, 0.5, 0.0, 0.0,
|
|
|
|
0.0, 0.0, 0.5, 0.0,
|
|
|
|
0.5, 0.5, 0.5, 1.0
|
|
|
|
);
|
|
|
|
|
|
|
|
std::vector<glm::mat4> depthBiasVPs = std::vector<glm::mat4>(depthViewProjectionMatrices.size());
|
|
|
|
for (unsigned int i = 0; i<depthBiasVPs.size(); i++) {
|
|
|
|
depthBiasVPs.at(i) = biasMatrix * depthViewProjectionMatrices.at(i);
|
2015-02-28 13:40:54 +00:00
|
|
|
}
|
2014-12-04 00:13:59 +00:00
|
|
|
|
2015-03-07 18:59:52 +00:00
|
|
|
// set fog Parameters
|
|
|
|
lightingShader->setUniform("cameraCenter", level->getCameraCenter()->getPosition());
|
2015-03-21 20:46:58 +00:00
|
|
|
if(level->getDirectionalLight()) {
|
|
|
|
lightingShader->setUniform("directionalLightVector",
|
|
|
|
level->getDirectionalLight()->getPosition());
|
|
|
|
}
|
2015-03-07 18:59:52 +00:00
|
|
|
|
|
|
|
// set Material Parameters
|
|
|
|
lightingShader->setUniform("camera", level->getPhysics()->getCameraPosition());
|
2015-03-09 13:14:17 +00:00
|
|
|
textureMovementPosition += wind/5.0f;
|
|
|
|
lightingShader->setUniform("movingTextureOffset", textureMovementPosition);
|
2015-03-08 00:28:34 +00:00
|
|
|
lightingShader->setUniform("movement", wind);
|
2015-03-09 13:14:17 +00:00
|
|
|
|
2015-03-07 18:59:52 +00:00
|
|
|
lightingShader->setUniform("time", (float) time);
|
2015-03-13 14:43:10 +00:00
|
|
|
|
|
|
|
if (renderWorld) {
|
|
|
|
// render the level
|
2015-03-21 14:05:22 +00:00
|
|
|
level->enqueueObjects(this);
|
|
|
|
for (unsigned int i = 0; i<Material::getAllTextures()->size(); i++) {
|
2015-03-21 17:44:08 +00:00
|
|
|
bool parametersSet = false;
|
2015-03-21 14:05:22 +00:00
|
|
|
for(unsigned int j = 0; j<renderQueue.size(); j++) {
|
2015-03-21 17:44:08 +00:00
|
|
|
if(renderQueue.at(j)->at(i).size() != 0) {
|
|
|
|
if (!parametersSet) {
|
|
|
|
parametersSet = true;
|
|
|
|
Material* material = renderQueue.at(j)->at(i).at(0)->getMaterial();
|
|
|
|
if (material->isMoving()) {
|
|
|
|
lightingShader->setUniform("movingTexture", true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lightingShader->setUniform("movingTexture", false);
|
|
|
|
}
|
|
|
|
lightingShader->setUniform("uTexture", material->getTextureUnit());
|
|
|
|
lightingShader->setUniform("ambientFactor", material->getAmbientFactor());
|
|
|
|
lightingShader->setUniform("diffuseFactor", material->getDiffuseFactor());
|
|
|
|
lightingShader->setUniform("specularFactor", material->getSpecularFactor());
|
|
|
|
lightingShader->setUniform("shininess", material->getShininess());
|
|
|
|
}
|
|
|
|
for(unsigned int k = 0; k<renderQueue.at(j)->at(i).size(); k++) {
|
2015-04-12 19:06:59 +00:00
|
|
|
renderQueue.at(j)->at(i).at(k)->render(lightingShader, true, &lightingViewProjectionMatrix, &depthBiasVPs);
|
2015-03-21 17:44:08 +00:00
|
|
|
}
|
2015-03-20 22:45:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-21 18:02:24 +00:00
|
|
|
// render water plane last for correct transparency
|
|
|
|
if (level->getWaterPlane()) {
|
|
|
|
Material* material = level->getWaterPlane()->getMaterial();
|
|
|
|
if (material->isMoving()) {
|
|
|
|
lightingShader->setUniform("movingTexture", true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lightingShader->setUniform("movingTexture", false);
|
|
|
|
}
|
|
|
|
lightingShader->setUniform("uTexture", material->getTextureUnit());
|
|
|
|
lightingShader->setUniform("ambientFactor", material->getAmbientFactor());
|
|
|
|
lightingShader->setUniform("diffuseFactor", material->getDiffuseFactor());
|
|
|
|
lightingShader->setUniform("specularFactor", material->getSpecularFactor());
|
|
|
|
lightingShader->setUniform("shininess", material->getShininess());
|
2015-04-12 19:06:59 +00:00
|
|
|
level->getWaterPlane()->render(lightingShader, true, &lightingViewProjectionMatrix, &depthBiasVPs);
|
2015-03-21 18:02:24 +00:00
|
|
|
}
|
|
|
|
renderQueue.clear();
|
2015-03-13 14:43:10 +00:00
|
|
|
}
|
2015-03-07 18:59:52 +00:00
|
|
|
|
2015-03-14 13:34:45 +00:00
|
|
|
if (renderDebug) {
|
|
|
|
debugDrawer.setDebugMode(btIDebugDraw::DBG_DrawWireframe);
|
|
|
|
level->getPhysics()->getWorld()->debugDrawWorld();
|
|
|
|
debugDrawer.setDebugMode(btIDebugDraw::DBG_NoDebug);
|
|
|
|
unsigned int data_count = debugDrawer.getData()->size();
|
|
|
|
float* debugData = new float[data_count];
|
|
|
|
for (unsigned int i = 0; i<data_count; i++) {
|
|
|
|
debugData[i] = debugDrawer.getData()->at(i);
|
|
|
|
}
|
|
|
|
debug_ab->setDataElements(data_count/6, debugData);
|
|
|
|
debugDrawer.clearData();
|
2015-03-22 12:25:43 +00:00
|
|
|
delete[] debugData;
|
2015-03-14 13:34:45 +00:00
|
|
|
debugShader->use();
|
|
|
|
debugShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix);
|
|
|
|
debug_vao->render();
|
|
|
|
}
|
|
|
|
|
2015-03-07 18:59:52 +00:00
|
|
|
// draw flames on top
|
2015-03-12 18:24:10 +00:00
|
|
|
if (renderFlames) {
|
|
|
|
flameShader->use();
|
|
|
|
// cull faces to get consistent color while using alpha
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glCullFace(GL_BACK);
|
|
|
|
|
|
|
|
// draw with colors
|
|
|
|
for(unsigned int i = 0; i<closestFlames.size(); i++) {
|
|
|
|
closestFlames.at(i)->render(flameShader, lightingViewProjectionMatrix, float(time), true, wind);
|
|
|
|
}
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
|
|
|
|
framebuffer_light->bind();
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer_light->getObjectName());
|
|
|
|
glBlitFramebuffer(0, 0, windowSize.x, windowSize.y, 0, 0, windowSize.x, windowSize.y,
|
|
|
|
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
|
|
|
|
|
|
|
|
// draw slightly larger only for stencil buffer to blur edges
|
|
|
|
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
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i<closestFlames.size(); i++) {
|
|
|
|
closestFlames.at(i)->render(flameShader, lightingViewProjectionMatrix, float(time), false, wind);
|
|
|
|
}
|
2015-03-07 18:59:52 +00:00
|
|
|
|
2015-03-12 18:24:10 +00:00
|
|
|
glStencilFunc(GL_EQUAL, 1, 0xFF); //Pass test if stencil value is 1
|
|
|
|
glStencilMask(0x00);// don't write to stencil buffer
|
2015-03-07 18:59:52 +00:00
|
|
|
|
2015-03-12 18:24:10 +00:00
|
|
|
flamePostShader->use();
|
|
|
|
fullscreen_quad->render();
|
|
|
|
glDepthMask(GL_TRUE);
|
2015-03-07 18:59:52 +00:00
|
|
|
|
2015-03-12 18:24:10 +00:00
|
|
|
glDisable(GL_STENCIL_TEST);
|
2015-03-07 18:59:52 +00:00
|
|
|
|
2015-03-12 18:24:10 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer_light->getObjectName());
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
|
|
|
glBlitFramebuffer(0, 0, windowSize.x, windowSize.y, 0, 0, windowSize.x, windowSize.y,
|
|
|
|
GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
|
|
|
}
|
2015-02-28 13:40:54 +00:00
|
|
|
}
|
2015-01-25 23:06:31 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 22:30:59 +00:00
|
|
|
void Graphics::updateLights() {
|
2015-06-01 22:18:22 +00:00
|
|
|
std::vector<std::shared_ptr<Light>> oldClosestLights = std::vector<std::shared_ptr<Light>>(*closestLights);
|
2015-06-02 19:37:58 +00:00
|
|
|
closestLights = level->getClosestLights(maxShadowSampleCount);
|
2015-03-24 18:50:26 +00:00
|
|
|
if (closestLights->size() > 0) {
|
2015-03-08 14:59:35 +00:00
|
|
|
lightingShader->use();
|
2015-03-24 18:50:26 +00:00
|
|
|
lightingShader->setUniform("lightCount", (int) closestLights->size());
|
2015-06-01 08:47:26 +00:00
|
|
|
lightingShader->setUniform("maxShadowRenderCount", min((int)closestLights->size(), maxShadowSampleCount));
|
2014-10-31 11:56:09 +00:00
|
|
|
|
2015-06-01 22:18:22 +00:00
|
|
|
// find new closest lights for the shadow render queue
|
|
|
|
unsigned int i = 0;
|
|
|
|
std::vector<std::shared_ptr<Light>> compareClosestLights = std::vector<std::shared_ptr<Light>>(*closestLights);
|
|
|
|
while(i<oldClosestLights.size()) {
|
|
|
|
bool found = false;
|
|
|
|
for(unsigned int j = 0; j<compareClosestLights.size(); j++) {
|
|
|
|
if (oldClosestLights.at(i) == compareClosestLights.at(j)){
|
|
|
|
found = true;
|
|
|
|
compareClosestLights.erase(compareClosestLights.begin() + j);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
oldClosestLights.erase(oldClosestLights.begin() + i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(oldClosestLights.size() == compareClosestLights.size());
|
|
|
|
|
|
|
|
// replace old lights with the new ones in the shadow render queue
|
|
|
|
for(unsigned int i = 0; i<oldClosestLights.size(); i++) {
|
|
|
|
for(unsigned int j = 0; j<shadowRenderQueue.size(); j++) {
|
|
|
|
if(oldClosestLights.at(i) == shadowRenderQueue.at(j).light) {
|
|
|
|
shadowRenderQueue.at(j).light = compareClosestLights.at(i);
|
|
|
|
// 15000 is larger priority than any light can get during one tick
|
|
|
|
shadowRenderQueue.at(j).currentPriority = 15000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update priority of the shadow render queue
|
|
|
|
for(unsigned int i = 0; i<shadowRenderQueue.size(); i++) {
|
|
|
|
float distance = glm::distance(level->getCameraCenter()->getPosition(), shadowRenderQueue.at(i).light->getPosition());
|
2015-06-04 09:22:27 +00:00
|
|
|
shadowRenderQueue.at(i).priority = (int) 100*std::exp(5.0f - 0.1f * distance);
|
2015-06-01 22:18:22 +00:00
|
|
|
}
|
2014-10-31 11:56:09 +00:00
|
|
|
|
|
|
|
// Build light position array
|
2015-06-02 15:36:55 +00:00
|
|
|
glm::vec3 lightSources[shadowRenderQueue.size()];
|
|
|
|
for(unsigned int i = 0; i<shadowRenderQueue.size(); i++) {
|
|
|
|
lightSources[i] = shadowRenderQueue.at(i).light->getPosition();
|
2014-10-31 11:56:09 +00:00
|
|
|
}
|
2014-12-04 00:13:59 +00:00
|
|
|
glUniform3fv(lightingShader->getUniformLocation("lightSources"),
|
2014-11-03 22:54:35 +00:00
|
|
|
sizeof(lightSources), (GLfloat*) lightSources);
|
|
|
|
// Build light colour array
|
2015-06-02 15:36:55 +00:00
|
|
|
glm::vec3 lightColours[shadowRenderQueue.size()];
|
|
|
|
for(unsigned int i = 0; i<shadowRenderQueue.size(); i++) {
|
|
|
|
lightColours[i] = shadowRenderQueue.at(i).light->getColour();
|
2014-11-03 22:54:35 +00:00
|
|
|
}
|
2014-12-04 00:13:59 +00:00
|
|
|
glUniform3fv(lightingShader->getUniformLocation("lightColors"),
|
2014-11-03 22:54:35 +00:00
|
|
|
sizeof(lightColours), (GLfloat*) lightColours);
|
2014-11-03 23:47:35 +00:00
|
|
|
// Build light attenuation array
|
2015-06-02 15:36:55 +00:00
|
|
|
float lightIntensities[shadowRenderQueue.size()];
|
|
|
|
for(unsigned int i = 0; i<shadowRenderQueue.size(); i++) {
|
|
|
|
lightIntensities[i] = shadowRenderQueue.at(i).light->getIntensity();
|
2014-11-03 23:47:35 +00:00
|
|
|
}
|
2014-12-04 00:13:59 +00:00
|
|
|
glUniform1fv(lightingShader->getUniformLocation("lightIntensities"),
|
2014-11-03 23:47:35 +00:00
|
|
|
sizeof(lightIntensities), (GLfloat*) lightIntensities);
|
2015-06-01 22:18:22 +00:00
|
|
|
|
2015-06-02 15:36:55 +00:00
|
|
|
bool isFlame[shadowRenderQueue.size()];
|
2015-06-01 22:18:22 +00:00
|
|
|
closestFlames = std::vector<Flame*>();
|
2015-06-02 15:36:55 +00:00
|
|
|
for(unsigned int i = 0; i<shadowRenderQueue.size(); i++) {
|
|
|
|
if (shadowRenderQueue.at(i).light->isFlame()) {
|
|
|
|
closestFlames.push_back(shadowRenderQueue.at(i).light->getFlame());
|
2015-06-01 22:18:22 +00:00
|
|
|
isFlame[i] = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
isFlame[i] = false;
|
|
|
|
}
|
2015-02-24 22:30:59 +00:00
|
|
|
}
|
2015-06-01 22:18:22 +00:00
|
|
|
glUniform1iv(lightingShader->getUniformLocation("isFlame"), sizeof(isFlame), (GLint*) isFlame);
|
2015-02-24 22:30:59 +00:00
|
|
|
}
|
2014-11-14 15:47:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 11:54:01 +00:00
|
|
|
void Graphics::saveWindowSize(glm::uvec2 windowSize) {
|
|
|
|
this->windowSize = windowSize;
|
|
|
|
}
|
|
|
|
|
2014-12-04 00:13:59 +00:00
|
|
|
void Graphics::resize(glm::uvec2 windowSize) {
|
2014-11-14 15:47:47 +00:00
|
|
|
this->windowSize = windowSize;
|
2015-03-15 21:11:26 +00:00
|
|
|
if (gameStart) {
|
|
|
|
for (unsigned int i = 0; i<depth_directionalMaps.size(); i++) {
|
|
|
|
depth_directionalMaps.at(i)->resize(glm::vec2(windowSize.x, windowSize.y));
|
|
|
|
}
|
|
|
|
light_fbo_color_texture->resize(windowSize);
|
|
|
|
light_fbo_depth_texture->resize(windowSize);
|
|
|
|
flamePostShader->setUniform("windowSizeX", int(windowSize.x));
|
|
|
|
flamePostShader->setUniform("windowSizeY", int(windowSize.y));
|
|
|
|
bindTextureUnits();
|
2015-02-28 13:40:54 +00:00
|
|
|
}
|
2015-04-12 18:16:24 +00:00
|
|
|
else {
|
|
|
|
float quadData[24];
|
|
|
|
if (loadingScreenWidth/loadingScreenHeight < ((float)windowSize.x)/((float)windowSize.y)) {
|
|
|
|
float quadTemp[24] ={
|
|
|
|
-(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), 1.0f, 0.0f, 1.0f,
|
|
|
|
(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), 1.0f, 1.0f, 1.0f,
|
|
|
|
(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), -1.0f, 1.0f, 0.0f,
|
|
|
|
|
|
|
|
(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), -1.0f, 1.0f, 0.0f,
|
|
|
|
-(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), -1.0f, 0.0f, 0.0f,
|
|
|
|
-(((float)windowSize.y*loadingScreenWidth)/((float)windowSize.x*loadingScreenHeight)), 1.0f, 0.0f, 1.0f
|
|
|
|
};
|
|
|
|
for(int i = 0; i<24; i++) {
|
|
|
|
quadData[i] = quadTemp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
float quadTemp[24] = {
|
|
|
|
-1.0f, ((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 0.0f, 1.0f,
|
|
|
|
1.0f, ((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 1.0f, 1.0f,
|
|
|
|
1.0f, -((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 1.0f, 0.0f,
|
|
|
|
|
|
|
|
1.0f, -((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 1.0f, 0.0f,
|
|
|
|
-1.0f, -((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 0.0f, 0.0f,
|
|
|
|
-1.0f, ((float)windowSize.x*loadingScreenHeight)/((float)windowSize.y*loadingScreenWidth), 0.0f, 1.0f
|
|
|
|
};
|
|
|
|
for(int i = 0; i<24; i++) {
|
|
|
|
quadData[i] = quadTemp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fullscreen_quad_ab_loading->setDataElements(6, quadData);
|
|
|
|
fullscreen_quad_loading = SharedVertexArrayObject(new VertexArrayObject);
|
|
|
|
fullscreen_quad_loading->attachAllAttributes(fullscreen_quad_ab_loading);
|
|
|
|
}
|
2014-10-20 15:31:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
glm::mat4 Graphics::buildViewMatrix(Level* level) {
|
2014-12-15 10:53:31 +00:00
|
|
|
//construct lookAt (cameraPosition = cameraCenter + cameraVector)
|
2015-02-13 14:46:00 +00:00
|
|
|
if(level->getCamera()->getIsPhysicsCamera())
|
|
|
|
return glm::lookAt(level->getCamera()->getPosition(), level->getCamera()->getPosition() + level->getCamera()->getDirection(), glm::vec3(0.0f, 1.0f, 0.0f));
|
2015-01-19 16:12:58 +00:00
|
|
|
|
2014-11-17 12:29:41 +00:00
|
|
|
return glm::lookAt((level->getCameraCenter()->getPosition() + level->getCamera()->getVector()),
|
|
|
|
level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f));
|
2014-11-13 17:17:58 +00:00
|
|
|
}
|
2014-11-21 01:38:03 +00:00
|
|
|
|
|
|
|
float Graphics::getFarPlane() {
|
|
|
|
return farPlane;
|
|
|
|
}
|
2015-01-26 20:48:44 +00:00
|
|
|
|
2015-01-27 10:14:00 +00:00
|
|
|
void Graphics::saveDepthBufferToDisk(int face, std::string filename) {
|
2015-01-26 20:48:44 +00:00
|
|
|
printf("Starting saving of depth buffer...\n");
|
2015-01-26 22:11:04 +00:00
|
|
|
float *depthbuffer = new float[1024*1024];
|
|
|
|
std::vector<unsigned char> image (1024 * 1024 * 4);
|
|
|
|
|
2015-01-27 10:14:00 +00:00
|
|
|
glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer);
|
2015-01-26 22:11:04 +00:00
|
|
|
for (unsigned int i = 0; i<1024*1024; i++) {
|
|
|
|
image[i * 4 + 0] = depthbuffer[i] * 255;
|
|
|
|
image[i * 4 + 1] = depthbuffer[i] * 255;
|
|
|
|
image[i * 4 + 2] = depthbuffer[i] * 255;
|
|
|
|
image[i * 4 + 3] = 255;
|
2015-01-26 20:48:44 +00:00
|
|
|
}
|
2015-01-26 22:11:04 +00:00
|
|
|
unsigned error = lodepng::encode(filename.c_str(), image, 1024, 1024);
|
2015-01-26 20:48:44 +00:00
|
|
|
if (error) {
|
|
|
|
std::cout << "Encoder error " << error << ": " << lodepng_error_text(error) << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("Saving complete!\n");
|
|
|
|
}
|
|
|
|
delete [] depthbuffer;
|
|
|
|
}
|
2015-03-07 18:59:52 +00:00
|
|
|
|
|
|
|
void Graphics::startGame() {
|
|
|
|
gameStart = true;
|
|
|
|
}
|
2015-03-12 18:24:10 +00:00
|
|
|
|
|
|
|
void Graphics::setRenderShadows(bool state) {
|
|
|
|
if(!state) {
|
|
|
|
for(unsigned int i = 0; i<framebuffer_directional.size(); i++) {
|
|
|
|
framebuffer_directional.at(i)->bind();
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
for(unsigned int i_pointlight = 0; i_pointlight<depth_cubeMaps.size(); i_pointlight++) {
|
|
|
|
for(int i_face = 0; i_face<6; i_face++) {
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i_face, depth_cubeMaps.at(i_pointlight)->getObjectName(), 0);
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
renderShadows = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::setRenderFlames(bool state) {
|
|
|
|
renderFlames = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Graphics::getRenderShadows() {
|
|
|
|
return renderShadows;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Graphics::getRenderFlames() {
|
|
|
|
return renderFlames;
|
|
|
|
}
|
2015-03-13 14:30:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
void Graphics::setRenderDebug(bool state) {
|
|
|
|
renderDebug = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Graphics::getRenderDebug() {
|
|
|
|
return renderDebug;
|
|
|
|
}
|
2015-03-13 14:43:10 +00:00
|
|
|
|
|
|
|
void Graphics::setRenderWorld(bool state) {
|
|
|
|
renderWorld = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Graphics::getRenderWorld() {
|
|
|
|
return renderWorld;
|
|
|
|
}
|
2015-03-21 14:05:22 +00:00
|
|
|
|
|
|
|
void Graphics::enqueueObjects(std::vector<std::vector<Object*>>* queue){
|
|
|
|
renderQueue.push_back(queue);
|
|
|
|
}
|
2015-06-01 08:47:26 +00:00
|
|
|
|
|
|
|
void Graphics::initShadowRenderQueue() {
|
2015-06-02 19:37:58 +00:00
|
|
|
closestLights = level->getClosestLights(maxShadowSampleCount);
|
2015-06-01 08:47:26 +00:00
|
|
|
int maxLights = min((int)closestLights->size(), maxShadowSampleCount);
|
|
|
|
shadowRenderQueue = std::vector<ShadowRenderQueueSlot>(maxLights);
|
|
|
|
glViewport(0, 0, cube_size, cube_size);
|
|
|
|
glm::mat4 depthProjectionMatrix_pointlights = glm::perspective(1.571f, (float)cube_size/(float)cube_size, 0.1f, 50.0f);
|
|
|
|
glm::vec3 looking_directions[6] = {glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f),
|
|
|
|
glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f)};
|
|
|
|
glm::vec3 upvectors[6] = {glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, 0.0f, -1.0f),
|
|
|
|
glm::vec3(0.0f, 0.0f, -1.0f),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f)};
|
|
|
|
|
|
|
|
framebuffer_cube->bind();
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i<shadowRenderQueue.size(); i++){
|
|
|
|
shadowRenderQueue.at(i).light = closestLights->at(i);
|
|
|
|
shadowRenderQueue.at(i).currentPriority = 0;
|
|
|
|
// render depth textures for point lights
|
|
|
|
depthCubeShader->use();
|
|
|
|
// render each side of the cube
|
|
|
|
for (int i_face = 0; i_face<6; i_face++) {
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i_face, depth_cubeMaps.at(i)->getObjectName(), 0);
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
|
|
|
glm::mat4 viewMatrix = glm::lookAt(shadowRenderQueue.at(i).light->getPosition(),
|
|
|
|
shadowRenderQueue.at(i).light->getPosition() + looking_directions[i_face], upvectors[i_face]);
|
|
|
|
glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix;
|
|
|
|
std::vector<glm::mat4> viewMatrixVector = std::vector<glm::mat4>();
|
|
|
|
viewMatrixVector.push_back(viewMatrix);
|
|
|
|
level->render(depthCubeShader, false, shadowRenderQueue.at(i).light->getPosition(), 1, &depthViewProjectionMatrix_face, &viewMatrixVector);
|
|
|
|
if (!framebuffer_cube->isFrameBufferObjectComplete()) {
|
|
|
|
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|