OpenGL Es .obj加载程序

时间:2014-02-10 20:06:39

标签: android opengl-es loader

您好我正在为Android安装一个Opengl es Obj加载程序。我可以读取文件,但我可能在索引上出错了。我希望有人可以帮我解决这个问题。

SquareRenderer:

package book.BouncySquare;

import javax.microedition.khronos.egl.EGL10;

import javax.microedition.khronos.egl.EGLConfig;

import javax.microedition.khronos.opengles.GL10;


import android.content.Context;
import android.content.res.AssetManager;
import android.opengl.GLSurfaceView;

import java.lang.Math;
import java.util.*;



class SquareRenderer implements GLSurfaceView.Renderer{

public SquareRenderer(boolean useTranslucentBackground, AssetManager asset){

    mTranslucentBackground = useTranslucentBackground;

    mSquare = new Modelfile(asset);
}

public void onDrawFrame(GL10 gl){

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    gl.glClearColor(0.0f, 0.5f, 0.5f, 1.0f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);

    gl.glLoadIdentity();

    mAngle+=.4;

    gl.glTranslatef(0.0f, (float)((float)Math.sin(mTransY)/2.0f), -7.0f);

    gl.glRotatef(mAngle, 0.0f, 1.0f, 0.0f);

    gl.glRotatef(mAngle, 1.0f, 0.0f, 0.0f);


    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

    mSquare.draw(gl);

    mTransY += .075f;
}

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


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

    float aspectRatio;

    float zNear= .1f;

    float zFar = 1000;

    float fieldOfView= 30.0f/57.3f;

    float size;

    gl.glEnable(GL10.GL_NORMALIZE);

    aspectRatio = (float) width / height;

    gl.glMatrixMode(GL10.GL_PROJECTION);

    size = zNear * (float)(Math.tan((double)(fieldOfView/2.0f)));



    gl.glFrustumf(-size, size, -size/aspectRatio, size/aspectRatio, zNear, zFar);

    gl.glMatrixMode(GL10.GL_MODELVIEW);

}

public void onSurfaceCreated(GL10 gl, EGLConfig config){

    gl.glDisable(GL10.GL_DITHER);

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

    if (mTranslucentBackground){

        gl.glClearColor(0, 0, 0, 0);
    }

    else
    {
        gl.glClearColor(1, 1, 1, 1);
    }

    //gl.glEnable(GL10.GL_CULL_FACE);

    gl.glShadeModel(GL10.GL_SMOOTH);

    gl.glEnable(GL10.GL_DEPTH_TEST);

}

private boolean mTranslucentBackground;
private Modelfile mSquare;
private float mTransY;
private float mAngle;

}

Modelfile:

package book.BouncySquare;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;

import javax.microedition.khronos.opengles.GL11;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class Modelfile {

objfileLoader loader;

int vertextcount=8;

public Modelfile(AssetManager asset){
    try {
        loader= new objfileLoader("cone.obj", asset);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }




    float[] vertices= loader.vertecisA;



    byte maxColor= (byte)255;

    byte colors[] = {

            maxColor, maxColor, 0, maxColor,
            0, maxColor, maxColor,maxColor,
            0, 0, 0, maxColor,
            maxColor, 0, maxColor, maxColor,

            maxColor, 0, 0, maxColor,
            0, maxColor, 0, maxColor,
            0, 0, maxColor, maxColor,
            0, 0, 0, maxColor,

            maxColor, 0, 0, maxColor,
            0, maxColor, 0, maxColor,
            0, 0, maxColor, maxColor,
            0, 0, 0, maxColor
    };

    byte tfan1[] =loader.indexesA;


    this.vertextcount= tfan1.length;

    byte tfan2[] ={
            7,4,5,
            7,5,6,
            7,6,2,
            7,2,3,
            7,3,0,
            7,0,4

    };

    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);

    vbb.order(ByteOrder.nativeOrder());

    mFVertexBuffer = vbb.asFloatBuffer();

    mFVertexBuffer.put(vertices);

    mFVertexBuffer.position(0);

    mColorBuffer=ByteBuffer.allocateDirect(colors.length);
    mColorBuffer.put(colors);
    mColorBuffer.position(0);

    mTfan1 = ByteBuffer.allocateDirect(tfan1.length);

    mTfan1.put(tfan1);

    mTfan1.position(0);

    mTfan2 = ByteBuffer.allocateDirect(tfan2.length);

    mTfan2.put(tfan2);

    mTfan2.position(0);


}


