I want to implement textures in my multi-instanced rendering. According to some posts here (sorry, the side doesn’t allow me to paste links) I decided to chose approach with with atlases. I already implemented them in my C++ code and they works fine, but I faced problem with shader.
This is how my shader looked before
#version 450
uniform mat4 MVPs[256];
attribute vec3 vCol;
attribute vec2 vUV;
attribute vec3 vPos;
varying vec3 color;
void main()
{
gl_Position = MVPs[gl_InstanceID] * vec4(vPos, 1.0);
color = vCol;
}
And how it looks now
#version 450
layout(location=000) uniform mat4 MVPs[100]; // 16
layout(location=100) uniform int atlasIDs[100]; // 1
layout(location=200) uniform vec2 atlasScales[100]; // 2
layout(location=300) uniform vec2 atlasOffsets[100]; // 2
in vec3 vCol;
in vec2 vUV;
in vec3 vPos;
out vec3 color;
out vec2 uv;
void main()
{
gl_Position = MVPs[gl_InstanceID] * vec4(vPos, 1.0);
color = vCol;
uv = vUV * atlasScales[atlasIDs[gl_InstanceID]] + atlasOffsets[atlasIDs[gl_InstanceID]];
}
atlasScales and atlasOffsets represents how current instance UV should be transformet to fin into atlas entry, atlasIDs represents atlas entry id for each instance
As you can see, I had to decrease amount of MVPs (available instances to draw). I also had to greatly decrease my atlas size from, as I planned, ±1024 (just in case, I’m not sure) to 100. If I increase size for one of these arrays for about 100, I get into situation when my game render nothing at all (black screen), which leads me to conclusion that it’s just data size overflow.
This is how my renderer system works
/* STRUCTURE EXAMPLE
*
* Game
* |
* +--basic_shader <- shader
* | |
* | +--player_model <- mesh
* | | |
* | | +--player_1 <- instance
* | | |
* | | +--player_2
* | |
* | +--wolf_model
* | |
* | +--wolf_1
* | |
* | +--wolf_2
* | |
* | +--wolf_3
* |
* +--transparent_shader
* |
* +--bottle_model
* |
* +--bottle_1
*/
and I’m also not sure where to inject the textures here.
Am I going in a correct way?