打开GL ES 2.0 - 空白屏幕

时间:2016-10-11 15:51:38

标签: android opengl-es-2.0

我尝试渲染我的.obj文件,但只获得空白屏幕。测试文件读取代码,.obj文件包含中心位于0,0坐标的对象。问题只是关于GLES20代码。你能帮忙搞清楚出了什么问题吗?

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLES20;
import java.util.Hashtable;
import java.util.ArrayList;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.util.Log;

public class MyGLSurfaceView extends GLSurfaceView implements GLSurfaceView.Renderer
{
private Context context;
private Hashtable<String, ArrayList<Float>> obj;

public MyGLSurfaceView(Context context)
{
    super(context);
    this.context = context;
    setEGLContextClientVersion(2);
    setRenderer(this);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
    obj = new Hashtable<String, ArrayList<Float>>();

    try
    {
        BufferedReader reader = new BufferedReader(
          new InputStreamReader(context.getAssets().open("calculator.obj")));

        ArrayList<Float> vertex = new ArrayList<Float>();

        String name = null;
        String line = null;
        while ((line = reader.readLine()) != null)
        {
            if (line.startsWith("v "))
            {
                String[] parts = line.substring(2).trim().split("\\s+");

                vertex.add(Float.valueOf(Float.valueOf(parts[0]).floatValue()));
                vertex.add(Float.valueOf(Float.valueOf(parts[1]).floatValue()));
                vertex.add(Float.valueOf(Float.valueOf(parts[2]).floatValue()));
            }

            if (line.startsWith("f "))
            {
                String[] parts = line.substring(2).trim().split("\\s+");

                obj.get(name).add(vertex.get(Integer.valueOf(parts[0]).intValue() - 1));
                obj.get(name).add(vertex.get(Integer.valueOf(parts[1]).intValue() - 1));
                obj.get(name).add(vertex.get(Integer.valueOf(parts[2]).intValue() - 1));
            }

            if (line.startsWith("g "))
            {
                name = line.substring(2).trim();

                obj.put(name, new ArrayList<Float>());
           }
        }

        reader.close();
    }
    catch (Exception e)
    {
        System.exit(0);
    }

    int i;

    ByteBuffer fByteBuffer = ByteBuffer.allocateDirect(obj.get("calculator").size() * 4);
    fByteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer faces = fByteBuffer.asFloatBuffer();
    for (i = 0; i < obj.get("calculator").size(); i++)
      faces.put(obj.get("calculator").get(i).floatValue());            
    faces.position(0);

    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    GLES20.glClearDepthf(1.0f);

    int index[] = new int[1];
    GLES20.glGenBuffers(1, index, 0);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, index[0]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, faces.capacity(), faces, GLES20.GL_STATIC_DRAW);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, faces.capacity() / 3);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    GLES20.glFlush();
}

public void onSurfaceChanged(GL10 gl, int width, int height)
{
    GLES20.glViewport(0, 0, width, height);
}

public void onDrawFrame(GL10 gl)
{

}
}

0 个答案:

没有答案