OpenGL/GLSL - 编写智能纹理着色器

发布于 2024-12-08 14:16:12 字数 2044 浏览 0 评论 0原文

假设我的纹理是 256x256 像素。它包含 16 个 64x64 像素的子纹理。子纹理将渲染为四边形。为了提高性能,我想将离散值传递给着色器,该着色器对每个顶点纹理坐标进行编码。例如,值 0、1、2、3 应对应于每个顶点纹理坐标的 (0,64)、(64,64)、(64,0)、(0,0) 等。

这是我到目前为止所拥有的。

我加载并绑定 mipmap 纹理:

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);

然后加载我的数据:

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);
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);

这里的属性先前已使用 glBindAttribLocation 正确分配给 myProgram。 这就是我显示四边形的方式:

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

现在 mipmap 纹理不断绑定,应该可以从着色器访问它。到目前为止,我的着色器看起来如下。

顶点着色器:

#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;
}

片段着色器:

#version 130

uniform sampler2D tex;
out vec4 outColor;

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

其中 tex 设置为零。这些着色器甚至不获取整个纹理,而仅将整个四边形设置为纹理中的一种颜色(我认为它是左上角像素)。显然我对着色器没有太多经验。基本上,我的问题是:我应该如何修改我的着色器以达到开头描述的目的? (我能够对每个顶点纹理坐标进行数学计算 - 我主要需要的是如何在着色器中处理纹理的提示。请注意:我的硬件不支持 glGenSamplers() )。

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);
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() ).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花伊自在美 2024-12-15 14:16:12

首先,除非您使用简单的映射(即 f(x) -> (u,v),其中 f 是一些简单的函数),否则您应该创建一个执行映射的间接纹理。在您的情况下,它将是 1D 纹理并具有 16 个(两分量)条目。

其次,您需要对间接纹理进行点采样,以找出从实际纹理中采样的位置。假设您的四边形有 0..1 UV 坐标,生成的着色器将如下所示:

// 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]);
}

tex 纹理很可能是线性过滤的,offsetTexture 需要点 - - 您可以使用 glTexParameter

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文