Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dbartolini committed Aug 7, 2024
1 parent 1b9b7af commit ced22a8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/world/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace crown
{
void Material::bind(ResourceManager &rm, ShaderManager &sm, u8 view, s32 depth) const
void Material::bind(ShaderManager &sm, u8 view, s32 depth) const
{
using namespace material_resource;

Expand All @@ -22,12 +22,10 @@ void Material::bind(ResourceManager &rm, ShaderManager &sm, u8 view, s32 depth)
const TextureData *td = texture_data(_resource, i);
const TextureHandle *th = texture_handle(_resource, i, _data);

const TextureResource *teximg = (TextureResource *)rm.get(RESOURCE_TYPE_TEXTURE, td->id);

bgfx::UniformHandle sampler;
bgfx::TextureHandle texture;
sampler.idx = th->sampler_handle;
texture.idx = teximg->handle.idx;
texture.idx = th->texture_handle;

bgfx::setTexture(i
, sampler
Expand Down Expand Up @@ -78,4 +76,11 @@ void Material::set_matrix4x4(StringId32 name, const Matrix4x4 &value)
*(Matrix4x4 *)(p + sizeof(u32)) = value;
}

void Material::set_texture(StringId32 sampler_name, ResourceId texture_resource)
{
const TextureResource *tr = (TextureResource *)_resource_manager->get(RESOURCE_TYPE_TEXTURE, texture_resource);
TextureHandle *th = (TextureHandle *)material_resource::texture_handle_by_name(_resource, sampler_name, _data);
th->texture_handle = tr->handle.idx;
}

} // namespace crown
5 changes: 5 additions & 0 deletions src/world/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include "core/math/types.h"
#include "resource/resource_id.h"
#include "resource/types.h"
#include "world/types.h"

Expand All @@ -16,6 +17,7 @@ namespace crown
/// @ingroup World
struct Material
{
ResourceManager *_resource_manager;
const MaterialResource *_resource;
char *_data;

Expand All @@ -36,6 +38,9 @@ struct Material

/// Sets the @a value of the variable @a name.
void set_matrix4x4(StringId32 name, const Matrix4x4 &value);

/// Sets the @a texture of the variable @a name.
void set_texture(StringId32 sampler_name, ResourceId texture_resource);
};

} // namespace crown
3 changes: 3 additions & 0 deletions src/world/material_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "core/strings/string_id.inl"
#include "resource/material_resource.h"
#include "resource/resource_manager.h"
#include "resource/texture_resource.h"
#include "world/material_manager.h"
#include <bgfx/bgfx.h>
#include <string.h> // memcpy
Expand Down Expand Up @@ -52,7 +53,9 @@ void MaterialManager::online(StringId64 id, ResourceManager &rm)
for (u32 i = 0; i < mr->num_textures; ++i) {
TextureData *td = texture_data(mr, i);
TextureHandle *th = texture_handle(mr, i, base);
TextureResource *tr = (TextureResource *)rm.get(RESOURCE_TYPE_TEXTURE, td->id);
th->sampler_handle = bgfx::createUniform(texture_name(mr, td), bgfx::UniformType::Sampler).idx;
th->texture_handle = tr->handle.idx;
}

for (u32 i = 0; i < mr->num_uniforms; ++i) {
Expand Down

0 comments on commit ced22a8

Please sign in to comment.