android opengl es繪製圓柱體

來源:互聯網
上載者:User

先上個:

  

直接上關鍵類Render了:

   

package com.example.opengl.render;

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

import com.example.opengl.document.Cylinder;

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

public class CylinderRender implements Renderer{
public static class MyTouchListener implements OnTouchListener {
private CylinderRender tgl;
private GLSurfaceView surface;
private float previousX;
private float previousY;
public MyTouchListener(CylinderRender tgl,GLSurfaceView surface)
{
this.tgl=tgl;
this.surface=surface;
}

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(arg1.getAction()==MotionEvent.ACTION_MOVE)
{
float dX=(arg1.getX()-previousX)*0.4f;
float dY=(arg1.getY()-previousY)*0.4f;

tgl.sety(dX);
tgl.setx(dY);

}
previousX=arg1.getX();
previousY=arg1.getY();
return true;
}

}

private Cylinder cLinder=new Cylinder(10,2,5,18);
@Override
public void onDrawFrame(GL10 gl) {
//啟用背景色和深度緩衝
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);

gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
//設定座標位移量
gl.glTranslatef(0, 0, -10f);

//gl.glPushMatrix();
//繪製圓柱體
cLinder.drawSelf(gl);
//gl.glPopMatrix();
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float ratio=(float)width/height;
gl.glFrustumf(-ratio, ratio,-1,1,1,100);
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(1, 1, 1, 1);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
private void setx(float x) {
cLinder.setxAngle(x);
}
private void sety(float y) {
cLinder.setyAngle(y);
}
private void setz(float z) {
cLinder.setzAngle(z);
}

}

 

還有一個封裝的圓柱體類:

  

package com.example.opengl.document;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

public class Cylinder {
private FloatBuffer vertexBuffer;
private int vCount;
private float xAngle;
private float yAngle;
private float zAngle;

public Cylinder(float length,float radius,int blocks,float sectorBlocks)
{
float len=length/blocks;
float sectorAngle=360/sectorBlocks;

List<Float> arrayList=new ArrayList<Float>();

for(int i=0;i<blocks;i++)
{
for(float angle=360;angle>0;angle=angle-sectorAngle)
{
//左上方頂點座標
float x1=-(length/2-i*len);
float y1=(float) (radius*Math.sin(Math.toRadians(angle)));
float z1=(float) (radius*Math.cos(Math.toRadians(angle)));

//右上方頂點座標
float x2=x1+len;
float y2=y1;
float z2=z1;

//右下角頂點座標
float x3=x2;
float y3=(float) (radius*Math.sin(Math.toRadians(angle-sectorAngle)));
float z3=(float) (radius*Math.cos(Math.toRadians(angle-sectorAngle)));

//左下角頂點座標
float x4=x1;
float y4=y3;
float z4=z3;

//第一條線
arrayList.add(x1);
arrayList.add(y1);
arrayList.add(z1);

arrayList.add(x2);
arrayList.add(y2);
arrayList.add(z2);

//第二條線
arrayList.add(x2);
arrayList.add(y2);
arrayList.add(z2);

arrayList.add(x4);
arrayList.add(y4);
arrayList.add(z4);

//第三條線
arrayList.add(x4);
arrayList.add(y4);
arrayList.add(z4);

arrayList.add(x1);
arrayList.add(y1);
arrayList.add(z1);

//第四條線

arrayList.add(x2);
arrayList.add(y2);
arrayList.add(z2);

arrayList.add(x3);
arrayList.add(y3);
arrayList.add(z3);

//第五條線

arrayList.add(x3);
arrayList.add(y3);
arrayList.add(z3);

arrayList.add(x4);
arrayList.add(y4);
arrayList.add(z4);

}
}
int size=arrayList.size();
vCount=size/3;
float[] vertex=new float[size];
for(int i=0;i<size;i++)
{
vertex[i]=arrayList.get(i);
}

ByteBuffer vB=ByteBuffer.allocateDirect(size*4);
vB.order(ByteOrder.nativeOrder());
vertexBuffer=vB.asFloatBuffer();
vertexBuffer.put(vertex);
vertexBuffer.flip();
vertexBuffer.position(0);

}

public void drawSelf(GL10 gl)
{
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3,GL10.GL_FLOAT,0,vertexBuffer);
gl.glColor4f(0.2f, 0.2f, 0.2f, 0.2f);//設定繪製線的顏色
gl.glRotatef(xAngle,1, 0, 0);
gl.glRotatef(yAngle,0, 1, 0);
gl.glRotatef(zAngle,0, 0, 1);
gl.glDrawArrays(GL10.GL_LINES,0,vCount);
}

/**
* @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.