在决定尝试现代OpenGL编程后,我放弃了固定功能管道,但不确定是否能获得之前相同的功能。
我正在尝试纹理映射大小完美的四边形,与纹理大小匹配。例如,128x128的纹理映射到大小为128x128的四边形。
这是我的顶点着色器。
我正在尝试纹理映射大小完美的四边形,与纹理大小匹配。例如,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统一中进行翻译和缩放的建议吗?
OpenGL
标签时,指定你的 OpenGL 版本会很有帮助。特别是因为你的代码示例听起来像 OpenGL 2.0,这确实很有用,但现在并不是真正的“现代”。 :) - Kos