Merge branch 'terrain-test'
Conflicts: level.cc terrain.cc
This commit is contained in:
commit
c0a730f58a
11
level.cc
11
level.cc
@ -19,16 +19,16 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
|
|||||||
// load a texture:
|
// load a texture:
|
||||||
Material material = Material("stoneTexture.png", 0.1f, 0.5f, 0.5f, 3.0f);
|
Material material = Material("stoneTexture.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, 7.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);
|
||||||
|
|
||||||
|
|
||||||
//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, 1.0f, 1.0f), 2.0f);
|
Light light = Light(glm::vec3(-3.0f, 6.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 10.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(1.0f, 1.0f, 1.0f), 2.0f);
|
Light light2 = Light(glm::vec3(3.0f, 6.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 10.0f);
|
||||||
lights.push_back(light2);
|
lights.push_back(light2);
|
||||||
|
|
||||||
|
|
||||||
@ -36,11 +36,10 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
|
|||||||
this->terrain.load();
|
this->terrain.load();
|
||||||
Model terrainModel = Model(this->terrain.getModel());
|
Model terrainModel = Model(this->terrain.getModel());
|
||||||
// load a texture:
|
// load a texture:
|
||||||
Material terrainMaterial = Material("clownfishBunny.png", 1.0f, 0.0f, 0.0f, 3.0f);
|
Material terrainMaterial = Material("terrainTexture.png", 0.1f, 0.8f, 0.2f, 3.0f);
|
||||||
//Create object
|
//Create object
|
||||||
Object terrainObject = Object(terrainModel, terrainMaterial,
|
Object terrainObject = Object(terrainModel, terrainMaterial,
|
||||||
//glm::vec3(-0.5f*(float)this->terrain.getHeightmapHeight(), -0.5f, -0.5f*(float)this->terrain.getHeightmapWidth()),
|
glm::vec3(-0.5f*(float)this->terrain.getHeightmapHeight(), 0.0f, -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), 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);
|
objects.push_back(object);
|
||||||
|
6096
lodepng.cpp
Normal file
6096
lodepng.cpp
Normal file
File diff suppressed because it is too large
Load Diff
68
terrain.cc
68
terrain.cc
@ -1,4 +1,5 @@
|
|||||||
#include "terrain.hh"
|
#include "terrain.hh"
|
||||||
|
#include "lodepng.h"
|
||||||
|
|
||||||
Terrain::Terrain(std::string filePath){
|
Terrain::Terrain(std::string filePath){
|
||||||
this->filePath = filePath;
|
this->filePath = filePath;
|
||||||
@ -12,28 +13,21 @@ Terrain::~Terrain() {
|
|||||||
|
|
||||||
|
|
||||||
void Terrain::load() {
|
void Terrain::load() {
|
||||||
this->filePath = "../Levels/LevelTest/terrain"; //TODO remove this, its only for testing
|
filePath = "../Levels/LevelTest/terrain/"; //TODO remove this, its only for testing
|
||||||
|
|
||||||
std::ifstream terrain_png(this->filePath + "/heightmap2.png");
|
std::vector<unsigned char> image; //the raw pixels
|
||||||
unsigned int rowNum, columnNum, heightmapValue;
|
unsigned error = lodepng::decode(image, heightmapWidth, heightmapHeight, filePath + "heightmap.png");
|
||||||
|
if (error) {
|
||||||
terrain_png.seekg(16); //skip part of the header
|
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
|
||||||
|
}
|
||||||
char temp[4];
|
this->heightmap = new float*[this->heightmapHeight]; //initialize the heightmap
|
||||||
terrain_png.read(temp, 4); //read width
|
for(unsigned int rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap
|
||||||
this->heightmapWidth = (temp[3]<<0) | (temp[2]<<8) | (temp[1]<<16) | (temp[0]<<24); //convert from network to host byte order
|
this->heightmap[rowNum] = new float[this->heightmapWidth];
|
||||||
terrain_png.read(temp, 4); //read height
|
for(unsigned int columnNum = 0; columnNum < this->heightmapWidth; columnNum++){
|
||||||
this->heightmapHeight = (temp[3]<<0) | (temp[2]<<8) | (temp[1]<<16) | (temp[0]<<24); //convert from network to host byte order
|
this->heightmap[rowNum][columnNum] = (float)(image[(rowNum*heightmapWidth+columnNum)*4]) / 32;
|
||||||
|
|
||||||
heightmap = new float*[this->heightmapHeight]; //initialize the heightmap
|
|
||||||
for(rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap
|
|
||||||
heightmap[rowNum] = new float[this->heightmapWidth];
|
|
||||||
for(columnNum = 0; columnNum < this->heightmapWidth; columnNum++){
|
|
||||||
terrain_png.read((char *)&heightmapValue, 1);
|
|
||||||
heightmap[rowNum][columnNum] = (float)heightmapValue / 256;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this->makeTriangleMesh();
|
this->makeTriangleMesh();
|
||||||
heightmapChanged = false; //no need to make a TriangleMesh again before rendering
|
heightmapChanged = false; //no need to make a TriangleMesh again before rendering
|
||||||
|
|
||||||
@ -47,7 +41,7 @@ void Terrain::makeTriangleMesh(){
|
|||||||
ab->defineAttribute("aTexCoord", GL_FLOAT, 2);
|
ab->defineAttribute("aTexCoord", GL_FLOAT, 2);
|
||||||
ab->defineAttribute("aNormal", GL_FLOAT, 3);
|
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;
|
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 * floatsPerVertex];
|
float* abData = new float[numVertices * floatsPerVertex];
|
||||||
@ -91,23 +85,7 @@ void Terrain::makeTriangleMesh(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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<ACGL::OpenGL::VertexArrayObject>();
|
this->triangleMesh = std::make_shared<ACGL::OpenGL::VertexArrayObject>();
|
||||||
this->triangleMesh->bind();
|
this->triangleMesh->bind();
|
||||||
this->triangleMesh->setMode(GL_TRIANGLE_STRIP);
|
this->triangleMesh->setMode(GL_TRIANGLE_STRIP);
|
||||||
@ -133,19 +111,19 @@ void Terrain::set_abData(float* abData, unsigned int dataCount, unsigned int row
|
|||||||
abData[dataCount+7] = 0.0;
|
abData[dataCount+7] = 0.0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
glm::vec3 sumNormals;
|
glm::vec3 sumNormals = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
for (int i=-1; i<2; i+=1) {
|
for (int i=-1; i<2; i+=2) {
|
||||||
for (int j=-1; j<2; j+=1) {
|
for (int j=-1; j<2; j+=2) {
|
||||||
glm::vec3 vecA, vecB, normal;
|
glm::vec3 vecA, vecB, normal;
|
||||||
vecA = glm::vec3((float)i, (heightmap[rowNum+i][columnNum] - heightmap[rowNum][columnNum]), 0.0f);
|
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);
|
vecB = glm::vec3(0.0f, (heightmap[rowNum][columnNum+j] - heightmap[rowNum][columnNum]), (float)j);
|
||||||
normal = glm::normalize(glm::cross(vecA, vecB));
|
normal = glm::normalize(glm::cross(vecA, vecB));
|
||||||
if(i+j==0)
|
if(i+j!=0)
|
||||||
normal = normal*(-1.0f);
|
normal = normal*(-1.0f);
|
||||||
sumNormals += normal;
|
sumNormals += normal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sumNormals = sumNormals*0.111f;
|
sumNormals = glm::normalize(sumNormals);
|
||||||
abData[dataCount+5] = sumNormals[0];
|
abData[dataCount+5] = sumNormals[0];
|
||||||
abData[dataCount+6] = sumNormals[1];
|
abData[dataCount+6] = sumNormals[1];
|
||||||
abData[dataCount+7] = sumNormals[2];
|
abData[dataCount+7] = sumNormals[2];
|
||||||
@ -161,11 +139,9 @@ float** Terrain::getHeightmap(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Terrain::getHeightmapHeight(){
|
unsigned int Terrain::getHeightmapHeight(){
|
||||||
//return this->heightmapHeight;
|
return this->heightmapHeight;
|
||||||
return 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Terrain::getHeightmapWidth(){
|
unsigned int Terrain::getHeightmapWidth(){
|
||||||
//return this->heightmapWidth;
|
return this->heightmapWidth;
|
||||||
return 2;
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "material.hh"
|
#include "material.hh"
|
||||||
#include <fstream>
|
|
||||||
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
|
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
|
||||||
#include "model.hh"
|
#include "model.hh"
|
||||||
class Terrain {
|
class Terrain {
|
||||||
|
Loading…
Reference in New Issue
Block a user