GLSL 偏移多重纹理

GLSL offsetting multitexture

如何在 GLSL 着色器中执行类似的操作?

vec2 attribute texture_sky;
vec2 attribute texture_floor;

if(texture_sky) gl_position = position+Xoffset;
else gl_position = position; 

我想将一个纹理移动到另一个纹理之上。可以使用顶点着色器吗?

What I type up there is a pseudo code: not the actual code. More explanation; Let's say i have two textures(2 images bind as textures) overlap each other. I want to display one texture with X+0.5 displacement while the other remain constant. The problem I am facing is distinguishing two textures in the Shader code.

这不是您单独使用顶点着色器可以做到的事情。

您可以在顶点着色器中对纹理坐标应用偏移量,但您肯定不会更改顶点位置。多重纹理的整个想法是一次应用多个纹理,避免绘制两个不同的多边形副本。

在硬件能够在单通道中对多个纹理进行采样之前 (Riva TNT),您实际上必须多次绘制每个多边形并混合结果才能应用多个纹理。现在你只需要使用片段着色器就可以了,因为 OpenGL 3.0 要求所有硬件至少支持 16 个同步纹理。

非常粗略地说,伪代码如下所示:

顶点着色器:

#version 110

// Input texture coordinates (from vertex buffer)
attribute vec2 texture_sky;
attribute vec2 texture_floor;

// Output texture coordinates (to fragment shader)
varying vec2 sky_tc;
varying vec2 floor_tc;

// Your offset
uniform vec2 Xoffset;

void main (void) {
  sky_tc   = texture_sky + Xoffset;
  floor_tc = texture_floor;

  gl_Position = ...;
}

片段着色器

#version 110

// Input texture coordinates (from vertex shader)
varying vec2 sky_tc;
varying vec2 floor_tc;

// Samplers
uniform sampler2D sky_tex;
uniform sampler2D floor_tex;

void main (void) {
  vec4 sky   = texture2D (sky_tex,   sky_tc);
  vec4 floor = texture2D (floor_tex, floor_tc);

  //
  // You have to blend these, so pick one of the following...
  //

  // Additive Blending (GL_ONE, GL_ONE):
  gl_FragColor = sky + floor;

  // or

  // Alpha Blending (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA):
  gl_FragColor = mix (sky, floor, sky.a);
}