Skip to content

Commit

Permalink
Model Renderer|GL: Shader defines texture mapping for itself
Browse files Browse the repository at this point in the history
The shader definition can specify which texture map is to be bound
to which position in the vertex attributes. The model renderer checks
this information when a model is being prepared for drawing.
  • Loading branch information
skyjake committed Aug 16, 2014
1 parent ad19cae commit bdd6bdc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ group model {
# Shader for skeletal animation and generic per-pixel lighting:
# diffuse color, normal map, emission map, specular intensity.
shader skeletal.normal_specular_emission {
# Mapping when used with ModelDrawable.
textureMapping <diffuse, normals, specular, emission>
vertex = "
uniform highp mat4 uMvpMatrix;
uniform highp mat4 uBoneMatrices[64];
Expand Down
33 changes: 27 additions & 6 deletions doomsday/client/src/render/modelrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static String const DEF_FRONT_VECTOR("front");
DENG2_PIMPL(ModelRenderer)
, DENG2_OBSERVES(filesys::AssetObserver, Availability)
, DENG2_OBSERVES(Bank, Load)
, DENG2_OBSERVES(ModelDrawable, AboutToGLInit)
{
#define MAX_LIGHTS 4

Expand All @@ -59,7 +60,7 @@ DENG2_PIMPL(ModelRenderer)
Instance(Public *i) : Base(i)
{
observer.audienceForAvailability() += this;
bank.audienceForLoad() += this;
bank.audienceForLoad() += this;
}

void init()
Expand Down Expand Up @@ -93,14 +94,15 @@ DENG2_PIMPL(ModelRenderer)

uTex = *atlas;

/*
// All loaded items should use this atlas.
bank.iterate([this] (DotPath const &path)
{
if(bank.isLoaded(path))
{
setupModel(bank.model(path), path);
setupModel(bank.model(path));
}
});
});*/
}

void deinit()
Expand Down Expand Up @@ -134,15 +136,29 @@ DENG2_PIMPL(ModelRenderer)
*
* @param model Model to configure.
*/
void setupModel(ModelDrawable &model, String const &path)
void setupModel(ModelDrawable &model)
{
if(atlas)
{
model.setAtlas(*atlas);
model.setTextureMapping(ModelDrawable::diffuseNormalsSpecularEmission());

model.setDefaultTexture(ModelDrawable::Normals, defaultNormals);
model.setDefaultTexture(ModelDrawable::Emission, defaultEmission);
model.setDefaultTexture(ModelDrawable::Specular, defaultSpecular);

// Use the texture mapping specified in the shader. This has to be done
// only now because earlier we may not have the shader available yet.
Record const &def = ClientApp::shaders().names().subrecord("model.skeletal.normal_specular_emission");
if(def.has("textureMapping"))
{
ModelDrawable::Mapping mapping;
for(Value const *value : def.geta("textureMapping").elements())
{
mapping << ModelDrawable::textToTextureMap(value->asText());
}
//qDebug() << "using mapping" << mapping;
model.setTextureMapping(mapping);
}
}
else
{
Expand All @@ -151,6 +167,11 @@ DENG2_PIMPL(ModelRenderer)
model.setProgram(program);
}

void modelAboutToGLInit(ModelDrawable &model)
{
setupModel(model);
}

/**
* When model assets have been loaded, we can parse their metadata to see if there
* are any animation sequences defined. If so, we'll set up a shared lookup table
Expand All @@ -162,7 +183,7 @@ DENG2_PIMPL(ModelRenderer)
{
// Models use the shared atlas.
ModelDrawable &model = bank.model(path);
setupModel(model, path);
model.audienceForAboutToGLInit() += this;

auto const asset = App::asset(path);

Expand Down

0 comments on commit bdd6bdc

Please sign in to comment.