From 13beac44e04bd4313109347498bff1ebb5f03c65 Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 14 Nov 2014 18:31:26 +0100 Subject: [PATCH 01/13] Changed rotation from three to two vertices. --- camera.cc | 10 +++++----- camera.hh | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/camera.cc b/camera.cc index 2036d94..c5b3c77 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,14 @@ 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) { +void Camera::updateRotation(glm::vec2 rotation) { 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 From 93363d1391c3b2d12a921f590f9b96dc01304897 Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 14 Nov 2014 18:32:16 +0100 Subject: [PATCH 02/13] Changed constructor call of camera. --- level.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/level.cc b/level.cc index c726e08..f55a5d4 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: From 04234b1f1cb3bef8fb089b2b1a0fe2ffbe5867be Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 14 Nov 2014 18:33:42 +0100 Subject: [PATCH 03/13] Made mouse control camera. (Stupid init of camera...) --- graphics.cc | 8 ++++++-- level.cc | 5 +++-- level.hh | 2 +- main.cc | 7 ++++++- 4 files changed, 16 insertions(+), 6 deletions(-) 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 f55a5d4..bb8cc9f 100644 --- a/level.cc +++ b/level.cc @@ -41,9 +41,10 @@ void Level::render() { this->terrain.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)); + camera.updateRotation(mouseDelta/50.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..4ae6e46 100644 --- a/main.cc +++ b/main.cc @@ -126,7 +126,12 @@ int main( int argc, char *argv[] ) 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(); From a5e5087ebdb0e9fd08395fe683f19ca4e0d4dd44 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Fri, 14 Nov 2014 21:38:26 +0100 Subject: [PATCH 04/13] Changed default camera angle. Slowed camera movement. --- level.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/level.cc b/level.cc index 9f03ea4..09740bb 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::vec2(-0.8f, 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: @@ -57,7 +57,7 @@ void Level::render() { void Level::update(float runTime, glm::vec2 mouseDelta) { // rotate bunny //cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f)); - camera.updateRotation(mouseDelta/50.0f); + camera.updateRotation(mouseDelta/100.0f); } glm::vec3 Level::getAmbientLight() { From 29ee7268adec097d15a37e248ae04e512db1beef Mon Sep 17 00:00:00 2001 From: Faerbit Date: Fri, 14 Nov 2014 22:54:27 +0100 Subject: [PATCH 05/13] Should hide the cursor(doesn't for me. --- main.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.cc b/main.cc index 4ae6e46..535cbc1 100644 --- a/main.cc +++ b/main.cc @@ -94,7 +94,9 @@ int main( int argc, char *argv[] ) std::vector tmp = 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_HIDDEN, 1); //glfwSetWindowSizeCallback(app.getGraphics(), resizeCallback); glfwSetKeyCallback(app.getGraphics()->getWindow(), keyCallback ); From 3625cd31d7856f254bb5a1396c2bf7e68c6f0435 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Fri, 14 Nov 2014 22:55:29 +0100 Subject: [PATCH 06/13] Stop x rotation at the top and at the bottom to prevent flipping. --- camera.cc | 12 +++++++++++- level.cc | 12 +++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/camera.cc b/camera.cc index c5b3c77..08ea557 100644 --- a/camera.cc +++ b/camera.cc @@ -30,5 +30,15 @@ void Camera::setRotation(glm::vec2 rotation) { } void Camera::updateRotation(glm::vec2 rotation) { - this->rotation += 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/level.cc b/level.cc index 09740bb..94ebb52 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::vec2(0.8f, 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: @@ -51,13 +51,19 @@ void Level::render() { for(unsigned int i = 0; iterrain.render(); } void Level::update(float runTime, glm::vec2 mouseDelta) { // rotate bunny //cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f)); - camera.updateRotation(mouseDelta/100.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() { From 229ff9c4f87b0fddd630c576801cb558ad683bb9 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Fri, 14 Nov 2014 23:11:03 +0100 Subject: [PATCH 07/13] Ignoring another (-Wunused-variable) compiler warning. --- extern/bullet/src/LinearMath/btAlignedObjectArray.h | 2 ++ 1 file changed, 2 insertions(+) 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" From 3916e58efc2393508165b69c12107bc98290fc50 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Fri, 14 Nov 2014 23:11:35 +0100 Subject: [PATCH 08/13] Adding namespacing. --- main.cc | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/main.cc b/main.cc index 535cbc1..50a8626 100644 --- a/main.cc +++ b/main.cc @@ -18,11 +18,6 @@ #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); } @@ -91,7 +86,7 @@ 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); @@ -120,8 +115,8 @@ 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; From 98270e919dae0ff107b99c4f0902fd296fc00717 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 15 Nov 2014 00:03:52 +0100 Subject: [PATCH 09/13] Changed how the rotation of objects is saved to better work with bullet together. --- entity.cc | 13 ++++++++++++- entity.hh | 6 ++++-- object.cc | 4 +--- 3 files changed, 17 insertions(+), 6 deletions(-) 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/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(); From b32bf02b21745bbacf9c5e3add4f7d0ca19069f8 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 15 Nov 2014 11:02:22 +0100 Subject: [PATCH 10/13] Fixing call for hiding mouse cursor. --- main.cc | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/main.cc b/main.cc index 50a8626..02104a2 100644 --- a/main.cc +++ b/main.cc @@ -58,11 +58,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,7 +86,7 @@ int main( int argc, char *argv[] ) // Ensure we can capture the escape key being pressed below glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_STICKY_KEYS, 1); // Hide mouse cursor - glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_CURSOR_HIDDEN, 1); + glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN); //glfwSetWindowSizeCallback(app.getGraphics(), resizeCallback); glfwSetKeyCallback(app.getGraphics()->getWindow(), keyCallback ); From 47ca5c910876cf7ecae9127d2270604cc17cb340 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 15 Nov 2014 13:42:12 +0100 Subject: [PATCH 11/13] Ignoring Qtcreator files. --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) 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 From 213a936fbbaf9a5d9fed167f5af2bfd56a14d17d Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 15 Nov 2014 13:45:05 +0100 Subject: [PATCH 12/13] Changing how Attribute locations are looked up. --- main.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/main.cc b/main.cc index 02104a2..5991a78 100644 --- a/main.cc +++ b/main.cc @@ -16,8 +16,6 @@ #include #include -#include "model.hh" - Application::Application() { graphics = Graphics(glm::uvec2(1024, 786), 0.1f, 100.0f); } @@ -42,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 From 5ca77ae3cccb2a74186d7c851889efca42a4a6bf Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 15 Nov 2014 13:46:20 +0100 Subject: [PATCH 13/13] Constructed a simple quad for debugging terrain generation. --- level.cc | 7 +-- terrain.cc | 128 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 79 insertions(+), 56 deletions(-) diff --git a/level.cc b/level.cc index 94ebb52..88bf785 100644 --- a/level.cc +++ b/level.cc @@ -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.1f, 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); diff --git a/terrain.cc b/terrain.cc index a969c2f..beb98f5 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->defineAttribute("aPosition", GL_FLOAT, 3); //TODO: ArrayBuffer for the texture coordinates - ab->defineAttribute("aNormal", GL_FLOAT, 3); + // 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, abNumFloats=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 * abNumFloats]; @@ -57,36 +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 += 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; - } } + 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); + ab->setDataElements(numVertices, abData);*/ + float abData[32] = {0.0f, 0.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + + 1.0f, 0.0f, 0.0f, + 1.0f, 1.0f, + 0.0f, 1.0f, 0.0f, + + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + + 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); @@ -101,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(){