Android OpenGL例学習
14552 ワード
例は添付の通りです
Android Manifest.xml
OGADActivity.java
VortexView.java
円を描く:
円を動かす:
http://hi.baidu.com/fairzy/blog/item/959200fcd1b60dfbfc037f9b.htmlここのいくつかのファイルは悪くないです。
Android Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="anson.code.openGLDemo1"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".OGDActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
OGADActivity.java
package anson.code.openGLDemo1;
import android.app.Activity;
import android.os.Bundle;
public class OGDActivity extends Activity {
/** Called when the activity is first created. */
VortexView vortexView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
vortexView = new VortexView(this);
setContentView(vortexView);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
vortexView.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
vortexView.onResume();
}
}
VortexView.java
package anson.code.openGLDemo1;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class VortexView extends GLSurfaceView {
private VortexRenderer renderer;
public VortexView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public VortexView(Context context) {
super(context);
// TODO Auto-generated constructor stub
renderer = new VortexRenderer();
setRenderer(renderer);
}
@Override
public boolean onTouchEvent(final MotionEvent event) {
// TODO Auto-generated method stub
queueEvent(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
renderer.setColor(event.getX()/getWidth(), event.getY() /getHeight(), 1.0f);
renderer.setAngle(event.getX()/10);
}
});
return super.onTouchEvent(event);
}
}
VortexRender.javapackage anson.code.openGLDemo1;
/**
* :onSurfaceCreated --> onSurfaceChanged --> onDrawFrame --> onDrawFrame --> onDrawFrame -->
* :onSurfaceChanged --> onDrawFrame --> onSurfaceChanged --> onDrawFrame --> onDrawFrame -->
*
* 1) onDrawFrame() , , glclear , OpenGl ES 。
* 2) onSurfaceChanged() surface , openGL , Camera
* 3) onSurfaceCreated() ,OpenGL ES ( activity , .
*/
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.opengl.GLSurfaceView.Renderer;
public class VortexRenderer implements Renderer {
private float red = 0f;
private float green = 0f;
private float blue = 0f;
private ShortBuffer indexBuffer;
private FloatBuffer vertexBuffer;
private short[] indicesArray = {0, 1, 2};
private int numberOfVertices = 3;
private float angle;
private void initTriangle(){
/**
* allocate allocateDirect ,
* allocateDirect " " Buffer.
* 。 , .
* Sun allocateDirect .
*/
ByteBuffer vbb = ByteBuffer.allocateDirect(numberOfVertices *3 *4);//36;
// ByteOrder nativeOrder() .
vbb.order(ByteOrder.nativeOrder());// ByteOrder , ByteBuffer.
/** ByteBuffer CharBuffer 。
* buffer ByteBuffer ByteBuffer
*/
vertexBuffer = vbb.asFloatBuffer();
ByteBuffer ibb = ByteBuffer.allocateDirect(numberOfVertices *2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();
/**
* coords ,
* : X, Y, Z
* :
* , ( X/2, Y/2)
* : 0, 0, 0
* , Z .
* [0, 1] .
*/
float[] coords = {-0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
0f, 0.5f, 0.1f};
vertexBuffer.put(coords);// vertexBuffer, .
indexBuffer.put(indicesArray);
vertexBuffer.position(0);// .
indexBuffer.position(0);
}
public void setColor(float red, float green, float blue){
this.red = red;
this.green = green;
this.blue = blue;
}
public void setAngle(float an){
this.angle = an;
}
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
/**
* RGB + A
* Alpha 。 ,alpha 0.0 。alpha 1.0 。
* , , AFA , 0. (0,0,0,0),
*/
gl.glClearColor(red, green, blue, 0.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);// ,glClear() 。
/**
*
* glRotatef( , X, Y, Z);
*/
gl.glRotatef(angle, 0f, 0f, 1f);
gl.glColor4f(0.5f, 0f, 0f, 0.5f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, numberOfVertices, GL10.GL_UNSIGNED_SHORT, indexBuffer);
}
@Override
public void onSurfaceChanged(GL10 gl, int w, int h) {
// TODO Auto-generated method stub
gl.glViewport(0, 0, w, h);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig cf) {
// TODO Auto-generated method stub
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
initTriangle();
}
}
円を描く:
package anson.code.openGLDemo1;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class CRenderer implements Renderer {
float rotateAngle;
float rotateY;
float r = 0.5f;
// ,GL ES ?
private float[] vertices = new float[720];
//
public float DegToRad(float deg)
{
//360 = 2PI
float Pi = 3.14159265358979323846f;
float Du = 360;
float v = deg * (2*Pi) / Du;
return v;
}
public void setAnX(float angle){
rotateAngle -= angle;
}
public void setAnY(float ay){
rotateY += ay;
}
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
//
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
//
drawCircle(gl);
}
public void drawCircle(GL10 gl)
{
//
gl.glLoadIdentity();
// , (Z )5 , x, y , z
gl.glTranslatef(0.0f, 0.0f, -1.5f);
// , angle, x, y , z
gl.glRotatef(rotateAngle, 1.0f, 0f, 0f);
gl.glRotatef(rotateY, 0f, 1f, 0f);
// , R, G, B, Alpha
gl.glColor4f(1.0f, 0.1f, 0.1f, 1.0f);
// ,
FloatBuffer verBuffer = FloatBuffer.wrap(vertices);
// (GL_FLOAT),
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, verBuffer);
//
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// OGL
/**
* GL_TRIANGLE_FAN
* GL_LINES
* GL_LINES_LOOP
*/
gl.glDrawArrays(GL10.GL_LINES, 0, 360);
//
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
//
gl.glFinish();
//
//rotateAngle += 0.5;
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
float ratio = (float) width / height;
// OpenGL
gl.glViewport(0, 0, width, height);
// , OPENGL .
gl.glMatrixMode(GL10.GL_PROJECTION);
// , .
gl.glLoadIdentity();
//
/**
* glFrustumf(float left, float right, float bottom, float top, float zNear, float zFar)
* , , .
* , .
* , .
*/
//gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
gl.glFrustumf(-ratio, ratio, 1f, -1f, 1f, 25);
//
gl.glMatrixMode(GL10.GL_MODELVIEW);
//
gl.glLoadIdentity();
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
//
/** glShadeModel opengl 。
* GL_SMOOTH( ),GL_FLAT。OpenGL , 。
* , 。
* ,GL_SMOOTH ,GL_FLAT 。
*/
gl.glShadeModel(GL10.GL_SMOOTH);
//
gl.glClearColor(0, 0, 0, 0);
//
gl.glClearDepthf(1.0f);
//
/**
* A, B , ,
* OpenGL , glEnable(GL10.GL_DEPTH_TEST) .
*/
gl.glEnable(GL10.GL_DEPTH_TEST);
/**
*
* GL_LESS GL_LEQUAL
* 。
*/
gl.glDepthFunc(GL10.GL_LEQUAL);
//
/**
* OpenGL , 。
* , glHint() , 。
* :void glHint(GLenum target,GLenum hint);
* OpenGL 。
* target
* hint :GL_FASTEST( )、GL_NICEST( )、GL_DONT_CARE( )。
*/
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
//
for (int i = 0; i < 720; i += 2) {
// x value
if(i%4 ==0)
vertices[i] = (float) (Math.cos(DegToRad(i)) * r);
// y value
if(i%4 ==0)
vertices[i+1] = -(float) (Math.sin(DegToRad(i)) * r);
}
}
}
円を動かす:
package anson.code.openGLDemo1;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class CView extends GLSurfaceView {
CRenderer renderer;
float oX = 0f;
float oY = 0f;
public CView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CView(Context context) {
super(context, null);
// TODO Auto-generated constructor stub
renderer = new CRenderer();
setRenderer(renderer);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//android.util.Log.d("AnsonLog", "TouchEvent:::::::::");
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
oX = event.getRawX();
oY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float cX = event.getRawX();
float cY = event.getRawY();
float disX = cX - oX;
float disY = cY - oY;
oX = cX;
oY = cY;
renderer.setAnX(disY/4f);
renderer.setAnY(disX/4f);
break;
case MotionEvent.ACTION_UP:
oX = 0f;
oY = 0f;
break;
}
return true;
}
}
http://hi.baidu.com/fairzy/blog/item/959200fcd1b60dfbfc037f9b.htmlここのいくつかのファイルは悪くないです。