Merge branch 'master' of github.com:Faerbit/swp

This commit is contained in:
Fabian Klemp 2014-11-14 18:33:52 +01:00
commit 176f7e8176
6 changed files with 94 additions and 56 deletions

BIN
Geometry/terrainTexture.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 KiB

View File

@ -19,26 +19,39 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
// load a texture: // load a texture:
Material material = Material("clownfishBunny.png", 0.1f, 0.5f, 0.5f, 3.0f); Material material = Material("clownfishBunny.png", 0.1f, 0.5f, 0.5f, 3.0f);
//Create object //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, 1.0472f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f), shader); glm::vec3(0.0f, 0.0f, 0.0f), shader);
objects.push_back(object);
cameraCenter = &objects[0];
//set lighting parameters //set lighting parameters
ambientLight = glm::vec3(1.0f, 1.0f, 1.0f); 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); 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); lights.push_back(light2);
// load terrain // load terrain
this->terrain.load(); 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() { void Level::render() {
for(unsigned int i = 0; i<objects.size(); i++) { for(unsigned int i = 0; i<objects.size(); i++) {
objects[i].render(); objects[i].render();
} }
this->terrain.render(); // this->terrain.render();
} }
void Level::update(float runTime, glm::vec2 mouseDelta) { void Level::update(float runTime, glm::vec2 mouseDelta) {

View File

@ -2,14 +2,12 @@
Model::Model(std::string filePath, float scale) { Model::Model(std::string filePath, float scale) {
reference = ACGL::OpenGL::VertexArrayObjectCreator(filePath).create(); reference = ACGL::OpenGL::VertexArrayObjectCreator(filePath).create();
reference->bind();
this->scale = scale; this->scale = scale;
} }
Model::Model(std::string filePath) { Model::Model(ACGL::OpenGL::SharedVertexArrayObject vao, float scale){
reference = ACGL::OpenGL::VertexArrayObjectCreator(filePath).create(); reference = vao;
reference->bind(); this->scale = scale;
this->scale = 1.0f;
} }
Model::Model(){ Model::Model(){

View File

@ -6,8 +6,8 @@
class Model { class Model {
public: public:
Model(std::string filePath, float scale); Model(std::string filePath, float scale=1.0f);
Model(std::string filePath); Model(ACGL::OpenGL::SharedVertexArrayObject vao, float scale=1.0f);
Model(); Model();
~Model(); ~Model();
ACGL::OpenGL::SharedVertexArrayObject getReference(); ACGL::OpenGL::SharedVertexArrayObject getReference();

View File

@ -42,31 +42,27 @@ void Terrain::load() {
void Terrain::makeTriangleMesh(){ void Terrain::makeTriangleMesh(){
ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared<ACGL::OpenGL::ArrayBuffer>(); ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared<ACGL::OpenGL::ArrayBuffer>();
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; bool movingRight = true, isUp = true;
int numVertices = (this->heightmapHeight - 1) * (this->heightmapWidth * 2 + 1) + 1; 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! while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip!
abData[dataCount] = (float)rowNum; set_abData(abData, dataCount, rowNum, columnNum);
abData[dataCount+1] = heightmap[rowNum][columnNum]; dataCount += abNumFloats;
abData[dataCount+2] = (float)columnNum;
dataCount += 3;
if (isUp){ if (isUp){
rowNum = rowNum + 1; rowNum = rowNum + 1;
isUp = false; isUp = false;
}else if (movingRight){ }else if (movingRight){
if (columnNum == this->heightmapWidth - 1){ if (columnNum == this->heightmapWidth - 1){
abData[dataCount] = (float)rowNum; set_abData(abData, dataCount, rowNum, columnNum);
abData[dataCount+1] = heightmap[rowNum][columnNum]; dataCount += abNumFloats;
abData[dataCount+2] = (float)columnNum; set_abData(abData, dataCount, rowNum, columnNum);
dataCount += 3; dataCount += abNumFloats;
abData[dataCount] = (float)rowNum;
abData[dataCount+1] = heightmap[rowNum][columnNum];
abData[dataCount+2] = (float)columnNum;
dataCount += 3;
movingRight = false; movingRight = false;
rowNum = rowNum + 1; rowNum = rowNum + 1;
} else{ } else{
@ -76,14 +72,10 @@ void Terrain::makeTriangleMesh(){
} }
}else{ }else{
if (columnNum == 0){ if (columnNum == 0){
abData[dataCount] = (float)rowNum; set_abData(abData, dataCount, rowNum, columnNum);
abData[dataCount+1] = heightmap[rowNum][columnNum]; dataCount += abNumFloats;
abData[dataCount+2] = (float)columnNum; set_abData(abData, dataCount, rowNum, columnNum);
dataCount += 3; dataCount += abNumFloats;
abData[dataCount] = (float)rowNum;
abData[dataCount+1] = heightmap[rowNum][columnNum];
abData[dataCount+2] = (float)columnNum;
dataCount += 3;
movingRight = true; movingRight = true;
rowNum = rowNum + 1; rowNum = rowNum + 1;
}else{ }else{
@ -99,28 +91,59 @@ void Terrain::makeTriangleMesh(){
this->triangleMesh->bind(); this->triangleMesh->bind();
this->triangleMesh->setMode(GL_TRIANGLE_STRIP); this->triangleMesh->setMode(GL_TRIANGLE_STRIP);
this->triangleMesh->attachAllAttributes(ab); this->triangleMesh->attachAllAttributes(ab);
//TODO unbind? }
//TODO remove this TestCode (that doesnt even work yet...): void Terrain::set_abData(float* abData, unsigned int dataCount, unsigned int rowNum, unsigned int columnNum){
/* ACGL::OpenGL::SharedArrayBuffer tex = std::make_shared<ACGL::OpenGL::ArrayBuffer>(); //set Position
tex->defineAttribute("color", GL_FLOAT, 3); abData[dataCount] = (float)rowNum;
float* texData = new float[numVertices*3]; abData[dataCount+1] = heightmap[rowNum][columnNum];
for (int i=0; i<numVertices*3; i++){ abData[dataCount+2] = (float)columnNum;
texData[i] = 1.0;
//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];
} }
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() { Model Terrain::getModel(){
if (heightmapChanged) return Model(this->triangleMesh);
this->makeTriangleMesh(); }
this->triangleMesh->render();
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() {

View File

@ -5,7 +5,7 @@
#include "material.hh" #include "material.hh"
#include <fstream> #include <fstream>
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh> #include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
#include "model.hh"
class Terrain { class Terrain {
public: public:
Terrain(std::string filePath); Terrain(std::string filePath);
@ -13,15 +13,21 @@ class Terrain {
~Terrain(); ~Terrain();
void load(); void load();
void render(); void render();
Model getModel();
float** getHeightmap();
unsigned int getHeightmapHeight();
unsigned int getHeightmapWidth();
private: private:
Material material; Material material;
std::string filePath; std::string filePath;
unsigned int heightmapWidth, heightmapHeight; unsigned int heightmapHeight, heightmapWidth;
float** heightmap; //can be accessed like 'float[][]' float** heightmap; //can be accessed like 'float[][]'
bool heightmapChanged; bool heightmapChanged;
ACGL::OpenGL::SharedVertexArrayObject triangleMesh; ACGL::OpenGL::SharedVertexArrayObject triangleMesh;
void makeTriangleMesh(); void makeTriangleMesh();
void set_abData(float* abData, unsigned int dataCount, unsigned int rowNum, unsigned int columnNum);
}; };
#endif #endif