Android OpenGL/NeHe 教材的第一課

Android

簡介

使用方法

開發工具

開始寫程式

視覺化介面

視覺元件

對話元件

核心物件

事件處理

資料儲存

查詢功能

影音功能

繪圖功能

網路功能

衛星地圖

特殊功能

資源管理

裝置管理

系統核心

問題與回答

刷機升級

常用軟體

Eclipse

教學錄影

訊息

相關網站

參考文獻

最新修改

簡體版

English

執行結果

GL1run.png

程式原始碼

// 本範例修改自 Savas Ziplies 的 nehe-android-ports
// 來源:http://insanitydesign.com/wp/projects/nehe-android-ports/
package ccc.android;
 
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
 
public class GL1 extends Activity {
    private GLSurfaceView glSurface;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        glSurface = new GLSurfaceView(this);
        glSurface.setRenderer(new GLRenderer());
        setContentView(glSurface);
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        glSurface.onResume();
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        glSurface.onPause();
    }
 
    public class GLRenderer implements Renderer {
        private Triangle triangle;
        private Square square;
 
        public GLRenderer() {
            triangle = new Triangle();
            square = new Square();
        }
 
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {        
            gl.glShadeModel(GL10.GL_SMOOTH);             //Enable Smooth Shading
            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);     //Black Background
            gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
            gl.glEnable(GL10.GL_DEPTH_TEST);             //Enables Depth Testing
            gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do
 
            //Really Nice Perspective Calculations
            gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 
        }
 
        public void onDrawFrame(GL10 gl) {
            //Clear Screen And Depth Buffer
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
            gl.glLoadIdentity();                    //Reset The Current Modelview Matrix
 
            gl.glTranslatef(0.0f, -1.2f, -6.0f);    //Move down 1.2 Unit And Into The Screen 6.0
            square.draw(gl);                        //Draw the square
 
            gl.glTranslatef(0.0f, 2.5f, 0.0f);        //Move up 2.5 Units
            triangle.draw(gl);                        //Draw the triangle        
        }
 
        /**
         * If the surface changes, reset the view
         */
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            if(height == 0) {                         //Prevent A Divide By Zero By
                height = 1;                         //Making Height Equal One
            }
 
            gl.glViewport(0, 0, width, height);     //Reset The Current Viewport
            gl.glMatrixMode(GL10.GL_PROJECTION);     //Select The Projection Matrix
            gl.glLoadIdentity();                     //Reset The Projection Matrix
 
            //Calculate The Aspect Ratio Of The Window
            GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
 
            gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
            gl.glLoadIdentity();                     //Reset The Modelview Matrix
        }
    }
 
    public class Triangle {
        private FloatBuffer vertexBuffer;
        private float vertices[] = { 
                                    0.0f, 1.0f, 0.0f,     //Top
                                    -1.0f, -1.0f, 0.0f, //Bottom Left
                                    1.0f, -1.0f, 0.0f     //Bottom Right
                                                    };
        public Triangle() {
            ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
            byteBuf.order(ByteOrder.nativeOrder());
            vertexBuffer = byteBuf.asFloatBuffer();
            vertexBuffer.put(vertices);
            vertexBuffer.position(0);
        }
 
        public void draw(GL10 gl) {
            //Set the face rotation
            gl.glFrontFace(GL10.GL_CW);
            //Point to our vertex buffer
            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
            //Enable vertex buffer
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            //Draw the vertices as triangle strip
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
            //Disable the client state before leaving
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        }
    }
 
    public class Square {
        private FloatBuffer vertexBuffer;
        private float vertices[] = { 
                                    -1.0f, -1.0f, 0.0f, //Bottom Left
                                    1.0f, -1.0f, 0.0f,     //Bottom Right
                                    -1.0f, 1.0f, 0.0f,     //Top Left
                                    1.0f, 1.0f, 0.0f     //Top Right
                                                    };
        public Square() {
            //
            ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
            byteBuf.order(ByteOrder.nativeOrder());
            vertexBuffer = byteBuf.asFloatBuffer();
            vertexBuffer.put(vertices);
            vertexBuffer.position(0);
        }
 
        public void draw(GL10 gl) {
            //Set the face rotation
            gl.glFrontFace(GL10.GL_CW);
            //Point to our vertex buffer
            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
            //Enable vertex buffer
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            //Draw the vertices as triangle strip
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
            //Disable the client state before leaving
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        }
    }    
}

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License