12

运行 OpenGL 应用程序是否需要任何特殊的模拟器设置?

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

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

经过一番努力,我运行了该应用程序,但它没有在模拟器中绘制任何内容,但是当我在设备中测试时它可以工作,但是在模拟器中它仍然只显示一个绿屏,我在这里的 Google 群组中找到了关于它的讨论。我尝试按照其中的说明设置视口。但它仍然没有显示任何结果,在表面改变我已经添加了这一行

gl.glViewport(0, 0, 宽度, 高度);

这是设置视口的正确方法吗?

这是我的渲染类,

 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);
    }
}

Herse 是 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;
    }
}

这里出了什么问题?是否有更好的 Open GL 动态壁纸示例代码?

4

1 回答 1

2

最后我找到了它..

我需要做的只是添加

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

onSurfaceCreated方法以及代码行

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

MyRenderer类的onSurfaceChanged方法中

我在堆栈本身 中发现了一个类似的问题[但是对我有用的解决方案未标记为正确:(]

于 2012-10-07T19:03:05.957 回答