使用 OpenGL 绘制像素缓冲区 Android
Draw pixels buffers with OpenGL Android
我有一个字节缓冲区数组
static IntBuffer byteBuffer = BufferUtils.createIntBuffer(8300000);
我在上面放了很多像素,如下所示:
byteBuffer.put(256) // ALPHA
byteBuffer.put(150) // RED
byteBuffer.put(152) // GREEN
byteBuffer.put(23) // BLUE
byteBuffer.put(152) // ALPHA
... etc etc
在实现 GLSurfaceView.Renderer
的 Render class 中,我覆盖了这 3 个方法:
public void onDrawFrame(GL10 gl) {
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, 1920, 1080, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, SocketActivity.byteBuffer);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
init(width, height);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
我想在 1920x1080 分辨率下渲染大纹理,所以我分配了 8300000 (1920*1080*4)。
但是什么都不显示
已解决!这是给任何想做的人的命令。
在init
函数中:
glViewport(GL_ZERO, GL_ZERO, width, height);
glGenTextures(1, &idTexture);
glBindTexture(GL_TEXTURE_2D, idTexture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, idTexture);
glUseProgram(gProgram);
在draw
函数中:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, GL_ZERO, GL_RGB, width, height, GL_ZERO, GL_RGB, GL_UNSIGNED_BYTE, buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat), vertices);
glVertexAttribPointer(inputTextureCoordinate, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat), vVertices);
glEnableVertexAttribArray(gvPositionHandle);
glEnableVertexAttribArray(inputTextureCoordinate);
glUniform1i(baseMapLoc, GL_ZERO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
你明白了。非常感谢所有启发我的人。
我有一个字节缓冲区数组
static IntBuffer byteBuffer = BufferUtils.createIntBuffer(8300000);
我在上面放了很多像素,如下所示:
byteBuffer.put(256) // ALPHA
byteBuffer.put(150) // RED
byteBuffer.put(152) // GREEN
byteBuffer.put(23) // BLUE
byteBuffer.put(152) // ALPHA
... etc etc
在实现 GLSurfaceView.Renderer
的 Render class 中,我覆盖了这 3 个方法:
public void onDrawFrame(GL10 gl) {
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, 1920, 1080, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, SocketActivity.byteBuffer);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
init(width, height);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
我想在 1920x1080 分辨率下渲染大纹理,所以我分配了 8300000 (1920*1080*4)。
但是什么都不显示
已解决!这是给任何想做的人的命令。
在init
函数中:
glViewport(GL_ZERO, GL_ZERO, width, height);
glGenTextures(1, &idTexture);
glBindTexture(GL_TEXTURE_2D, idTexture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, idTexture);
glUseProgram(gProgram);
在draw
函数中:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, GL_ZERO, GL_RGB, width, height, GL_ZERO, GL_RGB, GL_UNSIGNED_BYTE, buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat), vertices);
glVertexAttribPointer(inputTextureCoordinate, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat), vVertices);
glEnableVertexAttribArray(gvPositionHandle);
glEnableVertexAttribArray(inputTextureCoordinate);
glUniform1i(baseMapLoc, GL_ZERO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
你明白了。非常感谢所有启发我的人。