在决定尝试在现代 OpenGL 中进行编程后,我已经放弃了固定功能管道,并且我不完全确定能否获得与以前相同的功能。
我正在尝试用像素完美大小来纹理贴图四边形,匹配纹理大小。例如,一个 128x128 的纹理映射到一个大小为 128x128 的四边形。
这是我的顶点着色器。
#version 110
uniform float xpos;
uniform float ypos;
uniform float tw; // texture width in pixels
uniform float th; // texture height in pixels
attribute vec4 position;
varying vec2 texcoord;
void main()
{
mat4 projectionMatrix = mat4( 2.0/600.0, 0.0, 0.0, -1.0,
0.0, 2.0/800.0, 0.0, -1.0,
0.0, 0.0, -1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
gl_Position = position * projectionMatrix;
texcoord = (gl_Position.xy);
}
这是我的片段着色器:
#version 110
uniform float fade_factor;
uniform sampler2D textures[1];
varying vec2 texcoord;
void main()
{
gl_FragColor = texture2D(textures[0], texcoord);
}
我的顶点数据就是这样,其中 w 和 h 是纹理的宽度和高度。
[
0, 0,
w, 0,
w, h,
0, h
]
我加载了 128x128 纹理,使用这些着色器,我看到图像重复了 4 次:http: //i.stack.imgur.com/UY7Ts.jpg
鉴于 tw th、xpos、xpos 制服,任何人都可以就正确的翻译和缩放方式提供建议吗?