android opengl

引用:http://weimingtom.iteye.com/blog/1616972

二维坐标系变换为原点在左上角(测试用)

* GLES

* JOGL

* LWJGL

* libgdx(使用g2d与pixmap)

Java代码  收藏代码
  1. package com.iteye.weimingtom.testgl;  
  2.   
  3. import java.nio.ByteBuffer;  
  4. import java.nio.ByteOrder;  
  5. import java.nio.IntBuffer;  
  6.   
  7. import javax.microedition.khronos.egl.EGLConfig;  
  8. import javax.microedition.khronos.opengles.GL10;  
  9.   
  10. import android.app.Activity;  
  11. import android.opengl.GLSurfaceView;  
  12. import android.os.Bundle;  
  13. import android.view.Window;  
  14. import android.view.WindowManager;  
  15.   
  16. public class Testgl3Activity extends Activity implements GLSurfaceView.Renderer {  
  17.     private boolean USE_ORIGIN_TOPLEFT = true;  
  18.       
  19.     private IntBuffer vertices;  
  20.     private int vertexBytes;  
  21.     private int vertexNum;  
  22.       
  23.     private float[] arrVertices;  
  24.     private int[] tmpBuffer;  
  25.       
  26.     private GLSurfaceView contentView;  
  27.       
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  32.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  33.                              WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  34.         contentView = new GLSurfaceView(this);  
  35.         contentView.setRenderer(this);  
  36.         setContentView(contentView);  
  37.     }  
  38.   
  39.     @Override  
  40.     protected void onPause() {  
  41.         super.onPause();  
  42.         contentView.onPause();  
  43.     }  
  44.   
  45.     @Override  
  46.     protected void onResume() {  
  47.         super.onResume();  
  48.         contentView.onResume();  
  49.     }  
  50.   
  51.     @Override  
  52.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
  53.         loadData();  
  54.         gl.glDisable(GL10.GL_DITHER);  
  55.         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);  
  56.         gl.glClearColor(1111);  
  57.         gl.glDisable(GL10.GL_CULL_FACE);  
  58.         gl.glShadeModel(GL10.GL_SMOOTH);  
  59.         gl.glEnable(GL10.GL_DEPTH_TEST);  
  60.     }  
  61.       
  62.     @Override  
  63.     public void onSurfaceChanged(GL10 gl, int width, int height) {  
  64.         gl.glMatrixMode(GL10.GL_PROJECTION);  
  65.         gl.glLoadIdentity();  
  66.         gl.glViewport(00, width, height);  
  67.         //gl.glViewport(0, height, width, height);  
  68.         if (USE_ORIGIN_TOPLEFT) {  
  69.             gl.glOrthof(0, width, -height, 001);  
  70.         } else {  
  71.             gl.glOrthof(0, width, 0, height, 01);  
  72.         }  
  73.         updateData(width, height);  
  74.     }  
  75.   
  76.     @Override  
  77.     public void onDrawFrame(GL10 gl) {  
  78.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);  
  79.         gl.glMatrixMode(GL10.GL_MODELVIEW);  
  80.         gl.glLoadIdentity();  
  81.         if (USE_ORIGIN_TOPLEFT) {  
  82.             gl.glScalef(1f, -1f, 1f);  
  83.         }  
  84.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);  
  85.           
  86.         vertices.position(0);  
  87.         gl.glVertexPointer(3, GL10.GL_FLOAT, vertexBytes, vertices);  
  88.         gl.glEnableClientState(GL10.GL_COLOR_ARRAY);  
  89.         vertices.position(3);  
  90.         gl.glColorPointer(4, GL10.GL_FLOAT, vertexBytes, vertices);  
  91.         gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, vertexNum);  
  92.           
  93.         gl.glDisableClientState(GL10.GL_COLOR_ARRAY);  
  94.         gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);  
  95.     }  
  96.       
  97.     private void loadData() {  
  98.         final float factor = 200f / 320f * 100;  
  99.         this.arrVertices = new float[] {   
  100.                 0f * factor, 0f * factor, 00011,  
  101.                 1f * factor, 0f * factor,   00011,  
  102.                 1f * factor, 1f * factor, 00011,  
  103.                 0f * factor,  1f * factor,  00011,  
  104.         };  
  105.         this.vertexBytes = (3 + 4) * (Integer.SIZE / 8);  
  106.         this.vertexNum = arrVertices.length / (this.vertexBytes / (Integer.SIZE / 8));  
  107.         this.tmpBuffer = new int[vertexNum * vertexBytes / (Integer.SIZE / 8)];  
  108.         ByteBuffer buffer = ByteBuffer.allocateDirect(vertexNum * vertexBytes);  
  109.         buffer.order(ByteOrder.nativeOrder());  
  110.         vertices = buffer.asIntBuffer();  
  111.         this.vertices.clear();  
  112.         for (int i = 0, j = 0; i < arrVertices.length; i++, j++) {  
  113.             tmpBuffer[j] = Float.floatToRawIntBits(arrVertices[i]);  
  114.         }  
  115.         this.vertices.put(tmpBuffer, 0, tmpBuffer.length);  
  116.         this.vertices.flip();  
  117.     }  
  118.       
  119.     private void updateData(int width, int height) {  
  120.         arrVertices[0] = 100f;  
  121.         arrVertices[1] = 100f;  
  122.         arrVertices[0 + 7] = width - 10;  
  123.         arrVertices[1 + 7] = 0;  
  124.         arrVertices[0 + 14] = width - 10;  
  125.         arrVertices[1 + 14] = height - 10;        
  126.         arrVertices[0 + 21] = 0;  
  127.         arrVertices[1 + 21] = height - 10;    
  128.         this.vertices.clear();  
  129.         for (int i = 0, j = 0; i < arrVertices.length; i++, j++) {  
  130.             tmpBuffer[j] = Float.floatToRawIntBits(arrVertices[i]);  
  131.         }  
  132.         this.vertices.put(tmpBuffer, 0, tmpBuffer.length);  
  133.         this.vertices.flip();  
  134.     }  
  135. }  

