android opengl es 绘制六边形

先上效果图:

  

android opengl es 绘制六边形_第1张图片

直接上代码了 :

  首先是一个Activity:

package com.example.opengl;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;

import com.example.opengl.render.CylinderRender;
import com.example.opengl.render.HaxagonRender;
import com.example.opengl.render.RotateTriangle;
/**
* OpenGL练习
* @author YangBaoBao
*
*/
public class OpenGLActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
GLSurfaceView surface=new GLSurfaceView(this);
surface.requestFocus();//获取焦点
surface.setFocusableInTouchMode(true);//设置为可触控
// trigle(surface);
// sixshape(surface);
clindershape(surface);
setContentView(surface);
}
private void trigle(GLSurfaceView surface)
{
RotateTriangle rtgl=new RotateTriangle();
surface.setOnTouchListener(new RotateTriangle.MyTouchListener(rtgl));
surface.setRenderer(rtgl);
}
private void sixshape(GLSurfaceView surface)
{
HaxagonRender hr=new HaxagonRender();
surface.setOnTouchListener(new HaxagonRender.MyTouchListener(hr,surface));
surface.setRenderer(hr);
}
private void clindershape(GLSurfaceView surface)
{
CylinderRender hr=new CylinderRender();
surface.setOnTouchListener(new CylinderRender.MyTouchListener(hr,surface));
surface.setRenderer(hr);
}
}

 

然后是一个Render:

  

package com.example.opengl.render;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.example.opengl.document.Hexagon;
import com.example.opengl.document.Triangle;

import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class HaxagonRender implements Renderer{
private final static float TOUCH_SCALE_FACTOR = 180.0f/320;
static float xAngle;
Hexagon[] hs=new Hexagon[]{
new Hexagon(0),
new Hexagon(-2),
new Hexagon(-4),
new Hexagon(-6),
new Hexagon(-8),
new Hexagon(-10),
new Hexagon(-12)
};
public HaxagonRender()
{
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();

gl.glTranslatef(0, 0, -1.4f);

for(Hexagon h:hs)
{ h.setxAngle(xAngle);
h.drawSelf(gl);
}
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float ratio=(float)320/480;
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
gl.glDisable(GL10.GL_DITHER);//关闭抗抖动
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST);//设置特定Hint项目的模式,这里为设置使用快速模式
gl.glClearColor(0, 0, 0, 0);//设置屏幕背景色为黑色
gl.glEnable(GL10.GL_DEPTH_TEST);//启用深度检测
}
public Hexagon getHs() {
return hs[0];
}
public static class MyTouchListener implements OnTouchListener
{
float previousX=0;
float previousY=0;
HaxagonRender tgl;
GLSurfaceView surface;
public MyTouchListener(HaxagonRender tgl,GLSurfaceView surface)
{
this.tgl=tgl;
this.surface=surface;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_MOVE)
{
float dX=event.getX()-previousX;
float dY=event.getY()-previousY;
xAngle+=dY;
// surface.requestRender();
}
previousX=event.getX();
previousY=event.getY();
return true;
}

}
}

 

还有一个封装的六边形:

  

package com.example.opengl.document;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Hexagon {
private static float xAngle;
private static float yAngle;
private static float zAngle;
private int one=10000;

private IntBuffer pointBuffer;

private byte[] index=new byte[]{
0,1,2,3,4,5,6,1
};
private ByteBuffer indexBuffer;

final int color_one = 65535;
private int[] color=new int[]{
0,0,0,0,
color_one,0,0,0,
color_one,0,0,color_one,

color_one,0,color_one,0,
color_one,color_one,0,0,
color_one,color_one,0,color_one,
color_one,color_one,color_one,0
};
private IntBuffer colorBuffer;

public Hexagon(int z)
{

int[] points=new int[]{
0,0,z*one,
2*one,3*one,z*one,
4*one,0,z*one,
2*one,-3*one,z*one,
-2*one,-3*one,z*one,
-4*one,0,z*one,
-2*one,3*one,z*one
};


ByteBuffer pb=ByteBuffer.allocateDirect(points.length*4);
pb.order(ByteOrder.nativeOrder());
pointBuffer=pb.asIntBuffer();
pointBuffer.put(points);
pointBuffer.flip();
pointBuffer.position(0);


ByteBuffer ib=ByteBuffer.allocateDirect(index.length);
ib.order(ByteOrder.nativeOrder());
indexBuffer=ib;
indexBuffer.put(index);
indexBuffer.flip();
indexBuffer.position(0);

ByteBuffer cb=ByteBuffer.allocateDirect(color.length*4);
cb.order(ByteOrder.nativeOrder());
colorBuffer=cb.asIntBuffer();
colorBuffer.put(color);
colorBuffer.flip();
colorBuffer.position(0);



}
public void drawSelf(GL10 gl)
{
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glVertexPointer(3,GL10.GL_FIXED,0,pointBuffer);
gl.glColorPointer(4, GL10.GL_FIXED,0,colorBuffer);
gl.glRotatef(xAngle, 1, 0, 0);
gl.glDrawElements(GL10.GL_TRIANGLE_FAN,index.length,GL10.GL_UNSIGNED_BYTE,indexBuffer);
}
/**
* @param xAngle the xAngle to set
*/
public void setxAngle(float xAngle) {
this.xAngle += xAngle;
}
/**
* @param yAngle the yAngle to set
*/
public void setyAngle(float yAngle) {
this.yAngle += yAngle;
}
/**
* @param zAngle the zAngle to set
*/
public void setzAngle(float zAngle) {
this.zAngle += zAngle;
}
}

你可能感兴趣的:(OpenGL,es)