2014-10-20 16:49:10 +00:00
|
|
|
#include "graphics.hh"
|
2014-10-20 15:31:26 +00:00
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
Graphics::Graphics() {
|
|
|
|
}
|
2014-10-20 15:31:26 +00:00
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
GLFWwindow* Graphics::getWindow() {
|
|
|
|
return window;
|
2014-10-20 15:31:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 17:33:42 +00:00
|
|
|
glm::uvec2 Graphics::getWindowSize() {
|
|
|
|
return windowSize;
|
|
|
|
}
|
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
void Graphics::setGLFWHintsForOpenGLVersion( unsigned int _version )
|
2014-10-20 15:31:26 +00:00
|
|
|
{
|
2014-11-14 15:47:47 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#if (ACGL_OPENGL_VERSION >= 30)
|
|
|
|
// request OpenGL 3.2, will return a 4.1 context on Mavericks
|
|
|
|
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
|
|
|
|
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 );
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
// non-apple
|
|
|
|
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, _version / 10 );
|
|
|
|
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, _version % 10 );
|
|
|
|
#ifdef ACGL_OPENGL_PROFILE_CORE
|
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
#endif
|
|
|
|
#endif
|
2014-10-20 15:31:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
bool Graphics::createWindow()
|
2014-10-20 15:31:26 +00:00
|
|
|
{
|
2014-11-14 15:47:47 +00:00
|
|
|
// Initialise GLFW
|
|
|
|
if ( !glfwInit() )
|
|
|
|
{
|
2014-12-01 16:40:39 +00:00
|
|
|
ACGL::Utils::error() << "Failed to initialize GLFW" << std::endl;
|
2014-11-14 15:47:47 +00:00
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure OpenGL context
|
|
|
|
setGLFWHintsForOpenGLVersion( ACGL_OPENGL_VERSION );
|
|
|
|
|
|
|
|
// activate multisampling (second parameter is the number of samples):
|
|
|
|
//glfwWindowHint( GLFW_SAMPLES, 8 );
|
|
|
|
|
|
|
|
// request an OpenGL debug context:
|
|
|
|
glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, true );
|
|
|
|
|
|
|
|
// define whether the window can get resized:
|
2014-11-15 14:19:48 +00:00
|
|
|
glfwWindowHint(GLFW_RESIZABLE, true);
|
2014-11-14 15:47:47 +00:00
|
|
|
|
|
|
|
// try to create an OpenGL context in a window and check the supported OpenGL version:
|
|
|
|
// R,G,B,A, Depth,Stencil
|
|
|
|
window = glfwCreateWindow(windowSize.x, windowSize.y, "SWP MarbleGame Group C", NULL, NULL);
|
|
|
|
if (!getWindow()) {
|
2014-12-01 16:40:39 +00:00
|
|
|
ACGL::Utils::error() << "Failed to open a GLFW window - requested OpenGL: " << ACGL_OPENGL_VERSION << std::endl;
|
2014-11-14 15:47:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
ACGL::init();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Graphics::Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane) {
|
|
|
|
this->windowSize = windowSize;
|
|
|
|
this->nearPlane = nearPlane;
|
|
|
|
this->farPlane = farPlane;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Graphics::render(Level* level, ACGL::OpenGL::SharedShaderProgram shader)
|
|
|
|
{
|
2014-10-20 15:31:26 +00:00
|
|
|
// clear the framebuffer:
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2014-11-13 00:22:33 +00:00
|
|
|
//set view and projection matrix
|
2014-11-14 15:47:47 +00:00
|
|
|
shader->setUniform("projectionMatrix", buildFrustum(75.0f, 0.1f, 100.0f, (float)windowSize.x/(float)windowSize.y) );
|
|
|
|
shader->setUniform("viewMatrix", buildViewMatrix(level));
|
2014-10-20 15:31:26 +00:00
|
|
|
|
2014-10-31 09:38:57 +00:00
|
|
|
//set lighting parameters
|
2014-11-14 15:47:47 +00:00
|
|
|
if (level->getLights().size() > 0) {
|
|
|
|
shader->setUniform("lightCount", (int) level->getLights().size());
|
2014-10-31 11:56:09 +00:00
|
|
|
|
|
|
|
// TODO look into doing this less often
|
|
|
|
// Build light position array
|
2014-11-14 15:47:47 +00:00
|
|
|
glm::vec3 lightSources[level->getLights().size()];
|
|
|
|
for(unsigned int i = 0; i<level->getLights().size(); i++) {
|
|
|
|
lightSources[i] = level->getLights()[i].getPosition();
|
2014-10-31 11:56:09 +00:00
|
|
|
}
|
2014-11-08 01:45:32 +00:00
|
|
|
glUniform3fv(shader->getUniformLocation("lightSources"),
|
2014-11-03 22:54:35 +00:00
|
|
|
sizeof(lightSources), (GLfloat*) lightSources);
|
|
|
|
// Build light colour array
|
2014-11-14 15:47:47 +00:00
|
|
|
glm::vec3 lightColours[level->getLights().size()];
|
|
|
|
for(unsigned int i = 0; i<level->getLights().size(); i++) {
|
|
|
|
lightColours[i] = level->getLights()[i].getColour();
|
2014-11-03 22:54:35 +00:00
|
|
|
}
|
2014-11-08 01:45:32 +00:00
|
|
|
glUniform3fv(shader->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-11-14 15:47:47 +00:00
|
|
|
float lightIntensities[level->getLights().size()];
|
|
|
|
for(unsigned int i = 0; i<level->getLights().size(); i++) {
|
|
|
|
lightIntensities[i] = level->getLights()[i].getIntensity();
|
2014-11-03 23:47:35 +00:00
|
|
|
}
|
2014-11-08 01:45:32 +00:00
|
|
|
glUniform1fv(shader->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()) {
|
|
|
|
shader->setUniform("directionalLightVector",
|
|
|
|
level->getDirectionalLight()->getPosition());
|
|
|
|
shader->setUniform("directionalColor",
|
|
|
|
level->getDirectionalLight()->getColour());
|
|
|
|
shader->setUniform("directionalIntensity",
|
|
|
|
level->getDirectionalLight()->getIntensity());
|
|
|
|
}
|
2014-10-31 09:38:57 +00:00
|
|
|
|
2014-11-19 00:58:48 +00:00
|
|
|
// set fog Parameters
|
2014-11-25 12:54:00 +00:00
|
|
|
shader->setUniform("fogEnd", (farPlane/2.0f)-10.0f);
|
2014-11-19 00:58:48 +00:00
|
|
|
shader->setUniform("fogColor", level->getFogColor());
|
2014-11-21 01:46:02 +00:00
|
|
|
shader->setUniform("cameraCenter", level->getCameraCenter()->getPosition());
|
2014-11-19 00:58:48 +00:00
|
|
|
|
2014-11-07 10:37:26 +00:00
|
|
|
// set Material Parameters
|
2014-11-14 15:47:47 +00:00
|
|
|
shader->setUniform("ambientColor", level->getAmbientLight());
|
2014-11-19 00:58:48 +00:00
|
|
|
shader->setUniform("camera", level->getCameraPosition());
|
2014-11-07 10:37:26 +00:00
|
|
|
|
2014-11-27 23:17:56 +00:00
|
|
|
// render the level
|
|
|
|
level->render(shader);
|
2014-11-14 15:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::setWindowSize(glm::uvec2 windowSize) {
|
|
|
|
this->windowSize = windowSize;
|
2014-10-20 15:31:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
glm::mat4 Graphics::buildFrustum( float phiInDegree, float _near, float _far, float aspectRatio) {
|
2014-10-20 16:16:47 +00:00
|
|
|
|
|
|
|
float phiHalfInRadians = 0.5*phiInDegree * (M_PI/180.0);
|
|
|
|
float top = _near * tan( phiHalfInRadians );
|
|
|
|
float bottom = -top;
|
|
|
|
float left = bottom * aspectRatio;
|
|
|
|
float right = -left;
|
|
|
|
|
|
|
|
return glm::frustum(left, right, bottom, top, _near, _far);
|
|
|
|
}
|
2014-11-13 17:17:58 +00:00
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
glm::mat4 Graphics::buildViewMatrix(Level* level) {
|
2014-11-13 17:17:58 +00:00
|
|
|
//construct lookAt (cameraPosition = cameraCenter + cameraVector
|
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;
|
|
|
|
}
|