andorid openGLの簡単な使用
4407 ワード
mainActivity:public class OpenGLDemoActivity extends Activity {
Renderの実装クラス:
これは線分です.
参考自:eoeAndroidの記事
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// GLSurfaceView Render
GLSurfaceView mGlSurfaceView = new GLSurfaceView(this);
mGlSurfaceView.setRenderer(new TriggleRender());
// mGlSurfaceView.setRenderMode()
setContentView(mGlSurfaceView);
}
}
Renderの実装クラス:
public class TriggleRender implements Renderer {
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
ByteBuffer vbb= ByteBuffer.allocateDirect(vertexArray.length*4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertex = vbb.asFloatBuffer();
vertex.put(vertexArray);
vertex.position(0);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -4);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);
index++;
index%=10;
switch(index){
case 0:
case 1:
case 2:
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);
break;
case 3:
case 4:
case 5:
gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 6);
break;
case 6:
case 7:
case 8:
case 9:
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 6);
break;
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
// ,
gl.glViewport(0, 0, width, height);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
}
float vertexArray[] = {
-0.8f, -0.4f * 1.732f, 0.0f,
0.0f, -0.4f * 1.732f, 0.0f,
-0.4f, 0.4f * 1.732f, 0.0f,
0.0f, -0.0f * 1.732f, 0.0f,
0.8f, -0.0f * 1.732f, 0.0f,
0.4f, 0.4f * 1.732f, 0.0f,
};
int index=0;
}
これは線分です.
public class OpenGLRender implements Renderer {
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
ByteBuffer vbb= ByteBuffer.allocateDirect(vertexArray.length*4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertex = vbb.asFloatBuffer();
vertex.put(vertexArray);
vertex.position(0);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -4);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);
index++;
index%=10;
switch(index){
case 0:
case 1:
case 2:
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_LINES, 0, 4);
break;
case 3:
case 4:
case 5:
gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, 4);
break;
case 6:
case 7:
case 8:
case 9:
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 4);
break;
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
@Override
public void onSurfaceChanged(GL10 arg0, int width, int height) {
// TODO Auto-generated method stub
arg0.glViewport(0, 0, width, height);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
// TODO Auto-generated method stub
// gl.glClearColor(0.9f, 0.2f, 0.2f, 1.0f);
// gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
float vertexArray[] = {
-0.8f, -0.4f * 1.732f, 0.0f,
-0.4f, 0.4f * 1.732f, 0.0f,
0.0f, -0.4f * 1.732f, 0.0f,
0.4f, 0.4f * 1.732f, 0.0f,
};
int index=0;
参考自:eoeAndroidの記事