Skip to content

Commit

Permalink
Image class added
Browse files Browse the repository at this point in the history
  • Loading branch information
beaumanvienna committed Jun 14, 2024
1 parent 6763bfe commit 85cafaa
Show file tree
Hide file tree
Showing 22 changed files with 114 additions and 183 deletions.
79 changes: 0 additions & 79 deletions engine/renderer/baseTerrain.cpp

This file was deleted.

44 changes: 0 additions & 44 deletions engine/renderer/baseTerrain.h

This file was deleted.

8 changes: 5 additions & 3 deletions engine/renderer/builder/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include "auxiliary/hash.h"
#include "stb_image.h"

#include "core.h"
#include "renderer/builder/builder.h"
#include "scene/scene.h"
#include "auxiliary/hash.h"
#include "renderer/builder/builder.h"
#include "renderer/model.h"

#include "stb_image.h"

#include "VKmodel.h"

Expand Down
10 changes: 7 additions & 3 deletions engine/renderer/builder/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@

#pragma once

#include "renderer/model.h"
#include "scene/scene.h"

namespace GfxRenderEngine
{

class Cubemap;
class Model;
class Scene;
class Submesh;
class Vertex;

class Builder
{

Expand Down
3 changes: 3 additions & 0 deletions engine/renderer/builder/fastgltfBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include "stb_image.h"

#include "core.h"
#include "scene/scene.h"
#include "renderer/model.h"
#include "renderer/instanceBuffer.h"
#include "renderer/builder/fastgltfBuilder.h"
#include "renderer/materialDescriptor.h"
#include "renderer/resourceDescriptor.h"
Expand Down
19 changes: 17 additions & 2 deletions engine/renderer/builder/fastgltfBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,27 @@
#include <fastgltf/util.hpp>
#include <fastgltf/glm_element_traits.hpp>

#include "renderer/model.h"
#include "scene/material.h"
#include "scene/gltf.h"
#include "scene/scene.h"

namespace GfxRenderEngine
{
namespace Armature
{
class Skeleton;
}
class Buffer;
class Dictionary;
class InstanceBuffer;
class Material;
class Model;
class Scene;
class SceneGraph;
class SkeletalAnimations;
class Submesh;
class Texture;
class TransformComponent;
class Vertex;

class FastgltfBuilder
{
Expand Down
1 change: 1 addition & 0 deletions engine/renderer/builder/fbxBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "assimp/postprocess.h"

#include "core.h"
#include "renderer/instanceBuffer.h"
#include "renderer/builder/fbxBuilder.h"
#include "renderer/materialDescriptor.h"
#include "auxiliary/instrumentation.h"
Expand Down
1 change: 1 addition & 0 deletions engine/renderer/builder/gltfBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "stb_image.h"

#include "core.h"
#include "renderer/instanceBuffer.h"
#include "renderer/builder/gltfBuilder.h"
#include "renderer/materialDescriptor.h"
#include "auxiliary/instrumentation.h"
Expand Down
60 changes: 30 additions & 30 deletions engine/renderer/builder/terrainBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,47 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include "core.h"
#include "renderer/image.h"
#include "renderer/model.h"
#include "renderer/instanceBuffer.h"
#include "renderer/builder/terrainBuilder.h"
#include "auxiliary/file.h"
#include "auxiliary/stbWrapper.h"
#include "scene/scene.h"
#include "stb_image.h"

namespace GfxRenderEngine
{
void TerrainBuilder::ColorTerrain(Terrain::TerrainSpec const& terrainSpec, glm::ivec3& heightMapProperties)
void TerrainBuilder::ColorTerrain(Terrain::TerrainSpec const& terrainSpec, Image const& heightMap)
{
glm::ivec3 colorMapProperties;
uchar* rawData = stbi_load(terrainSpec.m_FilepathColorMap.c_str(), &colorMapProperties.x /*width*/,
&colorMapProperties.y /*height*/, &colorMapProperties.z /*bytes per pixel*/, 0);
if (!rawData)
Image colorMap{terrainSpec.m_FilepathColorMap};

if (!colorMap.IsValid())
{
LOG_CORE_CRITICAL("color map did not load: {0}", terrainSpec.m_FilepathColorMap);
return;
}
if (!(colorMapProperties.z == 4))

if (!(colorMap.BytesPerPixel() == 4))
{
LOG_CORE_CRITICAL("color map must be rgba (got {0} bytes per pixel) from {1}", colorMapProperties.z,
LOG_CORE_CRITICAL("color map must be rgba (got {0} bytes per pixel) from {1}", colorMap.BytesPerPixel(),
terrainSpec.m_FilepathColorMap);
stbi_image_free(rawData);
return;
}
if (!((colorMapProperties.x == heightMapProperties.x) && (colorMapProperties.y == heightMapProperties.y)))

if (!((colorMap.Width() == heightMap.Width()) && (colorMap.Height()) == heightMap.Height()))
{
LOG_CORE_CRITICAL("color map and height map dimensions must match: color map width: {0}, color map height: "
"{1}, height map width: {2}, height map height: {3}, color map: {4}, heigh map: {5}",
colorMapProperties.x, colorMapProperties.y, heightMapProperties.x, heightMapProperties.y,
colorMap.Width(), colorMap.Height(), heightMap.Width(), heightMap.Height(),
terrainSpec.m_FilepathColorMap, terrainSpec.m_FilepathHeightMap);
stbi_image_free(rawData);
return;
}

uint* imageData = reinterpret_cast<uint*>(rawData);
uint* imageData = reinterpret_cast<uint*>(colorMap.Get());
size_t vertexCounter = 0;
for (int y = 0; y < colorMapProperties.y; ++y)
for (int y = 0; y < colorMap.Height(); ++y)
{
int yOffset = y * colorMapProperties.x;
for (int x = 0; x < colorMapProperties.x; ++x)
int yOffset = y * colorMap.Width();
for (int x = 0; x < colorMap.Width(); ++x)
{ // image format is rgba in reverse
uint abgr = imageData[yOffset + x];
float r = (0xff & (abgr >> 0)) / 255.0f;
Expand All @@ -73,13 +73,13 @@ namespace GfxRenderEngine
++vertexCounter;
}
}
stbi_image_free(rawData);
}

bool TerrainBuilder::PopulateTerrainData(uchar* heightMap, glm::ivec3 const& heightMapProperties)
bool TerrainBuilder::PopulateTerrainData(Image const& heightMap)
{
int const& cols = heightMapProperties.x;
int const& rows = heightMapProperties.y;

int const& cols = heightMap.GetProperties().x;
int const& rows = heightMap.GetProperties().y;
if (!(rows > 0 && cols > 0))
{
LOG_CORE_CRITICAL("heightmap is incomplete");
Expand Down Expand Up @@ -179,37 +179,36 @@ namespace GfxRenderEngine

bool TerrainBuilder::LoadTerrainHeightMap(Scene& scene, int instanceCount, Terrain::TerrainSpec const& terrainSpec)
{
stbi_set_flip_vertically_on_load(false);

TerrainComponent terrainComponent{};
m_Vertices.clear();
m_Indices.clear();
m_Submeshes.clear();

// terrain data
{
glm::ivec3 heightMapProperties{};
StbWrapper terrainData{terrainSpec.m_FilepathHeightMap.c_str(), &heightMapProperties.x, &heightMapProperties.y,
&heightMapProperties.z, 0};
if (!terrainData.Get())
terrainComponent.m_HeightMap = std::make_shared<Image>(terrainSpec.m_FilepathHeightMap);
Image& heightMap = *terrainComponent.m_HeightMap.get();

if (!heightMap.IsValid())
{
LOG_CORE_CRITICAL("TerrainBuilder::LoadTerrainHeightMap: Couldn't load file {0}",
terrainSpec.m_FilepathHeightMap);
return false;
}

if (heightMapProperties.z != 1)
if (heightMap.BytesPerPixel() != 1)
{
LOG_CORE_CRITICAL("TerrainBuilder::LoadTerrainHeightMap: heightmap {0} must be 8bpc GRAY (1 byte per pixel)",
terrainSpec.m_FilepathHeightMap);
return false;
}

bool succesful = PopulateTerrainData(terrainData.Get(), heightMapProperties);
bool succesful = PopulateTerrainData(heightMap);
if (!succesful)
{
return false;
}
ColorTerrain(terrainSpec, heightMapProperties);
ColorTerrain(terrainSpec, heightMap);
}

// create game objects for all instances
Expand Down Expand Up @@ -280,6 +279,7 @@ namespace GfxRenderEngine

MeshComponent mesh{shortName, model};
registry.emplace<MeshComponent>(entity, mesh);
registry.emplace<TerrainComponent>(entity, terrainComponent);
}
}
return true;
Expand Down
10 changes: 5 additions & 5 deletions engine/renderer/builder/terrainBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#pragma once

#include "renderer/model.h"
#include "scene/scene.h"
#include "scene/terrain.h"

namespace GfxRenderEngine
{
class Scene;
class Image;

class TerrainBuilder
{

Expand All @@ -37,8 +37,8 @@ namespace GfxRenderEngine
bool LoadTerrainHeightMap(Scene& scene, int instanceCount, Terrain::TerrainSpec const& terrainSpec);

private:
bool PopulateTerrainData(uchar* heightMap, glm::ivec3 const& heightMapProperties);
void ColorTerrain(Terrain::TerrainSpec const& terrainSpec, glm::ivec3& heightMapProperties);
bool PopulateTerrainData(Image const& heightMap);
void ColorTerrain(Terrain::TerrainSpec const& terrainSpec, Image const& heightMap);
void CalculateTangents();
void CalculateTangentsFromIndexBuffer(std::vector<uint> const& indices);

Expand Down
1 change: 1 addition & 0 deletions engine/renderer/builder/ufbxBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "stb_image.h"

#include "core.h"
#include "renderer/instanceBuffer.h"
#include "renderer/builder/ufbxBuilder.h"
#include "renderer/materialDescriptor.h"
#include "auxiliary/instrumentation.h"
Expand Down
Loading

0 comments on commit 85cafaa

Please sign in to comment.