openGL上画背景图片

转自:http://blog.csdn.net/sandy_kisa/article/details/6910381

Android上关于openGL画背景图片的相当少,对openGL知识更是小白一个。

纠缠了几天,总算找到例子学习,修改完成了,希望能给其它需要此功能的人提供一些帮助。

直接贴代码,有很多关于openGL的参数,也是一知半解的。

想要更加深入学习的,参考http://www.owlei.com/DancingWind/Course/Tutorial_32.htm学习吧。

先画背景图放远些,然后扩大填充整个画面,然后在画其它的物体,注意各物体的深度。

  1. import javax.microedition.khronos.egl.EGLConfig;  
  2. import javax.microedition.khronos.opengles.GL10;  
  3. import android.content.Context;  
  4. import android.content.res.Resources;  
  5. import android.opengl.GLU;  
  6. import android.opengl.GLSurfaceView.Renderer;  
  7. public class OpenGLRenderer implements Renderer {  
  8.   
  9.     private float centerX = -1;  
  10.     private float centerY =  -5f;  
  11.     private float centerZ = -15f;  
  12.     private Context mContext  ;  
  13.     private int mTexture;  
  14.     private Cube cube;  
  15.   
  16.     public OpenGLRenderer(Context context){  
  17.         super();  
  18.         this.mContext = context;  
  19.          cube = new Cube(); //所画物体  
  20.          cube .z = centerZ;  
  21.          cube.y = centerY;  
  22.          cube.x = centerX;  
  23.      }  
  24.        
  25.      public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
  26.         // Set the background color to black ( rgba ).  
  27.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);  
  28.         // Enable Smooth Shading, default not really needed.  
  29.         gl.glShadeModel(GL10.GL_SMOOTH);  
  30.         // Depth buffer setup.  
  31.         gl.glClearDepthf(1.0f);  
  32.         // Enables depth testing.  
  33.         gl.glEnable(GL10.GL_DEPTH_TEST);  
  34.         // The type of depth testing to do.  
  35.         gl.glDepthFunc(GL10.GL_LEQUAL);  
  36.         // Really nice perspective calculations.  
  37.         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,  
  38.                           GL10.GL_NICEST);  
  39.   
  40.       
  41.         //背景图片  
  42.         Resources res = mContext.getResources();  
  43.         this.mTexture = GraphicUtil.loadTexture(gl, res, R.drawable.image);  
  44.   
  45.     }  
  46.       
  47.     public void onDrawFrame(GL10 gl) {  
  48.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  
  49.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);   
  50.   
  51.         //背景  
  52.         drawBackgroundImage(gl,mTexture);  
  53.         cube.draw(gl);  
  54.   
  55.     }  
  56.       
  57.     public void drawBackgroundImage(GL10 gl, int textureId) {  
  58.         gl.glLoadIdentity();  
  59.         gl.glCullFace(GL10.GL_BACK);  
  60.         gl.glPushMatrix();  
  61.   
  62.         gl.glTranslatef(00,-20);//比所画物体更远一些  
  63.         gl.glScalef(2f, 2f, 1f);//扩大填充至整个界面  
  64.   
  65.         gl.glEnable(GL10.GL_DEPTH_TEST);  
  66.         gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);  
  67.   
  68.         GraphicUtil.drawTexture(gl, 0.0f, 0.0f, 8.0f, 8.0f,  
  69.                 textureId, 1.0f, 1.0f, 1.0f, 1.0f);  
  70.         gl.glPopMatrix();  
  71.         gl.glDisable(GL10.GL_DEPTH_TEST);  
  72.     }  
  73.   
  74.     public void onSurfaceChanged(GL10 gl, int width, int height) {  
  75.         // Sets the current view port to the new size.  
  76.         gl.glViewport(00, width, height);  
  77.         // Select the projection matrix  
  78.         gl.glMatrixMode(GL10.GL_PROJECTION);  
  79.         // Reset the projection matrix  
  80.         gl.glLoadIdentity();  
  81.         // Calculate the aspect ratio of the window  
  82.         GLU.gluPerspective(gl, 45.0f,  
  83.                                    (float) width / (float) height,  
  84.                                    0.1f, 100.0f);  
  85.         // Select the modelview matrix  
  86.         gl.glMatrixMode(GL10.GL_MODELVIEW);  
  87.         // Reset the modelview matrix  
  88.         gl.glLoadIdentity();  
  89.     }  

  1. <p>  
  2. import java.nio.ByteBuffer;  
  3. import java.nio.ByteOrder;  
  4. import java.nio.FloatBuffer;  
  5. import java.util.Hashtable;  
  6. import javax.microedition.khronos.opengles.GL10;  
  7. import javax.microedition.khronos.opengles.GL11;  
  8.   
  9. import android.content.res.Resources;  
  10. import android.graphics.Bitmap;  
  11. import android.graphics.BitmapFactory;  
  12. import android.graphics.Bitmap.Config;  
  13. import android.opengl.GLUtils;  
  14.   
  15. public class GraphicUtil {  
  16.   
  17.     private static Hashtable<Integer, float[]> verticesPool = new Hashtable<Integer, float[]>();  
  18.     private static Hashtable<Integer, float[]> colorsPool = new Hashtable<Integer, float[]>();  
  19.     private static Hashtable<Integer, float[]> coordsPool = new Hashtable<Integer, float[]>();  
  20.   
  21.     public static float[] getVertices(int n) {  
  22.         if (verticesPool.containsKey(n)) {  
  23.             return verticesPool.get(n);  
  24.         }  
  25.         float[] vertices = new float[n];  
  26.         verticesPool.put(n, vertices);  
  27.         return vertices;  
  28.     }  
  29.   
  30.     public static float[] getColors(int n) {  
  31.         if (colorsPool.containsKey(n)) {  
  32.             return colorsPool.get(n);  
  33.         }  
  34.         float[] colors = new float[n];  
  35.         colorsPool.put(n, colors);  
  36.         return colors;  
  37.     }  
  38.   
  39.     public static float[] getCoords(int n) {  
  40.         if (coordsPool.containsKey(n)) {  
  41.             return coordsPool.get(n);  
  42.         }  
  43.         float[] coords = new float[n];  
  44.         coordsPool.put(n, coords);  
  45.         return coords;  
  46.     }  
  47.   
  48.     private static Hashtable<Integer, FloatBuffer> squareVerticesPool = new Hashtable<Integer, FloatBuffer>();  
  49.     private static Hashtable<Integer, FloatBuffer> squareColorsPool = new Hashtable<Integer, FloatBuffer>();  
  50.     private static Hashtable<Integer, FloatBuffer> texCoordsPool = new Hashtable<Integer, FloatBuffer>();  
  51.   
  52.     public static final FloatBuffer makeVerticesBuffer(float[] arr) {  
  53.         FloatBuffer fb = null;  
  54.         if (squareVerticesPool.containsKey(arr.length)) {  
  55.             fb = squareVerticesPool.get(arr.length);  
  56.             fb.clear();  
  57.             fb.put(arr);  
  58.             fb.position(0);  
  59.             return fb;  
  60.         }  
  61.         fb = makeFloatBuffer(arr);  
  62.         squareVerticesPool.put(arr.length, fb);  
  63.         return fb;  
  64.     }  
  65.   
  66.     public static final FloatBuffer makeColorsBuffer(float[] arr) {  
  67.         FloatBuffer fb = null;  
  68.         if (squareColorsPool.containsKey(arr.length)) {  
  69.             fb = squareColorsPool.get(arr.length);  
  70.             fb.clear();  
  71.             fb.put(arr);  
  72.             fb.position(0);  
  73.             return fb;  
  74.         }  
  75.         fb = makeFloatBuffer(arr);  
  76.         squareColorsPool.put(arr.length, fb);  
  77.         return fb;  
  78.     }  
  79.   
  80.     public static final FloatBuffer makeTexCoordsBuffer(float[] arr) {  
  81.         FloatBuffer fb = null;  
  82.         if (texCoordsPool.containsKey(arr.length)) {  
  83.             fb = texCoordsPool.get(arr.length);  
  84.             fb.clear();  
  85.             fb.put(arr);  
  86.             fb.position(0);  
  87.             return fb;  
  88.         }  
  89.         fb = makeFloatBuffer(arr);  
  90.         texCoordsPool.put(arr.length, fb);  
  91.         return fb;  
  92.     }  
  93.   
  94.         //画四边形,贴图  
  95.     public static final void drawTexture(GL10 gl, float x, float y, float w, float h, int texture, float u, float v, float tex_w, float tex_h, float r, float g, float b, float a) {  
  96.         float[] vertices = getVertices(8);  
  97.         vertices[0] = -0.5f * w + x; vertices[1] = -0.5f * h + y;  
  98.         vertices[2] =  0.5f * w + x; vertices[3] = -0.5f * h + y;  
  99.         vertices[4] = -0.5f * w + x; vertices[5] =  0.5f * h + y;  
  100.         vertices[6] =  0.5f * w + x; vertices[7] =  0.5f * h + y;  
  101.   
  102.         float[] colors = getColors(16);  
  103.         for (int i = 0; i < 16; i++) {  
  104.             colors[i++] = r;  
  105.             colors[i++] = g;  
  106.             colors[i++] = b;  
  107.             colors[i]   = a;  
  108.         }  
  109.   
  110.         float[] coords = getCoords(8);  
  111.         coords[0] = u; coords[1] = v + tex_h;  
  112.         coords[2] = u + tex_w; coords[3] = v + tex_h;  
  113.         coords[4] = u; coords[5] = v;  
  114.         coords[6] = u + tex_w; coords[7] = v;  
  115.   
  116.         FloatBuffer squareVertices = makeVerticesBuffer(vertices);  
  117.         FloatBuffer squareColors = makeColorsBuffer(colors);  
  118.         FloatBuffer texCoords = makeTexCoordsBuffer(coords);  
  119.   
  120.         gl.glEnable(GL10.GL_TEXTURE_2D);  
  121.         gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);  
  122.         gl.glVertexPointer(2, GL10.GL_FLOAT, 0, squareVertices);  
  123.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);  
  124.         gl.glColorPointer(4, GL10.GL_FLOAT, 0, squareColors);  
  125.         gl.glEnableClientState(GL10.GL_COLOR_ARRAY);  
  126.         gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoords);  
  127.         gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  
  128.   
  129.         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 04);  
  130.   
  131.         gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  
  132.         gl.glDisable(GL10.GL_TEXTURE_2D);  
  133.     }  
  134.   
  135.     public static final void drawTexture(GL10 gl, float x, float y, float w, float h, int texture, float r, float g, float b, float a) {  
  136.         drawTexture(gl, x, y, w, h, texture, 0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a);  
  137.     }  
  138.   
  139.     public static final int loadTexture(GL10 gl, Resources resources, int res) {  
  140.         int[] textures = new int[1];  
  141.   
  142.         //Bitmap  
  143.         Bitmap bmp = BitmapFactory.decodeResource(resources, res, options);  
  144.         if (bmp == null) {  
  145.             return 0;  
  146.         }  
  147.   
  148.         gl.glGenTextures(1, textures, 0);  
  149.         gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);  
  150.         GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);  
  151.         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);  
  152.         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);  
  153.         gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);  
  154.   
  155.         bmp.recycle();  
  156.   
  157.           return textures[0];  
  158.     }  
  159.     private static final BitmapFactory.Options options = new BitmapFactory.Options();  
  160.     static {  
  161.         options.inScaled = false;  
  162.         options.inPreferredConfig = Config.ARGB_8888;  
  163.     }  
  164.   
  165.     public static final FloatBuffer makeFloatBuffer(float[] arr) {  
  166.         ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);  
  167.         bb.order(ByteOrder.nativeOrder());  
  168.         FloatBuffer fb = bb.asFloatBuffer();  
  169.         fb.put(arr);  
  170.         fb.position(0);  
  171.         return fb;  
  172.     }  
  173. }  
  174. </p>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值