Api Demo - .graphics(21)>>StaticTriangleRenderer

package com.example.android.apis.graphics;

import static android.opengl.GLES10.*;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

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

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.os.SystemClock;

import com.example.android.apis.R;

/**
 * A GLSurfaceView.Renderer that uses the Android-specific
 * android.opengl.GLESXXX static OpenGL ES APIs. The static APIs
 * expose more of the OpenGL ES features than the
 * javax.microedition.khronos.opengles APIs, and also
 * provide a programming model that is closer to the C OpenGL ES APIs, which
 * may make it easier to reuse code and documentation written for the
 * C OpenGL ES APIs.
 *
 */
public class StaticTriangleRenderer implements GLSurfaceView.Renderer{

    public interface TextureLoader {
        /**
         * Load a texture into the currently bound OpenGL texture.
         */
        void load(GL10 gl);
    }

    public StaticTriangleRenderer(Context context) {
        init(context, new RobotTextureLoader());
    }

    public StaticTriangleRenderer(Context context, TextureLoader loader) {
        init(context, loader);
    }

    private void init(Context context, TextureLoader loader) {
        mContext = context;
        mTriangle = new Triangle();
        mTextureLoader = loader;
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    	//在这个方法中主要用来设置一些绘制时不常变化的参数
        glDisable(GL_DITHER);//关闭颜色抖动,降低图片显示效果来提高运行效率,默认为打开
        
        glHint(GL_PERSPECTIVE_CORRECTION_HINT,//选择透视模式
                GL_FASTEST);

        glClearColor(.5f, .5f, .5f, 1);//设置背景颜色
        glShadeModel(GL_SMOOTH);//开启平滑效果
        glEnable(GL_DEPTH_TEST);//开启深度检测
        glEnable(GL_TEXTURE_2D);//开启纹理

      

        int[] textures = new int[1];//创建纹理数组用来存放
        glGenTextures(1, textures, 0);//生成纹理ID存放到数组里

        mTextureID = textures[0];
        glBindTexture(GL_TEXTURE_2D, mTextureID);//绑定ID,通知OPENGL 可以使用此纹理

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                GL_NEAREST);//给纹理设置参数,第2个参数指定缩放模式,第3个指定清晰度。
        glTexParameterf(GL_TEXTURE_2D,
                GL_TEXTURE_MAG_FILTER,
                GL_LINEAR);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,//第2个参数指定横向填充方向(横/竖),第3个指定纵向填充模式。
                GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                GL_CLAMP_TO_EDGE);

        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
                GL_REPLACE);//设置像素界别的,能够影响混合等其他像素级别的操作。

        mTextureLoader.load(gl);//用纹理装载器装置纹理。
    }

    public void onDrawFrame(GL10 gl) {//绘制图形。
        glDisable(GL_DITHER);

        glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
                GL_MODULATE);

        
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清空屏幕。

        glMatrixMode(GL_MODELVIEW);//选择投影矩阵。
        /*
         *指定哪一个矩阵是当前矩阵,对接下来要对什么进行操作做一下说明
         * GL_TEXTURE,对纹理矩阵堆栈应用随后的矩阵操作.GL_MODELVIEW 是模型矩阵 ,GL_PROJECTION 是投影矩阵。 
         * gluPerspective的是创建一个投影矩阵并且与当前矩阵相乘,得到的矩阵设定为当前变换,
         * 但要先通过glMatrixMode设定成投影矩阵才会得到想要的投影矩阵变换。
        */

        glLoadIdentity();//重置投影矩阵。

        GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
        /*
         * 该函数定义一个视图矩阵,并与当前矩阵相乘。   
         * 第2,3,4参数 相机在世界坐标的位置   
         * 第5,6,7 相机镜头对准的物体在世界坐标的位置   
         * 第8,9,10 相机向上的方向在世界坐标中的方向 
         */

        glEnableClientState(GL_VERTEX_ARRAY);//开启顶点设置
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);//开启纹理设置。

        glActiveTexture(GL_TEXTURE0);
        /*
         * 选择哪一个纹理在后面的纹理状态改变时有效,纹理单元的数量是依据该纹理单元所被支持的具体实现。
         * 一个纹理单元由纹理可用状态、纹理矩阵堆、纹理环境以及当前渲染纹理组成。修改其中任一成分均会改变当前活动纹理单元。
         */
        

        glBindTexture(GL_TEXTURE_2D, mTextureID);绑定ID,通知OPENGL 可以使用此纹理
        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,//设置填充模式
                GL_REPEAT);
        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                GL_REPEAT);

        long time = SystemClock.uptimeMillis() % 4000L;
        float angle = 0.090f * ((int) time);

        glRotatef(angle, 0, 0, 1.0f);//旋转

        mTriangle.draw(gl);
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        glViewport(0, 0, w, h);

        /*
        * Set our projection matrix. This doesn't have to be done
        * each time we draw, but usually a new projection needs to
        * be set when the viewport is resized.
        */

        float ratio = (float) w / h;
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glFrustumf(-ratio, ratio, -1, 1, 3, 7);
    }

    private Context mContext;
    private Triangle mTriangle;
    private int mTextureID;
    private TextureLoader mTextureLoader;

    private class RobotTextureLoader implements TextureLoader {
        public void load(GL10 gl) {
            InputStream is = mContext.getResources().openRawResource(
                    R.raw.robot);
            Bitmap bitmap;
            try {
                bitmap = BitmapFactory.decodeStream(is);
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    // Ignore.
                }
            }

            GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);
            bitmap.recycle();
        }
    }

    static class Triangle {
        public Triangle() {

            // Buffers to be passed to gl*Pointer() functions
            // must be direct, i.e., they must be placed on the
            // native heap where the garbage collector cannot
            // move them.
            //
            // Buffers with multi-byte datatypes (e.g., short, int, float)
            // must have their byte order set to native order

            ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
            vbb.order(ByteOrder.nativeOrder());
            mFVertexBuffer = vbb.asFloatBuffer();//存放顶点数组的buffer.

            ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
            tbb.order(ByteOrder.nativeOrder());
            mTexBuffer = tbb.asFloatBuffer();//存放纹理数组的buffer.

            ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
            ibb.order(ByteOrder.nativeOrder());
            mIndexBuffer = ibb.asShortBuffer();//存放索引数组的buffer.

            // A unit-sided equilateral triangle centered on the origin.
            float[] coords = {
                    // X, Y, Z
                    -0.5f, -0.25f, 0,
                     0.5f, -0.25f, 0,
                     0.0f,  0.559016994f, 0
            };

            for (int i = 0; i < VERTS; i++) {
                for(int j = 0; j < 3; j++) {
                    mFVertexBuffer.put(coords[i*3+j] * 2.0f);
                }
            }

            for (int i = 0; i < VERTS; i++) {
                for(int j = 0; j < 2; j++) {
                    mTexBuffer.put(coords[i*3+j] * 2.0f + 0.5f);
                }
            }

            for(int i = 0; i < VERTS; i++) {
                mIndexBuffer.put((short) i);
            }

            mFVertexBuffer.position(0);
            mTexBuffer.position(0);
            mIndexBuffer.position(0);
        }

        public void draw(GL10 gl) {
            glFrontFace(GL_CCW);//设置正反面。
            glVertexPointer(3, GL_FLOAT, 0, mFVertexBuffer);//设置顶点
            glEnable(GL_TEXTURE_2D);//开启纹理
            glTexCoordPointer(2, GL_FLOAT, 0, mTexBuffer);//设置纹理
            glDrawElements(GL_TRIANGLE_STRIP, VERTS,//索引法绘图。
                    GL_UNSIGNED_SHORT, mIndexBuffer);
        }

        private final static int VERTS = 3;

        private FloatBuffer mFVertexBuffer;
        private FloatBuffer mTexBuffer;
        private ShortBuffer mIndexBuffer;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值