debuged the load function in terrain.cc loading and rendering kind of works (no texture)
This commit is contained in:
parent
c38629823d
commit
8eec5fa49e
Binary file not shown.
Before Width: | Height: | Size: 223 KiB After Width: | Height: | Size: 35 KiB |
4
level.cc
4
level.cc
@ -12,7 +12,7 @@ Level::~Level() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
|
void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
|
||||||
//this->terrain.load();
|
this->terrain.load();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 Level::getAmbientLight() {
|
glm::vec3 Level::getAmbientLight() {
|
||||||
|
63
terrain.cc
63
terrain.cc
@ -12,29 +12,28 @@ Terrain::~Terrain() {
|
|||||||
|
|
||||||
|
|
||||||
void Terrain::load() {
|
void Terrain::load() {
|
||||||
|
this->filePath = "../Levels/LevelTest/terrain"; //TODO remove this, its only for testing
|
||||||
|
|
||||||
std::ifstream terrain_png(this->filePath + "/heightmap.png"); //TODO: filepath organization
|
std::ifstream terrain_png(this->filePath + "/heightmap.png"); //TODO: filepath organization
|
||||||
unsigned int rowNum, columnNum, heightmapValue;
|
unsigned int rowNum, columnNum, heightmapValue;
|
||||||
|
|
||||||
terrain_png.seekg(16); //skip part of the header
|
terrain_png.seekg(16); //skip part of the header
|
||||||
|
|
||||||
terrain_png.read((char *)&this->heightmapWidth, 4); //read width
|
char temp[4];
|
||||||
terrain_png.read((char *)&this->heightmapHeight, 4); //read height
|
|
||||||
this->heightmapWidth = ntohl(this->heightmapWidth); //convert from network to host byte order
|
|
||||||
this->heightmapHeight = ntohl(this->heightmapHeight);
|
|
||||||
/* //alternate implementation that does NOT work at all
|
|
||||||
char temp[2];4???
|
|
||||||
terrain_png.read(temp, 4); //read width
|
terrain_png.read(temp, 4); //read width
|
||||||
this->heightmapWidth = (temp[1]<<0) | (temp[0]<<8); //convert from network to host byte order
|
this->heightmapWidth = (temp[3]<<0) | (temp[2]<<8) | (temp[1]<<16) | (temp[0]<<24); //convert from network to host byte order
|
||||||
terrain_png.read(temp, 4); //read height
|
terrain_png.read(temp, 4); //read height
|
||||||
this->heightmapHeight = (temp[1]<<0) | (temp[0]<<8); //convert from network to host byte order
|
this->heightmapHeight = (temp[3]<<0) | (temp[2]<<8) | (temp[1]<<16) | (temp[0]<<24); //convert from network to host byte order
|
||||||
*/
|
|
||||||
|
printf("Width: %d ", (int)this->heightmapWidth);
|
||||||
|
printf("Height: %d ", (int)this->heightmapHeight);
|
||||||
|
|
||||||
heightmap = new float*[this->heightmapHeight]; //initialize the heightmap
|
heightmap = new float*[this->heightmapHeight]; //initialize the heightmap
|
||||||
for(rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap
|
for(rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap
|
||||||
heightmap[rowNum] = new float[this->heightmapWidth];
|
heightmap[rowNum] = new float[this->heightmapWidth];
|
||||||
for(columnNum = 0; columnNum < this->heightmapWidth; columnNum++){
|
for(columnNum = 0; columnNum < this->heightmapWidth; columnNum++){
|
||||||
terrain_png.read((char *)&heightmapValue, 1);
|
terrain_png.read((char *)&heightmapValue, 1);
|
||||||
heightmap[rowNum][columnNum] = (float)heightmapValue / 5;
|
heightmap[rowNum][columnNum] = (float)heightmapValue / 256;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,24 +44,33 @@ void Terrain::load() {
|
|||||||
|
|
||||||
void Terrain::makeTriangleMesh(){
|
void Terrain::makeTriangleMesh(){
|
||||||
|
|
||||||
ACGL::OpenGL::SharedArrayBuffer arrayBuffer = ACGL::OpenGL::SharedArrayBuffer();
|
ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared<ACGL::OpenGL::ArrayBuffer>();
|
||||||
arrayBuffer->defineAttribute("pos", GL_FLOAT, 3); //TODO: ArrayBuffer for the texture coordinates
|
ab->defineAttribute("pos", GL_FLOAT, 3); //TODO: ArrayBuffer for the texture coordinates
|
||||||
|
|
||||||
unsigned int rowNum=0, columnNum=0; //initializing:
|
unsigned int rowNum=0, columnNum=0, dataCount=0; //initializing:
|
||||||
bool movingRight = true, isUp = true;
|
bool movingRight = true, isUp = true;
|
||||||
|
int numVertices = (this->heightmapHeight - 1) * (this->heightmapWidth * 2 + 1) + 1;
|
||||||
|
printf("NumberofVertices: %d ", numVertices);
|
||||||
|
float* abData = new float[numVertices * 3];
|
||||||
|
|
||||||
while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip!
|
while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip!
|
||||||
float newPos[3];
|
abData[dataCount] = (float)rowNum;
|
||||||
newPos[0] = (float)rowNum;
|
abData[dataCount+1] = heightmap[rowNum][columnNum];
|
||||||
newPos[1] = (float)columnNum;
|
abData[dataCount+2] = (float)columnNum;
|
||||||
newPos[2] = heightmap[rowNum][columnNum];
|
dataCount += 3;
|
||||||
arrayBuffer->setDataElements(1, &newPos);
|
|
||||||
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){
|
||||||
arrayBuffer->setDataElements(1, &newPos);
|
abData[dataCount] = (float)rowNum;
|
||||||
arrayBuffer->setDataElements(1, &newPos);
|
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;
|
||||||
movingRight = false;
|
movingRight = false;
|
||||||
rowNum = rowNum + 1;
|
rowNum = rowNum + 1;
|
||||||
} else{
|
} else{
|
||||||
@ -72,8 +80,14 @@ void Terrain::makeTriangleMesh(){
|
|||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
if (columnNum == 0){
|
if (columnNum == 0){
|
||||||
arrayBuffer->setDataElements(1, &newPos);
|
abData[dataCount] = (float)rowNum;
|
||||||
arrayBuffer->setDataElements(1, &newPos);
|
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;
|
||||||
movingRight = true;
|
movingRight = true;
|
||||||
rowNum = rowNum + 1;
|
rowNum = rowNum + 1;
|
||||||
}else{
|
}else{
|
||||||
@ -84,10 +98,11 @@ void Terrain::makeTriangleMesh(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this->triangleMesh = ACGL::OpenGL::SharedVertexArrayObject();
|
ab->setDataElements(numVertices, abData);
|
||||||
|
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);
|
||||||
this->triangleMesh->attachAllAttributes(arrayBuffer);
|
this->triangleMesh->attachAllAttributes(ab);
|
||||||
//TODO unbind?
|
//TODO unbind?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "material.hh"
|
#include "material.hh"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
|
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
|
||||||
|
|
||||||
class Terrain {
|
class Terrain {
|
||||||
|
Loading…
Reference in New Issue
Block a user