openGL es學習(1.畫點與畫線)


1.創建一個接口,好處在於直接在activity修改渲染操作,免得換來換去

package com.opengles;

import javax.microedition.khronos.opengles.GL10;

public interface IOpenGLDemo {
    public void DrawScene(GL10 gl);
}


2.創建渲染類OpenGLRenderer

package com.opengles;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;

public class OpenGLRenderer implements Renderer {

    private final IOpenGLDemo openGLDemo;

    public OpenGLRenderer(IOpenGLDemo demo) {
        openGLDemo = demo;
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Set the background color to black ( rgba ).
        //黑色背景
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
        // Enable Smooth Shading, default not really needed.
        // 啟用陰影平滑(不是必須的)
        gl.glShadeModel(GL10.GL_SMOOTH);
        // Depth buffer setup.
        // 設置深度緩存
        gl.glClearDepthf(1.0f);
        // Enables depth testing.
        // 啟用深度測試
        gl.glEnable(GL10.GL_DEPTH_TEST);
        // The type of depth testing to do.
        // 所作深度測試的類型
        gl.glDepthFunc(GL10.GL_LEQUAL);
        // Really nice perspective calculations.
        // 對透視進行修正
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

    }

    public void onDrawFrame(GL10 gl) {
        if (openGLDemo != null) {
            openGLDemo.DrawScene(gl);
        }
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // Sets the current view port to the new size.
        // 設置畫面的大小
        gl.glViewport(0, 0, width, height);
        // Select the projection matrix
        // 設置投影矩陣
        gl.glMatrixMode(GL10.GL_PROJECTION);
        // Reset the projection matrix
        // 重置投影矩陣
        gl.glLoadIdentity();
        // Calculate the aspect ratio of the window
        // 設置畫面比例
        GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
                100.0f);
        // Select the modelview matrix
        // 選擇模型觀察矩陣
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        // Reset the modelview matrix
        // 重置模型觀察矩陣
        gl.glLoadIdentity();
    }
}

3.創建activity,實現上面的接口

package com.opengles;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class Main extends Activity implements IOpenGLDemo {

    private GLSurfaceView mGLSurfaceView;

    // 畫點的坐標
    float[] vertexArray = new float[] { 
            -0.8f, -0.4f * 1.732f, 0.0f, 
            0.8f,-0.4f * 1.732f, 0.0f,
            0.0f, 0.4f * 1.732f, 0.0f, };

    // 畫線的坐標
    float vertexArray2[] = { 
            -0.8f, -0.4f * 1.732f, 0.0f,
            -0.4f, 0.4f * 1.732f,0.0f, 
            0.0f, -0.4f * 1.732f, 0.0f, 
            0.4f, 0.4f * 1.732f, 0.0f, };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        mGLSurfaceView = new GLSurfaceView(this);
        mGLSurfaceView.setRenderer(new OpenGLRenderer(this));
        setContentView(mGLSurfaceView);
    }

//渲染操作就在DrawScene(GL10 gl)這個辦法操作
// 畫背景色 // public void DrawScene(GL10 gl) { // gl.glClearColor(0f, 0f, 1.0f, 0.0f);//設置背景顏色 // // Clears the screen and depth buffer. // gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // } // 畫點 // public void DrawScene(GL10 gl) { // gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);//每次畫頁面把上次的緩存清除掉 // //System.out.println("這里已經執行"); // ByteBuffer vbb = ByteBuffer.allocateDirect(vertexArray.length * 4); // vbb.order(ByteOrder.nativeOrder()); // FloatBuffer vertex = vbb.asFloatBuffer(); // vertex.put(vertexArray); // vertex.position(0); // // gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);//點的顏色 // gl.glPointSize(18f); // gl.glLoadIdentity(); // gl.glTranslatef(0, 0, -4); // // gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); // // gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);//3表示xyz3唯坐標 // gl.glDrawArrays(GL10.GL_POINTS, 0, 3); // // gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // // } // 畫線 public void DrawScene(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); ByteBuffer vbb = ByteBuffer.allocateDirect(vertexArray2.length * 4); vbb.order(ByteOrder.nativeOrder()); FloatBuffer vertex = vbb.asFloatBuffer(); vertex.put(vertexArray); vertex.position(0); gl.glLoadIdentity(); gl.glTranslatef(0, 0, -4); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex); int index = new Random().nextInt(4); switch (index) { case 1: gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f); gl.glDrawArrays(GL10.GL_LINES, 0, 4); break; case 2: gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f); gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, 4); break; case 3: gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f); gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 4); break; } gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } @Override protected void onResume() { // Ideally a game should implement // onResume() and onPause() // to take appropriate action when the // activity looses focus super.onResume(); mGLSurfaceView.onResume(); } @Override protected void onPause() { // Ideally a game should implement onResume() // and onPause() // to take appropriate action when the // activity looses focus super.onPause(); mGLSurfaceView.onPause(); } }


參考於:http://www.apkbus.com/android-20427-1-1.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM