我正在用OpenGL ES 2.0構建Java(Android)中的精靈(具有紋理的矩形)類。OpenGL ES 2.0矢量翻譯不能通過矩陣乘法工作(在頂點着色器中)
我的目標是隻使用紋理的一部分,因此我將一個矩陣傳遞給頂點着色器。在下面的例子中,我想只看到紋理的右半部分(但我seing左一半),規模在整個精靈寬度:
/ 0.5, 0, 0, 0.5 \ /x \ /0.5 * x + 0.5 \
| 0 , 1, 0, 0 | \/ | y | --- | y |
| 0 , 0, 1, 0 | /\ | 1 | --- | 1 |
\ 0 , 0, 0, 1 / \ 1/ \ 1 /
如果我把我的紋理座標,它工作得很好(在紙上):
(0,1) -> (0.5,1) // bottom left
(1,1) -> (1, 1) // bottom right
(0,0) -> (0.5,0) // top left
(1,0) -> (1, 0) // top right
但在現實中,我得到這些結果:
(0,1) -> (0 ,1)
(1,1) -> (0.5,1)
(0,0) -> (0 ,0)
(1,0) -> (0.5,0)
這意味着縮放部作品,但翻譯部分沒有。
代碼:
的Vertex Shader:
attribute vec4 position;
attribute vec2 texturePosition;
uniform mat4 mvpMatrix;
uniform mat4 textureMatrix;
varying vec2 iTexturePosition;
void main() {
gl_Position = mvpMatrix * position;
vec4 tp = vec4(texturePosition.x, texturePosition.y, 1, 1);
tp = textureMatrix * tp;
//tp[0] += 0.5;
iTexturePosition = tp.xy;
}
片段着色器:
precision mediump float;
uniform sampler2D textureUnit;
varying vec2 iTexturePosition;
void main() {
vec4 color = texture2D(textureUnit, iTexturePosition);
if (color.a <= 0.25) {
discard;
}
if (iTexturePosition[0] <= 0.4) {
//discard;
}
gl_FragColor = color;
}
Java端:
private static int textureMatrixHandle = -1;
private static final String textureMatrixName = "textureMatrix";
private float[] textureMatrix = new float[16];
// more code ...
@Override
public void draw { // only relevant lines
textureMatrixHandle = GLES20.glGetUniformLocation(glProgram, textureMatrixName);
Matrix.setIdentityM(textureMatrix, 0);
setTextureSection(textureMatrix, 0, 0.5f, 1, 1, 0);
GLES20.glUniformMatrix4fv(textureMatrixHandle, 1, false, textureMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, VERTEX_COUNT);
}
protected void translateTextureSection(float[] textureMatrix, float x, float y) {
Matrix.multiplyMM(textureMatrix, 0, new float[] {
1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, 0,
0, 0, 0, 1,
}, 0, textureMatrix, 0);
}
protected void scaleTextureSection(float[] textureMatrix, float x, float y) {
Matrix.multiplyMM(textureMatrix, 0, new float[] {
x, 0, 0, 0,
0, y, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
}, 0, textureMatrix, 0);
}
protected void rotateTextureSection(float[] textureMatrix, float angle) {
angle = (float) Math.toRadians(angle);
float sin = (float) Math.sin(angle);
float cos = (float) Math.cos(angle);
Matrix.multiplyMM(textureMatrix, 0, new float[] {
cos, -sin, 0, 0,
sin, cos, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
}, 0, textureMatrix, 0);
}
protected void setTextureSection(float[] textureMatrix, float rotationAngle, float left, float right, float bottom, float top) {
rotateTextureSection(textureMatrix, rotationAngle);
translateTextureSection(textureMatrix, left, top);
scaleTextureSection(textureMatrix, right - left, bottom - top);
}
當你調用glUniformMatrixf4v(或不管它是什麼)時,你是否傳遞真或假的轉置標誌? – samgak
我錯過了。如我所料,縮放發生在x座標上(我正在拼接紋理的全高和寬度的一半,它只是寬度的錯誤的一半) – EvilDevil
@samgak我正在像這樣設置制服:GLES20.glUniformMatrix4fv( textureMatrixHandle,1,false,textureMatrix,0); – EvilDevil