refactor: code refactor

This commit is contained in:
matixezor 2022-01-24 20:02:48 +01:00
parent ebb270c746
commit f2f5a18cc1
11 changed files with 82 additions and 203 deletions

View File

@ -1,98 +0,0 @@
#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>v143</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">

View File

@ -3,26 +3,21 @@
#include <iostream>
#include <math.h>
# define MY_PI 3.14159265358979323846
# define MY_PI 3.1415927
std::random_device rd; // obtain a random number from hardware
const int HeightGenerator::SEED = rd();
const float HeightGenerator::AMPLITUDE = 75.f;
const int HeightGenerator::OCTAVES = 3;
const float HeightGenerator::ROUGHNESS = 0.3f;
const int HeightGenerator::xOffset = 0;
const int HeightGenerator::zOffset = 0;
float HeightGenerator::generateHeight(int x, int z) {
float total = 0;
float d = pow(2, OCTAVES - 1);
for (int i = 0; i < OCTAVES; i++) {
float freq = pow(2, i) / d;
float amp = pow(ROUGHNESS, i) * AMPLITUDE;
total += getInterpolatedNoise((x + xOffset) * freq, (z + zOffset) * freq) * amp;
int p = 0.5f;
for (int i = 0; i < 5; i++) {
float freq = pow(2, i);
float amp = pow(p, i);
total += getInterpolatedNoise(x * freq, z * freq) * amp;
}
return round(total * 1000.0)/1000;
return total;
}
float HeightGenerator::getInterpolatedNoise(float x, float z) {
@ -43,19 +38,18 @@ float HeightGenerator::getInterpolatedNoise(float x, float z) {
float HeightGenerator::interpolate(float a, float b, float blend) {
double theta = blend * MY_PI;
float f = float(1.f - cos(theta)) * 0.5f;
return a * (1.f - f) + b * f;
return a * (1 - f) + b * f;
}
float HeightGenerator::getSmoothNoise(int x, int z) {
float corners = (getNoise(x - 1, z - 1) + getNoise(x + 1, z - 1) + getNoise(x - 1, z + 1) + getNoise(x + 1, z + 1)) / 16.f;
float sides = (getNoise(x - 1, z) + getNoise(x + 1, z) + getNoise(x, z - 1) + getNoise(x, z + 1)) / 8.f;
float center = getNoise(x, z) / 4.f;
float corners = float(getNoise(x - 1, z - 1) + getNoise(x + 1, z - 1) + getNoise(x - 1, z + 1) + getNoise(x + 1, z + 1)) / 16;
float sides = float(getNoise(x - 1, z) + getNoise(x + 1, z) + getNoise(x, z - 1) + getNoise(x, z + 1)) / 8;
float center = float(getNoise(x, z)) / 4;
return corners + sides + center;
}
float HeightGenerator::getNoise(int x, int z) {
std::mt19937 gen(SEED + x * 49632 + z * 325176);
std::uniform_real_distribution<> distr(0, 100000000);
return distr(gen) * 2.f - 1.f;
std::uniform_real_distribution<> distr(-3, 3);
return distr(gen);
}

View File

@ -6,11 +6,6 @@ public:
float generateHeight(int x, int z);
static const int SEED;
private:
static const float AMPLITUDE;
static const int OCTAVES;
static const float ROUGHNESS;
static const int xOffset;
static const int zOffset;
float getInterpolatedNoise(float x, float z);
float getSmoothNoise(int x, int z);
float interpolate(float a, float b, float blend);

View File

@ -1,33 +1,15 @@
#include "Terrain.h"
const float Terrain::SIZE = 100.f;
const int Terrain::VERTEX_COUNT = 2;
const float Terrain::SIZE = 50.f;
const int Terrain::VERTEX_COUNT = 64;
const int Terrain::COUNT = Terrain::VERTEX_COUNT * Terrain::VERTEX_COUNT;
Terrain::Terrain(int gridX, int gridZ, HeightGenerator heightGenerator) {
x = gridX * SIZE;
z = gridZ * SIZE;
Terrain::Terrain(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];
@ -37,17 +19,14 @@ 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;
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 + 1] = getHeight(j, i);
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;
textureCoords[vertexPointer * 2] = float(j) / float(VERTEX_COUNT - 1);
textureCoords[vertexPointer * 2 + 1] = float(i) / float(VERTEX_COUNT - 1);
vertexPointer++;
}
}
@ -88,11 +67,3 @@ glm::vec3 Terrain::calculateNormal(int x, int z) {
float Terrain::getHeight(int x, int z) {
return heightGenerator.generateHeight(x, z);
}
int Terrain::getX() {
return x;
}
int Terrain::getZ() {
return z;
}

View File

@ -10,17 +10,12 @@ class Terrain
{
public:
Terrain() = default;
Terrain(int gridX, int gridZ, HeightGenerator heightGenerator);
Terrain(HeightGenerator heightGenerator);
obj::Model generateTerrain();
int getX();
int getZ();
private:
static const float SIZE;
static const int VERTEX_COUNT;
static const int COUNT;
float x;
float z;
HeightGenerator heightGenerator;
glm::vec3 calculateNormal(int x, int z);
float getHeight(int x, int z);

View File

@ -29,7 +29,7 @@ unsigned int cubemapTexture, skyboxVAO;
unsigned int cubeVAO, cubeVBO;
float skyboxVerticeParameter = 50.0f;
float skyboxBoundary = 480.0f;
float skyboxBoundary = 48.0f;
std::vector<glm::vec3> bubbleArray[300];
float old_x, old_y = -1;
@ -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,23 +385,26 @@ 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, -10, 1)), textureTerrain, programTexture);
drawObjectTexture(terrainContext, glm::translate(glm::vec3(1, 10, 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);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glm::mat4 terrainTransformation = glm::translate(glm::vec3(50, -49, 50)) * glm::rotate(glm::radians(180.f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(2.f));
drawObjectTexture(terrainContext, terrainTransformation, textureTerrain, programTexture);
glutSwapBuffers();
}
@ -490,6 +493,7 @@ void initKeyRotation() {
void initCube()
{
cubemapTexture = loadCubemap();
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &cubeVBO);
glBindVertexArray(cubeVAO);
@ -501,36 +505,54 @@ void initCube()
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
}
void init()
void initTerrainContext()
{
glEnable(GL_DEPTH_TEST);
programTexture = shaderLoader.CreateProgram((char*)"shaders/shader_tex.vert", (char*)"shaders/shader_tex.frag");
skyboxProgram = shaderLoader.CreateProgram((char*)"shaders/skybox.vert", (char*)"shaders/skybox.frag");
bubbleProgram = shaderLoader.CreateProgram((char*)"shaders/bubble.vert", (char*)"shaders/bubble.frag");
cubemapTexture = loadCubemap();
loadModelToContext("models/submarine.obj", submarineContext);
textureSubmarine = Core::LoadTexture("textures/submarine.png");
loadModelToContext("models/fish.obj", fishContext);
textureFish = Core::LoadTexture("textures/fish.png");
textureTerrain = Core::LoadTexture("textures/sand.jpg");
terrain = Terrain(0, 0, heightGenerator);
textureTerrain = Core::LoadTexture("textures/terrain.jpg");
terrain = Terrain(heightGenerator);
obj::Model model = terrain.generateTerrain();
terrainContext.initFromOBJ(model);
}
void initFishContext()
{
loadModelToContext("models/fish.obj", fishContext);
textureFish = Core::LoadTexture("textures/fish.png");
}
initKeyRotation();
void initSubmarineContext()
{
loadModelToContext("models/submarine.obj", submarineContext);
textureSubmarine = Core::LoadTexture("textures/submarine.png");
}
void initBubblesContext()
{
loadModelToContext("models/sphere.obj", bubbleContext);
textureBubble = Core::LoadTexture("textures/bubble.png");
generateBubbleArray();
}
void initContexts()
{
initTerrainContext();
initFishContext();
initSubmarineContext();
initBubblesContext();
}
void initShaderPrograms()
{
programTexture = shaderLoader.CreateProgram((char*)"shaders/shader_tex.vert", (char*)"shaders/shader_tex.frag");
skyboxProgram = shaderLoader.CreateProgram((char*)"shaders/skybox.vert", (char*)"shaders/skybox.frag");
bubbleProgram = shaderLoader.CreateProgram((char*)"shaders/bubble.vert", (char*)"shaders/bubble.frag");
}
void init()
{
glEnable(GL_DEPTH_TEST);
initShaderPrograms();
initContexts();
initKeyRotation();
initCube();
initSkybox();
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 441 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 891 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB