GL壁纸示例在模拟器中显示为绿屏,但在设备中工作正常。

12

运行OpenGL应用程序需要特殊的模拟器设置吗?

我已经将“GPU仿真”属性设置为“是”。

我正在尝试运行一个Android示例动态壁纸,使用从此链接找到的示例源代码,期望的输出是旋转的三角形。

经过一番努力,我让应用程序运行起来了,但在模拟器中它不绘制任何东西,在设备上测试时可以工作。但在模拟器中仍然只显示绿色屏幕,我在这里发现了有关它的讨论。我尝试按照它所说的设置视口,但仍然没有显示任何结果,对于surface changed事件,我添加了这行代码:

gl.glViewport(0, 0, width, height);

这是否是设置视口的正确方式?

以下是我的渲染类,

 public class MyRenderer implements GLWallpaperService.Renderer {
    GLTriangle mTriangle;

    public void onDrawFrame(GL10 gl) {


        gl.glClearColor(0.2f, 0.4f, 0.2f, 1f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        autoRotate(gl);
        gl.glColor4f(.2f, 0f, .5f, 1f);

        mTriangle.draw(gl);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {

        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU.gluPerspective(gl, 60f, (float)width/(float)height, 1f, 100f);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, -5);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        mTriangle = new GLTriangle();



        gl.glClearDepthf(1f);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glDepthFunc(GL10.GL_LEQUAL);
    }

    /**
     * Called when the engine is destroyed. Do any necessary clean up because
     * at this point your renderer instance is now done for.
     */
    public void release() {

    }

    private void autoRotate(GL10 gl) {
        gl.glRotatef(1, 0, 1, 0);
        gl.glRotatef(0.5f, 1, 0, 0);
    }
}

这里是GLTriangle类

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

public class GLTriangle {
    private FloatBuffer _vertexBuffer;
    private final int _nrOfVertices = 3;

    private ShortBuffer _indexBuffer;

    public GLTriangle() {
        init();
    }

    private void init() {
        // We use ByteBuffer.allocateDirect() to get memory outside of
        // the normal, garbage collected heap. I think this is done
        // because the buffer is subject to native I/O.
        // See http://download.oracle.com/javase/1.4.2/docs/api/java/nio/ByteBuffer.html#direct

        // 3 is the number of coordinates to each vertex.
        _vertexBuffer = BufferFactory.createFloatBuffer(_nrOfVertices * 3);

        _indexBuffer = BufferFactory.createShortBuffer(_nrOfVertices);

        // Coordinates for the vertexes of the triangle.
        float[] coords = {
                -1f, -1f,  0f,  // (x1, y1, z1)
                 1f, -1f,  0f,  // (x2, y2, z2)
                 0f,  1f,  0f   // (x3, y3, z3)
        };

        short[] _indicesArray = {0, 1, 2};

        _vertexBuffer.put(coords);
        _indexBuffer.put(_indicesArray);

        _vertexBuffer.position(0);
        _indexBuffer.position(0);
    }

    public void draw(GL10 gl) {
        // 3 coordinates in each vertex
        // 0 is the space between each vertex. They are densely packed
        //   in the array, so the value is 0
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, getVertexBuffer());

        // Draw the primitives, in this case, triangles.
        gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
    }

    private FloatBuffer getVertexBuffer() {
        return _vertexBuffer;
    }
}

这里出了什么问题?有没有更好的OpenGL实时壁纸示例代码?


1
在你的模拟器 AVD 设置中,你是否已经将 "GPU 模拟" 属性设置为 "是"? 在 AVD 配置中,这也是 hw.gpu.enabled 属性。 - kelnos
是的,我已经将“GPU仿真”属性设置为“是”,但是抱歉,我不明白您所说的“在AVD配置中也是hw.gpu.enabled属性”的意思。 - Renjith K N
1个回答

2
最终我找到了它...
我需要做的就是添加。
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

onSurfaceCreated 方法中,连同这行代码。
gl.glViewport(0, 0, width, height);

在MyRenderer类的onSurfaceChanged方法中,我在stack itself上发现了一个类似的问题。但是那个解决方案对我来说并没有标记为正确。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接