I'm trying to write a shader which renders a multitextured geometry wihout rerendering the geometry with each texture.
In my test I'm not using textures, I just convert the texture index to a color (0: red, 1: blue, 2: green). I'm rendering 3 planes with 3 different "texture index" attributes. The vertex shader just passes that float to the fragment shader where I convert it to an integer (and use this interger as an array indexer in the main shader).
So a vertex consists 4 floats (0-2: position, 3: texture index). Everything is fine until I'm trying to use that "texture index".
Here's the VertexShader:
#version 130
uniform mat4 proj;
uniform mat4 view;
uniform mat4 world;
in vec3 in_position;
in float in_fl;
smooth out float FL;
void main(void)
{
FL = in_fl;
gl_Position = proj * view * world * vec4(in_position, 1);
}
FragmentShader:
#version 130
out vec4 out_frag_color;
smooth in float FL;
void main(void)
{
int i = int(FL);
if(i == 0)
out_frag_color = vec4(1,0,0,1);
else if(i == 1)
out_frag_color = vec4(0,0,1,1);
else if(i == 2)
out_frag_color = vec4(0,1,0,1);
else
out_frag_color = vec4(1,1,1,1);
}
And the result (the numbers are the "texture index" of the plane):
Tested on an Intel HD 4000 GPU and an nVidia GTS 250, same result.
As you can see 0 is OK, but 2.0 is sometimes 2 (green), sometimes 1 (blue), why?
If I pass that "i" to a sampler2D array, nothing shows up on the screen, because "i" is out of the array bounds.
The rendering is done using OpenGL 3.0, with a VAO, an ArrayBuffer and an ElementArrayBuffer.
This is how I setup the attrib pointers:
GL.EnableVertexAttribArray(0); //pos
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, sizeof(float) * 4, 0);
GL.EnableVertexAttribArray(1); //tetxure index
GL.VertexAttribPointer(1, 1, VertexAttribPointerType.Float, true, sizeof(float) * 4, sizeof(float) * 3);
I've tried using VertexAttribPointerType.Int and int attributes, Also I've tried setting the "FL" varaible in the vertex shader, with 0, the result is red which is good, but if I use 1 or 2 the result is again a mess.
So what do you think? It's a rounding problem? Or how should I render the geometry with multiple textures wihout rerendering the geometry with each texture?
I'm trying to write a shader which renders a multitextured geometry without rerendering the geometry with each texture
What are you trying to achieve, did you look into multitexturing ? you only render the geometry once with multiple textures. Explaining what are you trying to achieve is better than describing your solution \$\endgroup\$