JOGL:

Java代码  收藏代码
  1. import java.awt.Frame;  
  2. import java.awt.event.WindowAdapter;  
  3. import java.awt.event.WindowEvent;  
  4. import java.nio.ByteBuffer;  
  5. import java.nio.ByteOrder;  
  6. import java.nio.IntBuffer;  
  7.   
  8. import javax.media.opengl.GL2ES1;  
  9. import javax.media.opengl.GLAutoDrawable;  
  10. import javax.media.opengl.GLCapabilities;  
  11. import javax.media.opengl.GLEventListener;  
  12. import javax.media.opengl.GLProfile;  
  13. import javax.media.opengl.awt.GLCanvas;  
  14.   
  15. import com.jogamp.opengl.util.FPSAnimator;  
  16.   
  17. /** 
  18.  * gluegen-rt-natives-windows-i586.jar 
  19.  * gluegen-rt.jar 
  20.  * gluegen.jar 
  21.  * jogl-all-natives-windows-i586.jar 
  22.  * jogl.all.jar 
  23.  *  
  24.  * @see sites.google.com/site/justinscsstuff/jogl-tutorial-3 
  25.  * @author Administrator 
  26.  * 
  27.  */  
  28. public class TestGLES1 implements GLEventListener {  
  29.     private boolean USE_ORIGIN_TOPLEFT = true;  
  30.       
  31.     private IntBuffer vertices;  
  32.     private int vertexBytes;  
  33.     private int vertexNum;  
  34.       
  35.     private float[] arrVertices;  
  36.     private int[] tmpBuffer;  
  37.       
  38.     @Override  
  39.     public void init(GLAutoDrawable drawable) {  
  40.         GL2ES1 gl = drawable.getGL().getGL2ES1();  
  41.         loadData();  
  42.         gl.glDisable(GL2ES1.GL_DITHER);  
  43.         gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL2ES1.GL_FASTEST);  
  44.         gl.glClearColor(1111);  
  45.         gl.glDisable(GL2ES1.GL_CULL_FACE);  
  46.         gl.glShadeModel(GL2ES1.GL_SMOOTH);  
  47.         gl.glEnable(GL2ES1.GL_DEPTH_TEST);  
  48.     }  
  49.   
  50.     @Override  
  51.     public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {  
  52.         GL2ES1 gl = drawable.getGL().getGL2ES1();  
  53.         gl.glMatrixMode(GL2ES1.GL_PROJECTION);  
  54.         gl.glLoadIdentity();  
  55.         gl.glViewport(00, w, h);  
  56.         //gl.glViewport(0, height, width, height);  
  57.         if (USE_ORIGIN_TOPLEFT) {  
  58.             gl.glOrthof(0, w, -h, 001);  
  59.         } else {  
  60.             gl.glOrthof(0, w, 0, h, 01);  
  61.         }  
  62.         updateData(w, h);  
  63.     }  
  64.       
  65.     @Override  
  66.     public void display(GLAutoDrawable drawable) {  
  67.         GL2ES1 gl = drawable.getGL().getGL2ES1();  
  68.         gl.glClear(GL2ES1.GL_COLOR_BUFFER_BIT | GL2ES1.GL_DEPTH_BUFFER_BIT);  
  69.         gl.glMatrixMode(GL2ES1.GL_MODELVIEW);  
  70.         gl.glLoadIdentity();  
  71.         if (USE_ORIGIN_TOPLEFT) {  
  72.             gl.glScalef(1f, -1f, 1f);  
  73.         }  
  74.         gl.glEnableClientState(GL2ES1.GL_VERTEX_ARRAY);  
  75.           
  76.         vertices.position(0);  
  77.         gl.glVertexPointer(3, GL2ES1.GL_FLOAT, vertexBytes, vertices);  
  78.         gl.glEnableClientState(GL2ES1.GL_COLOR_ARRAY);  
  79.         vertices.position(3);  
  80.         gl.glColorPointer(4, GL2ES1.GL_FLOAT, vertexBytes, vertices);  
  81.         gl.glDrawArrays(GL2ES1.GL_TRIANGLE_FAN, 0, vertexNum);  
  82.           
  83.         gl.glDisableClientState(GL2ES1.GL_COLOR_ARRAY);  
  84.         gl.glDisableClientState(GL2ES1.GL_VERTEX_ARRAY);  
  85.     }  
  86.   
  87.     @Override  
  88.     public void dispose(GLAutoDrawable drawable) {  
  89.           
  90.     }  
  91.           
  92.     private void loadData() {  
  93.         final float factor = 200f / 320f * 100;  
  94.         this.arrVertices = new float[] {   
  95.                 0f * factor, 0f * factor, 00011,  
  96.                 1f * factor, 0f * factor,   00011,  
  97.                 1f * factor, 1f * factor, 00011,  
  98.                 0f * factor,  1f * factor,  00011,  
  99.         };  
  100.         this.vertexBytes = (3 + 4) * (Integer.SIZE / 8);  
  101.         this.vertexNum = arrVertices.length / (this.vertexBytes / (Integer.SIZE / 8));  
  102.         this.tmpBuffer = new int[vertexNum * vertexBytes / (Integer.SIZE / 8)];  
  103.         ByteBuffer buffer = ByteBuffer.allocateDirect(vertexNum * vertexBytes);  
  104.         buffer.order(ByteOrder.nativeOrder());  
  105.         vertices = buffer.asIntBuffer();  
  106.         this.vertices.clear();  
  107.         for (int i = 0, j = 0; i < arrVertices.length; i++, j++) {  
  108.             tmpBuffer[j] = Float.floatToRawIntBits(arrVertices[i]);  
  109.         }  
  110.         this.vertices.put(tmpBuffer, 0, tmpBuffer.length);  
  111.         this.vertices.flip();  
  112.     }  
  113.       
  114.     private void updateData(int width, int height) {  
  115.         arrVertices[0] = 100f;  
  116.         arrVertices[1] = 100f;  
  117.         arrVertices[0 + 7] = width - 10;  
  118.         arrVertices[1 + 7] = 0;  
  119.         arrVertices[0 + 14] = width - 10;  
  120.         arrVertices[1 + 14] = height - 10;        
  121.         arrVertices[0 + 21] = 0;  
  122.         arrVertices[1 + 21] = height - 10;    
  123.         this.vertices.clear();  
  124.         for (int i = 0, j = 0; i < arrVertices.length; i++, j++) {  
  125.             tmpBuffer[j] = Float.floatToRawIntBits(arrVertices[i]);  
  126.         }  
  127.         this.vertices.put(tmpBuffer, 0, tmpBuffer.length);  
  128.         this.vertices.flip();  
  129.     }  
  130.       
  131.     public static void main(String[] args) {  
  132.         GLProfile glp = GLProfile.getDefault();  
  133.         GLCapabilities caps = new GLCapabilities(glp);  
  134.         GLCanvas canvas = new GLCanvas(caps);  
  135.   
  136.         Frame frame = new Frame("AWT Window Test");  
  137.         frame.setSize(240320);  
  138.         frame.setResizable(false);  
  139.         frame.setLocationRelativeTo(null);  
  140.         frame.add(canvas);  
  141.         frame.setVisible(true);  
  142.   
  143.         frame.addWindowListener(new WindowAdapter() {  
  144.             public void windowClosing(WindowEvent e) {  
  145.                 System.exit(0);  
  146.             }  
  147.         });  
  148.   
  149.         canvas.addGLEventListener(new TestGLES1());  
  150.           
  151.         FPSAnimator animator = new FPSAnimator(canvas, 60);  
  152.         animator.add(canvas);  
  153.         animator.start();  
  154.     }  
  155. }  

 

