I'm using assimp to import 3d models, and each model defines a set of material properties as well, such as material diffuseColor, ambientColor, specularColor and emissiveColor. Where would I store this information, in 4 additional textures, one for each material property or is there a smarter way?

Below is my current fragment shader for the first pass

"#version 330                                                                                                           \n \
                                                                                                                        \n \
layout(std140) uniform;                                                                                                 \n \
                                                                                                                        \n \
uniform UnifGeometry                                                                                                    \n \
{                                                                                                                       \n \
    mat4 mWVPMatrix;                                                                                                    \n \
    mat4 mWorldMatrix;                                                                                                  \n \
    bool mHasDiffuseTexture;                                                                                            \n \
    bool mHasNormalTexture;                                                                                             \n \
    float mTextureTilingFactor;                                                                                         \n \
} UnifGeometryPass;                                                                                                     \n \
                                                                                                                        \n \
uniform sampler2D unifDiffuseTexture;                                                                                   \n \
uniform sampler2D unifNormalTexture;                                                                                    \n \
                                                                                                                        \n \
in vec3 frag_position;                                                                                                  \n \
in vec3 frag_normal;                                                                                                    \n \
in vec2 frag_texcoord;                                                                                                  \n \
in vec3 frag_tangent;                                                                                                   \n \
in vec3 frag_bitangent;                                                                                                 \n \
                                                                                                                        \n \
layout(location = 0) out vec3 outPosition;                                                                              \n \
layout(location = 1) out vec3 outNormal;                                                                                \n \
layout(location = 2) out vec3 outTexcoord;                                                                     \n \
layout(location = 3) out vec3 outColor;                                                                                 \n \
                                                                                                                        \n \
void main()                                                                                                             \n \
{                                                                                                                       \n \
    outPosition = frag_position;                                                                                        \n \
                                                                                                                        \n \
    if (UnifGeometryPass.mHasNormalTexture)                                                                             \n \
    {                                                                                                                   \n \
        vec3 normalTangentSpace  = normalize(texture(unifNormalTexture, frag_texcoord).xyz * 2.0 - 1.0);                \n \
        mat3 tangentToWorldSpace = mat3(normalize(frag_tangent), normalize(frag_bitangent), normalize(frag_normal));    \n \
                                                                                                                        \n \
        outNormal = tangentToWorldSpace * normalTangentSpace;                                                           \n \
    }                                                                                                                   \n \
    else                                                                                                                \n \
        outNormal = normalize(frag_normal);                                                                             \n \
                                                                                                                        \n \
    outTexcoord = vec3(frag_texcoord, 0.0);                                                                             \n \
                                                                                                                        \n \
    if (UnifGeometryPass.mHasDiffuseTexture)                                                                            \n \
        outColor = texture(unifDiffuseTexture, frag_texcoord).xyz;                                                      \n \
    else                                                                                                                \n \
        outColor = vec3(1.0);                                                                                           \n \
}                                                                                                                       \n";

1 Answer 1

  • If the properties are uniform across the object (exact same value on every part of the surface), then you could sen them as additional uniform values per object or as instanced uniform.
  • If there are slight variations (per-triangle variations), then sending values as attributes might be an option.
  • If properties really differ on different parts of the object then textures might be the only way.

It really depends upon your needs, but it is either sending them like textures or as one per-instance uniform value.

As for how to store them:

You have four framebuffers, each with four channels. Plenty of room to store any information that you might need.

  • 1st fb: RGB - position, A - specular
  • 2nd fb: RGB - normal, A - ?
  • 3rd fb: RGB - diffuse color, A - ?
  • 4th fb: RGB - emissive, A - ?

So you basically have 3 more channels to store the information you need.

If you need more space, there's a way to pack normals and position in only two channels.

Hope this helps.

  • \$\begingroup\$ They are uniform across the object, but I need their values in the shading pass when I apply the lighting, and thus I believe I need to save them in an output texture \$\endgroup\$ Commented Dec 2, 2013 at 9:58
  • \$\begingroup\$ Edited answer. Hope this resolves your problem. \$\endgroup\$ Commented Dec 2, 2013 at 10:11
  • \$\begingroup\$ Position should not be packed down in the framebuffers, use the depth directly and transform the depth during rendering. that is a vital part of defferd rendering. \$\endgroup\$
    – Tordin
    Commented Dec 2, 2013 at 10:29
  • \$\begingroup\$ That sounds promising, but one thing I dont understand, these four material properties mentioned are all originally vec4's - how do you for example condense the ambient color into the alpha channel of the normals? \$\endgroup\$ Commented Dec 2, 2013 at 10:30
  • 1
    \$\begingroup\$ Since ambient colour is applied just once, rather than per-light, there may be a way to bake it into your existing colours... I think emissive = emissive + diffuse * ambient might do it, depending exactly how the emissive is used. You're basically treating ambient light as a soft, texture-modulated glow. \$\endgroup\$
    – DMGregory
    Commented Dec 2, 2013 at 13:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .