Hello,
The problem is as follows:
I am trying to store vertex positions into textures in OpenGL and pass them into GLSL and is succeding quite well appart from some vertices are missing or missplaced :S. I think the problem lies in the generations of the texture, when one applies glTexParameters to the texture such as GL_NEAREST or GL_REPEAT. I have checked with normal gl_Vertex and they work fine… so the vertices position is correct.
I have stored vertices position and normals in textures in OpenGL like this:
Code:
glGenTextures(2, textures);
float *surfels = new float[ o.vertexList.size()*3];
int k = 0;
for(int i= 0; i < (const int) o.vertexList.size(); i++)
{
surfels[k] = o.vertexList.at(i).x;
surfels[k+1] =o.vertexList.at(i).y;
surfels[k+2] = o.vertexList.at(i).z;
k = k+3;
}
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, ceil(sqrt((float)o.vertexList.size())),ceil(sqrt((float)o.vertexList.size())), 0, GL_RGB, GL_FLOAT, surfels);
The relevant parts of the vertex shader:
Code:
uniform sampler2D positionMap;
void main(void)
{
st = gl_MultiTexCoord0.xy;
vec4 v = vec4(texture2D(positionMap,st).xyz,1.0);
gl_Position = gl_ModelViewProjectionMatrix*v;
}
The problem is that almost every point is nicely placed in openGL but some of the vertices position are lost or placed at another place. Can it have soemthing to do with using GL_NEAREST in the glTexParametri.
Maybe there is a better way to store vertices to a texture and pass it to a fragmentshader ? FBO:s? but won’t I run in to the same problem?
it seems that the problem is not the data it self because I’ve checked that. However the problem is that if I use GL_NEIREST it will copy the value next to during mip_mapping, but I only want the specific value on the specific pixel.
I’ve read that the problem also could be the size of the image…should be 2^n +2. Since I now take the square root of the entire image to use as the width and height and then ceil to get more pixels than need which should be ok I think.
So the question is: How do I store vertex positions in a texture, in a good way?
I have googled it a many times but can’t find anyone that has a good answer to this specific question.
Thank you for taking your time.