LWJGL:

Java代码  收藏代码
  1. import org.lwjgl.LWJGLException;  
  2. import org.lwjgl.opengl.Display;  
  3. import org.lwjgl.opengl.DisplayMode;  
  4. import org.lwjgl.opengl.GL11;  
  5.   
  6. /** 
  7.  * lwjgl.jar : native library location -> testlwjgl/native/windows 
  8.  *  
  9.  * @see http://lwjgl.org/wiki/index.php?title=LWJGL_Basics_1_(The_Display) 
  10.  * @see http://lwjgl.org/wiki/index.php?title=LWJGL_Basics_3_(The_Quad) 
  11.  * @author Administrator 
  12.  * 
  13.  */  
  14. public class Testlwjgl1 {  
  15.     private boolean USE_ORIGIN_TOPLEFT = true;  
  16.       
  17.     private final static int SCREEN_WIDTH = 240;  
  18.     private final static int SCREEN_HEIGHT = 320;  
  19.       
  20.     public void start() {  
  21.         try {  
  22.             Display.setDisplayMode(new DisplayMode(  
  23.                     SCREEN_WIDTH, SCREEN_HEIGHT));  
  24.             Display.create();  
  25.         } catch (LWJGLException e) {  
  26.             e.printStackTrace();  
  27.             System.exit(0);  
  28.         }  
  29.           
  30.         init();  
  31.           
  32.         while (!Display.isCloseRequested()) {  
  33.             render();  
  34.             Display.update();  
  35.         }  
  36.         Display.destroy();  
  37.     }  
  38.       
  39.     private void init() {  
  40.         GL11.glMatrixMode(GL11.GL_PROJECTION);  
  41.         GL11.glLoadIdentity();  
  42.         if (USE_ORIGIN_TOPLEFT) {  
  43.             GL11.glOrtho(0, SCREEN_WIDTH,   
  44.                     -SCREEN_HEIGHT, 0,   
  45.                     1, -1);           
  46.         } else {  
  47.             GL11.glOrtho(0, SCREEN_WIDTH,   
  48.                     0, SCREEN_HEIGHT,   
  49.                     1, -1);  
  50.         }  
  51.     }  
  52.       
  53.     private void render() {  
  54.         GL11.glMatrixMode(GL11.GL_MODELVIEW);  
  55.         GL11.glLoadIdentity();  
  56.         if (USE_ORIGIN_TOPLEFT) {  
  57.             GL11.glScalef(1f, -1f, 1f);  
  58.         }  
  59.           
  60.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT |   
  61.                 GL11.GL_DEPTH_BUFFER_BIT);    
  62.         GL11.glClearColor(1111);  
  63.           
  64.         GL11.glColor3f(0.0f, 0.0f, 1.0f);  
  65.         GL11.glBegin(GL11.GL_TRIANGLE_FAN);  
  66.         {  
  67.             GL11.glVertex2f(100100);  
  68.             GL11.glVertex2f(SCREEN_WIDTH - 100);  
  69.             GL11.glVertex2f(SCREEN_WIDTH - 10, SCREEN_HEIGHT - 10);  
  70.             GL11.glVertex2f(0, SCREEN_HEIGHT - 10);  
  71.         }  
  72.         GL11.glEnd();  
  73.     }  
  74.       
  75.     public static void main(String[] args) {  
  76.         new Testlwjgl1().start();  
  77.     }  
  78. }  

 

