2014-10-20 16:49:10 +00:00
|
|
|
#include "graphics.hh"
|
2015-01-26 20:48:44 +00:00
|
|
|
#include "lodepng.h"
|
2014-10-20 15:31:26 +00:00
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
|
2014-12-01 16:49:59 +00:00
|
|
|
#include <ACGL/OpenGL/Creator/ShaderProgramCreator.hh>
|
|
|
|
|
2014-12-04 00:13:59 +00:00
|
|
|
using namespace ACGL::OpenGL;
|
|
|
|
|
2014-12-15 01:09:33 +00:00
|
|
|
const int Graphics::cube_size = 1024;
|
2015-01-25 23:18:09 +00:00
|
|
|
const double lightUpdateDelay = 0.5f;
|
2014-12-15 01:09:33 +00:00
|
|
|
|
2014-12-01 16:49:59 +00:00
|
|
|
Graphics::Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane) {
|
|
|
|
this->windowSize = windowSize;
|
|
|
|
this->nearPlane = nearPlane;
|
|
|
|
this->farPlane = farPlane;
|
|
|
|
}
|
|
|
|
|
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-01-25 23:18:09 +00:00
|
|
|
// update lights on creation
|
|
|
|
lastUpdate = -lightUpdateDelay;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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();
|
2014-12-04 00:13:59 +00:00
|
|
|
|
|
|
|
depthShader = ShaderProgramCreator("depth")
|
2014-12-04 16:19:58 +00:00
|
|
|
.attributeLocations(vao->getAttributeLocations()).create();
|
|
|
|
|
2015-01-18 23:52:57 +00:00
|
|
|
depthTexture = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT16));
|
|
|
|
depthTexture->setMinFilter(GL_NEAREST);
|
|
|
|
depthTexture->setMagFilter(GL_NEAREST);
|
|
|
|
depthTexture->setWrapS(GL_CLAMP_TO_EDGE);
|
|
|
|
depthTexture->setWrapT(GL_CLAMP_TO_EDGE);
|
|
|
|
depthTexture->setCompareMode(GL_COMPARE_REF_TO_TEXTURE);
|
|
|
|
|
|
|
|
framebuffer = SharedFrameBufferObject(new FrameBufferObject());
|
|
|
|
framebuffer->setDepthTexture(depthTexture);
|
|
|
|
framebuffer->validate();
|
2014-12-15 01:09:33 +00:00
|
|
|
|
2014-12-15 14:58:55 +00:00
|
|
|
/*depth_cubeMaps = std::vector<ACGL::OpenGL::SharedTextureCubeMap>(level->getLights()->size());
|
|
|
|
for (unsigned int i = 0; i<depth_cubeMaps.size(); i++) {*/
|
2014-12-19 12:09:25 +00:00
|
|
|
depth_cubeMaps = std::vector<ACGL::OpenGL::SharedTextureCubeMap>(std::min(int(level->getLights()->size()), 1));
|
|
|
|
for (unsigned int i = 0; i<1 && i<depth_cubeMaps.size(); i++) {
|
2014-12-15 01:09:33 +00:00
|
|
|
depth_cubeMaps.at(i) = SharedTextureCubeMap(new TextureCubeMap(glm::vec2(cube_size, cube_size), GL_DEPTH_COMPONENT16));
|
|
|
|
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
|
|
|
}
|
2014-12-15 01:09:33 +00:00
|
|
|
|
|
|
|
framebuffer_cube = SharedFrameBufferObject(new FrameBufferObject());
|
2015-01-19 17:22:25 +00:00
|
|
|
|
|
|
|
depthTexture_cube = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT16));
|
|
|
|
depthTexture_cube->setMinFilter(GL_NEAREST);
|
|
|
|
depthTexture_cube->setMagFilter(GL_NEAREST);
|
|
|
|
depthTexture_cube->setWrapS(GL_CLAMP_TO_EDGE);
|
|
|
|
depthTexture_cube->setWrapT(GL_CLAMP_TO_EDGE);
|
|
|
|
depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE);
|
2015-01-26 20:48:44 +00:00
|
|
|
|
|
|
|
saveDepthBufferBool = false;
|
2014-12-01 16:49:59 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 17:33:42 +00:00
|
|
|
glm::uvec2 Graphics::getWindowSize() {
|
|
|
|
return windowSize;
|
|
|
|
}
|
|
|
|
|
2015-01-26 20:48:44 +00:00
|
|
|
void Graphics::saveDepthBuffer() {
|
|
|
|
saveDepthBufferBool = true;
|
|
|
|
}
|
|
|
|
|
2015-01-25 23:06:31 +00:00
|
|
|
void Graphics::render(double time)
|
2014-10-20 15:31:26 +00:00
|
|
|
{
|
2014-12-15 01:09:33 +00:00
|
|
|
// At first render shadows
|
|
|
|
depthShader->use();
|
|
|
|
// render depth textures for point lights
|
2014-12-15 21:00:23 +00:00
|
|
|
glViewport(0, 0, cube_size, cube_size);
|
2014-12-15 10:22:32 +00:00
|
|
|
glm::mat4 depthProjectionMatrix_pointlights = glm::perspective(1.571f, (float)cube_size/(float)cube_size, 0.1f, farPlane);
|
2015-01-26 20:48:44 +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.0001f, 1.0f, 0.0f),
|
|
|
|
glm::vec3(0.0001f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f)};
|
2014-12-15 01:09:33 +00:00
|
|
|
|
2014-12-15 21:00:23 +00:00
|
|
|
framebuffer_cube->bind();
|
2015-01-26 20:48:44 +00:00
|
|
|
static bool printed = false;
|
2014-12-15 14:58:55 +00:00
|
|
|
//for (unsigned int i_pointlight = 0; i_pointlight<level->getLights()->size(); i_pointlight++) {
|
2014-12-19 12:09:25 +00:00
|
|
|
for (unsigned int i_pointlight = 0; i_pointlight<1 && i_pointlight<level->getLights()->size(); i_pointlight++) {
|
2014-12-15 01:09:33 +00:00
|
|
|
// 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_pointlight)->getObjectName(), 0);
|
2015-01-23 13:33:04 +00:00
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
2014-12-15 01:09:33 +00:00
|
|
|
glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * glm::lookAt(level->getLights()->at(i_pointlight).getPosition(),
|
|
|
|
level->getLights()->at(i_pointlight).getPosition() + looking_directions[i_face], glm::vec3(0.0f, 1.0f, 0.0f));
|
2015-01-26 20:48:44 +00:00
|
|
|
if (!printed) {
|
|
|
|
printf("\n\nView matrix:\n %2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n\n\n",
|
|
|
|
depthViewProjectionMatrix_face[0][0], depthViewProjectionMatrix_face[0][1], depthViewProjectionMatrix_face[0][2], depthViewProjectionMatrix_face[0][3],
|
|
|
|
depthViewProjectionMatrix_face[1][0], depthViewProjectionMatrix_face[1][1], depthViewProjectionMatrix_face[1][2], depthViewProjectionMatrix_face[1][3],
|
|
|
|
depthViewProjectionMatrix_face[2][0], depthViewProjectionMatrix_face[2][1], depthViewProjectionMatrix_face[2][2], depthViewProjectionMatrix_face[2][3],
|
|
|
|
depthViewProjectionMatrix_face[3][0], depthViewProjectionMatrix_face[3][1], depthViewProjectionMatrix_face[3][2], depthViewProjectionMatrix_face[3][3]
|
|
|
|
);
|
|
|
|
}
|
2015-01-19 17:22:25 +00:00
|
|
|
level->render(depthShader, false, &depthViewProjectionMatrix_face);
|
2014-12-15 01:09:33 +00:00
|
|
|
if (!framebuffer_cube->isFrameBufferObjectComplete()) {
|
|
|
|
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
|
|
|
}
|
2015-01-26 22:11:04 +00:00
|
|
|
if (saveDepthBufferBool && i_face == 3) {
|
2015-01-26 20:48:44 +00:00
|
|
|
printf("Doing stuff...\n");
|
|
|
|
saveDepthBufferToDisk(framebuffer_cube, "face2.png");
|
|
|
|
saveDepthBufferBool = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!printed) {
|
|
|
|
printed = true;
|
2014-12-15 01:09:33 +00:00
|
|
|
}
|
2014-12-15 21:00:23 +00:00
|
|
|
}
|
2014-12-04 00:13:59 +00:00
|
|
|
// render depth texture for sun
|
2014-12-15 01:09:33 +00:00
|
|
|
glViewport(0, 0, windowSize.x, windowSize.y);
|
2014-12-04 16:19:58 +00:00
|
|
|
|
|
|
|
// far pass
|
2015-01-18 23:52:57 +00:00
|
|
|
framebuffer->bind();
|
2014-12-04 16:19:58 +00:00
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
2015-01-18 23:52:57 +00:00
|
|
|
glm::vec3 sunVector = (level->getCameraCenter()->getPosition() + level->getDirectionalLight()->getPosition());
|
|
|
|
glm::mat4 depthViewProjectionMatrix = glm::ortho<float>(-farPlane/2.0f, farPlane/2.0f, -farPlane/2.0f, farPlane/2.0f, -farPlane/2.0f, farPlane/2.0f) *
|
2014-12-04 00:13:59 +00:00
|
|
|
glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0));
|
2015-01-18 23:52:57 +00:00
|
|
|
level->render(depthShader, false, &depthViewProjectionMatrix);
|
|
|
|
if (!framebuffer->isFrameBufferObjectComplete()) {
|
2014-12-04 00:13:59 +00:00
|
|
|
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
2014-11-14 15:47:47 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 00:13:59 +00:00
|
|
|
// final render pass
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2014-10-20 15:31:26 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2014-12-04 00:13:59 +00:00
|
|
|
lightingShader->use();
|
|
|
|
|
2015-01-25 23:06:31 +00:00
|
|
|
if (level->getLights()->size() > 0) {
|
|
|
|
lightingShader->setTexture("shadowMap_cube", depth_cubeMaps.at(0), 4);
|
|
|
|
}
|
|
|
|
|
2014-10-31 09:38:57 +00:00
|
|
|
//set lighting parameters
|
2015-01-25 23:06:31 +00:00
|
|
|
|
|
|
|
// TODO look into doing this less often, offload to another thread?
|
|
|
|
// TODO figure out how to deal with bigger numbers of lights. load the nearest on demand?
|
2015-01-25 23:18:09 +00:00
|
|
|
double nextUpdate = lastUpdate + lightUpdateDelay;
|
2015-01-25 23:06:31 +00:00
|
|
|
if (time >= nextUpdate)
|
|
|
|
{
|
|
|
|
updateLights();
|
|
|
|
lastUpdate = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
);
|
|
|
|
glm::mat4 depthBiasVP = biasMatrix*depthViewProjectionMatrix;
|
|
|
|
|
|
|
|
lightingShader->setTexture("shadowMap", depthTexture, 1);
|
|
|
|
|
|
|
|
lightingShader->setUniform("farPlane", farPlane);
|
|
|
|
|
|
|
|
// set fog Parameters
|
|
|
|
lightingShader->setUniform("fogColor", level->getFogColour());
|
|
|
|
lightingShader->setUniform("cameraCenter", level->getCameraCenter()->getPosition());
|
|
|
|
|
|
|
|
// set Material Parameters
|
|
|
|
lightingShader->setUniform("ambientColor", level->getAmbientLight());
|
|
|
|
lightingShader->setUniform("camera", level->getCameraPosition());
|
|
|
|
|
|
|
|
//set view and projection matrix
|
|
|
|
glm::mat4 lightingViewProjectionMatrix = glm::perspective(1.571f, (float)windowSize.x/(float)windowSize.y, 0.1f, farPlane) * buildViewMatrix(level);
|
|
|
|
|
|
|
|
std::vector<glm::mat4> shadowVPs = std::vector<glm::mat4>();
|
|
|
|
shadowVPs.push_back(depthBiasVP);
|
|
|
|
|
|
|
|
// render the level
|
|
|
|
level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::updateLights() {
|
2014-12-15 00:12:51 +00:00
|
|
|
if (level->getLights()->size() > 0) {
|
|
|
|
lightingShader->setUniform("lightCount", (int) level->getLights()->size());
|
2014-10-31 11:56:09 +00:00
|
|
|
|
|
|
|
// Build light position array
|
2014-12-15 00:12:51 +00:00
|
|
|
glm::vec3 lightSources[level->getLights()->size()];
|
|
|
|
for(unsigned int i = 0; i<level->getLights()->size(); i++) {
|
|
|
|
lightSources[i] = level->getLights()->at(i).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
|
2014-12-15 00:12:51 +00:00
|
|
|
glm::vec3 lightColours[level->getLights()->size()];
|
|
|
|
for(unsigned int i = 0; i<level->getLights()->size(); i++) {
|
|
|
|
lightColours[i] = level->getLights()->at(i).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
|
2014-12-15 00:12:51 +00:00
|
|
|
float lightIntensities[level->getLights()->size()];
|
|
|
|
for(unsigned int i = 0; i<level->getLights()->size(); i++) {
|
|
|
|
lightIntensities[i] = level->getLights()->at(i).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);
|
2014-10-31 09:38:57 +00:00
|
|
|
}
|
2014-11-17 16:51:15 +00:00
|
|
|
// set directional Light
|
|
|
|
if(level->getDirectionalLight()) {
|
2014-12-04 00:13:59 +00:00
|
|
|
lightingShader->setUniform("directionalLightVector",
|
2014-11-17 16:51:15 +00:00
|
|
|
level->getDirectionalLight()->getPosition());
|
2014-12-04 00:13:59 +00:00
|
|
|
lightingShader->setUniform("directionalColor",
|
2014-11-17 16:51:15 +00:00
|
|
|
level->getDirectionalLight()->getColour());
|
2014-12-04 00:13:59 +00:00
|
|
|
lightingShader->setUniform("directionalIntensity",
|
2014-11-17 16:51:15 +00:00
|
|
|
level->getDirectionalLight()->getIntensity());
|
|
|
|
}
|
2014-11-14 15:47:47 +00:00
|
|
|
}
|
|
|
|
|
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-01-18 23:52:57 +00:00
|
|
|
depthTexture->resize(glm::vec2(windowSize.x, windowSize.y));
|
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-01-19 16:12:58 +00:00
|
|
|
//return glm::lookAt(level->getCamera()->getPosition(), level->getCamera()->getPosition() + level->getCamera()->getDirection(), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
|
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));
|
2015-01-19 16:12:58 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
void Graphics::saveDepthBufferToDisk(SharedFrameBufferObject fbo, std::string filename) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
glGetTexImage(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer);
|
|
|
|
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;
|
|
|
|
}
|