2011-03-22 74 views
0

编辑:解决它!我犯了一个愚蠢的错误,我有一个纹理,我忘记了它应该使用的textureID。在OpenGL ES中为2D游戏渲染图像android

好吧,我充分意识到,这是一个反复出现的问题,并且有大量的教程和开放源代码。但是我一直在尽可能好地尝试一段时间,而我的屏幕仍然是空白的(使用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); 

} 

它不会崩溃,没有例外,只是一个空白屏幕颜色。我在那里印了东西,所以我很确定它全部被执行。

我知道这是不是最佳的,只是贴上代码,但在那一刻,我只是想能够做什么,我能够用帆布:)做

非常感谢

+0

即时问题:你是否将模型视图和项目堆栈初始化到某处不显示? – Tommy 2011-03-22 18:00:49

+0

可能是这样,因为我不确定你会怎么做,我想我会展示大部分代码。它确实运行并给了我背景颜色,所以我正在做正确的事情。你能详细说明你的意思吗? (对不起,我缺乏理解,这是一个可怕的未知领域:S) – 2011-03-22 18:17:52

+0

我需要发布一个答案才能获得足够的空间,即使它不一定是答案。 – Tommy 2011-03-23 00:29:29

回答

1

如果您'获取背景颜色,这意味着您的窗口已正确设置。 OpenGL连接到屏幕的那个区域。但是,OpenGL会剪切到近端和远端剪辑平面,以确保对象不会与摄像机交叉或相交(在数学和逻辑上都不合理),而且距离太远的对象不会出现。因此,如果您没有正确设置模型视图和投影,很可能所有的几何图形都将被剪裁。

模型变换用于映射从世界到眼睛空间。投影从眼睛空间映射到屏幕空间。因此,典型的应用程序使用前者在场景内定位对象,并将场景相对于相机进行定位,然后后者处理相机是否有视角看,有多少个世界单元制作多少个屏幕单元等。

如果你看一下例子like this one,特别onSurfaceChanged,你会看到固定在原点的相机的透视投影的一个例子。由于摄像机位于(0,0,0)处,因此在z = 0时将几何图形留在z = 0上会使其被剪裁。在该示例代码,他们已经成立近剪裁平面是在z = 0.1,所以在现有的代码,你可以改变:

gl.glTranslatef(posX, posY, 0); 

要:

gl.glTranslatef(posX, posY, -1.0); 

把你的几何图形返回足够远远地出现在屏幕上。

+0

谢谢,但它没有改变任何东西:S你可以看到,在顶部那里,我设置glTranslatef(0,0,-4),并据我了解的教程,应该已经做到了这一招 – 2011-03-23 10:35:20

+0

您的代码仍然不会显示您设置模型视图和投影的位置。我真的认为这就是你绊倒的地方,如果没有看到那些东西,就很难提供更具体的建议。 – Tommy 2011-03-23 12:09:11