简体   繁体   English

Android OpenGL ES 2.0 multible对象

[英]Android OpenGL ES 2.0 multible objects

We use Opengl ES 2.0 for android and are trying to display two Cubes stacked on each other. 我们将Opengl ES 2.0用于Android,并尝试显示彼此堆叠的两个多维数据集。 For that we have two vertexbuffers (mCubePositions1 and mCubePositions2) which store the Cubedata (the vertices combined to triangles) and call a seperate draw method for each of them: 为此,我们有两个顶点缓冲区(mCubePositions1和mCubePositions2),它们存储Cubedata(组合成三角形的顶点)并为它们中的每一个调用单独的绘制方法:

    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,0, mCubePositions1); //the second time mCubePositions2
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    //some Code concerning lightning and textures
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 36);

The result is two cubes are displayed, but if we let them rotate, the cube which is drawn second is always displayed on top (2nd Cube is shining through the 1st). 结果是显示了两个多维数据集,但是如果我们让它们旋转,则第二个绘制的多维数据集将始终显示在顶部(第二个多维数据集从第一个多维数据集开始闪烁)。 In the onSurfaceCreated method we initialise the depth-buffer: 在onSurfaceCreated方法中,我们初始化深度缓冲区:

    // Set the background clear color to black.
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    GLES20.glClearDepthf(1.0f);
    // Use culling to remove back faces.
    GLES20.glEnable(GLES20.GL_CULL_FACE);
    // Enable depth testing
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
    GLES20.glDepthMask(true);

There is a solution combining the two buffers into one and than calling just one draw-method, but this is not a solution for us, because we want to move the Cubes seperatly. 有一种解决方案将两个缓冲区组合为一个,而不是仅调用一个绘制方法,但这对我们来说不是解决方案,因为我们想单独移动多维数据集。

If this is not enougth code to answer please ask for more. 如果这不是答案,请要求更多。

Thank you for every answer :) 谢谢你的每一个回答:)

If you enable depth testing, and it still does not work, this typically means that you don't have a depth buffer. 如果启用深度测试,但仍然无法正常工作,则通常意味着您没有深度缓冲区。

When using a GLSurfaceView in Android, you request what buffers you need while the view is initialized, by calling the setEGLConfigChooser method. 在Android中使用GLSurfaceView时,您可以通过调用setEGLConfigChooser方法来请求在初始化视图时需要哪些缓冲区。 There are a few overloads for this method. 此方法有一些重载。 The most commonly used one takes a size (number of bits) for each buffer. 最常用的一个为每个缓冲区取一个大小(位数)。 A typical call will look like this: 一个典型的呼叫将如下所示:

setEGLConfigChooser(8, 8, 8, 0, 16, 0);

This means that you want 8 bits each for RGB, do not require alpha, want a 16-bit depth buffer, and do not need stencil. 这意味着您需要每个8位用于RGB,不需要alpha,需要16位深度缓冲区以及不需要模板。

Note that it is not guaranteed that you will get exactly the specified sizes, but the best possible match among the available configurations. 请注意,不能保证您将获得完全指定的大小,但是可以在可用配置中获得最佳匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM