added a normal and texture coordinate to the terrain, rendering moved to level

This commit is contained in:
Steffen Fündgens 2014-11-14 17:36:30 +01:00
parent 3343369732
commit 4ef7a2597b
3 changed files with 89 additions and 49 deletions

View File

@ -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; i<objects.size(); i++) {
objects[i].render();
}
this->terrain.render();
// this->terrain.render();
}
void Level::update(float runTime) {

View File

@ -42,31 +42,27 @@ void Terrain::load() {
void Terrain::makeTriangleMesh(){
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;
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<ACGL::OpenGL::ArrayBuffer>();
tex->defineAttribute("color", GL_FLOAT, 3);
float* texData = new float[numVertices*3];
for (int i=0; i<numVertices*3; i++){
texData[i] = 1.0;
void Terrain::set_abData(float* abData, unsigned int dataCount, unsigned int rowNum, unsigned int columnNum){
//set Position
abData[dataCount] = (float)rowNum;
abData[dataCount+1] = heightmap[rowNum][columnNum];
abData[dataCount+2] = (float)columnNum;
//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() {
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() {

View File

@ -5,7 +5,7 @@
#include "material.hh"
#include <fstream>
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
#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