Question

I'm studying clipmap algorithm, and I want to get elevations by VTF. But I've got a problem when using vertex textures. I don't know what's wrong. the related code is like this:

int width=127;

float *data=new float[width*width];
for(int i=0;i<width*width;i++)
data[i]=float(rand()%100)/100.0;
glGenTextures(1, &vertexTexture);   
//glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, vertexTexture);

//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST_MIPMAP_NEAREST);
glTexImage2D(
        GL_TEXTURE_2D, 0, GL_LUMINANCE_FLOAT32_ATI, 
        width, width, 0, GL_LUMINANCE, GL_FLOAT, data);

the GLSL code in vertex shader is like this:

#version 410
uniform mat4    mvpMatrix;
uniform sampler2D vertexTexture;
in vec2 vVertex;

void main(void) 
{ 
    vec2 vTexCoords=vVertex/127.0; 
    float height = texture2DLod(vertexTexture, vTexCoords,0.0).x*100.0;
    // I also tried texture2D(vertexTexture, vTexCoords) 
    // and texture(vertexTexture, vTexCoords),but they don't work.
    vec4 position=vec4(vVertex.x,height,vVertex.y,1.0);
    gl_Position = mvpMatrix * position;
}

I store some random floats in array data then store them with a texture,and as the vertex shader showing,I want to get some values to the y coordinate by VTF.but the result is that the height is always 0.I think something must be wrong. I don't know what's wrong and how to do it the right way.

Was it helpful?

Solution

it's solved now.the answer is here.thank you all!

Try setting the texture's minification filter to GL_NEAREST or GL_LINEAR after glTexImage2D():

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

The OpenGL default is to use mipmaps and you didn't send any which makes the texture incomplete and will disable that texture image unit.

Then you can use texture(vertexTexture, vTexCoords) inside the shader instead of the deprecated texture2DLOD() version with the explicit LOD access.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top