diff --git a/.gitignore b/.gitignore index 0af37a1..1f663d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ binaries build Makefile +CMakeLists.txt.user +*.cbp diff --git a/camera.cc b/camera.cc index 2036d94..08ea557 100644 --- a/camera.cc +++ b/camera.cc @@ -1,12 +1,12 @@ #include "camera.hh" -Camera::Camera(glm::vec3 rotation, float distance) { +Camera::Camera(glm::vec2 rotation, float distance) { this->rotation = rotation; this->distance = distance; } Camera::Camera() { - rotation = glm::vec3(0.0f, 0.0f, 0.0f); + rotation = glm::vec2(0.0f, 0.0f); distance = 1.0f; } @@ -21,14 +21,24 @@ void Camera::setDistance(float distance) { this->distance = distance; } -glm::vec3 Camera::getRotation() { +glm::vec2 Camera::getRotation() { return rotation; } -void Camera::setRotation(glm::vec3 rotation) { +void Camera::setRotation(glm::vec2 rotation) { this->rotation = rotation; } -void Camera::updateRotation(glm::vec3 rotation) { - this->rotation += rotation;; +void Camera::updateRotation(glm::vec2 rotation) { + if((this->rotation.x + rotation.x) >= 1.57f) { + this->rotation.x = 1.57; + this->rotation.y += rotation.y; + } + else if ((this->rotation.x + rotation.x) <= -1.57f) { + this->rotation.x = -1.57f; + this->rotation.y += rotation.y; + } + else { + this-> rotation += rotation; + } } diff --git a/camera.hh b/camera.hh index 7cf3d98..7391fa3 100644 --- a/camera.hh +++ b/camera.hh @@ -5,17 +5,17 @@ class Camera { public: - Camera(glm::vec3 rotation, float distance); + Camera(glm::vec2 rotation, float distance); Camera(); ~Camera(); float getDistance(); void setDistance(float distance); - glm::vec3 getRotation(); - void setRotation(glm::vec3 rotation); - void updateRotation(glm::vec3 rotation); //adds to current rotation + glm::vec2 getRotation(); + void setRotation(glm::vec2 rotation); + void updateRotation(glm::vec2 rotation); //adds to current rotation private: float distance; - glm::vec3 rotation; + glm::vec2 rotation; }; #endif diff --git a/entity.cc b/entity.cc index 4f68a6d..20a92d8 100644 --- a/entity.cc +++ b/entity.cc @@ -1,6 +1,11 @@ #include "entity.hh" Entity::Entity(glm::vec3 position, glm::vec3 rotation) { + this->position = position; + setRotation(rotation); +} + +Entity::Entity(glm::vec3 position, glm::mat4 rotation) { this->position = position; this->rotation = rotation; } @@ -15,7 +20,7 @@ glm::vec3 Entity::getPosition() { return position; } -glm::vec3 Entity::getRotation() { +glm::mat4 Entity::getRotation() { return rotation; } @@ -24,5 +29,11 @@ void Entity::setPosition(glm::vec3 position) { } void Entity::setRotation(glm::vec3 rotation) { + this->rotation = glm::rotate(rotation.x, glm::vec3(1.0f, 0.0f, 0.0f)) + * glm::rotate(rotation.y, glm::vec3(0.0f, 1.0f, 0.0f)) + * glm::rotate(rotation.z, glm::vec3(0.0f, 0.0f, 1.0f)); +} + +void Entity::setRotation(glm::mat4 rotation) { this->rotation = rotation; } diff --git a/entity.hh b/entity.hh index 959cf5d..c712644 100644 --- a/entity.hh +++ b/entity.hh @@ -6,15 +6,17 @@ class Entity { public: Entity(glm::vec3 position, glm::vec3 rotation); + Entity(glm::vec3 position, glm::mat4 rotation); Entity(); ~Entity(); void setPosition(glm::vec3 positon); void setRotation(glm::vec3 rotation); + void setRotation(glm::mat4 rotation); glm::vec3 getPosition(); - glm::vec3 getRotation(); + glm::mat4 getRotation(); private: glm::vec3 position; - glm::vec3 rotation; + glm::mat4 rotation; }; #endif diff --git a/extern/bullet/src/LinearMath/btAlignedObjectArray.h b/extern/bullet/src/LinearMath/btAlignedObjectArray.h index 24e59ab..ab6c41c 100644 --- a/extern/bullet/src/LinearMath/btAlignedObjectArray.h +++ b/extern/bullet/src/LinearMath/btAlignedObjectArray.h @@ -17,6 +17,8 @@ subject to the following restrictions: #ifndef BT_OBJECT_ARRAY__ #define BT_OBJECT_ARRAY__ +#pragma GCC diagnostic ignored "-Wunused-variable" + #include "btScalar.h" // has definitions like SIMD_FORCE_INLINE #include "btAlignedAllocator.h" diff --git a/graphics.cc b/graphics.cc index 35513f5..651fe00 100644 --- a/graphics.cc +++ b/graphics.cc @@ -20,6 +20,10 @@ GLFWwindow* Graphics::getWindow() { return window; } +glm::uvec2 Graphics::getWindowSize() { + return windowSize; +} + void Graphics::setGLFWHintsForOpenGLVersion( unsigned int _version ) { #ifdef __APPLE__ @@ -159,8 +163,8 @@ glm::mat4 Graphics::buildFrustum( float phiInDegree, float _near, float _far, fl glm::mat4 Graphics::buildViewMatrix(Level* level) { glm::vec4 cameraVector = glm::vec4(0.0f, 0.0f, level->getCamera().getDistance(), 0.0f); // rotate vector - glm::mat4 rotationMatrix = glm::rotate(level->getCamera().getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f)) * - glm::rotate(level->getCamera().getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) * glm::rotate(level->getCamera().getRotation()[2], glm::vec3(0.0f, 0.0f, 1.0f)); + glm::mat4 rotationMatrix = + glm::rotate(level->getCamera().getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) *glm::rotate(level->getCamera().getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f)); cameraVector = rotationMatrix * cameraVector; //construct lookAt (cameraPosition = cameraCenter + cameraVector return glm::lookAt(level->getCameraCenter()->getPosition() + glm::vec3(cameraVector), level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f)); diff --git a/level.cc b/level.cc index 99287d6..88bf785 100644 --- a/level.cc +++ b/level.cc @@ -13,7 +13,7 @@ Level::~Level() { void Level::load(ACGL::OpenGL::SharedShaderProgram shader) { // currently hard coded should later read this stuff out of a file - this->camera = Camera(glm::vec3(-0.8f, 0.0f, 0.0f), 3.0f); + this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f); // load the geometry of the stanford bunny and build a VAO: Model model = Model("Bunny.obj", 0.25f); // load a texture: @@ -34,12 +34,13 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) { // load terrain this->terrain.load(); - Model terrainModel = this->terrain.getModel(); + Model terrainModel = Model(this->terrain.getModel()); // load a texture: - Material terrainMaterial = Material("clownfishBunny.png", 0.7f, 0.7f, 0.3f, 1.0f); + Material terrainMaterial = Material("clownfishBunny.png", 1.0f, 0.0f, 0.0f, 3.0f); //Create object Object terrainObject = Object(terrainModel, terrainMaterial, - glm::vec3(-0.5f*(float)this->terrain.getHeightmapHeight(), 0.0f, -0.5f*(float)this->terrain.getHeightmapWidth()), + //glm::vec3(-0.5f*(float)this->terrain.getHeightmapHeight(), -0.5f, -0.5f*(float)this->terrain.getHeightmapWidth()), + glm::vec3(-1.0f, 0.0f, -1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), shader); objects.push_back(object); @@ -51,12 +52,19 @@ void Level::render() { for(unsigned int i = 0; iterrain.render(); } -void Level::update(float runTime) { +void Level::update(float runTime, glm::vec2 mouseDelta) { // rotate bunny - cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f)); + //cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f)); + // Ignore first two mouse updates, because they are incorrect + static int i = 0; + if (i <2) { + i++; + } + else { + camera.updateRotation(mouseDelta/100.0f); + } } glm::vec3 Level::getAmbientLight() { diff --git a/level.hh b/level.hh index e472536..9af2592 100644 --- a/level.hh +++ b/level.hh @@ -15,7 +15,7 @@ class Level { Level(); ~Level(); void load(ACGL::OpenGL::SharedShaderProgram shader); // Shader is necessary for correct texture assigning - void update(float runTime); + void update(float runTime, glm::vec2 mouseDelta); void render(); glm::vec3 getAmbientLight(); std::vector getLights(); diff --git a/main.cc b/main.cc index 6efddf7..5991a78 100644 --- a/main.cc +++ b/main.cc @@ -16,13 +16,6 @@ #include #include -#include "model.hh" - -using namespace std; -using namespace ACGL::OpenGL; -using namespace ACGL::Base; -using namespace ACGL::Utils; - Application::Application() { graphics = Graphics(glm::uvec2(1024, 786), 0.1f, 100.0f); } @@ -47,13 +40,17 @@ void Application::init() ACGL::Base::Settings::the()->setTexturePath("Geometry/"); ACGL::Base::Settings::the()->setGeometryPath("Geometry/"); - // 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"); + // construct VAO to give shader correct Attribute locations + ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared(); + 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(); + vao->attachAllAttributes(ab); // look up all shader files starting with 'phong' and build a ShaderProgram from it: shader = ACGL::OpenGL::ShaderProgramCreator("phong").attributeLocations( - model.getReference()->getAttributeLocations()).create(); + vao->getAttributeLocations()).create(); shader->use(); // load Level @@ -63,11 +60,6 @@ void Application::init() openGLCriticalError(); } -/********************************************************************************************************************** - * Returns true if a window with the desired context could get created. - * Requested OpenGL version gets set by ACGL defines. - */ - static void keyCallback(GLFWwindow* _window, int _key, int, int _action, int) { if (_key == GLFW_KEY_ESCAPE && _action == GLFW_PRESS) { @@ -91,10 +83,12 @@ int main( int argc, char *argv[] ) ///////////////////////////////////////////////////////////////////////////////////// // Set window title to binary name (without the path): // - std::vector tmp = StringHelpers::split( std::string( argv[0] ), '/' ); + std::vector tmp = ACGL::Utils::StringHelpers::split( std::string( argv[0] ), '/' ); glfwSetWindowTitle(app.getGraphics()->getWindow(), tmp[tmp.size()-1].c_str() ); // Ensure we can capture the escape key being pressed below - glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_STICKY_KEYS, 1 ); + glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_STICKY_KEYS, 1); + // Hide mouse cursor + glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN); //glfwSetWindowSizeCallback(app.getGraphics(), resizeCallback); glfwSetKeyCallback(app.getGraphics()->getWindow(), keyCallback ); @@ -118,15 +112,20 @@ int main( int argc, char *argv[] ) double now = glfwGetTime(); if (showNextFPS <= now) { - stringstream sstream (stringstream::in | stringstream::out); - sstream << setprecision(1) << std::fixed + std::stringstream sstream (std::stringstream::in | std::stringstream::out); + sstream << std::setprecision(1) << std::fixed << tmp[tmp.size()-1] << " - FPS: " << frameCount / (now-showNextFPS + FPSdelay) << " " << 1000 * (now-showNextFPS + FPSdelay)/frameCount << " msec"; glfwSetWindowTitle(app.getGraphics()->getWindow(), sstream.str().c_str() ); showNextFPS = now + FPSdelay; frameCount = 0; } - app.getLevel()->update(now - startTimeInSeconds); + double xpos, ypos; + glfwGetCursorPos(app.getGraphics()->getWindow(), &xpos, &ypos); + glfwSetCursorPos(app.getGraphics()->getWindow(), app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2); + + app.getLevel()->update(now - startTimeInSeconds, glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2, + (float)xpos-app.getGraphics()->getWindowSize().x/2)); app.getGraphics()->render(app.getLevel(), app.getShader()); openGLCriticalError(); diff --git a/object.cc b/object.cc index a917b4f..7bd7580 100644 --- a/object.cc +++ b/object.cc @@ -24,9 +24,7 @@ void Object::render() { shader->setUniform("shininess", material.getShininess()); shader->setTexture("uTexture", material.getReference(), 0); // set model matrix - glm::mat4 rotationMatrix = glm::rotate(this->getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f)) * - glm::rotate(this->getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) * glm::rotate(this->getRotation()[2], glm::vec3(0.0f, 0.0f, 1.0f)); - glm::mat4 modelMatrix = glm::translate(this->getPosition()) * rotationMatrix * glm::scale(glm::vec3(model.getScale())); + glm::mat4 modelMatrix = glm::translate(getPosition()) * getRotation() * glm::scale(glm::vec3(model.getScale())); shader->setUniform( "modelMatrix", modelMatrix); // draw model.getReference()->render(); diff --git a/terrain.cc b/terrain.cc index dff4437..80ef0fd 100644 --- a/terrain.cc +++ b/terrain.cc @@ -42,11 +42,12 @@ void Terrain::load() { void Terrain::makeTriangleMesh(){ ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared(); - ab->defineAttributeWithOffset("aPosition", GL_FLOAT, 3, 0); - ab->defineAttributeWithOffset("aNormal", GL_FLOAT, 3, 3); - ab->defineAttributeWithOffset("aTexCoord", GL_FLOAT, 2, 6); + // Do NOT change the order of this! + ab->defineAttribute("aPosition", GL_FLOAT, 3); + ab->defineAttribute("aTexCoord", GL_FLOAT, 2); + ab->defineAttribute("aNormal", GL_FLOAT, 3); - unsigned int rowNum=0, columnNum=0, dataCount=0, floatsPerVertex=8; //initializing: +/* unsigned int rowNum=0, columnNum=0, dataCount=0, abNumFloats=8; //initializing: bool movingRight = true, isUp = true; int numVertices = (this->heightmapHeight - 1) * (this->heightmapWidth * 2 + 1) + 1; float* abData = new float[numVertices * floatsPerVertex]; @@ -57,53 +58,56 @@ void Terrain::makeTriangleMesh(){ if (isUp){ rowNum = rowNum + 1; isUp = false; - }else if (movingRight){ - if (columnNum == this->heightmapWidth - 1){ - set_abData(abData, dataCount, rowNum, columnNum); - dataCount += floatsPerVertex; - set_abData(abData, dataCount, rowNum, columnNum); - dataCount += floatsPerVertex; - movingRight = false; - rowNum = rowNum + 1; - } else{ - rowNum = rowNum - 1; - columnNum = columnNum + 1; - isUp = true; - } - }else{ - if (columnNum == 0){ - set_abData(abData, dataCount, rowNum, columnNum); - dataCount += floatsPerVertex; - set_abData(abData, dataCount, rowNum, columnNum); - dataCount += floatsPerVertex; - movingRight = true; - rowNum = rowNum + 1; - }else{ - rowNum = rowNum - 1; - columnNum = columnNum - 1; - isUp = true; - } } + else if (movingRight) { + if (columnNum == this->heightmapWidth - 1) { + set_abData(abData, dataCount, rowNum, columnNum); + dataCount += abNumFloats; + set_abData(abData, dataCount, rowNum, columnNum); + dataCount += abNumFloats; + movingRight = false; + rowNum = rowNum + 1; + } + else { + rowNum = rowNum - 1; + columnNum = columnNum + 1; + isUp = true; + } + } + else { + if (columnNum == 0){ + set_abData(abData, dataCount, rowNum, columnNum); + dataCount += abNumFloats; + set_abData(abData, dataCount, rowNum, columnNum); + dataCount += abNumFloats; + movingRight = true; + rowNum = rowNum + 1; + } + else { + rowNum = rowNum - 1; + columnNum = columnNum - 1; + isUp = true; + } + } } - //ab->setDataElements(numVertices, abData); - float* testData = new float[32]; - testData[0]=0.0f;testData[1]=0.0f;testData[2]=0.0f; - testData[3]=0.0f;testData[4]=1.0f;testData[5]=0.0f; - testData[6]=0.0f;testData[7]=0.0f; + ab->setDataElements(numVertices, abData);*/ + float abData[32] = {0.0f, 0.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, - testData[8]=1.0f;testData[9]=0.2f;testData[10]=0.0f; - testData[11]=0.0f;testData[12]=1.0f;testData[13]=0.0f; - testData[14]=1024.0f;testData[15]=.0f; + 1.0f, 0.0f, 0.0f, + 1.0f, 1.0f, + 0.0f, 1.0f, 0.0f, - testData[16]=0.0f;testData[17]=0.2f;testData[18]=1.0f; - testData[19]=0.0f;testData[20]=1.0f;testData[21]=0.0f; - testData[22]=0.0f;testData[23]=1024.0f; + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, - testData[24]=1.0f;testData[25]=0.0f;testData[26]=1.0f; - testData[27]=0.0f;testData[28]=1.0f;testData[29]=0.0f; - testData[30]=1024.0f;testData[31]=1024.0f; - ab->setDataElements(numVertices, testData); + 1.0f, 0.0f, 1.0f, + 0.0f, 1.0f, + 0.0f, 1.0f, 0.0f}; + ab->setDataElements(4, abData); this->triangleMesh = std::make_shared(); this->triangleMesh->bind(); this->triangleMesh->setMode(GL_TRIANGLE_STRIP); @@ -118,33 +122,34 @@ void Terrain::set_abData(float* abData, unsigned int dataCount, unsigned int row abData[dataCount+1] = heightmap[rowNum][columnNum]; abData[dataCount+2] = (float)columnNum; + //set Texture Coordinate + abData[dataCount+3] = (float)(rowNum % 2); + abData[dataCount+4] = (float)(columnNum % 2); + //setNormal if (rowNum==0 || rowNum==(this->heightmapHeight-1) || columnNum==0 || columnNum==(this->heightmapWidth-1)){ - abData[dataCount+3] = 0.0; - abData[dataCount+4] = 1.0; abData[dataCount+5] = 0.0; - }else{ - glm::vec3 sumNormals; - for (int i=-1; i<2; i+=1){ - for (int j=-1; j<2; j+=1){ - glm::vec3 vecA, vecB, normal; - vecA = glm::vec3((float)i, (heightmap[rowNum+i][columnNum] - heightmap[rowNum][columnNum]), 0.0f); - vecB = glm::vec3(0.0f, (heightmap[rowNum][columnNum+j] - heightmap[rowNum][columnNum]), (float)j); - normal = glm::normalize(glm::cross(vecA, vecB)); - if(i+j==0) - normal = normal*(-1.0f); - sumNormals += normal; - } - } - sumNormals = sumNormals*0.111f; - abData[dataCount+3] = sumNormals[0]; - abData[dataCount+4] = sumNormals[1]; - abData[dataCount+5] = sumNormals[2]; + abData[dataCount+6] = 1.0; + abData[dataCount+7] = 0.0; + } + else { + glm::vec3 sumNormals; + for (int i=-1; i<2; i+=1) { + for (int j=-1; j<2; j+=1) { + glm::vec3 vecA, vecB, normal; + vecA = glm::vec3((float)i, (heightmap[rowNum+i][columnNum] - heightmap[rowNum][columnNum]), 0.0f); + vecB = glm::vec3(0.0f, (heightmap[rowNum][columnNum+j] - heightmap[rowNum][columnNum]), (float)j); + normal = glm::normalize(glm::cross(vecA, vecB)); + if(i+j==0) + normal = normal*(-1.0f); + sumNormals += normal; + } + } + sumNormals = sumNormals*0.111f; + abData[dataCount+5] = sumNormals[0]; + abData[dataCount+6] = sumNormals[1]; + abData[dataCount+7] = sumNormals[2]; } - - //set Texture Coordinate - abData[dataCount+6] = (float)(rowNum % 2); - abData[dataCount+7] = (float)(columnNum % 2); } Model Terrain::getModel(){ @@ -164,14 +169,3 @@ unsigned int Terrain::getHeightmapWidth(){ //return this->heightmapWidth; return 2; } - - - - - - - - - - -