简体   繁体   中英

How to dynamically bind an array of multiple texture coordinates sets for mixing in modern OpenGL?

I'm trying to get my head around the whole system of binding multiple textures that contain different UV coordinate values, specifically for map tiling (repeating) in order to mix an albedo + normal map that contains different scalar values. To dynamically set multiple texture coordinates to one mesh would be beneficial in many scenarios - one being that normal map details would really stand out for things like skin, rusty metal ect. This would simply be acomplished by scaling the UV coord values to something relatively high, and leaving the albedo map scale set to 1.

How would one go about communicating these texture coord sets from C++ to a glsl shader, and what would be the most efficient / dynamic pipeline to practice for real-time rendering?

I've already tried to parse multiple arrays through a number of location layouts into GLSL, however this becomes too static for dynamic situations.

This is my vertex struct, however I only parse one set of vertex positions, and one set of texture coordinates:

    struct VertexElement {
     uint16_t               componentSize;
     std::vector<float>     data;

     VertexElement(uint16_t comp_size, std::vector<float> vertex_data)
     {
    componentSize = comp_size;
    data = vertex_data;
     }
    };

    struct Vertex {
     uint16_t stride;
     std::vector<VertexElement> vertexElements;

     inline Vertex() = default;
     inline Vertex(uint16_t _stride, std::vector<VertexElement> 
         _vertex_elements) 
         {
    stride = _stride;
    vertexElements = _vertex_elements;
     }
    };

Essentially, I'm looking to combine multiple textures containing completely different coordinate values without having to statically allocate in GLSL like this:

       location (layout = 0) in vec2 texCord0
       location (layout = 1) in vec2 texCord1        <----- BAD!!
       location (layout = 2) in vec2 texCord3
       ...

I've tried this however I need to readjust the texcoord set size dynamically for real-time editing. Is this possible in OpenGL?

Thanks.

The interface between shaders is always statically defined. A shader has X inputs, always.

If you want some information to be determined dynamically, then you're going to have to use existing tools (image load/store, SSBOs, UBOs, etc) to read that information yourself. This is entirely possible for vertex shaders, since the gl_VertexID index can be used as an index into an array.

Generally speaking however, people don't need to do this. They simply define a particular vertex format and stick with that. Indeed, your specific use case doesn't seem to need this, since you're only changing the values of the texture coordinates, not whether they are being used or not.

I need to readjust the texcoord set size dynamically for real-time editing.

That's not really possible, particularly for the use case you outlined.

Consider a normal map. The difference between using a normal map and not using a normal map is substantial. Not merely in what parameters you're passing to the VS but the fundamental nature of your shader.

Normal maps usually are tangent-space. So your shader now needs a full NBT matrix. And you're probably going to have to pass that matrix to your fragment shader, which will use it to construct the normal from the normal map. Of course, if you don't use a normal map, you just get a per-vertex normal. So you're talking about substantial changes to the structure of your code, not just the inputs.

Shaders are just strings, so you are free to dynamically build them as you like. But that's going to be a lot more complex than just what inputs your VS gets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM