Reputation: 176
I have multiple mesh that I want to draw in a glDrawElementsInstanced
but I want all the vertices to all have a different height so to do this I'm trying to pass the height by a sampler1D.
This is how I create the texture :
glGenTextures(1, &meshHeightTexId_);
glBindTexture(GL_TEXTURE_1D, meshHeightTexId_);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// meshHeight_.size() = meshNumber_*meshSize_
glTexImage1D(GL_TEXTURE_1D, 0, GL_RED, meshNumber_*meshSize_, 0, GL_RED, GL_FLOAT, meshHeight_.data());
glBindTexture(GL_TEXTURE_1D, 0);
(meshNumber_ is the numbers of mesh, meshSize_ is the number of vertices is contain and meshHeight_ is a float vector containing all the height data)
The call I do before the draw :
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_1D, meshHeightTexId_);
And in the shader I gather the value like this :
int index = gl_InstanceID * meshSize + gl_VertexID;
vec4 tempo = texture(heightData, index);
float height = tempo.r;
(heightData is the name of the sampler1D I use and meshSize is an uniform int containing the number of vertices in a mesh)
Then I use the height as the y value of the vertice.
But by doing this it look like I only get height equal to 0 (Exept 1 in an angle for no reason).
I may doing something wrong at the initialization of the texture..
And also if you have a better idea than passing the height of meshes by texture I would take it.
Upvotes: 2
Views: 246
Reputation: 72479
Just like GL_TEXTURE_2D
, GL_TEXTURE_1D
uses normalized texture coordinates. Since you set GL_CLAMP_TO_EDGE
all values beyond [0, 1] will be clamped to that interval, so you get only the first and the last height value.
The function you're looking for is texelFetch
:
vec4 tempo = texelFetch(heightData, index, 0);
Alternatively you can create a UBO and read the data from an array within.
Upvotes: 2