刚刚开始学习opengl es 写了一个四面体,贴图后强制关闭,这是什么问题?求大神们帮忙

解决方案 »

  1.   

    这是源码:
    package com.SiMianTi;import java.nio.ByteBuffer;
    import java.nio.ByteOrder;
    import java.nio.IntBuffer;import javax.microedition.khronos.opengles.GL10;class Simianti {
    public Simianti(){
    final int one =  0x10000;

    int vertices[]={
    one, one, one, 
    one, one, -one,
    one, -one, one,
     

    one, one, one,
    one, -one, one,
    -one, one, one,


     one, one, one,
     -one, one, one,
    one, one, -one,

    one, one, -one,
    one, -one, one,
    one, one, -one
    /*
    one, one, one,
    one, one, -one,
    one, -one, one,
    -one, one, one
    */
    }; int texcoords[]={
    one, one, one, 0,
    one, 0, one, one,
    one, one, 0 ,one,
    0, one, one, one

    };

     byte indices []={
    /* 0, 1, 2,     0, 2, 3,
    0, 3, 1,     1, 2, 3
    */
     0, 2, 1,
     3, 5, 4,
     6, 8, 7,
     9, 11, 10
    };
    /*
    int colors[]={
    one, 0, 0, one,
    0, one, 0, one,
    0, 0, one, one,
    one, one, 0, one,


    };*/
      ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
          vbb.order(ByteOrder.nativeOrder());
          mVertexBuffer = vbb.asIntBuffer();
          mVertexBuffer.put(vertices);
          mVertexBuffer.position(0);
        /*  
          ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
          cbb.order(ByteOrder.nativeOrder());
          mColorBuffer = cbb.asIntBuffer();
          mColorBuffer.put(colors);
          mColorBuffer.position(0);
        */  
          mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
          mIndexBuffer.put(indices);
          mIndexBuffer.position(0);
          
          ByteBuffer tbb = ByteBuffer.allocateDirect(texcoords.length);
          tbb.order(ByteOrder.nativeOrder());
          mTexBuffer = tbb.asIntBuffer();
          mTexBuffer.position(0);
                
    }

    public void draw(GL10 gl){

    gl.glFrontFace(GL10.GL_CCW);
        
         //启用深度测试    3D 图形一般都启用
         gl.glEnable(GL10.GL_DEPTH_TEST);
        
          //预缓冲 顶点坐标数组
         gl.glEnableClientState(gl.GL_VERTEX_ARRAY); 
         gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);
        
         gl.glScalef(0.65f, 0.65f, 0.65f);
        
         gl.glEnable(GL10.GL_TEXTURE_2D);  
         gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);

    // 创建纹理

         gl.glBindTexture(GL10.GL_TEXTURE_2D, mtexture);
         gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 12, GL10.GL_UNSIGNED_BYTE, mIndexBuffer);
            //为图形设置已经定义好的颜色,mColorBuffer为颜色的缓冲矩阵
     //      gl.glColorPointer(4, gl.GL_FIXED, 0, mColorBuffer);
                    
           
           
            
    }
       private IntBuffer   mVertexBuffer;
       private IntBuffer  mTexBuffer;
       private ByteBuffer mIndexBuffer;
       public static int mtexture;
    //    private IntBuffer mColorBuffer;
       
    }
    ///////////////////////////////////////////
    package com.SiMianTi;import java.nio.IntBuffer;import javax.microedition.khronos.egl.EGLConfig;
    import javax.microedition.khronos.opengles.GL10;import android.opengl.GLSurfaceView;
    import android.opengl.GLUtils;
    import android.content.Context;
    import android.content.res.Resources;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;public class GLRender implements GLSurfaceView.Renderer{
    private float mAngle =1.0f;
    public GLRender(Context context)
    {
    mContext = context;
    mSimianti = new Simianti();

    } public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glTranslatef(0, 0.0f, -1.2f);
            gl.glRotatef(mAngle, 1.0f, 0.0f, 0.0f);  
            gl.glRotatef(mAngle, 0.0f, 1.0f, 0.0f);  
           
           
     //       gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      /*
       * 开启颜色设置功能
      */
     // gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
     
     
            mSimianti.draw(gl);
            
            mAngle +=0.2f;
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        
                 
    } public void onSurfaceChanged(GL10 gl, int width, int height) {
    // TODO Auto-generated method stub
    gl.glViewport(0, 0, width, height);
        
         float ratio = (float) width / height;
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
    } public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // TODO Auto-generated method stub
    gl.glDisable(GL10.GL_DITHER);  
            gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
                    GL10.GL_FASTEST);
            gl.glClearColor(1,1,1,1);
            gl.glEnable(GL10.GL_CULL_FACE);
            gl.glShadeModel(GL10.GL_SMOOTH);
            gl.glEnable(GL10.GL_DEPTH_TEST);
              //默认旋转
            gl.glRotatef(45.0f, 1.0f, -1.0f, 1.0f);
            //启用纹理
         gl.glClearDepthf(1.0f);
    //深度测试的类型
    gl.glDepthFunc(GL10.GL_LEQUAL);

      //      gl.glEnable(GL10.GL_TEXTURE_2D);
           
            //创建纹理
      //      IntBuffer intBufer = IntBuffer.allocate(1);
    int textures[] = new int [1];
            gl.glGenTextures(1, textures, 0);
            mSimianti.mtexture = textures[0];
            gl.glBindTexture(GL10.GL_TEXTURE_2D, mSimianti.mtexture);
            
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap, 0);
            
            gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
        //    gl.glTranslatef(0.0f, 0.0f, -6.0f);
    }
    private Context mContext;
    private Simianti mSimianti;
    }
    class  GLImage{
    public static Bitmap mBitmap;
    public static void load(Resources resources)
    {
    mBitmap = BitmapFactory.decodeResource(resources, R.drawable.photo5);
    }

    }