Saxum/graphics.cc

107 lines
3.9 KiB
C++
Raw Normal View History

2014-10-20 16:49:10 +00:00
#include "graphics.hh"
2014-10-20 15:31:26 +00:00
2014-10-24 08:57:28 +00:00
#include "model.hh"
#include <ACGL/OpenGL/Creator/ShaderProgramCreator.hh>
2014-10-24 08:57:28 +00:00
2014-10-20 15:31:26 +00:00
using namespace std;
ACGL::OpenGL::SharedShaderProgram shader;
Level level;
2014-10-20 15:31:26 +00:00
// gets called after the OpenGL window is prepared:
void initCustomResources()
{
// define where shaders and textures can be found:
2014-10-24 08:57:28 +00:00
ACGL::Base::Settings::the()->setResourcePath("../");
ACGL::Base::Settings::the()->setShaderPath("Shader/");
ACGL::Base::Settings::the()->setTexturePath("Geometry/");
ACGL::Base::Settings::the()->setGeometryPath("Geometry/");
2014-10-20 15:31:26 +00:00
// load Model to give shader correct Attribute locations
// TODO look up if this is really necessary, since this looks really stupid.
Model model = Model("Bunny.obj");
2014-10-20 15:31:26 +00:00
// look up all shader files starting with 'phong' and build a ShaderProgram from it:
shader = ACGL::OpenGL::ShaderProgramCreator("phong").attributeLocations(
model.getReference()->getAttributeLocations()).create();
shader->use();
2014-10-20 15:31:26 +00:00
// load Level
level.load(shader);
2014-10-20 15:31:26 +00:00
// just in case: check for errors
openGLCriticalError();
}
void deleteCustomResources()
{
// we have memory management via reference counting, so nothing to do here
}
void draw(float runTime)
2014-10-20 15:31:26 +00:00
{
// update Level first TODO: move this with the rest of the stuff that doesn't belong here to main
level.update(runTime);
2014-10-20 15:31:26 +00:00
// clear the framebuffer:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//set view and projection matrix
shader->setUniform("projectionMatrix", buildFrustum(75.0, 0.1, 100.0, (float)g_windowSize.x/(float)g_windowSize.y) );
// the + (0,1,0) compensates bunny doesn't have its center at it's center
shader->setUniform("viewMatrix", glm::lookAt(level.getCameraPosition(), (level.getCameraCenter()->getPosition() + glm::vec3(0.0f, 1.0f, 0.0f)), glm::vec3(0.0f, 1.0f, 0.0f)));
2014-10-20 15:31:26 +00:00
//set lighting parameters
2014-10-31 11:56:09 +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-03 22:54:35 +00:00
glm::vec3 lightSources[level.getLights().size()];
for(unsigned int i = 0; i<level.getLights().size(); i++) {
2014-11-03 22:54:35 +00:00
lightSources[i] = level.getLights()[i].getPosition();
2014-10-31 11:56:09 +00:00
}
glUniform3fv(shader->getUniformLocation("lightSources"),
2014-11-03 22:54:35 +00:00
sizeof(lightSources), (GLfloat*) lightSources);
// Build light colour array
glm::vec3 lightColours[level.getLights().size()];
for(unsigned int i = 0; i<level.getLights().size(); i++) {
2014-11-03 22:54:35 +00:00
lightColours[i] = level.getLights()[i].getColour();
}
glUniform3fv(shader->getUniformLocation("lightColors"),
2014-11-03 22:54:35 +00:00
sizeof(lightColours), (GLfloat*) lightColours);
// Build light attenuation array
float lightIntensities[level.getLights().size()];
for(unsigned int i = 0; i<level.getLights().size(); i++) {
lightIntensities[i] = level.getLights()[i].getIntensity();
}
glUniform1fv(shader->getUniformLocation("lightIntensities"),
sizeof(lightIntensities), (GLfloat*) lightIntensities);
}
// set Material Parameters
shader->setUniform("ambientColor", level.getAmbientLight());
shader->setUniform("camera", glm::vec3(0.0f, 0.0f, 0.0f));
// render the level(currently only a bunny):
level.render();
2014-10-20 15:31:26 +00:00
}
void resizeCallback( GLFWwindow *, int newWidth, int newHeight )
{
// store the new window size and adjust the viewport:
g_windowSize = glm::uvec2( newWidth, newHeight);
glViewport( 0, 0, g_windowSize.x, g_windowSize.y );
}
glm::mat4 buildFrustum( float phiInDegree, float _near, float _far, float aspectRatio) {
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);
}