[EDIT]
Ich habe gerade gemerkt dass in der Breite alle Pixel gezeigt werden,
in der Höhe aber nur das erste Pixel.
Woran kann das liegen ?
Java
public class DNESprite {
private static final int SCALE = 0;
private static final int NO_SCALE = 1;
private final float VERTEXCOORDS[] = {-1f, -1f,
-1f, 1f,
1f, 1f,
1f, -1f};
private final short INDEXORDER[] = {0, 1, 2, 0, 2, 3};
private final float TEXTURECOORDS[] = {0f, 1f,
0f, 0f,
1f, 0f,
1f, 1f};
private final String VERTEXSHADERCODE =
"uniform mat4 vertexPositionMatrix;"
+ "uniform mat2 texturePositionMatrix;"
+ "attribute vec4 vertexPosition;"
+ "attribute vec2 texturePosition;"
+ "varying vec2 texture_Position;"
+ "void main() {"
+ "gl_Position = vertexPositionMatrix * vertexPosition;"
+ "texture_Position = texturePositionMatrix * texturePosition;"
+ "}";
private final String FRAGMENTSHADERCODE =
"precision mediump float;"
+ "uniform sampler2D texture;"
+ "varying vec2 texture_Position;"
+ "void main() {"
+ "gl_FragColor = texture2D(texture, texture_Position);"
+ "}";
private FloatBuffer vertexBuffer;
private ShortBuffer indexBuffer;
private FloatBuffer textureBuffer;
private int vertexShader;
private int fragmentShader;
private int program;
private int vertexPositionHandle;
private int vertexPositionMatrixHandle;
private int texturePositionHandle;
private int texturePositionMatrixHandle;
private float[] objectMatrix;
private float[] textureMatrix;
private int[] textureID;
private Bitmap spritesheet;
private int posX;
private int posY;
private int posZ;
private int scaleMode;
private float scaleX;
private float scaleY;
private float rotX;
private float rotY;
private float rotZ;
public DNESprite(Bitmap sheet) {
spritesheet = sheet;
scaleMode = DNESprite.SCALE;
scaleX = 0.5f;
scaleY = 0.5f;
// setup buffers
vertexBuffer = ByteBuffer.allocateDirect(VERTEXCOORDS.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertexBuffer.put(VERTEXCOORDS);
vertexBuffer.position(0);
indexBuffer = ByteBuffer.allocateDirect(INDEXORDER.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
indexBuffer.put(INDEXORDER);
indexBuffer.position(0);
textureBuffer = ByteBuffer.allocateDirect(TEXTURECOORDS.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
textureBuffer.put(TEXTURECOORDS);
textureBuffer.position(0);
// setup shaders
vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
GLES20.glShaderSource(vertexShader, VERTEXSHADERCODE);
GLES20.glCompileShader(vertexShader);
fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(fragmentShader, FRAGMENTSHADERCODE);
GLES20.glCompileShader(fragmentShader);
// setup program
program = GLES20.glCreateProgram();
GLES20.glAttachShader(program, vertexShader);
GLES20.glAttachShader(program, fragmentShader);
GLES20.glLinkProgram(program);
// setup texture
textureID = new int[1];
GLES20.glGenTextures(1, textureID, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, spritesheet, 0);
spritesheet.recycle();
objectMatrix = new float[16];
textureMatrix = new float[16];
}
public void render() {
GLES20.glUseProgram(program);
// get object and texture matrix
Matrix.setIdentityM(objectMatrix, 0);
Matrix.setIdentityM(textureMatrix, 0);
// get vertex and texture position
vertexPositionHandle = GLES20.glGetAttribLocation(program, "vertexPosition");
texturePositionHandle = GLES20.glGetAttribLocation(program, "texturePosition");
GLES20.glEnableVertexAttribArray(vertexPositionHandle);
GLES20.glEnableVertexAttribArray(texturePositionHandle);
GLES20.glVertexAttribPointer(vertexPositionHandle, 2, GLES20.GL_FLOAT, false, 8, vertexBuffer);
GLES20.glVertexAttribPointer(texturePositionHandle, 2, GLES20.GL_FLOAT, false, 8, textureBuffer);
// scale object matrix
Matrix.scaleM(objectMatrix, 0, scaleX, scaleY, 1.0f);
// position object matrix
Matrix.translateM(objectMatrix, 0, 0.0f, 0.0f, 0.0f);
// rotate object matrix
Matrix.rotateM(objectMatrix, 0, 0f, 1.0f, 1.0f, 1.0f);
// get and change vertex position matrix
vertexPositionMatrixHandle = GLES20.glGetUniformLocation(program, "vertexPositionMatrix");
GLES20.glUniformMatrix4fv(vertexPositionMatrixHandle, 1, false, objectMatrix, 0);
// scale texture matrix
Matrix.scaleM(textureMatrix, 0, 1.0f, 1.0f, 1.0f);
// position texture matrix
Matrix.translateM(textureMatrix, 0, 0.0f, 0.0f, 0.0f);
// get and change texture matrix
texturePositionMatrixHandle = GLES20.glGetUniformLocation(program, "texturePositionMatrix");
GLES20.glUniformMatrix2fv(texturePositionMatrixHandle, 1, false, textureMatrix, 0);
// draw vertex
GLES20.glDrawElements(GLES20.GL_TRIANGLES, INDEXORDER.length, GLES20.GL_UNSIGNED_SHORT, indexBuffer);
GLES20.glDisableVertexAttribArray(vertexPositionHandle);
GLES20.glDisableVertexAttribArray(vertexPositionMatrixHandle);
}
}
Alles anzeigen