在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ 繪制迷你太陽(yáng)系
繪制線段 Line Segment
投影變換 Projection
繪制迷你太陽(yáng)系
繪制一個(gè)球體
繪制三角形 Triangle
OpenGL 光照模型
三維坐標(biāo)系及坐標(biāo)變換初步
定義 3D 模型的前面和后面
繪制一個(gè) 20 面體
顏色 Color
Depth Buffer
材質(zhì)及光照示例
基本幾何圖形定義
關(guān)于EGL
導(dǎo)言
Viewing 和 Modeling(MODELVIEW) 變換
FrameBuffer
設(shè)置光照效果 Set Lighting
Viewport 變換
階段小結(jié)
繪制點(diǎn) Point
OpenGL ES API 命名習(xí)慣
通用的矩陣變換指令
關(guān)于 OpenGL ES
創(chuàng)建實(shí)例應(yīng)用 OpenGLDemos 程序框架
OpenGL ES 管道(Pipeline)
GLSurfaceView

繪制迷你太陽(yáng)系

前面介紹了3D坐標(biāo)系統(tǒng)和3D坐標(biāo)變換以及在 OpenGL ES 中坐標(biāo)變換的過程,并與相機(jī)拍照片的過程做類比,以便更好的理解這 OpenGL 中構(gòu)造3D模型的一部步驟:

http://wiki.jikexueyuan.com/project/opengl-es-guide/images/84.png" alt="" />

本例提供繪制一個(gè)迷你太陽(yáng)系系統(tǒng)作為前面知識(shí)的總結(jié),這個(gè)迷你太陽(yáng)系,有一個(gè)紅色的太陽(yáng),一個(gè)藍(lán)色的地圖和一個(gè)白色的月亮構(gòu)成:

  • 太陽(yáng)居中,逆時(shí)針自轉(zhuǎn)。
  • 地球繞太陽(yáng)順時(shí)針公轉(zhuǎn),本身不自轉(zhuǎn)。
  • 月亮繞地球順時(shí)針公轉(zhuǎn),自身逆時(shí)針自轉(zhuǎn)。

為簡(jiǎn)單起見,使用一個(gè)2D五角星做為天體而沒有使用球體(繪制球體在后面有介紹),構(gòu)造一個(gè) Star 類:

public class Star {
 // Our vertices.
 protected float vertices[];
 // Our vertex buffer.
 protected FloatBuffer vertexBuffer;
 public Star() {
 float a=(float)(1.0f/(2.0f-2f\*Math.cos(72f\*Math.PI/180.f)));
 float bx=(float)(a\*Math.cos(18\*Math.PI/180.0f));
 float by=(float)(a\*Math.sin(18\*Math.PI/180f));
 float cy=(float)(-a \* Math.cos(18\*Math.PI/180f));
 vertices=new float[]{
 0,a,0.5f,cy,-bx,by,bx,by,-0.5f,cy
 };
 ByteBuffer vbb
 = ByteBuffer.allocateDirect(vertices.length \* 4);
 vbb.order(ByteOrder.nativeOrder());
 vertexBuffer = vbb.asFloatBuffer();
 vertexBuffer.put(vertices);
 vertexBuffer.position(0);
 }
 /**
 * This function draws our star on screen.
 * @param gl
 */
 public void draw(GL10 gl) {
 // Counter-clockwise winding.
 gl.glFrontFace(GL10.GL\_CCW);
 // Enable face culling.
 gl.glEnable(GL10.GL\_CULL\_FACE);
 // What faces to remove with the face culling.
 gl.glCullFace(GL10.GL\_BACK);
 // Enabled the vertices buffer for writing
 //and to be used during
 // rendering.
 gl.glEnableClientState(GL10.GL\_VERTEX\_ARRAY);
 // Specifies the location and data format of
 //an array of vertex
 // coordinates to use when rendering.
 gl.glVertexPointer(2, GL10.GL\_FLOAT, 0,
 vertexBuffer);
 gl.glDrawArrays(GL10.GL_LINE_LOOP, 0,5);
 // Disable the vertices buffer.
 gl.glDisableClientState(GL10.GL\_VERTEX\_ARRAY);
 // Disable face culling.
 gl.glDisable(GL10.GL\_CULL\_FACE);
 }
}  

