2014-10-20 15:31:26 +00:00
|
|
|
#include "main.hh"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <ACGL/OpenGL/Objects.hh>
|
|
|
|
#include <ACGL/OpenGL/Data/TextureData.hh>
|
|
|
|
#include <ACGL/Math/Math.hh>
|
|
|
|
#include <ACGL/Utils/FileHelpers.hh>
|
|
|
|
#include <ACGL/Utils/StringHelpers.hh>
|
2014-11-14 15:47:47 +00:00
|
|
|
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
|
|
|
|
#include <ACGL/OpenGL/Creator/ShaderProgramCreator.hh>
|
2014-10-20 15:31:26 +00:00
|
|
|
|
|
|
|
#include <ACGL/OpenGL/glloaders/extensions.hh>
|
2014-11-14 15:47:47 +00:00
|
|
|
#include <ACGL/Base/Settings.hh>
|
|
|
|
|
|
|
|
Application::Application() {
|
2014-11-18 23:59:02 +00:00
|
|
|
graphics = Graphics(glm::uvec2(1024, 786), 0.1f, 150.0f);
|
2014-11-14 15:47:47 +00:00
|
|
|
}
|
2014-10-20 15:31:26 +00:00
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
Graphics* Application::getGraphics() {
|
|
|
|
return &graphics;
|
|
|
|
}
|
|
|
|
|
|
|
|
Level* Application::getLevel() {
|
|
|
|
return &level;
|
|
|
|
}
|
2014-10-20 15:31:26 +00:00
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
ACGL::OpenGL::SharedShaderProgram Application::getShader() {
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::init()
|
2014-10-20 15:31:26 +00:00
|
|
|
{
|
2014-11-14 15:47:47 +00:00
|
|
|
// define where shaders and textures can be found:
|
|
|
|
ACGL::Base::Settings::the()->setResourcePath("../");
|
|
|
|
ACGL::Base::Settings::the()->setShaderPath("Shader/");
|
2014-11-17 12:36:05 +00:00
|
|
|
ACGL::Base::Settings::the()->setTexturePath("Levels/Textures/");
|
|
|
|
ACGL::Base::Settings::the()->setGeometryPath("Levels/Geometry/");
|
2014-11-14 15:47:47 +00:00
|
|
|
|
2014-11-15 12:45:05 +00:00
|
|
|
// construct VAO to give shader correct Attribute locations
|
|
|
|
ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared<ACGL::OpenGL::ArrayBuffer>();
|
|
|
|
ab->defineAttribute("aPosition", GL_FLOAT, 3);
|
|
|
|
ab->defineAttribute("aTexCoord", GL_FLOAT, 2);
|
|
|
|
ab->defineAttribute("aNormal", GL_FLOAT, 3);
|
|
|
|
ACGL::OpenGL::SharedVertexArrayObject vao = std::make_shared<ACGL::OpenGL::VertexArrayObject>();
|
|
|
|
vao->attachAllAttributes(ab);
|
2014-11-14 15:47:47 +00:00
|
|
|
|
|
|
|
// look up all shader files starting with 'phong' and build a ShaderProgram from it:
|
|
|
|
shader = ACGL::OpenGL::ShaderProgramCreator("phong").attributeLocations(
|
2014-11-15 12:45:05 +00:00
|
|
|
vao->getAttributeLocations()).create();
|
2014-11-14 15:47:47 +00:00
|
|
|
shader->use();
|
|
|
|
|
|
|
|
// load Level
|
|
|
|
level.load(shader);
|
|
|
|
|
|
|
|
// just in case: check for errors
|
|
|
|
openGLCriticalError();
|
2014-10-20 15:31:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-15 14:19:48 +00:00
|
|
|
void resizeCallback(GLFWwindow* window, int newWidth, int newHeight)
|
|
|
|
{
|
|
|
|
// store the new window size and adjust the viewport:
|
|
|
|
app.getGraphics()->setWindowSize(glm::uvec2( newWidth, newHeight));
|
|
|
|
glViewport( 0, 0, newWidth, newHeight);
|
|
|
|
}
|
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
static void keyCallback(GLFWwindow* _window, int _key, int, int _action, int)
|
|
|
|
{
|
|
|
|
if (_key == GLFW_KEY_ESCAPE && _action == GLFW_PRESS) {
|
|
|
|
glfwSetWindowShouldClose( _window, GL_TRUE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 13:57:01 +00:00
|
|
|
static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
|
|
|
|
app.getLevel()->getCamera()->updateDistance(-(float)yoffset);
|
|
|
|
}
|
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
|
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
2014-11-15 13:57:01 +00:00
|
|
|
//Application app = Application();
|
2014-11-14 15:47:47 +00:00
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Create OpenGL capable window:
|
|
|
|
//
|
2014-11-14 15:47:47 +00:00
|
|
|
if ( !app.getGraphics()->createWindow() ) {
|
2014-10-20 15:31:26 +00:00
|
|
|
glfwTerminate();
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Set window title to binary name (without the path):
|
|
|
|
//
|
2014-11-14 22:11:35 +00:00
|
|
|
std::vector<std::string> tmp = ACGL::Utils::StringHelpers::split( std::string( argv[0] ), '/' );
|
2014-11-14 15:47:47 +00:00
|
|
|
glfwSetWindowTitle(app.getGraphics()->getWindow(), tmp[tmp.size()-1].c_str() );
|
2014-10-20 15:31:26 +00:00
|
|
|
// Ensure we can capture the escape key being pressed below
|
2014-11-14 21:54:27 +00:00
|
|
|
glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_STICKY_KEYS, 1);
|
|
|
|
// Hide mouse cursor
|
2014-11-15 10:02:22 +00:00
|
|
|
glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
|
2014-11-15 14:19:48 +00:00
|
|
|
glfwSetWindowSizeCallback(app.getGraphics()->getWindow(), resizeCallback);
|
2014-11-14 15:47:47 +00:00
|
|
|
glfwSetKeyCallback(app.getGraphics()->getWindow(), keyCallback );
|
2014-11-15 13:57:01 +00:00
|
|
|
glfwSetScrollCallback(app.getGraphics()->getWindow(), scrollCallback );
|
2014-10-20 15:31:26 +00:00
|
|
|
|
|
|
|
// Enable vertical sync (on cards that support it) with parameter 1 - 0 means off
|
|
|
|
glfwSwapInterval( 0 );
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// OpenGL state:
|
|
|
|
//
|
|
|
|
glClearColor( 0.0, 0.0, 0.0, 1.0 );
|
|
|
|
glEnable( GL_DEPTH_TEST );
|
2014-11-17 15:15:42 +00:00
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2014-11-14 15:47:47 +00:00
|
|
|
app.init();
|
2014-10-20 15:31:26 +00:00
|
|
|
|
|
|
|
int frameCount = 0;
|
|
|
|
|
|
|
|
const double FPSdelay = 2.0;
|
|
|
|
double startTimeInSeconds = glfwGetTime();
|
|
|
|
double showNextFPS = startTimeInSeconds + FPSdelay;
|
2014-11-17 11:57:16 +00:00
|
|
|
|
|
|
|
double lastUpdate=0.0f;
|
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
|
2014-11-17 11:57:16 +00:00
|
|
|
double now = glfwGetTime()- startTimeInSeconds;
|
|
|
|
|
2014-11-17 15:07:40 +00:00
|
|
|
int stateW = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_W);
|
|
|
|
int stateA = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_A);
|
|
|
|
int stateS = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_S);
|
|
|
|
int stateD = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_D);
|
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
if (showNextFPS <= now) {
|
2014-11-14 22:11:35 +00:00
|
|
|
std::stringstream sstream (std::stringstream::in | std::stringstream::out);
|
|
|
|
sstream << std::setprecision(1) << std::fixed
|
2014-10-20 15:31:26 +00:00
|
|
|
<< tmp[tmp.size()-1] << " - FPS: " << frameCount / (now-showNextFPS + FPSdelay) << " " << 1000 * (now-showNextFPS + FPSdelay)/frameCount << " msec";
|
2014-11-14 15:47:47 +00:00
|
|
|
glfwSetWindowTitle(app.getGraphics()->getWindow(), sstream.str().c_str() );
|
2014-10-20 15:31:26 +00:00
|
|
|
showNextFPS = now + FPSdelay;
|
|
|
|
frameCount = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-14 17:33:42 +00:00
|
|
|
double xpos, ypos;
|
|
|
|
glfwGetCursorPos(app.getGraphics()->getWindow(), &xpos, &ypos);
|
|
|
|
glfwSetCursorPos(app.getGraphics()->getWindow(), app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2);
|
|
|
|
|
2014-11-17 11:57:16 +00:00
|
|
|
app.getLevel()->update(now - lastUpdate,
|
|
|
|
glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2,
|
|
|
|
(float)xpos-app.getGraphics()->getWindowSize().x/2),
|
|
|
|
stateW == GLFW_PRESS,stateA == GLFW_PRESS,stateS == GLFW_PRESS,stateD == GLFW_PRESS);
|
|
|
|
lastUpdate = now;
|
2014-11-14 15:47:47 +00:00
|
|
|
app.getGraphics()->render(app.getLevel(), app.getShader());
|
2014-11-17 11:57:16 +00:00
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
openGLCriticalError();
|
|
|
|
|
|
|
|
// MacOS X will not swap correctly is another FBO is bound:
|
|
|
|
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
|
2014-11-14 15:47:47 +00:00
|
|
|
glfwSwapBuffers(app.getGraphics()->getWindow());
|
2014-10-20 15:31:26 +00:00
|
|
|
glfwPollEvents();
|
|
|
|
frameCount++;
|
2014-11-17 15:07:40 +00:00
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
} // Check if the window was closed
|
2014-11-14 15:47:47 +00:00
|
|
|
while( !glfwWindowShouldClose(app.getGraphics()->getWindow()) );
|
2014-10-20 15:31:26 +00:00
|
|
|
|
|
|
|
glfwTerminate();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|