From 4ef7a2597b433345e2d1b51846d219cdb2f172f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Fri, 14 Nov 2014 17:36:30 +0100 Subject: [PATCH] added a normal and texture coordinate to the terrain, rendering moved to level --- level.cc | 25 +++++++++---- terrain.cc | 103 ++++++++++++++++++++++++++++++++--------------------- terrain.hh | 10 ++++-- 3 files changed, 89 insertions(+), 49 deletions(-) diff --git a/level.cc b/level.cc index c726e08..a80fdb8 100644 --- a/level.cc +++ b/level.cc @@ -19,26 +19,39 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) { // load a texture: Material material = Material("clownfishBunny.png", 0.1f, 0.5f, 0.5f, 3.0f); //Create object - Object object = Object(model, material, glm::vec3(0.0f, -1.0f, -2.0f), + Object object = Object(model, material, glm::vec3(0.0f, 0.0f, -2.0f), glm::vec3(0.0f, 1.0472f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), shader); - objects.push_back(object); - cameraCenter = &objects[0]; + + //set lighting parameters ambientLight = glm::vec3(1.0f, 1.0f, 1.0f); - Light light = Light(glm::vec3(-3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f), 2.0f); + Light light = Light(glm::vec3(-3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 2.0f); lights.push_back(light); - Light light2 = Light(glm::vec3(3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), 2.0f); + Light light2 = Light(glm::vec3(3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 2.0f); lights.push_back(light2); + + // load terrain this->terrain.load(); + Model terrainModel = this->terrain.getModel(); + // load a texture: + Material terrainMaterial = Material("clownfishBunny.png", 0.1f, 0.7f, 0.3f, 1.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.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); + objects.push_back(terrainObject); + cameraCenter = &objects[0]; } void Level::render() { for(unsigned int i = 0; iterrain.render(); +// this->terrain.render(); } void Level::update(float runTime) { diff --git a/terrain.cc b/terrain.cc index 6db6db5..a969c2f 100644 --- a/terrain.cc +++ b/terrain.cc @@ -42,31 +42,27 @@ void Terrain::load() { void Terrain::makeTriangleMesh(){ ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared(); - ab->defineAttribute("pos", GL_FLOAT, 3); //TODO: ArrayBuffer for the texture coordinates + ab->defineAttribute("aPosition", GL_FLOAT, 3); //TODO: ArrayBuffer for the texture coordinates + ab->defineAttribute("aNormal", GL_FLOAT, 3); + ab->defineAttribute("aTexCoord", GL_FLOAT, 2); - unsigned int rowNum=0, columnNum=0, dataCount=0; //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 * 3]; + float* abData = new float[numVertices * abNumFloats]; - while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip! - abData[dataCount] = (float)rowNum; - abData[dataCount+1] = heightmap[rowNum][columnNum]; - abData[dataCount+2] = (float)columnNum; - dataCount += 3; + while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip! + set_abData(abData, dataCount, rowNum, columnNum); + dataCount += abNumFloats; if (isUp){ rowNum = rowNum + 1; isUp = false; }else if (movingRight){ if (columnNum == this->heightmapWidth - 1){ - abData[dataCount] = (float)rowNum; - abData[dataCount+1] = heightmap[rowNum][columnNum]; - abData[dataCount+2] = (float)columnNum; - dataCount += 3; - abData[dataCount] = (float)rowNum; - abData[dataCount+1] = heightmap[rowNum][columnNum]; - abData[dataCount+2] = (float)columnNum; - dataCount += 3; + set_abData(abData, dataCount, rowNum, columnNum); + dataCount += abNumFloats; + set_abData(abData, dataCount, rowNum, columnNum); + dataCount += abNumFloats; movingRight = false; rowNum = rowNum + 1; } else{ @@ -76,14 +72,10 @@ void Terrain::makeTriangleMesh(){ } }else{ if (columnNum == 0){ - abData[dataCount] = (float)rowNum; - abData[dataCount+1] = heightmap[rowNum][columnNum]; - abData[dataCount+2] = (float)columnNum; - dataCount += 3; - abData[dataCount] = (float)rowNum; - abData[dataCount+1] = heightmap[rowNum][columnNum]; - abData[dataCount+2] = (float)columnNum; - dataCount += 3; + set_abData(abData, dataCount, rowNum, columnNum); + dataCount += abNumFloats; + set_abData(abData, dataCount, rowNum, columnNum); + dataCount += abNumFloats; movingRight = true; rowNum = rowNum + 1; }else{ @@ -99,28 +91,59 @@ void Terrain::makeTriangleMesh(){ this->triangleMesh->bind(); this->triangleMesh->setMode(GL_TRIANGLE_STRIP); this->triangleMesh->attachAllAttributes(ab); - //TODO unbind? +} - //TODO remove this TestCode (that doesnt even work yet...): -/* ACGL::OpenGL::SharedArrayBuffer tex = std::make_shared(); - tex->defineAttribute("color", GL_FLOAT, 3); - float* texData = new float[numVertices*3]; - for (int i=0; iheightmapHeight-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]; } - tex->setDataElements(numVertices, texData); - this->triangleMesh->attachAllAttributes(tex); -*/ - + //set Texture Coordinate + abData[dataCount+6] = (float)(rowNum % 2); + abData[dataCount+7] = (float)(columnNum % 2); } -void Terrain::render() { - if (heightmapChanged) - this->makeTriangleMesh(); - this->triangleMesh->render(); +Model Terrain::getModel(){ + return Model(this->triangleMesh); +} + +float** Terrain::getHeightmap(){ + return this->heightmap; +} + +unsigned int Terrain::getHeightmapHeight(){ + return this->heightmapHeight; +} + +unsigned int Terrain::getHeightmapWidth(){ + return this->heightmapWidth; } @@ -133,5 +156,3 @@ void Terrain::render() { - - diff --git a/terrain.hh b/terrain.hh index 8418e8b..9015f2f 100644 --- a/terrain.hh +++ b/terrain.hh @@ -5,7 +5,7 @@ #include "material.hh" #include #include - +#include "model.hh" class Terrain { public: Terrain(std::string filePath); @@ -13,15 +13,21 @@ class Terrain { ~Terrain(); void load(); void render(); + Model getModel(); + float** getHeightmap(); + unsigned int getHeightmapHeight(); + unsigned int getHeightmapWidth(); + private: Material material; std::string filePath; - unsigned int heightmapWidth, heightmapHeight; + unsigned int heightmapHeight, heightmapWidth; float** heightmap; //can be accessed like 'float[][]' bool heightmapChanged; ACGL::OpenGL::SharedVertexArrayObject triangleMesh; void makeTriangleMesh(); + void set_abData(float* abData, unsigned int dataCount, unsigned int rowNum, unsigned int columnNum); }; #endif