Вот код, который реализует вывод текстуры на экран, но вместо нее выводит черный прямоугольник.
static ShaderProgram shaderProgram;
private FloatBuffer vertexBuffer;
private FloatBuffer textureBuffer;
private int textures[] = new int[1];
private float vertices[] = {
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f
};
private float texture[] = {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f
};
public Sprite(String path) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuffer.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(core.getAssets().open(path));
} catch (IOException e) { }
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
textureBuffer = byteBuffer.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}
public void draw() {
shaderProgram.use();
GLES20.glEnable(GL10.GL_TEXTURE_2D);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
int positionAttrib = shaderProgram.getAttribLocation("vertices");
int textureUniform = shaderProgram.getUniformLocation("tex");
int textureAttrib = shaderProgram.getAttribLocation("texCoord");
GLES20.glEnableVertexAttribArray(positionAttrib);
GLES20.glEnableVertexAttribArray(textureAttrib);
GLES20.glUniform1i(textureUniform, 0);
GLES20.glVertexAttribPointer(positionAttrib, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
GLES20.glVertexAttribPointer(textureAttrib, 2, GLES20.GL_FLOAT, false, 0, textureBuffer);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
GLES20.glDisableVertexAttribArray(positionAttrib);
GLES20.glDisableVertexAttribArray(textureAttrib);
}
Vertex shader
#version 150
in vec3 vertices;
in vec2 texCoord;
out vec2 fTexCoord;
void main() {
fTexCoord = texCoord;
gl_Position = vec4(vertices, 1);
}
Fragment shader
#version 150
uniform sampler2D tex;
in vec2 fTexCoord;
out vec4 finalColor;
void main() {
finalColor = texture(tex, fTexCoord);
}
glGetError
и проверку его значения. Место очередной вставки определяйте методом деления пополам. Так вы найдёте проблемную строку. – Arhad-the-dev 27 июн '16 в 19:45