Chinaunix首页 | 论坛 | 博客
  • 博客访问: 180564
  • 博文数量: 38
  • 博客积分: 638
  • 博客等级: 下士
  • 技术积分: 395
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-07 17:16
文章分类

全部博文(38)

文章存档

2013年(1)

2012年(5)

2011年(32)

分类: Java

2011-12-14 16:15:52

  1. //OpenglActivity.java
  2. package com.game.opengl;

  3. import android.app.Activity;
  4. import android.os.Bundle;

  5. public class OpenglActivity extends Activity {
  6.     /** Called when the activity is first created. */
  7.     private VortexView _vortexView;

  8.     @Override
  9.     public void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         _vortexView = new VortexView(this);
  12.         setContentView(_vortexView);
  13.     }
  14. }

//写一个控制类,当鼠标拖拽的时候控制三角体旋转
VortexView.java
package com.game.opengl;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;

public class VortexView extends GLSurfaceView {

private VortexRenderer _renderer;
private float _x = 0;
private float _y = 0;

public VortexView(Context context) {
super(context);
_renderer = new VortexRenderer();
setRenderer(_renderer);
}

public boolean onTouchEvent(final MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_x = event.getX();
_y = event.getY();
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
final float xdiff = (_x - event.getX());
final float ydiff = (_y - event.getY());
queueEvent(new Runnable() {
@Override
public void run() {
_renderer.setXAngle(_renderer.getXAngle() + ydiff);
_renderer.setYAngle(_renderer.getYAngle() + xdiff);
}
});
_x = event.getX();
_y = event.getY();
}
return true;
}

}

//VortexRenderer.java
package com.game.opengl;

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 _width = 320f;
private float _height = 480f;

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glMatrixMode(GL10.GL_PROJECTION);

float ratio = _width / _height;
float size = .001f * (float) Math.tan(Math.toRadians(45.0) / 2);

gl.glFrustumf(-size, size, -size / ratio, size / ratio, 0.01f, 100.0f);
gl.glViewport(0, 0, (int) _width, (int) _height);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glEnable(GL10.GL_DEPTH_TEST);

// define the color we want to be displayed as the "clipping wall"
gl.glClearColor(0f, 0f, 0f, 1f);
/*
                 * Some one-time OpenGL initialization can be made here
                 * probably based on features of this particular context
                 */
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
// enable the differentiation of which is drawn counter clockwise
gl.glEnable(GL10.GL_CULL_FACE);
// which is the front? the one which is drawn counter clockwise
gl.glFrontFace(GL10.GL_CCW);
// which one should NOT be drawn
gl.glCullFace(GL10.GL_BACK);

   
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

initTriangle();
}

@Override
public void onSurfaceChanged(GL10 gl, int w, int h) {
_width = w;
_height = h;
gl.glViewport(0, 0, w, h);
}

public void onDrawFrame(GL10 gl) {
// clear the color and the depth buffer to show the ClearColor
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);

// reset the matrix
gl.glLoadIdentity();
gl.glTranslatef(0f, 0f, -15f);

// set rotation
gl.glRotatef(_xAngle, 1f, 0f, 0f);
gl.glRotatef(_yAngle, 0f, 1f, 0f);
gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices,
GL10.GL_UNSIGNED_SHORT, _indexBuffer);
}

private ShortBuffer _indexBuffer;
private FloatBuffer _vertexBuffer;
private FloatBuffer _colorBuffer;
private int _nrOfVertices = 0;
private float _xAngle;
private float _yAngle;

public void setXAngle(float angle) {
_xAngle = angle;
}

public void setYAngle(float angle) {
_yAngle = angle;
}

public float getXAngle() {
return _xAngle;
}

public float getYAngle() {
return _yAngle;
}

private void initTriangle() {

float[] coords = { 
                                -0.5f, -0.5f, 0.5f, // 0
0.5f, -0.5f, 0.5f,  // 1
0f, -0.5f, -0.5f,   // 2
0f, 0.5f, 0f,       // 3
};

_nrOfVertices = coords.length;

float[] colors = { 
                                1f, 0f, 0f, 1f, // point 0 red
0f, 1f, 0f, 1f, // point 1 green
0f, 0f, 1f, 1f, // point 2 blue
1f, 1f, 1f, 1f, // point 3 white
};

short[] indices = new short[] { 
                                0, 1, 3, // rwg
0, 2, 1, // rbg
0, 3, 2, // rbw
1, 2, 3, // bwg
};

// float has 4 bytes, coordinate * 4 bytes
ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
vbb.order(ByteOrder.nativeOrder());
_vertexBuffer = vbb.asFloatBuffer();

// short has 2 bytes, indices * 2 bytes
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
_indexBuffer = ibb.asShortBuffer();

// float has 4 bytes, colors (RGBA) * 4 bytes
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
_colorBuffer = cbb.asFloatBuffer();

_vertexBuffer.put(coords);
_indexBuffer.put(indices);
_colorBuffer.put(colors);

_vertexBuffer.position(0);
_indexBuffer.position(0);
_colorBuffer.position(0);

}
}

阅读(1672) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~