【问题标题】:Android OpenGL ES Not DrawingAndroid OpenGL ES 不绘图
【发布时间】:2014-08-24 06:10:15
【问题描述】:

几个月前我开始学习 OpenGL ES,但是当我尝试创建一个用于绘制多个三角形的类时遇到了一些问题。

private Level l;
private Triangle t;

public Game()
{

    Bitmap b = BitmapFactory.decodeResource(MainProgram.res, R.drawable.black);
    //TEMP ARGUMENT
    l = new Level(b);
}

public void update()
{
    //TEMP!
    l.update();
}

等级

private int size;
private Color[] colorList;
private TriangleList triangleList;

public Level(Bitmap b)
{
    size = b.getWidth()/16;
    colorList = new Color[size];
    for(int i = 0;i<size;i++)
    {
        int pixColor = b.getPixel(i*16, 1);
        float r = (pixColor&0x00FF0000)>>16;
        float g = (pixColor&0x0000FF00)>>8;
        float blue = (pixColor&0x000000FF);
        colorList[i] = new Color(r,g,blue);
    }
    triangleList = new TriangleList(size,colorList);
    colorList = null;
}

public void update()
{
    triangleList.draw();
}

三角形列表

private float[] color;
private FloatBuffer vertexBuffer;
private ShortBuffer[] indicesBuffer;

public TriangleList(int size,Color[] colorList)
{
    float[] vertices = null;
    short[] indices = null;

    if(size == 1)
    {
        vertices = TriangleSize.stageOne;
        indices = TriangleSize.stageOneIndices;
    }



    color = new float[colorList.length*4];

    for(int i = 0;i<colorList.length;i++)
    {
        color[3*i] = colorList[i].getR();
        color[3*i+1] = colorList[i].getG();
        color[3*i+2] = colorList[i].getB();
        color[3*i+3] = 1.0f;

    }


    ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
    bb.order(ByteOrder.nativeOrder());

    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    bb = null;

    bb = ByteBuffer.allocateDirect(indices.length * 4);
    bb.order(ByteOrder.nativeOrder());

    indicesBuffer = new ShortBuffer[indices.length/3];

    for(int i = 0;i<indices.length;i+=3)
    {
        bb = ByteBuffer.allocateDirect(6);
        ShortBuffer b = bb.asShortBuffer();
        b.put(new short[]{indices[i],indices[i+1],indices[i+2]});
        b.position(0);
        indicesBuffer[i/3] = b;
    }

}

public void draw()
{
    for(int i = 0;i<color.length/4;i++)
    {
        int positionHandle = Renderer.shader.getAttribute("vPosition");

        int colorHandle = Renderer.shader.getUniform("vColor");

        glEnableVertexAttribArray(positionHandle);
        glVertexAttribPointer(positionHandle, 3, GL_FLOAT, false, 0, vertexBuffer);

        glUniform4fv(colorHandle, 1, new float[]{1.0f,1.0f,1.0f,1.0f}, 0);

        glDrawElements(GL_TRIANGLES, 3,GL_UNSIGNED_SHORT,indicesBuffer[i]);

        glDisableVertexAttribArray(positionHandle);

    }


}

它没有给我一个错误,但没有绘制任何内容。

【问题讨论】:

    标签: android opengl-es opengl-es-2.0


    【解决方案1】:

    您的代码中存在一些问题。首先,您不需要一个 ShortBuffers 数组作为索引列表缓冲区。

    private FloatBuffer vertexBuffer;
    private ShortBuffer indicesBuffer;
    

    您应该将所有数据紧密地打包到 vertexBuffer 和 indicesBuffer 中,并且只进行 1 次绘制调用。 下面是一个为绘制 2 个三角形形成一个矩形准备数据的示例:

    // The vertex buffer.
                vertexBuffer = ByteBuffer.allocateDirect(4 * 3 * 4) //amount of vertices * amount of coordinates * sizeof(float)
                    .order(ByteOrder.nativeOrder()).asFloatBuffer();
    
                // initialize byte buffer for the draw list
                indicesBuffer = ByteBuffer.allocateDirect(6 * 2) // amount of indices * sizeof(short)
                    .order(ByteOrder.nativeOrder()).asShortBuffer();
    
            vertexBuffer.position(0);
            indicesBuffer.position(0);
    
            float [] vertices = new float[] 
                    {200f, 400f, 100f, //vertex 0
                     200f, 200f, 100f, //vertex 1
                     400f, 200f, 100f, //vertex 2
                     400f, 400f, 100f}; //vertex 3
            //indices of vertices above that will be used to draw
            short indices[] = new short[] {0, 1, 2, 0, 2, 3};
    
            vertexBuffer.put(vertices);                 
            indicesBuffer.put(indices);
    
            vertexBuffer.position(0);
            indicesBuffer.position(0);
    

    同时删除渲染函数中的循环并仅使用一次绘图调用:

    public void draw()
    {
            int positionHandle = Renderer.shader.getAttribute("vPosition");
    
            int colorHandle = Renderer.shader.getUniform("vColor");
    
            glEnableVertexAttribArray(positionHandle);
            glVertexAttribPointer(positionHandle, 3, GL_FLOAT, false, 0, vertexBuffer);
    
            glUniform4fv(colorHandle, 1, new float[]{1.0f,1.0f,1.0f,1.0f}, 0);
    
            GLES20.glDrawElements(GLES20.GL_TRIANGLES, indicesBuffer.capacity(),
                     GLES20.GL_UNSIGNED_SHORT, indicesBuffer);
    
            glDisableVertexAttribArray(positionHandle);
    
    
    }
    

    如果还有什么不清楚的地方请告诉我,我会尽力澄清。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      相关资源
      最近更新 更多