點繪製螺旋線

來源:互聯網
上載者:User

標籤:android   color   使用   width   os   cti   

/**
 * 緩衝區工具類
 */
public class BufferUtil {
 /**
  * 將浮點數群組轉換成位元組緩衝區
  */
 public static ByteBuffer arr2ByteBuffer(float[] arr){
  ByteBuffer ibb = ByteBuffer.allocateDirect(arr.length * 4);
  ibb.order(ByteOrder.nativeOrder());
  FloatBuffer fbb = ibb.asFloatBuffer();
  fbb.put(arr);
  ibb.position(0);
  return ibb ;
 }
 
 /**
  * 將list轉換成位元組緩衝區
  */
 public static ByteBuffer list2ByteBuffer(List<Float> list){
  ByteBuffer ibb = ByteBuffer.allocateDirect(list.size() * 4);
  ibb.order(ByteOrder.nativeOrder());
  FloatBuffer fbb = ibb.asFloatBuffer();
  for(Float f : list){
   fbb.put(f);
  }
  ibb.position(0);
  return ibb ;
 }
}

 

/**  

* 渲染器

 */

public abstract class AbstractMyRenderer implements android.opengl.GLSurfaceView.Renderer

{  

 private float ratio;  

  public float xrotate = 0f;//圍繞x軸旋轉角度

public float yrotate = 0f;//圍繞x軸旋轉角度  

 /**

  * 1.

  */  

public void onSurfaceCreated(GL10 gl, EGLConfig config)

{  

 //清平色  

 gl.glClearColor(0f, 0f, 0f, 1f);   

//啟用頂點緩衝區數組

  gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);  

}

/**  

* 2.

  */

 public void onSurfaceChanged(GL10 gl, int width, int height) {

//設定視口

  gl.glViewport(0, 0, width, height);   

ratio = (float)width / (float)height;   

//投影矩陣  

 gl.glMatrixMode(GL10.GL_PROJECTION);

  //載入單位矩陣  

 gl.glLoadIdentity();  

 //設定平截頭體   

gl.glFrustumf(-ratio, ratio, -1, 1, 3f, 7f);  }

/**

  * 3.

  */  

public abstract void onDrawFrame(GL10 gl);

}

 

/**  

* 點渲染器,繪製螺旋線

 */

public class MyPointRenderer1 extends AbstractMyRenderer{

 public void onDrawFrame(GL10 gl) {

  //清除顏色緩衝區   

gl.glClear(GL10.GL_COLOR_BUFFER_BIT);   

//設定繪圖顏色   

gl.glColor4f(1f, 0f, 0f, 1f);     

 //操作模型視圖矩陣   

gl.glMatrixMode(GL10.GL_MODELVIEW);  

 gl.glLoadIdentity();  

 //設定眼球的參數   

GLU.gluLookAt(gl,0f,0f,5f, 0f, 0f, 0f, 0f,1f,0f);     

 //旋轉角度   

gl.glRotatef(xrotate, 1, 0, 0);  

 gl.glRotatef(yrotate, 0, 1, 0);     

 //計算點座標   

float r = 0.5f ;//半徑   

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

float x = 0f,y = 0f,z = 1f ;   

float zstep = 0.01f ;  

 for(float alpha = 0f ; alpha < Math.PI * 6 ; alpha = (float) (alpha + Math.PI / 16)){    

x = (float) (r * Math.cos(alpha));   

 y = (float) (r * Math.sin(alpha));   

 z = z - zstep ;   

 coordsList.add(x);   

 coordsList.add(y);    

coordsList.add(z);  

 }      

//轉換點成為緩衝區  

 ByteBuffer ibb = ByteBuffer.allocateDirect(coordsList.size() * 4);  

 ibb.order(ByteOrder.nativeOrder());   

FloatBuffer fbb = ibb.asFloatBuffer();   

for(float f : coordsList){

    fbb.put(f);   

}   

ibb.position(0);      //指定頂點指標   

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, ibb);  

 gl.glDrawArrays(GL10.GL_POINTS, 0, coordsList.size() / 3);  

}

}

 

//主介面

public class MainActivity extends Activity {

     private AbstractMyRenderer render;  

private MyGLSurfaceView view;

 public void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);        

view = new GLSurfaceView(this);        

render = new MyPointRenderer1();      

   view.setRenderer(render);        

//GLSurfaceView.RENDERMODE_CONTINUOUSLY:持續渲染(預設)        

//GLSurfaceView.RENDERMODE_WHEN_DIRTY:髒渲染,命令渲染        

view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);        

setContentView(view);    

}

public boolean onKeyDown(int keyCode, KeyEvent event) {

float step = 5f ;    

 //up    

 if(keyCode == KeyEvent.KEYCODE_DPAD_UP){       

render.xrotate = render.xrotate - step ;    

 }      else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){      

 render.xrotate = render.xrotate + step ;    

 }      else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){       

render.yrotate = render.yrotate + step ;    

 }      else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){       

render.yrotate = render.yrotate - step ;      

}      

//請求渲染,和髒渲染配合使用      

view.requestRender();    

 return super.onKeyDown(keyCode, event);    

}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.