This commit is contained in:
Matraf 2022-01-24 15:46:52 +01:00
parent 4f03876027
commit ebb270c746
8 changed files with 145 additions and 27 deletions

View File

@ -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<float> vVertices(std::begin(vertices), std::end(vertices));
std::vector<float> vTextures(std::begin(textureCoords), std::end(textureCoords));
std::vector<float> vNormals(std::begin(normals), std::end(normals));
std::map<std::string, std::vector<unsigned int>> faces;
faces[std::string("default")] = std::vector<unsigned int>(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;
}

View File

@ -29,7 +29,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">

View File

@ -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) {

View File

@ -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);

View File

@ -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<float> vVertices(std::begin(vertices), std::end(vertices));
std::vector<float> vTextures(std::begin(textureCoords), std::end(textureCoords));
std::vector<float> vNormals(std::begin(normals), std::end(normals));
std::map<std::string, std::vector<unsigned short>> faces;
faces[std::string("default")] = std::vector<unsigned short>(std::begin(indices), std::end(indices));
std::map<std::string, std::vector<unsigned int>> faces;
faces[std::string("default")] = std::vector<unsigned int>(std::begin(indices), std::end(indices));
obj::Model model = { vVertices, vTextures, vNormals, faces };
return model;

View File

@ -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();
}

View File

@ -40,7 +40,7 @@ struct Model {
std::vector<float> texCoord; //< 2 * N entries
std::vector<float> normal; //< 3 * N entries
std::map<std::string, std::vector<unsigned short> > faces; //< assume triangels and uniform indexing
std::map<std::string, std::vector<unsigned int> > faces; //< assume triangels and uniform indexing
};
struct ObjModel {
@ -221,10 +221,10 @@ Model convertToModel( const ObjModel & obj ) {
for(std::map<std::string, ObjModel::FaceList>::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<unsigned short> & v = model.faces[g->first];
std::vector<unsigned int> & v = model.faces[g->first];
v.reserve(fl.first.size());
for(std::vector<ObjModel::FaceVertex>::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<std::string, std::vector<unsigned short> >::const_iterator g = m.faces.begin(); g != m.faces.end(); ++g){
for(std::map<std::string, std::vector<unsigned int> >::const_iterator g = m.faces.begin(); g != m.faces.end(); ++g){
out << g->first << " ";
}
out << "\n";