1)不,不是。如果您只想放大和缩小图像(不使用缩放库),那么渲染到另一个纹理将是浪费时间。您可以直接放大视图。
2)着色器是能够计算和转换坐标(通常在顶点着色器中完成)并且能够使用这些坐标从纹理中读取的程序(当时只能在片段着色器中完成)。
我相信你的着色器可能看起来像这样:
precision mediump float;
uniform matrix4 modelviewProjectionMatrix; // input transformation matrix
attribute vec3 position;
attribute vec2 texcoord; // input coordinates of each vertex
varying vec2 _texcoord; // output coordinate for fragment shader
void main()
{
gl_Position = modelviewProjectionMatrix * vec4(position, 1.0); // transform position
_texcoord = texcoord; // copy texcoord
}
那是顶点着色器,现在是片段着色器:
precision mediump float;
varying vec2 _texcoord; // input coordinate from vertex shader
uniform sampler2D _sampler; // handle of a texturing unit (e.g. 0 for GL_TEXTURE0)
void main()
{
gl_FragColor = glTexture2D(_sampler, _texcoord); // read texture
}
您可以做的是在顶点着色器中包含一个缩放参数(所谓的统一):
precision mediump float;
uniform float zoom; // zoom
uniform matrix4 modelviewProjectionMatrix; // input transformation matrix
attribute vec3 position;
attribute vec2 texcoord; // input coordinates of each vertex
varying vec2 _texcoord; // output coordinate for fragment shader
void main()
{
gl_Position = modelviewProjectionMatrix * vec4(position, 1.0); // transform position
_texcoord = (texcoord - .5) * zoom + .5; // zoom texcoord
}
片段着色器没有变化。这只是为它计算不同的坐标,就是这么简单。要设置缩放,您需要:
int zoomLocation = glGetUniformLocation(yourProgram, "zoom");
// this is done in init, where you call glGetUniformLocation(yourProgram, "modelviewProjectionMatrix")
// also, zoomLocation is not local variable, it needs to be stored somewhere to be used later when drawing
glUniform1f(zoomLocation, 1 + .5f * (float)Math.sin(System.nanotime() * 1e-8f));
// this will set zoom that will animatically zoom in and out of the texture (must be called after glUseProgram(yourProgram))
现在这将导致两个纹理都放大和缩小。要解决此问题(我假设您只想缩放正确的纹理),您需要:
// draw the second quad
glUniform1f(zoomLocation, 1);
// this will set no zoom (1:1 scale)
// draw the first quad
我希望这有帮助 ...