`
dolaameng328
  • 浏览: 30292 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
Obj模型解析 opengl es, android
public class ObjLoader
{
	private short[] indices;
	private int[] mVertices;
	private int[] mTextures;
	private int[] mNormals;

	public ObjLoader(InputStream data)
	{
		this.loadData(data);
	}
	public void loadData(InputStream data)
	{
		BufferedReader reader;
		try
		{
			ArrayList<String> verticeLines = new ArrayList<String>();
			ArrayList<String> textureLines = new ArrayList<String>(); 
			ArrayList<String> normalLines = new ArrayList<String>();
			ArrayList<String> verticeIndiceLines = new ArrayList<String>();
			ArrayList<String> textureIndiceLines = new ArrayList<String>();
			ArrayList<String> normalIndiceLines = new ArrayList<String>();
			//input=new FileInputStream(data);
			reader = new BufferedReader(new InputStreamReader(data));
			String line = null;
			while((line = reader.readLine()) != null) 
			{
				if(line.startsWith("//") ||line.startsWith("#")|| line.trim().equals("")) 
				{
					continue;
				}
				String SPACE=" ";
				String SLASH="/";
				StringTokenizer st=new StringTokenizer(line,SPACE);
				String lineType = st.nextToken();
				if(lineType.equals("v"))
				{
					verticeLines.add(st.nextToken());
					verticeLines.add(st.nextToken());
					verticeLines.add(st.nextToken());
				}
				else if(lineType.equals("vt"))
				{
					textureLines.add(st.nextToken());
					textureLines.add(st.nextToken());
				}
				else if(lineType.equals("vn"))
				{
					normalLines.add(st.nextToken());
					normalLines.add(st.nextToken());
					normalLines.add(st.nextToken());
				}
				else if(lineType.equals("f"))
				{
					int nb = st.countTokens();
					if (nb == 3) {
						String v1=st.nextToken();
						String v2=st.nextToken();
						String v3=st.nextToken();
						
						StringTokenizer st1=new StringTokenizer(v1,SLASH);
						StringTokenizer st2=new StringTokenizer(v2,SLASH);
						StringTokenizer st3=new StringTokenizer(v3,SLASH);
						
						verticeIndiceLines.add(st1.nextToken());
						verticeIndiceLines.add(st2.nextToken());
						verticeIndiceLines.add(st3.nextToken());
						
						textureIndiceLines.add(st1.nextToken());
						textureIndiceLines.add(st2.nextToken());
						textureIndiceLines.add(st3.nextToken());
						
						normalIndiceLines.add(st1.nextToken());
						normalIndiceLines.add(st2.nextToken());
						normalIndiceLines.add(st3.nextToken());
					} else if (nb == 4){// more than one face
						int i = 0;
						StringTokenizer[] sts = new StringTokenizer[4];
						while (i < 4) {
							sts[i] = new StringTokenizer(st.nextToken(),SLASH);
							i++;
						}

						String[] vectors = new String[4];
						String[] textures = new String[4];
						String[] normals = new String[4];
						for (i = 0; i < 4; i++) {
							vectors[i] = sts[i].nextToken();
							textures[i] = sts[i].nextToken();
							normals[i] = sts[i].nextToken();
						}
						
						for(i=1; i<3; i++){
							verticeIndiceLines.add(vectors[0]);
							verticeIndiceLines.add(vectors[i]);
							verticeIndiceLines.add(vectors[i+1]);
							
							textureIndiceLines.add(textures[0]);
							textureIndiceLines.add(textures[i]);
							textureIndiceLines.add(textures[i+1]);
							
							normalIndiceLines.add(normals[0]);
							normalIndiceLines.add(normals[i]);
							normalIndiceLines.add(normals[i+1]);
						}
					}
				}
			}

			int indicesSize=verticeIndiceLines.size();
			indices=new short[indicesSize];
			int verticeSize=indicesSize*3;
			mVertices = new int[verticeSize];
			int textureSize=indicesSize*2;
			mTextures=new int[textureSize];
			int normalSize=indicesSize*3;
			mNormals =new int[normalSize];

			for(int i=0;i<verticeIndiceLines.size();i++)
			{
				indices[i]=(short)i;
				int indice=Integer.valueOf(verticeIndiceLines.get(i))-1;
				mVertices[i*3]=Utils.toFixed(Float.valueOf(verticeLines.get(indice*3)));
				mVertices[i*3+1]=Utils.toFixed(Float.valueOf(verticeLines.get(indice*3+1)));
				mVertices[i*3+2]=Utils.toFixed(Float.valueOf(verticeLines.get(indice*3+2)));
				
				
				int textureIndice=Integer.valueOf(textureIndiceLines.get(i))-1;
				mTextures[i*2]=Utils.toFixed(Float.valueOf(textureLines.get(textureIndice*2)));
				mTextures[i*2+1]=Utils.toFixed(1.0f - Float.valueOf(textureLines.get(textureIndice*2+1)));
				
				int normalIndice=Integer.valueOf(normalIndiceLines.get(i))-1;
				mNormals[i*3]=Utils.toFixed(Float.valueOf(normalLines.get(normalIndice*3)));
				mNormals[i*3+1]=Utils.toFixed(Float.valueOf(normalLines.get(normalIndice*3+1)));
				mNormals[i*3+2]=Utils.toFixed(Float.valueOf(normalLines.get(normalIndice*3+2))); 
			}
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public int[] getVertices()
	{
		return mVertices;
	}
	public int[] getTextures()
	{
		return mTextures;
	}
	public int[] getNormals()
	{
		return mNormals;
	}
	public short[] getIndices()
	{
		return indices;
	}
	
}
Android HelloOpenGLES20 android, opengl es, opengl es 2.0
//=============================== Activity GLSurfaceView =============================================================
import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;

public class HelloOpenGLES20 extends Activity {
  
    private GLSurfaceView mGLView;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity
        mGLView = new HelloOpenGLES20SurfaceView(this);
        setContentView(mGLView);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        // The following call pauses the rendering thread.
        // If your OpenGL application is memory intensive,
        // you should consider de-allocating objects that
        // consume significant memory here.
        mGLView.onPause();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // The following call resumes a paused rendering thread.
        // If you de-allocated graphic objects for onPause()
        // this is a good place to re-allocate them.
        mGLView.onResume();
    }
}
  
class HelloOpenGLES20SurfaceView extends GLSurfaceView {

	private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
    private HelloOpenGLES20Renderer mRenderer;
    private float mPreviousX;
    private float mPreviousY;
    
    public HelloOpenGLES20SurfaceView(Context context){
        super(context);
        
        // Create an OpenGL ES 2.0 context.
        setEGLContextClientVersion(2);
        
        // set the mRenderer member
        mRenderer = new HelloOpenGLES20Renderer();
        setRenderer(mRenderer);
        
        // Render the view only when there is a change
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

    }
    
    @Override 
    public boolean onTouchEvent(MotionEvent e) {
        // MotionEvent reports input details from the touch screen
        // and other input controls. In this case, you are only
        // interested in events where the touch position changed.

        float x = e.getX();
        float y = e.getY();
        
        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
    
                float dx = x - mPreviousX;
                float dy = y - mPreviousY;
    
                // reverse direction of rotation above the mid-line
                if (y > getHeight() / 2) {
                  dx = dx * -1 ;
                }
    
                // reverse direction of rotation to left of the mid-line
                if (x < getWidth() / 2) {
                  dy = dy * -1 ;
                }
              
                mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;
                requestRender();
        }

        mPreviousX = x;
        mPreviousY = y;
        return true;
    } 
}


//=============================== Renderer =============================================================
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.SystemClock;

public class HelloOpenGLES20Renderer implements GLSurfaceView.Renderer {
  
	private FloatBuffer triangleVB;
	private int mProgram;
    private int maPositionHandle;
    public float mAngle;
    
    private int muMVPMatrixHandle;
    private float[] mMVPMatrix = new float[16];
    private float[] mMMatrix = new float[16];
    private float[] mVMatrix = new float[16];
    private float[] mProjMatrix = new float[16];

    private final String vertexShaderCode = 
        // This matrix member variable provides a hook to manipulate
        // the coordinates of the objects that use this vertex shader
        "uniform mat4 uMVPMatrix;   \n" +
        
        "attribute vec4 vPosition;  \n" +
        "void main(){               \n" +
        
        // the matrix must be included as a modifier of gl_Position
        " gl_Position = uMVPMatrix * vPosition; \n" +
        
        "}  \n";
    
    private final String fragmentShaderCode = 
        "precision mediump float;  \n" +
        "void main(){              \n" +
        " gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n" +
        "}                         \n";

    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        // Set the background frame color
        GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        
        // initialize the triangle vertex array
        initShapes();

        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
        
        mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
        GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
        GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
        GLES20.glLinkProgram(mProgram);                  // creates OpenGL program executables
        
        // get handle to the vertex shader's vPosition member
        maPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    }
    
    public void onDrawFrame(GL10 unused) {
        // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
        
        // Create a rotation for the triangle (Boring! Comment this out:)
        // long time = SystemClock.uptimeMillis() % 4000L;
        // float angle = 0.090f * ((int) time);

        // Use the mAngle member as the rotation value
        Matrix.setRotateM(mMMatrix, 0, mAngle, 0, 0, 1.0f);

        // Apply a ModelView Projection transformation
        Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
        Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);

        //Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
        GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
        
        // Add program to OpenGL environment
        GLES20.glUseProgram(mProgram);
        
        // Prepare the triangle data
        GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 12, triangleVB);
        GLES20.glEnableVertexAttribArray(maPositionHandle);
        
        // Draw the triangle
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
    }
    
    public void onSurfaceChanged(GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
        
        float ratio = (float) width / height;
        
        // this projection matrix is applied to object coodinates
        // in the onDrawFrame() method
        Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
        muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
        Matrix.setLookAtM(mVMatrix, 0, 0, 3, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
    }
  
    private void initShapes(){
        
        float triangleCoords[] = {
            // X, Y, Z
            -0.5f, -0.25f, 0,
             0.5f, -0.25f, 0,
             0.0f,  0.559016994f, 0
        }; 
        
        // initialize vertex Buffer for triangle  
        ByteBuffer vbb = ByteBuffer.allocateDirect(
                // (# of coordinate values * 4 bytes per float)
                triangleCoords.length * 4); 
        vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
        triangleVB = vbb.asFloatBuffer();  // create a floating point buffer from the ByteBuffer
        triangleVB.put(triangleCoords);    // add the coordinates to the FloatBuffer
        triangleVB.position(0);            // set the buffer to read the first coordinate
    
    }

    private int loadShader(int type, String shaderCode){
        
        // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
        // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
        int shader = GLES20.glCreateShader(type); 
        
        // add the source code to the shader and compile it
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);
        
        return shader;
    }
}

//===================================== AndroidManifest.xml ========================================
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
Global site tag (gtag.js) - Google Analytics