Star 定義了五角星的五個(gè)頂點(diǎn),并使用 glDrawArrays 來(lái)繪制五角星,因此 vertices 頂點(diǎn)的順序比較重要。

然后定義一個(gè) DrawSolarSystem 來(lái)繪制這個(gè)迷你太陽(yáng)系:

public class DrawSolarSystem extends OpenGLESActivity
 implements IOpenGLDemo{
 private Star sun=new Star();
 private Star earth=new Star();
 private Star moon=new Star();
 private int angle=0;
 /** Called when the activity is first created. */
 \@Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 }
 public void DrawScene(GL10 gl) {
 super.DrawScene(gl);
 gl.glLoadIdentity();
 GLU.gluLookAt(gl,0.0f, 0.0f, 15.0f,
 0.0f, 0.0f, 0.0f,
 0.0f, 1.0f, 0.0f);
 // Star A
 // Save the current matrix.
 gl.glPushMatrix();
 // Rotate Star A counter-clockwise.
 gl.glRotatef(angle, 0, 0, 1);
 gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
 // Draw Star A.
 sun.draw(gl);
 // Restore the last matrix.
 gl.glPopMatrix();
 // Star B
 // Save the current matrix
 gl.glPushMatrix();
 // Rotate Star B before moving it,
 //making it rotate around A.
 gl.glRotatef(-angle, 0, 0, 1);
 // Move Star B.
 gl.glTranslatef(3, 0, 0);
 // Scale it to 50% of Star A
 gl.glScalef(.5f, .5f, .5f);
 gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
 // Draw Star B.
 earth.draw(gl);
 // Star C
 // Save the current matrix
 gl.glPushMatrix();
 // Make the rotation around B
 gl.glRotatef(-angle, 0, 0, 1);
 gl.glTranslatef(2, 0, 0);
 // Scale it to 50% of Star B
 gl.glScalef(.5f, .5f, .5f);
 // Rotate around it's own center.
 gl.glRotatef(angle\*10, 0, 0, 1);
 gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
 // Draw Star C.
 moon.draw(gl);
 // Restore to the matrix as it was before C.
 gl.glPopMatrix();
 // Restore to the matrix as it was before B.
 gl.glPopMatrix();
 // Increse the angle.
 angle++;
 }
}

使用 GLU 的 gluLookAt 來(lái)定義 modelview Matrix ,把相機(jī)放在正對(duì)太陽(yáng)中心(0,0,0),距離 15 (0,0,15)。

使用 glPushMatrix 和 glPopMatrix 來(lái)將當(dāng)前 Matrix 入?;蚴浅鰲?。

首先將當(dāng)前 matrix 入棧,以紅色繪制太陽(yáng),并逆向轉(zhuǎn)動(dòng),將當(dāng)前 matrix 入棧的目的是在能夠在繪制地球時(shí)恢復(fù)當(dāng)前棧。

然后繪制地球,使用局部坐標(biāo)系來(lái)想象地球和太陽(yáng)之間的相對(duì)運(yùn)動(dòng),地球離開一距離繞太陽(yáng)公轉(zhuǎn),相當(dāng)于先旋轉(zhuǎn)地球的局部坐標(biāo)系,然后再平移地球的局部坐標(biāo)系。對(duì)應(yīng)到代碼為先 glRotatef ,然后 glTranslate.

最后是繪制月亮,使用類似的空間想象方法。

http://wiki.jikexueyuan.com/project/opengl-es-guide/images/85.png" alt="" />

本例下載