Hi!
I want to pass the index to the texture as a vertex attribute, so I do this :
if (vboTextureIndexesBuffer == 0) {
GLuint vbo;
glCheck(glGenBuffers(1, &vbo));
vboTextureIndexesBuffer = static_cast<unsigned int>(vbo);
}
if (oldVerticesSize != m_vertices.size()) {
glCheck(glBindBuffer(GL_ARRAY_BUFFER, vboTextureIndexesBuffer));
glCheck(glBufferData(GL_ARRAY_BUFFER, m_texturesIndexes.size() * sizeof(unsigned int), &m_texturesIndexes[0], GL_DYNAMIC_DRAW));
//std::cout<<"vbo index texture : "<<vboTextureIndexesBuffer<<std::endl;
/*for (unsigned int i = 0; i < m_texturesIndexes.size(); i++) {
std::cout<<"texture indexes size : "<<m_texturesIndexes.size()<<" index : "<<m_texturesIndexes[i]<<std::endl;
}*/
glCheck(glBindBuffer(GL_ARRAY_BUFFER, 0));
} else {
GLvoid *pos_vbo = nullptr;
glCheck(glBindBuffer(GL_ARRAY_BUFFER, vboTextureIndexesBuffer));
glCheck(pos_vbo = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY));
if (pos_vbo != nullptr) {
memcpy(pos_vbo, &m_texturesIndexes[0], m_texturesIndexes.size() * sizeof(unsigned int));
glCheck(glUnmapBuffer(GL_ARRAY_BUFFER));
pos_vbo = nullptr;
}
glCheck(glBindBuffer(GL_ARRAY_BUFFER, 0));
}
The values of my std::vector are correct when I print them.
I call the vertex attributes pointers functions like this :
glCheck(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.vboVertexBuffer));
glCheck(glEnableVertexAttribArray(0));
glCheck(glEnableVertexAttribArray(1));
glCheck(glEnableVertexAttribArray(2));
glCheck(glVertexAttribPointer(0, 3,GL_FLOAT,GL_FALSE,sizeof(Vertex), (GLvoid*) 0));
glCheck(glVertexAttribPointer(1, 4,GL_UNSIGNED_BYTE,GL_TRUE,sizeof(Vertex),(GLvoid*) 12));
glCheck(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) 16));
glCheck(glEnableVertexAttribArray(3));
glCheck(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.vboNormalBuffer));
glCheck(glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(sf::Vector3f), (GLvoid*) 0));
glCheck(glEnableVertexAttribArray(4));
//std::cout<<"vbo texture index buffer : "<<vertexBuffer.vboTextureIndexesBuffer<<std::endl;
glCheck(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.vboTextureIndexesBuffer));
glCheck(glVertexAttribPointer(4, 1, GL_UNSIGNED_INT,GL_FALSE,sizeof(GLuint),(GLvoid*) 0));
glCheck(glDisableVertexAttribArray(0));
glCheck(glDisableVertexAttribArray(1));
glCheck(glDisableVertexAttribArray(2));
glCheck(glDisableVertexAttribArray(3));
glCheck(glDisableVertexAttribArray(4));
glCheck(glBindBuffer(GL_ARRAY_BUFFER, 0));
But the problem is that, in my vertex shader, the texture index have a very big value, or, the value should be 1.
I declare my vertex attributes like this in the vertex shader.
#version 460
#define M_PI 3.1415926535897932384626433832795
#define FPI M_PI/4
layout (location = 0) in vec3 position;
layout (location = 1) in vec4 color;
layout (location = 2) in vec2 texCoords;
layout (location = 3) in vec3 normals;
layout (location = 4) in uint textureIndex;
uniform mat4 textureMatrix/*[)"+core::conversionIntString(Texture::getAllTextures().size())+R"(]*/;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform float water;
uniform float time;
uniform vec3 resolution;
out vec2 fTexCoords;
out vec4 frontColor;
out uint texIndex;
void main () {
float xOff = 0;
float yOff = 0;
if (water > 0.9f) {
yOff = 0.05*sin(position.x*12+time*FPI)*resolution.y;
xOff = 0.025*cos(position.x*12+time*FPI)*resolution.x;
}
gl_Position = projectionMatrix * viewMatrix * vec4((position.x - xOff), (position.y + yOff), position.z, 1.f);
fTexCoords = /*(textureIndex != 0) ? */(textureMatrix * vec4(texCoords, 1.f, 1.f)).xy /*: texCoords*/;
frontColor = color;
texIndex = textureIndex;
}
I’ve written the value of texIndex as a color to see it and it’s full RED.
R"(#version 460
#extension GL_ARB_bindless_texture : enable
struct NodeType {
vec4 color;
float depth;
uint next;
};
layout(binding = 0, offset = 0) uniform atomic_uint nextNodeCounter;
layout(binding = 0, r32ui) uniform uimage2D headPointers;
layout(binding = 0, std430) buffer linkedLists {
NodeType nodes[];
};
layout(std140, binding = 0) uniform ALL_TEXTURES {
sampler2D textures[200];
};
uniform uint maxNodes;
uniform float haveTexture;
uniform sampler2D texture;
uniform float water;
in vec4 frontColor;
in vec2 fTexCoords;
in flat uint texIndex;
layout (location = 0) out vec4 fcolor;
void main() {
uint nodeIdx = atomicCounterIncrement(nextNodeCounter);
vec4 color = (haveTexture > 0) ? frontColor * texture2D(textures[texIndex-1], fTexCoords.xy) : frontColor;
if (nodeIdx < maxNodes) {
uint prevHead = imageAtomicExchange(headPointers, ivec2(gl_FragCoord.xy), nodeIdx);
nodes[nodeIdx].color = vec4(texIndex / 200.f, 0, 0, 1);
nodes[nodeIdx].depth = gl_FragCoord.z;
nodes[nodeIdx].next = prevHead;
}
fcolor = vec4(0, 0, 0, 0);
})";
Why is the value of texIndex so big ? It’s like if my unsigned int have a negative value in my shader.