public void draw(GL10 gl){


    gl.glVertexPointer(3, GL11.GL_FLOAT, 0, mFVertexBuffer);

    gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);


    gl.glDrawElements(gl.GL_TRIANGLES, vertextcount, GL11.GL_UNSIGNED_BYTE, mTfan1);
    //gl.glDrawElements(gl.GL_TRIANGLE_FAN, 6*3, GL11.GL_UNSIGNED_BYTE, mTfan2);

//      for(int i =0; i+2<  this.vertextcount; i++){
//gl.glDrawArrays(gl.GL_TRIANGLES, 0, 8);
//
//
//
//
//
//
//          Log.d("tim", i+""); 
//          
//      }
//      





}

private FloatBuffer mFVertexBuffer;

private ByteBuffer mColorBuffer;

private ByteBuffer mTfan1;
private ByteBuffer mTfan2;


}

objfileLoader:

package book.BouncySquare;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class objfileLoader {


    public float[] vertecisA;

    public byte[] indexesA;


    public ArrayList<Float> vertecis= new ArrayList<Float>();

    public ArrayList<Byte> indexes= new ArrayList<Byte>();

    public objfileLoader(String path, AssetManager asset) throws IOException{
        InputStream file= asset.open("cone.obj");

         BufferedReader read= new BufferedReader(new InputStreamReader(file));

         String currentLine;


         while((currentLine= read.readLine())!=null){

             if(currentLine.startsWith("v ")){
//               
                vertecis.add( Float.valueOf(currentLine.split(" ")[1]));
//               
               vertecis.add( Float.valueOf(currentLine.split(" ")[2]));
//               
                 vertecis.add( Float.valueOf(currentLine.split(" ")[3]));
//
//
//               
//               
             }


             if(currentLine.startsWith("f ")){
//               


                 indexes.add( (byte)Integer.parseInt(currentLine.split(" ")[1].split("/")[0]));
//               
                 indexes.add( (byte)Integer.parseInt(currentLine.split(" ")[2].split("/")[0]));
//               
                 indexes.add( (byte)Integer.parseInt(currentLine.split(" ")[3].split("/")[0]));

//               

//               

//
//
//               
//               
             }
             vertecisA= new float[vertecis.size()+3];
             float first=0;

             for(int i =0; i<vertecis.size(); i++){

                vertecisA[i]=vertecis.get(i)/3f;
//               if(i==0){
//                    vertecisA[vertecis.size()]=vertecis.get(0)/3;
//                   vertecisA[vertecis.size()+1]=vertecis.get(1)/3;
//                  vertecisA[vertecis.size()+2]=vertecis.get(2)/3;
//
//               }

             }




             //int test=2;

             //vertecisA[0]=(float)first;




         }
         indexesA= new byte[indexes.size()];

         for(int i =0; i<indexes.size(); i++){

            indexesA[i]=indexes.get(i);

         }

         ///
         for(int i=0; i<= indexesA.length-3; i+=3){
             Log.d("indexes List: ", i+": "+indexesA[i]+"/"+indexesA[i+1]+"/"+indexesA[i+2]);

         }




    }

}

cone.obj

# This file uses centimeters as units for non-parametric coordinates.

