Opengl Es2のひな形

opengl-es2-template.png

gitリポジトリ

関連クラス

Quaternion.java

package miffy.sora;
 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;//added
import java.nio.ByteOrder;
import java.nio.FloatBuffer;//added
 
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
 
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.Matrix;
import android.os.SystemClock;
import android.util.Log;
 
public class MiffyRenderer implements GLSurfaceView.Renderer {
    private int muMVPMatrixHandle;
    private float[] mMVPMatrix = new float[16];
    public float[] mMMatrix = new float[16];//model matrix
    private float[] mVMatrix = new float[16];
    private float[] mProjMatrix = new float[16];
    private int size = 64;
    private byte voldata[] = new byte[size * size * size];
    private FloatBuffer cubeVB;
    private static final String TAG = "Renderer";
    public float mZoom = -7.0f;
     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";
     private int mProgram;
     private int maPositionHandle;
   /*レンダリングの開始時点と、 OpenGL ES 描画コンテキストが再生成さたとき常に呼び出されます
    * テクスチャといった長い間生存している OpenGL リソースを生成するのに OnSurfaceCreated() は最適な場所です。*/
 
   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();
       InitShader();
     //  LoadVolumeFile();
 
   }
   private void InitShader(){
       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");
   }
   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;
   }
 
   public void onDrawFrame(GL10 unused) {
       // Redraw background color
       GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
       Matrix.setLookAtM(mVMatrix, 0, 0, 0, mZoom, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
       // Add program to OpenGL environment
       GLES20.glUseProgram(mProgram);
 
       Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);//mMMatrix=model matrixクオータニオンで回転させた結果が入ってる
       Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
 
       // Apply a ModelView Projection transformation
       GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
 
       DrawBoundingBox();
 
   }
   public void DrawBoundingBox(){
 
       GLES20.glVertexAttribPointer(maPositionHandle,3, GLES20.GL_FLOAT,false, 0, cubeVB);
       GLES20.glEnableVertexAttribArray(maPositionHandle);
       GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 0, 4);
       GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 4, 4);
       GLES20.glDrawArrays(GLES20.GL_LINES, 8, 8);
 
   }
   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, 100);
       muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
       Matrix.setLookAtM(mVMatrix, 0, 0, 0, mZoom, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
 
//       //left,right,bottom,top,near,far
//       // Enable use of vertex arrays
//       GLES20.glEnableClientState(GL10.GL_VERTEX_ARRAY);
 
   }
   private void LoadVolumeFile(){
        for(int i=0;i<size*size*size;i++){
            voldata[i]=0;
        }
        Log.v("miffy", "LoadVolumefile");
        BufferedInputStream in = null;
        FileInputStream file;
        try {//D:\\volumerendering\\data\\bonsai-16.raw
            file=new FileInputStream("/mnt/sdcard/bonsai-"+size+".raw");
            in = new BufferedInputStream(file);
 
            Log.d("LoadVolumefile", "openFileInput");
 
                in.read(voldata);
 
        } catch (FileNotFoundException e) {
            //カレントフォルダはAndroid Deviceのルートです。
            Log.d("FILE not found","not found");
 
        } catch (IOException e) {
            Log.d("ReadString", "IOException");
 
        }
 
    }
   private void initShapes(){
       float cubeCoords[] ={
               -1.0f,-1.0f,-1.0f,
                1.0f,-1.0f,-1.0f,
                1.0f, 1.0f,-1.0f,
               -1.0f, 1.0f,-1.0f,
 
               -1.0f,-1.0f, 1.0f,
                1.0f,-1.0f, 1.0f,
                1.0f, 1.0f, 1.0f,
               -1.0f, 1.0f, 1.0f,
               //ここから側面の線達(GL_LINESで描画)
               -1.0f,-1.0f,-1.0f,
               -1.0f,-1.0f, 1.0f,
                1.0f,-1.0f,-1.0f,
                1.0f,-1.0f, 1.0f,
               -1.0f,1.0f,-1.0f,
               -1.0f,1.0f,1.0f,
               1.0f,1.0f,-1.0f,
               1.0f,1.0f,1.0f
       };
 
       // initialize vertex Buffer for triangle  
       ByteBuffer vbb_cube = ByteBuffer.allocateDirect(
               // (# of coordinate values * 4 bytes per float)
               cubeCoords.length * 4); 
       vbb_cube.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
       cubeVB = vbb_cube.asFloatBuffer();  // create a floating point buffer from the ByteBuffer
       cubeVB.put(cubeCoords);    // add the coordinates to the FloatBuffer
       cubeVB.position(0);            // set the buffer to read the first coordinate
 
   }
}

サポートサイト Wikidot.com