android opengl es 繪製六邊形

來源:互聯網
上載者:User

先上:

  

直接上代碼了 :

  首先是一個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;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.