安卓版OpenGL ES中用于2D游戏的java渲染图像
编辑:解决了!我犯了一个愚蠢的错误,我有一个我应该用的textureId时忘记的textureId
好的,我完全知道这是一个反复出现的问题,有很多教程和开源代码。但我已经尽我所能在这里尝试了很长一段时间,我的屏幕仍然是空白的(无论我使用glClearColor()设置了什么颜色)
所以,如果有人能指点一下我做错了什么,或者更好的是,一些能够呈现资源映像的工作代码,我将不胜感激
我将在实现渲染器的类的onDrawFrame中展示到目前为止(通过进行一些巧妙的复制粘贴)我得到了什么。我已经删除了一些方法之间的跳转,只需按执行顺序粘贴即可
请随意忽略我当前的代码,如果有人能给我一段代码,我非常乐意重新开始
设置:
bitmap = BitmapFactory.decodeResource(panel.getResources(),
R.drawable.test);
addGameComponent(new MeleeAttackComponent());
// Mapping coordinates for the vertices
float textureCoordinates[] = { 0.0f, 2.0f, //
2.0f, 2.0f, //
0.0f, 0.0f, //
2.0f, 0.0f, //
};
short[] indices = new short[] { 0, 1, 2, 1, 3, 2 };
float[] vertices = new float[] { -0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f };
setIndices(indices);
setVertices(vertices);
setTextureCoordinates(textureCoordinates);
protected void setVertices(float[] vertices) {
// a float is 4 bytes, therefore we multiply the number if
// vertices with 4.
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mVerticesBuffer = vbb.asFloatBuffer();
mVerticesBuffer.put(vertices);
mVerticesBuffer.position(0);
}
protected void setIndices(short[] indices) {
// short is 2 bytes, therefore we multiply the number if
// vertices with 2.
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
mIndicesBuffer = ibb.asShortBuffer();
mIndicesBuffer.put(indices);
mIndicesBuffer.position(0);
mNumOfIndices = indices.length;
}
protected void setTextureCoordinates(float[] textureCoords) {
// float is 4 bytes, therefore we multiply the number of
// vertices with 4.
ByteBuffer byteBuf = ByteBuffer
.allocateDirect(textureCoords.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mTextureBuffer = byteBuf.asFloatBuffer();
mTextureBuffer.put(textureCoords);
mTextureBuffer.position(0);
}
//onDrawFrame(GL10 gl)
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -4);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// Specifies the location and data format of an array of vertex
// coordinates to use when rendering.
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVerticesBuffer);
if(shoudlLoadTexture){
loadGLTextures(gl);
shoudlLoadTexture = false;
}
if (mTextureId != -1 && mTextureBuffer != null) {
gl.glEnable(GL10.GL_TEXTURE_2D);
// Enable the texture state
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Point to our buffers
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
}
gl.glTranslatef(posX, posY, 0);
// Point out the where the color buffer is.
gl.glDrawElements(GL10.GL_TRIANGLES, mNumOfIndices,
GL10.GL_UNSIGNED_SHORT, mIndicesBuffer);
// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
if (mTextureId != -1 && mTextureBuffer != null) {
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
private void loadGLTextures(GL10 gl) {
int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);
mTextureID = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
}
它不会崩溃,没有例外,只是一个彩色的空白屏幕。我已经在里面印了东西,所以我很确定它都被执行了
我知道仅仅粘贴代码并不是最佳选择,但目前,我只想做我在canvas上能做的事情:)
非常感谢
# 1 楼答案
如果你得到了背景色,这意味着你的窗口设置正确。OpenGL连接到屏幕的该区域
但是,OpenGL将剪辑到近剪裁平面和远剪裁平面,以确保对象不会与摄影机相交(这在数学和逻辑上都没有意义),并且不会显示太远的对象。因此,如果您没有正确设置modelview和projection,很可能所有几何体都被剪裁了
Modelview用于从世界映射到眼睛空间。从眼睛空间到屏幕空间的投影图。因此,典型的应用程序使用前者来定位场景中的对象,并相对于相机定位场景,然后后者处理相机是否以透视方式观看,多少世界单位构成多少屏幕单位,等等
如果你看例子like this one,尤其是
onSurfaceChanged
,你会看到一个相机固定在原点的透视投影例子因为相机位于(0,0,0),所以将几何体保持在z=0,就像代码那样,会导致它被剪裁。在该示例代码中,他们已将“近剪裁平面”设置为z=0.1,因此在现有代码中可以更改:
致:
将几何体向后推足够远,以显示在屏幕上