GLSL Vertex Shader: Search

It's hard for me to debug my program. I'm trying to use an array as a lookup table to compensate for the location of a vertex in my vertex shader, but I can't even tell if I am binding my array correctly. The offset coefficient always ends at zero (aligning my vectors, not giving them a shape), so either I refer to the coordinates of the 1D texture, or the array is not attached to the texture.

Honestly, I don’t know which coordinates I should use to get the values ​​from the 1D structure ... but I tried all the combinations.

Here I tweak the array and bind it to the shader:

//FISH GLfloat fish_coords[100]; for (int i = 0; i < 50; i++){fish_coords[i] = 0;} for (int i = 50; i < 100; i++){fish_coords[i] = 1;} glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glGenTextures(1, &fishtexture); glBindTexture(GL_TEXTURE_1D, fishtexture); glTexImage1D(GL_TEXTURE_1D, 0, 1, 128,0,GL_RGBA, GL_UNSIGNED_BYTE, &fish_coords); switch(shadow_selection){ case 0: vertexShader = "fish.vert"; fragmentShader = "fish.frag"; setShaders(); GLint loc1; loc1 = glGetUniformLocation(shaderProgram,"fish_coords"); glUniform1i(loc1,0); 

And my vertex shader:

 uniform float spec_factor; uniform sampler1D fish_coords; varying vec3 lightDir,normal; void main() { vec4 v_pos; vec3 ldir; gl_TexCoord[0] = gl_MultiTexCoord0; v_pos = gl_ModelViewMatrix * gl_Vertex; ldir = vec3(gl_LightSource[0].position-v_pos); lightDir = normalize(ldir); normal = normalize(gl_NormalMatrix * gl_Normal); vec4 offset; offset = texture1D(fish_coords, gl_TexCoord[0].r); vec4 fish_shape; fish_shape.xz = gl_Vertex.xz; fish_shape.y = gl_Vertex.y * offset.x; fish_shape.w = 1; gl_Position = gl_ModelViewProjectionMatrix * fish_shape; } 
+4
source share
2 answers

There are two problems in the texture loading code:

1.- The MIN filter uses mipmaps by default, so the texture is not complete with this setting. Set the MIN / MAG filters after binding the texture to GL_NEAREST. 2.- Your texture data is a float, but you tell GL that they are unsigned bytes, change this to GL_FLOAT.

After that, your texture will change.

+3
source

There is a GLSL command that allows you to access single texture textures without filtering textures: texelFetch Instead of using normalized coordinates (0-1) it uses integers, so texelFetch (32, 32) will extract texel text 32 texels to the right and 32 text down .

You seem to be writing a rather old school of GLSL, although I don't know how applicable it is.

A warning. If you build your GLSL programs on an Nvidia card, with Nvidia drivers, it will compile, as it was in CG, and this is ridiculous. Interestingly, if you try to access the index of a sufficiently large array (you use the texture as an array, so this should not be a problem for you) using a solid index value, it will be compiled. However, if you use a dynamic index (variable) to access a field of the same array, it will not compile. The reason is that the Nvidias compiler optimizes all unused indexes (since you use a hard-coded value, the compiler believes that it can discard other values ​​of the array)!

This a few years ago caused me a headache.

By the way, I see that you are trying to select a school of fish or something else, it looks interesting, anywhere where you have a demo?

+1
source