v 1.291341 -0.084057 -0.417895
v 1.097171 -0.084057 -0.798975
v 0.794744 -0.084057 -1.101402
v 0.413663 -0.084057 -1.295572
v -0.008767 -0.084057 -1.362478
v -0.431198 -0.084057 -1.295572
v -0.812278 -0.084057 -1.101402
v -1.114705 -0.084057 -0.798975
v -1.308875 -0.084057 -0.417894
v -1.375781 -0.084057 0.004536
v -1.308875 -0.084057 0.426967
v -1.114705 -0.084057 0.808047
v -0.812278 -0.084057 1.110473
v -0.431198 -0.084057 1.304643
v -0.008767 -0.084057 1.371550
v 0.413663 -0.084057 1.304643
v 0.794743 -0.084057 1.110473
v 1.097170 -0.084057 0.808047
v 1.291340 -0.084057 0.426966
v 1.358246 -0.084057 0.004536
v -0.008767 1.805633 0.004536
vt 0.737764 0.172746
vt 0.702254 0.103054
vt 0.646946 0.047746
vt 0.577254 0.012236
vt 0.500000 -0.000000
vt 0.422746 0.012236
vt 0.353054 0.047746
vt 0.297746 0.103054
vt 0.262236 0.172746
vt 0.250000 0.250000
vt 0.262236 0.327254
vt 0.297746 0.396946
vt 0.353054 0.452254
vt 0.422746 0.487764
vt 0.500000 0.500000
vt 0.577254 0.487764
vt 0.646946 0.452254
vt 0.702254 0.396946
vt 0.737764 0.327254
vt 0.750000 0.250000
vt 0.250000 0.500000
vt 0.275000 0.500000
vt 0.300000 0.500000
vt 0.325000 0.500000
vt 0.350000 0.500000
vt 0.375000 0.500000
vt 0.400000 0.500000
vt 0.425000 0.500000
vt 0.450000 0.500000
vt 0.475000 0.500000
vt 0.500000 0.500000
vt 0.525000 0.500000
vt 0.550000 0.500000
vt 0.575000 0.500000
vt 0.600000 0.500000
vt 0.625000 0.500000
vt 0.650000 0.500000
vt 0.675000 0.500000
vt 0.700000 0.500000
vt 0.725000 0.500000
vt 0.750000 0.500000
vt 0.500000 1.000000
f 1/1 20/20 2/2
f 20/20 19/19 2/2
f 19/19 18/18 2/2
f 18/18 17/17 2/2
f 17/17 16/16 2/2
f 16/16 15/15 2/2
f 15/15 14/14 2/2
f 14/14 13/13 2/2
f 13/13 12/12 2/2
f 12/12 11/11 2/2
f 11/11 10/10 2/2
f 10/10 9/9 2/2
f 9/9 8/8 2/2
f 8/8 7/7 2/2
f 7/7 6/6 2/2
f 6/6 5/5 2/2
f 5/5 4/4 2/2
f 4/4 3/3 2/2
f 1/21 2/22 21/42
f 2/22 3/23 21/42
f 3/23 4/24 21/42
f 4/24 5/25 21/42
f 5/25 6/26 21/42
f 6/26 7/27 21/42
f 7/27 8/28 21/42
f 8/28 9/29 21/42
f 9/29 10/30 21/42
f 10/30 11/31 21/42
f 11/31 12/32 21/42
f 12/32 13/33 21/42
f 13/33 14/34 21/42
f 14/34 15/35 21/42
f 15/35 16/36 21/42
f 16/36 17/37 21/42
f 17/37 18/38 21/42
f 18/38 19/39 21/42
f 19/39 20/40 21/42
f 20/40 1/41 21/42

谢谢Matic Oblak。这很有效。我可以将一个立方体渲染成一个圆锥形但是当我按下三角形时,事情就变得很糟糕。当我尝试渲染这个颜色是有意的时,这就是我得到的: enter image description here

0 个答案:

没有答案