OpenGl入門

來源:互聯網
上載者:User

標籤:android   color   使用   width   資料   os   

/**  

* 入門,三角形  

*/

//自訂渲染器

class MyRenderer implements android.opengl.GLSurfaceView.Renderer{

 private float ratio;

 //表層建立時

 public void onSurfaceCreated(GL10 gl, EGLConfig config) {   

//設定清屏色   

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

//啟用頂點緩衝區.   

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);  

}

 //表層size時  

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

//設定視口,輸出畫面的地區   

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

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

//矩陣模式,投影矩陣,openGL基於狀態機器   

gl.glMatrixMode(GL10.GL_PROJECTION);  

 //載入單位矩陣  

 gl.glLoadIdentity();  

 //平截頭體   

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

}

 //繪圖  

public void onDrawFrame(GL10 gl) {     

 //清除顏色緩衝區   

gl.glClear(GL10.GL_COLOR_BUFFER_BIT);      

//模型視圖矩陣   

gl.glMatrixMode(GL10.GL_MODELVIEW);   

gl.glLoadIdentity();//載入單位矩陣   

//eyex,eyey,eyez:放置眼球的座標   

//centerx,centery,centerz:眼球的觀察點.   

//upx,upx,upx:指定眼球向上的向量   

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

 //畫三角形

  //繪製數組  

 //三角形座標   

float[] coords = {   

 0f,ratio,2f,  

  -1f,-ratio,2f,   

 1f,-ratio,2f   };      

//分配位元組緩衝區空間,存放頂點座標資料   

ByteBuffer ibb = ByteBuffer.allocateDirect(coords.length * 4);   

//設定的順序(本地順序)   

ibb.order(ByteOrder.nativeOrder());  

 //放置頂點座標數組   

FloatBuffer fbb = ibb.asFloatBuffer();  

 fbb.put(coords);  

 //定位指標的位置,從該位置開始讀取頂點資料   

ibb.position(0);     

 //設定繪圖顏色,紅色   

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

 //3:3維點,使用三個座標值表示一個點  

 //type:每個點的資料類型   

//stride:0,跨度.   

//ibb:指定頂點緩衝區   

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

 //繪製三角形   

//0:起始點:  

 //3:繪製點的數量   

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);

 }

}

聯繫我們

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