まず、私の愚かな英語力をお詫び申し上げます。
Google の Android Developer Example で openGL ES を学びました。
コードをテストしたところ、正しく動作しました。
しかし、自分のライブラリを作りたいので、コードを修正して配布しました。
(このように、Shader.java、ColorManager.java、ObjectManager.java ... Rectangle.java.)
私のレンダラーは背景色を描画しますが、フィギュアを描画しません。
これが私のGLSurfaceViewです
public class MenuView extends GraphicView{
EnhancedRenderer mRenderer;
public MenuView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mRenderer = new EnhancedRenderer();
setEGLContextClientVersion(2);
setRenderer(mRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
public MenuView(Context context, EnhancedRenderer mRenderer){
super(context, mRenderer);
setRenderer(mRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
@Override
public boolean onTouchEvent(MotionEvent e){
return true;
}
}
GraphicView
を拡張し、GLSurfaceView
カスタム メソッドを 1 つだけ追加するだけです。
これが私のレンダラーのソースコードです
public class EnhancedRenderer extends GraphicRenderer{
protected final float[] mMVPMatrix = new float[16];
protected final float[] mProjMatrix = new float[16];
protected final float[] mVMatrix = new float[16];
protected final float[] mRotationMatrix = new float[16];
private Rectangle rect1;
private Rectangle
rect2;
private Triangle tri;
// private Circle circle;
public EnhancedRenderer(){
super();
}
public EnhancedRenderer(Context context){
// super(context);
}
public void setBackgroundColor(short r, short g, short b, float Alpha){
float[] Color = ColorManager.custom_COLOR(r, g, b, Alpha);
GLES20.glClearColor(Color[0], Color[1], Color[2], Color[3]);
}
@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config){
setBackgroundColor((short)111,(short)0,(short)111,1000.0f);
rect1 = new Rectangle();
rect1.init_Figure();
rect2 = new Rectangle();
rect2.init_Figure();
tri = new Triangle();
tri.init_Figure();
}
@Override
public void onDrawFrame(GL10 unused){
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
Matrix.setLookAtM(mVMatrix, 0,
0.0f, 0.0f, -0.3f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
rect1.draw(mMVPMatrix);
Matrix.setLookAtM(mVMatrix, 0,
0.0f, 0.0f, -0.3f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
tri.draw(mMVPMatrix);
}
@Override
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 coordinates
// in the onDrawFrame() method
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
}
-GraphicRenderer
実装するだけGLSurfaceView.Renderer
ここに「init_Figure()」メソッドがあります
public boolean init_Figure(){
//only once Initialize
if(isProgramSetUp){return isProgramSetUp;}
//initialize the vertex and drawList buffer
vertexBuffer = BufferManager.makeFloatBuffer(coords);
drawListBuffer = BufferManager.makeShortBuffer(drawOrder);
//make Shader
vertexShader = Shader.get_VertexShader();
fragmentShader = Shader.get_FragmentShader();
//add Shader to Program
mProgram = Shader.makeProgram(vertexShader, fragmentShader);
//mProgram = Shader.makeProgram();
isProgramSetUp = true;
return isProgramSetUp;
}
ここに draw() メソッドがあります
public void draw(float[] mMVPMatrix){
//Add program to OpenGL environment
Log.d("draw()-","draw");
GLES20.glUseProgram(mProgram);
Log.d("draw()-","draw -mProgram-"+mProgram);
// get handle to vertex shader's vPosition member
mPosition_Handle = GLES20.glGetAttribLocation(mProgram, "vPosition");
Log.d("draw()-","draw -mPosition Handle-"+mPosition_Handle);
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(mPosition_Handle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(mPosition_Handle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertex_Stribe, vertexBuffer);
// get handle to fragment shader's vColor member
mColor_Handle = GLES20.glGetUniformLocation(mProgram, "vColor");
Log.d("draw()-","draw -mColor Handle-"+mColor_Handle);
// Set color for drawing the Square
GLES20.glUniform4fv(mColor_Handle, 1, color, 0);
Log.d("draw()-", color[0]+", "+color[1]+", "+color[2]+" length- "+color.length);
// get handle to shape's transformation matrix
mMVPMatrix_Handle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
GraphicRenderer.checkGlError("glGetUniformLocation");
Log.d("draw()-","draw -mMVPMatrix_Handle-"+mMVPMatrix_Handle);
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrix_Handle, 1, false, mMVPMatrix, 0);
GraphicRenderer.checkGlError("glUniformMatrix4fv");
// Draw the square
GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length,
GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
Log.d("draw()-", drawOrder[0]+", "+drawOrder[1]+", "+drawOrder[2]+" length- "+drawOrder.length);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPosition_Handle);
}
logcat を確認すると、Figure の色、coords、drawOrder、mProgram が正しく設定されています...