Skip to content

Commit

Permalink
Gloom: Added a separate class for materials
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent c38762c commit 02d7f4e
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 109 deletions.
2 changes: 2 additions & 0 deletions doomsday/tests/test_gloom/CMakeLists.txt
Expand Up @@ -54,6 +54,8 @@ set (SOURCES
gloom/render/mapbuild.h
gloom/render/maprender.cpp
gloom/render/maprender.h
gloom/render/materiallib.cpp
gloom/render/materiallib.h
gloom/render/render.cpp
gloom/render/render.h
gloom/render/screenquad.cpp
Expand Down
3 changes: 2 additions & 1 deletion doomsday/tests/test_gloom/gloom/render/context.cpp
Expand Up @@ -19,6 +19,7 @@
#include "gloom/render/context.h"
#include "gloom/render/gbuffer.h"
#include "gloom/render/maprender.h"
#include "gloom/render/materiallib.h"

using namespace de;

Expand All @@ -41,7 +42,7 @@ Context &Context::bindGBuffer(GLProgram &program)
Context &Context::bindMaterials(GLProgram &program)
{
program << uDiffuseAtlas << uEmissiveAtlas << uSpecGlossAtlas << uNormalDisplAtlas << uEnvMap
<< uEnvIntensity << mapRender->uTextureMetrics();
<< uEnvIntensity << mapRender->materialLibrary().uTextureMetrics();
return *this;
}

Expand Down
12 changes: 6 additions & 6 deletions doomsday/tests/test_gloom/gloom/render/mapbuild.cpp
Expand Up @@ -42,12 +42,12 @@ LIBGUI_VERTEX_FORMAT_SPEC(MapVertex, 21 * 4)

DENG2_PIMPL_NOREF(MapBuild)
{
const Map & map;
MaterialIds materials;
Mapper planeMapper;
Mapper texOffsetMapper;
const Map & map;
MaterialLib::Ids materials; // currently loaded materials
Mapper planeMapper;
Mapper texOffsetMapper;

Impl(const Map &map, const MaterialIds &materials)
Impl(const Map &map, const MaterialLib::Ids &materials)
: map(map)
, materials(materials)
{}
Expand Down Expand Up @@ -295,7 +295,7 @@ DENG2_PIMPL_NOREF(MapBuild)
}
};

MapBuild::MapBuild(const Map &map, const MaterialIds &materials)
MapBuild::MapBuild(const Map &map, const MaterialLib::Ids &materials)
: d(new Impl(map, materials))
{}

Expand Down
5 changes: 2 additions & 3 deletions doomsday/tests/test_gloom/gloom/render/mapbuild.h
Expand Up @@ -20,6 +20,7 @@
#define GLOOM_MAPBUILD_H

#include "gloom/world/map.h"
#include "gloom/render/materiallib.h"

#include <de/GLBuffer>
#include <de/Id>
Expand Down Expand Up @@ -58,8 +59,6 @@ class MapBuild
typedef de::GLBufferT<MapVertex> Buffer;

public:
typedef QHash<de::String, uint32_t> MaterialIds;

struct Mapper : public QHash<ID, uint32_t>
{
uint32_t insert(ID id)
Expand All @@ -75,7 +74,7 @@ class MapBuild
}
};

MapBuild(const Map &map, const MaterialIds &materials);
MapBuild(const Map &map, const MaterialLib::Ids &materials);
Buffer *build();

const Mapper &planeMapper() const;
Expand Down
110 changes: 13 additions & 97 deletions doomsday/tests/test_gloom/gloom/render/maprender.cpp
Expand Up @@ -18,6 +18,7 @@

