diff --git a/enc_temp_folder/ce10c2d1a66b9d94abaf178641d85/Terrain.cpp b/enc_temp_folder/ce10c2d1a66b9d94abaf178641d85/Terrain.cpp new file mode 100644 index 0000000..6092539 --- /dev/null +++ b/enc_temp_folder/ce10c2d1a66b9d94abaf178641d85/Terrain.cpp @@ -0,0 +1,98 @@ +#include "Terrain.h" + + +const float Terrain::SIZE = 100.f; +const int Terrain::VERTEX_COUNT = 2; +const int Terrain::COUNT = Terrain::VERTEX_COUNT * Terrain::VERTEX_COUNT; + + +Terrain::Terrain(int gridX, int gridZ, HeightGenerator heightGenerator) { + x = gridX * SIZE; + z = gridZ * SIZE; + this->heightGenerator = heightGenerator; +} + +float roundtwo(float var) +{ + // we use array of chars to store number + // as a string. + char str[400]; + + // Print in string the value of var + // with two decimal point + sprintf(str, "%.2f", var); + + // scan string value in var + sscanf(str, "%f", &var); + + return var; +} + +obj::Model Terrain::generateTerrain() { + float vertices[COUNT * 3]; + float normals[COUNT * 3]; + float textureCoords[COUNT * 2]; + int indices[6 * (VERTEX_COUNT - 1) * (VERTEX_COUNT - 1)]; + int vertexPointer = 0; + for (int i = 0; i < VERTEX_COUNT; i++) { + for (int j = 0; j < VERTEX_COUNT; j++) { + vertices[vertexPointer * 3] = float(j) / float(VERTEX_COUNT - 1) * SIZE; + float x = roundtwo(getHeight(j, i)); + float y = 6.55555f; + float z = roundtwo(6.55555f); + vertices[vertexPointer * 3 + 1] = round(getHeight(j,i)); //0.f + vertices[vertexPointer * 3 + 2] = float(i) / float(VERTEX_COUNT - 1) * SIZE; + glm::vec3 normal = calculateNormal(j, i); + normals[vertexPointer * 3] = normal.x; + normals[vertexPointer * 3 + 1] = normal.y; + normals[vertexPointer * 3 + 2] = normal.z; + textureCoords[vertexPointer * 2] = float(j) / float(VERTEX_COUNT - 1) * 20; + textureCoords[vertexPointer * 2 + 1] = float(i) / float(VERTEX_COUNT - 1) * 20; + vertexPointer++; + } + } + int pointer = 0; + for (int gz = 0; gz < VERTEX_COUNT - 1; gz++) { + for (int gx = 0; gx < VERTEX_COUNT - 1; gx++) { + int topLeft = gz * VERTEX_COUNT + gx; + int topRight = topLeft + 1; + int bottomLeft = (gz + 1) * VERTEX_COUNT + gx; + int bottomRight = bottomLeft + 1; + indices[pointer++] = topLeft; + indices[pointer++] = bottomLeft; + indices[pointer++] = topRight; + indices[pointer++] = topRight; + indices[pointer++] = bottomLeft; + indices[pointer++] = bottomRight; + } + } + std::vector vVertices(std::begin(vertices), std::end(vertices)); + std::vector vTextures(std::begin(textureCoords), std::end(textureCoords)); + std::vector vNormals(std::begin(normals), std::end(normals)); + std::map> faces; + faces[std::string("default")] = std::vector(std::begin(indices), std::end(indices)); + + obj::Model model = { vVertices, vTextures, vNormals, faces }; + return model; +} + +glm::vec3 Terrain::calculateNormal(int x, int z) { + float heightL = getHeight(x - 1, z); + float heightR = getHeight(x + 1, z); + float heightD = getHeight(x, z - 1); + float heightU = getHeight(x, z + 1); + glm::vec3 normal = glm::vec3(heightL - heightR, 2.f, heightD - heightU); + return glm::normalize(normal); +} + +float Terrain::getHeight(int x, int z) { + return heightGenerator.generateHeight(x, z); +} + +int Terrain::getX() { + return x; +} + +int Terrain::getZ() { + return z; +} \ No newline at end of file diff --git a/grafika_projekt/Debug/Render_Utils.obj b/grafika_projekt/Debug/Render_Utils.obj index b785889..9f3d556 100644 Binary files a/grafika_projekt/Debug/Render_Utils.obj and b/grafika_projekt/Debug/Render_Utils.obj differ diff --git a/grafika_projekt/grafika_projekt.vcxproj b/grafika_projekt/grafika_projekt.vcxproj index 68ed12b..a23f237 100644 --- a/grafika_projekt/grafika_projekt.vcxproj +++ b/grafika_projekt/grafika_projekt.vcxproj @@ -29,7 +29,7 @@ Application true - v142 + v143 Unicode diff --git a/grafika_projekt/src/HeightGenerator.cpp b/grafika_projekt/src/HeightGenerator.cpp index 48b1d73..b91ef23 100644 --- a/grafika_projekt/src/HeightGenerator.cpp +++ b/grafika_projekt/src/HeightGenerator.cpp @@ -22,7 +22,7 @@ float HeightGenerator::generateHeight(int x, int z) { float amp = pow(ROUGHNESS, i) * AMPLITUDE; total += getInterpolatedNoise((x + xOffset) * freq, (z + zOffset) * freq) * amp; } - return total; + return round(total * 1000.0)/1000; } float HeightGenerator::getInterpolatedNoise(float x, float z) { diff --git a/grafika_projekt/src/Render_Utils.cpp b/grafika_projekt/src/Render_Utils.cpp index b3d06fb..39869d3 100644 --- a/grafika_projekt/src/Render_Utils.cpp +++ b/grafika_projekt/src/Render_Utils.cpp @@ -19,7 +19,7 @@ void Core::RenderContext::initFromOBJ(obj::Model& model) unsigned int vertexTexBufferSize = sizeof(float) * model.texCoord.size(); size = model.faces["default"].size(); - unsigned int vertexElementBufferSize = sizeof(unsigned short) * size; + unsigned int vertexElementBufferSize = sizeof(unsigned int) * size; glGenVertexArrays(1, &vertexArray); diff --git a/grafika_projekt/src/Terrain.cpp b/grafika_projekt/src/Terrain.cpp index bed873d..6092539 100644 --- a/grafika_projekt/src/Terrain.cpp +++ b/grafika_projekt/src/Terrain.cpp @@ -12,6 +12,22 @@ Terrain::Terrain(int gridX, int gridZ, HeightGenerator heightGenerator) { this->heightGenerator = heightGenerator; } +float roundtwo(float var) +{ + // we use array of chars to store number + // as a string. + char str[400]; + + // Print in string the value of var + // with two decimal point + sprintf(str, "%.2f", var); + + // scan string value in var + sscanf(str, "%f", &var); + + return var; +} + obj::Model Terrain::generateTerrain() { float vertices[COUNT * 3]; float normals[COUNT * 3]; @@ -21,7 +37,10 @@ obj::Model Terrain::generateTerrain() { for (int i = 0; i < VERTEX_COUNT; i++) { for (int j = 0; j < VERTEX_COUNT; j++) { vertices[vertexPointer * 3] = float(j) / float(VERTEX_COUNT - 1) * SIZE; - vertices[vertexPointer * 3 + 1] = getHeight(j,i); + float x = roundtwo(getHeight(j, i)); + float y = 6.55555f; + float z = roundtwo(6.55555f); + vertices[vertexPointer * 3 + 1] = round(getHeight(j,i)); //0.f vertices[vertexPointer * 3 + 2] = float(i) / float(VERTEX_COUNT - 1) * SIZE; glm::vec3 normal = calculateNormal(j, i); normals[vertexPointer * 3] = normal.x; @@ -50,8 +69,8 @@ obj::Model Terrain::generateTerrain() { std::vector vVertices(std::begin(vertices), std::end(vertices)); std::vector vTextures(std::begin(textureCoords), std::end(textureCoords)); std::vector vNormals(std::begin(normals), std::end(normals)); - std::map> faces; - faces[std::string("default")] = std::vector(std::begin(indices), std::end(indices)); + std::map> faces; + faces[std::string("default")] = std::vector(std::begin(indices), std::end(indices)); obj::Model model = { vVertices, vTextures, vNormals, faces }; return model; diff --git a/grafika_projekt/src/main.cpp b/grafika_projekt/src/main.cpp index 098f928..4279b4d 100644 --- a/grafika_projekt/src/main.cpp +++ b/grafika_projekt/src/main.cpp @@ -366,8 +366,8 @@ void renderScene() glBindVertexArray(skyboxVAO); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture); - glDrawArrays(GL_TRIANGLES, 0, 36); + //glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture); + //glDrawArrays(GL_TRIANGLES, 0, 36); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -385,22 +385,23 @@ void renderScene() glm::vec3 change0 = glm::vec3(0, 0, 0); - for (int j = 0; j < 100; j++) { - drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.04f), 0.2f), textureBubble, bubbleProgram); - } + //for (int j = 0; j < 100; j++) { + // drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.04f), 0.2f), textureBubble, bubbleProgram); + //} - for (int i = 0; i < 5; i++) { - if (time > -10) { - drawObjectTexture(fishContext, animationMatrix(time + 15, change1, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture); - drawObjectTexture(fishContext, animationMatrix(time + 15, change2, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture); - drawObjectTexture(fishContext, animationMatrix(time + 15, change3, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture); - drawObjectTexture(fishContext, animationMatrix(time + 15, change4, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture); - - time -= 6; - } - } - drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture); - drawObjectTexture(terrainContext, glm::translate(glm::vec3(1, 1, 1)), textureTerrain, programTexture); + //for (int i = 0; i < 5; i++) { + // if (time > -10) { + // drawObjectTexture(fishContext, animationMatrix(time + 15, change1, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture); + // drawObjectTexture(fishContext, animationMatrix(time + 15, change2, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture); + // drawObjectTexture(fishContext, animationMatrix(time + 15, change3, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture); + // drawObjectTexture(fishContext, animationMatrix(time + 15, change4, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture); + // + // time -= 6; + // } + //} + //drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture); + drawObjectTexture(terrainContext, glm::translate(glm::vec3(1, -10, 1)), textureTerrain, programTexture); + drawObjectTexture(terrainContext, glm::translate(glm::vec3(1, 10, 1)), textureTerrain, programTexture); glutSwapBuffers(); } diff --git a/grafika_projekt/src/objload.h b/grafika_projekt/src/objload.h index 004ab4e..5bc514e 100644 --- a/grafika_projekt/src/objload.h +++ b/grafika_projekt/src/objload.h @@ -40,7 +40,7 @@ struct Model { std::vector texCoord; //< 2 * N entries std::vector normal; //< 3 * N entries - std::map > faces; //< assume triangels and uniform indexing + std::map > faces; //< assume triangels and uniform indexing }; struct ObjModel { @@ -221,10 +221,10 @@ Model convertToModel( const ObjModel & obj ) { for(std::map::const_iterator g = obj.faces.begin(); g != obj.faces.end(); ++g){ const std::string & name = g->first; const ObjModel::FaceList & fl = g->second; - std::vector & v = model.faces[g->first]; + std::vector & v = model.faces[g->first]; v.reserve(fl.first.size()); for(std::vector::const_iterator f = fl.first.begin(); f != fl.first.end(); ++f){ - const unsigned short index = std::distance(unique.begin(), std::lower_bound(unique.begin(), unique.end(), *f)); + const unsigned int index = std::distance(unique.begin(), std::lower_bound(unique.begin(), unique.end(), *f)); v.push_back(index); } } @@ -276,7 +276,7 @@ std::ostream & operator<<( std::ostream & out, const Model & m ){ } if(!m.faces.empty()){ out << "faces\t"; - for(std::map >::const_iterator g = m.faces.begin(); g != m.faces.end(); ++g){ + for(std::map >::const_iterator g = m.faces.begin(); g != m.faces.end(); ++g){ out << g->first << " "; } out << "\n";