Android OpenGL ES画四棱椎,贴图

不说废话,贴代码,画四棱椎

   // 旋转方向
    private float xrot, yrot, zrot;
   // 正三角顶点
    private IntBuffer tvertices =IntBuffer.wrap(new int[] { 
    		 0, one, 0,
             -one, -one,one,
             one, -one, one,
             
             0, one, 0, 
             one, -one, one,
             one, -one,-one,
             
             0, one, 0,
             one, -one, -one,
             -one, -one, -one, 

             0, one, 0,
             -one, -one, -one,
             -one, -one, one 
        });

    
    //纹理点
    private int[] texCoords = {   
            0, one,                 
            one, one, 
            0, 0, 
            one, 0 
    }; 

      // 初始化
        textureids = new int[1]; 
        // 实例化bitmap
        bitmap = BitGL.bitmap;

        ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4 * 6);
        tbb.order(ByteOrder.nativeOrder());
        texBuffer = tbb.asIntBuffer();
        //为每一个面贴上纹理
        for (int i = 0; i < 6; i++) {
            texBuffer.put(texCoords);
        }
        texBuffer.position(0); 

@Override
	public void onDrawFrame(GL10 gl) {
		// 清除深度和颜色缓存
        gl.glClear(GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        gl.glVertexPointer(3, GL10.GL_FIXED, 0, tvertices);
        gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texBuffer); // Define
        
        //向z轴里移入3.0f
        gl.glTranslatef(0.0f, 0.0f, -3.0f);
        // 设置旋转的方向
        gl.glRotatef(xrot, 0.0f, 1.0f, 0.0f);

        // 绘制四棱椎
        for (int i = 0; i < 4; i++) {
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 3, 3);
        } 

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

        // 设置旋转角度
        xrot += 1f;
        yrot += 1f;
        zrot += 0.3f;

	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		 // 视角
        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);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();

	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// 告诉系统对透视进行修正
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        // 黑色背景
        gl.glClearColor(0, 0, 0, 0);
        // 启用阴影平滑
        gl.glShadeModel(GL10.GL_SMOOTH);

        // 清除深度缓存
        gl.glClearDepthf(one);
        // 启用深度测试
        gl.glEnable(GL10.GL_DEPTH_TEST);
        // 所做深度测试的类型
        gl.glDepthFunc(GL10.GL_LEQUAL);

    
        
        gl.glEnable(GL10.GL_TEXTURE_2D);
        // 创建纹理
        gl.glGenTextures(1, textureids, 0);
        // 绑定要使用的纹理
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureids[0]);
        // 生成纹理
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 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); 

	}

你可能感兴趣的:(Android OpenGL ES画四棱椎,贴图)