简体   繁体   English

使用obj loader的OpenGL ES失真

[英]Opengl es distortion with obj loader

I just began learning OpenGL ES today(I already know WebGL and OpenGL) so that I can persue a game project I posted on reddit (reddit.com/r/gameideas/comments/3dsy8m/revolt/) and took a attempt at porting my OBJLoader but ran into a problem I couldn't solve and was wondering if anyone knew what was wrong. 我今天才开始学习OpenGL ES(我已经知道WebGL和OpenGL),以便可以说服我在reddit(reddit.com/r/gameideas/comments/3dsy8m/revolt/)上发布的游戏项目,并尝试移植我的OBJLoader但遇到了我无法解决的问题,想知道是否有人知道出了什么问题。 I am guessing that it is something that is usually enabled by default in OpenGL but not in OpenGL ES. 我猜想它通常是在OpenGL中默认启用的,但在OpenGL ES中却没有启用。

Here is a image of the distortion: 这是失真的图像:

在此处输入图片说明

and here is the important code: http://pastebin.com/1CgsJv21 这是重要的代码: http : //pastebin.com/1CgsJv21

public class GLMesh{
    public static Activity activity;
    private FloatBuffer verticesBuffer;
    private FloatBuffer texcoordsBuffer;
    private FloatBuffer normalsBuffer;
    private float[] vertices;
    private float[] texcoords;
    private float[] normals;
    private int numOfVertices;

    public GLMesh(){
        numOfVertices = 0;
    }

    public void setVertices(float[] vertices) {
        this.vertices = vertices;
    }

    public void setTexcoords(float[] texcoords) {
        this.texcoords = texcoords;
    }

    public void setNormals(float[] normals) {
        this.normals = normals;
    }

    private static FloatBuffer asBuffer(float[] data){
        ByteBuffer vbb = ByteBuffer.allocateDirect(data.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        FloatBuffer outBuffer = vbb.asFloatBuffer();
        outBuffer.put(data);
        outBuffer.position(0);
        return outBuffer;
    }

    public void load(String path){
        // Define List of Vertices
        ArrayList<GLVec3f> rVerts = new ArrayList<>();
        ArrayList<GLVec2f> rTexs = new ArrayList<>();
        ArrayList<GLVec3f> rNorms = new ArrayList<>();
        ArrayList<GLVec3f> rTris = new ArrayList<>();
        // Add Null Vertex
        rVerts.add(null);
        rTexs.add(null);
        rNorms.add(null);
        try{
            InputStream in = activity.getAssets().open(path);
            InputStreamReader isr = new InputStreamReader(in, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line;
            while((line = br.readLine()) != null){
                String[] splitLine = line.split(" ");
                switch(splitLine[0]){
                    case "v":
                        float vX = Float.parseFloat(splitLine[1]);
                        float vY = Float.parseFloat(splitLine[2]);
                        float vZ = Float.parseFloat(splitLine[3]);
                        GLVec3f vertex = new GLVec3f(vX, vY, vZ);
                        rVerts.add(vertex);
                        break;
                    case "vt":
                        float tS = Float.parseFloat(splitLine[1]);
                        float tT = Float.parseFloat(splitLine[2]);
                        GLVec2f texcoord = new GLVec2f(tS, tT);
                        rTexs.add(texcoord);
                        break;
                    case "vn":
                        float nX = Float.parseFloat(splitLine[1]);
                        float nY = Float.parseFloat(splitLine[2]);
                        float nZ = Float.parseFloat(splitLine[3]);
                        GLVec3f normal = new GLVec3f(nX, nY, nZ);
                        rNorms.add(normal);
                        break;
                    case "f":
                        String[] vtn1 = splitLine[1].split("/");
                        String[] vtn2 = splitLine[2].split("/");
                        String[] vtn3 = splitLine[3].split("/");

                        float vIA = Float.parseFloat(vtn1[0]);
                        float vIB = Float.parseFloat(vtn2[0]);
                        float vIC = Float.parseFloat(vtn3[0]);

                        float tIA = Float.parseFloat(vtn1[1]);
                        float tIB = Float.parseFloat(vtn2[1]);
                        float tIC = Float.parseFloat(vtn3[1]);

                        float nIA = Float.parseFloat(vtn1[2]);
                        float nIB = Float.parseFloat(vtn2[2]);
                        float nIC = Float.parseFloat(vtn3[2]);

                        GLVec3f indexA = new GLVec3f(vIA, tIA, nIA);
                        GLVec3f indexB = new GLVec3f(vIB, tIB, nIB);
                        GLVec3f indexC = new GLVec3f(vIC, tIC, nIC);

                        rTris.add(indexA);
                        rTris.add(indexB);
                        rTris.add(indexC);
                        break;
                }
            }
        }
        catch(IOException e){
            System.err.printf("Failed to fetch mesh @ %s", path);
            return;
        }
        // Put Data into useful format
        float[] verts = new float[rTris.size() * 3];
        float[] texs = new float[rTris.size() * 2];
        float[] norms = new float[rTris.size() * 3];
        int i = 0;
        // Get rid of need for indices
        for(GLVec3f index: rTris){
            // Collect Vertex Indices
            int vI = (int) index.x;
            int tI = (int) index.y;
            int nI = (int) index.z;
            // Collect Vertex Info
            GLVec3f v = rVerts.get(vI);
            GLVec2f t = rTexs.get(tI);
            GLVec3f n = rNorms.get(nI);
            // Populate arrays
            verts[i * 3 + 0] = v.x;
            verts[i * 3 + 1] = v.y;
            verts[i * 3 + 2] = v.z;
            texs[i * 2 + 0] = t.x;
            texs[i * 2 + 1] = t.y;
            norms[i * 3 + 0] = n.x;
            norms[i * 3 + 1] = n.y;
            norms[i * 3 + 2] = n.z;
            // Increment Counter
            i++;
        }
        numOfVertices = verts.length;
        // Populate Mesh
        setVertices(verts);
        setTexcoords(texs);
        setNormals(norms);
        compile();
    }

    public void compile(){
        verticesBuffer = asBuffer(vertices);
        texcoordsBuffer = asBuffer(texcoords);
        normalsBuffer = asBuffer(normals);
    }

    public void draw(GL10 gl){
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texcoordsBuffer);
        gl.glNormalPointer(GL10.GL_FLOAT, 0, normalsBuffer);
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, numOfVertices);

        gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }
}

The 3rd parameter passed to glDrawArrays is the number of indices (eg vertices) to be rendered. 传递给glDrawArrays的第三个参数是要渲染的索引数(例如,顶点)。 So numOfVertices should be set to the number of vertices that has been written to verts , not the number of floats in verts : 所以numOfVertices应设置为已被写入到顶点的数量verts ,而不是在花车的数量verts

numOfVertices = i;

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

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