libgdx (使用g2d与pixmap,而非使用GLES绘画)

Java代码  收藏代码
  1. package com.iteye.weimingtom.libgdxtest;  
  2.   
  3. import com.badlogic.gdx.Application;  
  4. import com.badlogic.gdx.Gdx;  
  5. import com.badlogic.gdx.Screen;  
  6. import com.badlogic.gdx.graphics.Color;  
  7. import com.badlogic.gdx.graphics.FPSLogger;  
  8. import com.badlogic.gdx.graphics.GL10;  
  9. import com.badlogic.gdx.graphics.Pixmap;  
  10. import com.badlogic.gdx.graphics.Texture;  
  11. import com.badlogic.gdx.graphics.g2d.SpriteBatch;  
  12. import com.badlogic.gdx.graphics.g2d.TextureRegion;  
  13. import com.badlogic.gdx.math.MathUtils;  
  14. import com.badlogic.gdx.math.Vector2;  
  15.   
  16. public class Test001Screen implements Screen {  
  17.     private final static boolean DIRECTION_DOWN = true;  
  18.     private final static int BOX_W = 50;  
  19.     private final static int BOX_H = 50;  
  20.       
  21.     private int LOG_LEVEL = Application.LOG_DEBUG;  
  22.     private final static String TAG = "Test001Screen";   
  23.     private FPSLogger logger;  
  24.       
  25.     private SpriteBatch sb;  
  26.     private Pixmap pixmap;  
  27.     private Texture texture;  
  28.     private TextureRegion textureRegion;  
  29.     private Vector2 pos;  
  30.     private Vector2 dir;  
  31.   
  32.     public Test001Screen() {  
  33.         Gdx.app.setLogLevel(LOG_LEVEL);  
  34.         logger = new FPSLogger();  
  35.         init();  
  36.     }  
  37.       
  38.     private void init() {  
  39.         log("init");  
  40.         sb = new SpriteBatch();  
  41.         int w = Gdx.graphics.getWidth();  
  42.         int h = Gdx.graphics.getHeight();  
  43.         pixmap = new Pixmap(w, h, Pixmap.Format.RGBA8888);  
  44.         final int potW = MathUtils.nextPowerOfTwo(w);  
  45.         final int potH = MathUtils.nextPowerOfTwo(h);  
  46.         texture = new Texture(potW, potH, Pixmap.Format.RGBA8888);  
  47.         if (DIRECTION_DOWN) {  
  48.             textureRegion = new TextureRegion(texture, 00, w, h);  
  49.         } else {  
  50.             textureRegion = new TextureRegion(texture, 0, h, w, -h);  
  51.         }  
  52.         pos = new Vector2(w / 2, h / 2);  
  53.         dir = new Vector2(11);  
  54.     }  
  55.       
  56.     @Override  
  57.     public void dispose() {  
  58.         log("dispose");  
  59.         texture.dispose();  
  60.         pixmap.dispose();  
  61.         sb.dispose();  
  62.     }  
  63.       
  64.     @Override  
  65.     public void render(float delta) {  
  66.         onUpdate(delta);  
  67.         GL10 gl = Gdx.gl10;  
  68.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  
  69.         gl.glClearColor(0000);  
  70.         //gl.glClearColor(1, 1, 1, 1);  
  71.         onRender();  
  72.         sb.begin();  
  73.         texture.draw(pixmap, 00);  
  74.         sb.draw(textureRegion, 00);  
  75.         sb.end();  
  76.         logger.log();  
  77.     }  
  78.       
  79.     @Override  
  80.     public void resize(int width, int height) {  
  81.         sb.getProjectionMatrix().setToOrtho2D(00, width, height);  
  82.     }  
  83.   
  84.     @Override  
  85.     public void show() {  
  86.           
  87.     }  
  88.   
  89.     @Override  
  90.     public void hide() {  
  91.           
  92.     }  
  93.   
  94.     @Override  
  95.     public void pause() {  
  96.           
  97.     }  
  98.   
  99.     @Override  
  100.     public void resume() {  
  101.           
  102.     }  
  103.       
  104.     private void onUpdate(float delta) {  
  105.         int w = pixmap.getWidth();  
  106.         int h = pixmap.getHeight();  
  107.           
  108.         pos.x += dir.x * delta * 60;  
  109.         pos.y += dir.y * delta * 60;  
  110.         if (pos.x < 0) {  
  111.             dir.x = -dir.x;  
  112.             pos.x = 0;  
  113.         }  
  114.         if (pos.x > w) {  
  115.             dir.x = -dir.x;  
  116.             pos.x = w;  
  117.         }  
  118.         if (pos.y < 0) {  
  119.             dir.y = -dir.y;  
  120.             pos.y = 0;  
  121.         }  
  122.         if (pos.y > h) {  
  123.             dir.y = -dir.y;  
  124.             pos.y = h;  
  125.         }  
  126.     }  
  127.       
  128.     private void onRender() {  
  129.         pixmap.setColor(0000);  
  130.         pixmap.fill();  
  131.         pixmap.setColor(Color.BLUE);  
  132.         pixmap.fillRectangle(  
  133.             (int)pos.x - BOX_W / 2, (int)pos.y - BOX_H / 2,   
  134.             BOX_W, BOX_H);  
  135.     }  
  136.       
  137.     private void log(String message) {  
  138.         Gdx.app.log(TAG, message);  
  139.     }  
  140. }  

 

X. 参考资料:

1. Beginning Android 4 Games Development

随书代码Source Code/Downloads

http://www.apress.com/9781430239871

原文地址:https://www.cnblogs.com/sode/p/3148607.html