Android OpenGL學習筆記(一)

來源:互聯網
上載者:User

大家好,今天我講一下Android OpenGL,這個系列是我的學習筆記,希望對大家有所協助!這一節將給大家簡潔的介紹一下術語,以及第一個Android OpenGL程式.

 

首先讓我看們看一下術語:

 

Vertex
(頂點)
A vertex is a point in 3D space and is the building block for many
objects. In OpenGL you can specify as few as two coordinates (X,Y) and
as many as four (X,Y,Z,W). The w-axis is optional, the default value is
set to 1.0. The z-axis is also optional, the default value is set to 0.
In this series, we will use the three main coordinates X, Y, and Z,
since the W is generally used as a placeholder. The plural of vertex is
vertices (mainly important for non native speakers, because it may
create confusion). All objects are drawn using vertices as their points,
so a point will refer to a vertex.

Triangle
(三角)
A triangle requires three points to be created. So in OpenGL, we use
three vertices to create one.

Polygon
(多邊形)
A polygon is an object which has at least three connected points.
Therefor a triangle is also a polygon.

 

Primitives
(基底實體)
A primitive is a three-dimensional object created using either triangles
or polygons. A bit ironic: A detailed model with 50.000 vertices is
also a primitive like a low detailed model with 500 vertices.

 

 

下面就是我們的第一個Android OpenGL程式:

package com.android.tutor;<br />import android.app.Activity;<br />import android.opengl.GLSurfaceView;<br />import android.os.Bundle;<br />public class OpenGl_Lesson1 extends Activity {<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);</p><p> GLSurfaceView mGlSurfaceView = new GLSurfaceView(this);</p><p> mGlSurfaceView.setRenderer(new OpenGLRender());<br /> setContentView(mGlSurfaceView);</p><p> }<br />}

 

我們這裡的View是用的GLSurfaceView,但是它要setRenderer()一下,就像我們Activity裡面的setContentView()方法一樣!

 

這裡的OpenGLRender是我重新寫的類,它繼承於GLSurfaceView.Renderer,我們要實現其種的三個方法:

 

onSurfaceCreated(),onSurfaceChanged(),onDrawFrame()。代碼如下:

 

package com.android.tutor;<br />import javax.microedition.khronos.egl.EGLConfig;<br />import javax.microedition.khronos.opengles.GL10;<br />import android.opengl.GLSurfaceView.Renderer;<br />public class OpenGLRender implements Renderer {<br />@Override<br />public void onSurfaceCreated(GL10 gl, EGLConfig config) {<br />// TODO Auto-generated method stub</p><p>gl.glClearColor(0.9f, 0.2f, 0.2f, 1.0f);<br />gl.glClear(GL10.GL_COLOR_BUFFER_BIT);<br />}<br />@Override<br />public void onSurfaceChanged(GL10 gl, int width, int height) {<br />gl.glViewport(0, 0, width, height);<br />}<br />@Override<br />public void onDrawFrame(GL10 gl) {<br />// TODO Auto-generated method stub<br />}</p><p>}<br />

 

 第10行就是相當於我們設定畫布顏色RGBA(紅綠藍透),第11行這裡是清除深度顏色緩衝,不加上一句,第10句不起作用,整個View的顏色

還是黑黑的!

 

運行效果如下:

 

聯繫我們

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