Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rlgl] Find a generic way to deal with shader maps #96

Closed
raysan5 opened this issue Mar 1, 2016 · 2 comments
Closed

[rlgl] Find a generic way to deal with shader maps #96

raysan5 opened this issue Mar 1, 2016 · 2 comments
Labels
enhancement This is an improvement of some feature

Comments

@raysan5
Copy link
Owner

raysan5 commented Mar 1, 2016

Right now shader maps are not generic; in our default shader we define uniform location points for three kind of maps:

  • diffuse -> binded to texture unit 0 (GL_TEXTURE0)
  • normal -> binded to texture unit 1 (GL_TEXTURE1)
  • specular -> binded to texture unit 2 (GL_TEXTURE2)

Those maps can be set with the following (not generic) functions:

void SetShaderMapDiffuse(Shader *shader, Texture2D texture);
void SetShaderMapSpecular(Shader *shader, const char *uniformName, Texture2D texture);
void SetShaderMapNormal(Shader *shader, const char *uniformName, Texture2D texture);

It's not possible to add additional maps to our default shader. To use additional maps, new uniform location points must be defined externally by user. Those map location points (new texture units) are also not considered by rlglDrawModel().

There is not an easy solution to this issue. One idea could be enabling a number of possible maps and keep track of the active ones:

typedef struct Shader {
    unsigned int id;                // Shader program id

    // Variable attributes
    int vertexLoc;        // Vertex attribute location point (vertex shader)
    int texcoordLoc;      // Texcoord attribute location point (vertex shader)
    int normalLoc;        // Normal attribute location point (vertex shader)
    int colorLoc;         // Color attibute location point (vertex shader)

    // Uniforms
    int mvpLoc;           // ModelView-Projection matrix uniform location point (vertex shader)
    int tintColorLoc;     // Color uniform location point (fragment shader)

    // Maps uniform locations
    int mapLoc[MAX_MAP_LOCATIONS];  // Map texture uniform location point (fragment shader)
    int mapCounter;                 // Number of maps used (texture units)
} Shader;

One function to set custom maps could be added:

void SetShaderMap(Shader *shader, Texture2D texture);

Obviously, the user should use them properly on the shader code.

A similar solution could also be applied to vertex attributes for custom attributes.

@raysan5 raysan5 added the enhancement This is an improvement of some feature label Mar 1, 2016
@raysan5
Copy link
Owner Author

raysan5 commented May 18, 2016

This system has been redesigned and now maps usage is simpler. Still, there are three kind of maps and they are binded by default to fixed texture units:

  • diffuse -> binded to texture unit 0 (GL_TEXTURE0)
  • normal -> binded to texture unit 1 (GL_TEXTURE1)
  • specular -> binded to texture unit 2 (GL_TEXTURE2)

But now, depending on textures assigned to material and selected shader, they are internally binded on drawing or not:

// Default material loading example
Material material = LoadDefaultMaterial();                 // Default shader assigned to material (supports  only diffuse map)
material.texDiffuse = LoadTexture("wood_diffuse.png");     // texture unit 0 activated (available in material shader)
material.texSpecular = LoadTexture("wood_specular.png");   // texture unit 2 activated (available in material shader)

// Standard material loading example
Material material = LoadStandardMaterial();                // Standard shader assigned to material (supports diffuse, normal and specular maps)
material.texDiffuse = LoadTexture("wood_diffuse.png");     // texture unit 0 activated (available in material shader)
material.texSpecular = LoadTexture("wood_specular.png");   // texture unit 2 NOT activated (not available in material shader)

By default, on shader loading, following fixed location names are expected for maps:

uniform sampler2D texture0;   // GL_TEXTURE0
uniform sampler2D texture1;   // GL_TEXTURE1
uniform sampler2D texture2;   // GL_TEXTURE2

Despite its name on material struct (texDiffuse, texNormal, texSpecular), user is free to use those maps in any way inside the custom shader.

@raysan5
Copy link
Owner Author

raysan5 commented May 31, 2016

@raysan5 raysan5 closed this as completed May 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This is an improvement of some feature
Projects
None yet
Development

No branches or pull requests

1 participant