added a normal and texture coordinate to the terrain, rendering moved to level
This commit is contained in:
parent
3343369732
commit
4ef7a2597b
25
level.cc
25
level.cc
@ -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) {
|
void Level::update(float runTime) {
|
||||||
|
101
terrain.cc
101
terrain.cc
@ -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() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
10
terrain.hh
10
terrain.hh
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user