#include "gloom/render/maprender.h"
#include "gloom/render/mapbuild.h"
#include "gloom/render/materiallib.h"
#include "gloom/render/databuffer.h"
#include "gloom/render/entityrender.h"
#include "gloom/render/lightrender.h"
Expand All @@ -35,27 +36,17 @@ namespace gloom {

DENG2_PIMPL(MapRender)
{
MapBuild::MaterialIds materials;
MapBuild::Mapper planeMapper;
MapBuild::Mapper texOffsetMapper;

using TexIds = std::array<Id, TextureMapCount>;
QHash<String, TexIds> loadedTextures; // name => atlas ID

struct Metrics {
struct Texture {
Vec4f uvRect;
Vec4f texelSize;
} texture[TextureMapCount];
};
MaterialLib matLib;
MapBuild::Mapper planeMapper;
MapBuild::Mapper texOffsetMapper;

struct TexOffsetData {
Vec2f offset;
Vec2f speed;
};

DataBuffer<Metrics> textureMetrics{"uTextureMetrics", Image::RGBA_32f, gl::Static}; //2 * TextureMapCount, 1};
DataBuffer<float> planes {"uPlanes", Image::R_32f};
DataBuffer<TexOffsetData> texOffsets {"uTexOffsets", Image::RGBA_32f};
DataBuffer<float> planes {"uPlanes", Image::R_32f};
DataBuffer<TexOffsetData> texOffsets{"uTexOffsets", Image::RGBA_32f};

Drawable surfaces;
GLProgram dirShadowProgram;
Expand All @@ -72,62 +63,6 @@ DENG2_PIMPL(MapRender)
surfaces.clear();
}

void loadMaterial(const String &name)
{
auto &ctx = self().context();

const char *suffix[TextureMapCount] = {
".diffuse", ".specgloss", ".emissive", ".normaldisp"
};
TexIds ids {{Id::None, Id::None, Id::None, Id::None}};

for (int i = 0; i < TextureMapCount; ++i)
{
if (ctx.images->has(name + suffix[i]))
{
ids[i] = ctx.atlas[i]->alloc(ctx.images->image(name + suffix[i]));
}
}
loadedTextures.insert(name, ids);
}

void updateTextureMetrics()
{
const float texelsPerMeter = 200.f;
auto &ctx = self().context();

textureMetrics.clear();
materials.clear();

for (auto i = loadedTextures.begin(); i != loadedTextures.end(); ++i)
{
Metrics metrics{};

// Load up metrics in an array.
for (int j = 0; j < 4; ++j)
{
const Id texId = i.value()[j];
Rectanglei rect;
Rectanglef rectf;
if (texId)
{
rect = ctx.atlas[j]->imageRect(texId);
rectf = ctx.atlas[j]->imageRectf(texId);

metrics.texture[j] = Metrics::Texture
{
{rectf.xywh()}, {Vec4f(rect.width(), rect.height(), texelsPerMeter)}
};
}
}

const uint32_t matId = textureMetrics.append(metrics);
materials.insert(i.key(), matId);
}

textureMetrics.update();
}

void buildMap()
{
auto &context = self().context();
Expand All @@ -137,7 +72,7 @@ DENG2_PIMPL(MapRender)

DENG2_ASSERT(map);

MapBuild builder{*map, materials};
MapBuild builder{*map, matLib.materials()};
auto *buf = builder.build();

planeMapper = builder.planeMapper();
Expand All @@ -160,7 +95,7 @@ DENG2_PIMPL(MapRender)

context.shaders->build(surfaces.program(), "gloom.surface.material")
<< planes.var
<< textureMetrics.var
<< matLib.uTextureMetrics()
<< texOffsets.var;

context.shaders->build(dirShadowProgram, "gloom.surface.shadow.dir")
Expand All @@ -182,17 +117,10 @@ DENG2_PIMPL(MapRender)

void glInit()
{
matLib.glInit(self().context());
ents .glInit(self().context());
lights.glInit(self().context());

// Load materials.
for (const char *name :
{"world.stone", "world.dirt", "world.grass", "world.test", "world.test2"})
{
loadMaterial(name);
}
updateTextureMetrics();

buildMap();
ents.createEntities();
lights.createLights();
Expand All @@ -202,20 +130,8 @@ DENG2_PIMPL(MapRender)
{
ents .glDeinit();
lights.glDeinit();
matLib.glDeinit();

for (const TexIds &texIds : loadedTextures)
{
for (int t = 0; t < TextureMapCount; ++t)
{
if (texIds[t])
{
self().context().atlas[t]->release(texIds[t]);
}
}
}

loadedTextures.clear();
textureMetrics.clear();
planes.clear();
texOffsets.clear();
clear();
Expand Down Expand Up @@ -250,9 +166,9 @@ LightRender &MapRender::lights()
return d->lights;
}

GLUniform &MapRender::uTextureMetrics()
MaterialLib &MapRender::materialLibrary()
{
return d->textureMetrics.var;
return d->matLib;
}

void MapRender::advanceTime(TimeSpan)
Expand Down
4 changes: 2 additions & 2 deletions doomsday/tests/test_gloom/gloom/render/maprender.h
Expand Up @@ -29,6 +29,7 @@ namespace gloom {

class ICamera;
class LightRender;
class MaterialLib;

class MapRender : public Render
{
Expand All @@ -43,8 +44,7 @@ class MapRender : public Render
void rebuild();

LightRender &lights();

de::GLUniform &uTextureMetrics();
MaterialLib &materialLibrary();

private:
DENG2_PRIVATE(d)
Expand Down

0 comments on commit 02d7f4e

Please sign in to comment.