开发者

OpenGL/GLSL - Writing A Smart Texture Shader

Let's say my texture is 256x256 pixel. It contains 16 sub-textures of 64x64 pixel. The sub-textures are to be rendered to quads. To increase performance I want to pass discrete values to the shader which encode per vertex texture coordinates. For example values 0, 1, 2, 3 should correspond to (0,64), (64,64), (64,0), (0,0) per vertex texture coordinates, etc.

Here is what I have so far.

I load and bind a mipmap texture:

glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D,3,TextureImage[0]->w,TextureImage[0]->h,GL_RGB,GL_UNSIGNED_BYTE,TextureImage[0]->pixels);

Then I load my data:

glGenVertexArrays(1, &VecArrObj);
glBindVertexArray(VecArrObj);

glGenBuffers(1, &VertexBO);
glBindBuffer(GL_ARRAY_BUFFER, VertexBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertAndTex), VertAndTex, GL_STREAM_DRAW);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
开发者_Python百科glVertexAttribPointer(0, 3, GL_SHORT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 1, GL_SHORT, GL_FALSE, 0, (void*)TexOffset);

glGenBuffers(1, &IndexBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indx), Indx, GL_STREAM_DRAW);

Here the attributes are previously properly assigned to myProgram using glBindAttribLocation. This is how I display my quads:

glUseProgram(myProgram);
glBindVertexArray(VecArrObj);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0);
glUseProgram(0);

Now that the mipmap texture is constantly bound, it should be accessible from the shaders. Until now my shaders look the following way.

Vertex shader:

#version 130

in vec4 posit;  //attr=0
in short textu; //attr=1
uniform mat4 trafoMat;

void main()
{
gl_Position = trafoMat * posit;
gl_TexCoord[0] = gl_MultiTexCoord0;
}

Fragment shader:

#version 130

uniform sampler2D tex;
out vec4 outColor;

void main()
{
outColor = texture2D(tex,gl_TexCoord[0].st);
}

Where tex is set to zero. These shaders don't even acquire the whole texture but only set the whole quads to one color from the texture (I think it is the upper left corner pixel). Obviously I do not have much experience with shaders. Basically, my question is: How should I modify my shaders for the purposes described in the beginning? (I am capable to do the math for per vertex texture coordinates - what I primarily require is a hint how to handle textures in shaders. Please note: my hardware does not support glGenSamplers() ).


First of all, unless you are using a simple mapping (i.e. f(x) -> (u,v) with f being some simple function), you should create an indirection texture which performs the mapping. In your case, it would be a 1D texture and have 16 (two-component) entries.

Second, you need to point-sample into the indirection texture to find out where to sample from the actual texture. Assuming your quads have 0..1 UV coordinates, the resulting shader will look something like this:

// gl_TexCoord[0].st is 0..1, per quad
void main ()
{
    gl_Position = trafoMat * posit;
    // 0..1 UV coordinate
    gl_TexCoord[0] = gl_MultiTexCoord0;
    // Rescale into sub-tile
    gl_TexCoord[0] *= (1/4.0);
    // Add the offset, if your hardware does not support
    // texture fetches in the vertex shader, move this into the
    // fragment shader.
    // gl_MultiTexCoord1 is the 1D offset into the indirection
    // texture
    gl_TexCoord[0] += texture2D(offsetTexture,gl_MultiTexCoord1);
}

// This is the fragment shader!
void main ()
{
    outColor = texture2D(tex,gl_TexCoord[0]);
}

The tex texture should be most likely linearly filtered, and the offsetTexture needs point -- you can set them manually without using samplers by using glTexParameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