Opengl ES 1.x NDK实例开发之六:纹理贴图

转自:http://blog.csdn.net/mnorst/article/details/40346641

【实例讲解】

OpenglES要求生成纹理的图片长宽为2的n次方,支持各种格式(BMP, GIF, JPEG, PNG...)

本例中使用的图片为png格式,尺寸为128*128

本例中,在上层GLJNIView.java中生成纹理,将纹理句柄传递给Native层进行绘制,详见

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. private void genTexture(GL10 gl, Context context)  


【实例源码】

[GLJNIActivity.java]

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * Copyright (C) 2007 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *  
  16.  * author: mnorst@foxmail.com 
  17.  */  
  18.   
  19. package com.android.gljni;  
  20.   
  21. import com.android.gljni.GLJNIView;  
  22.   
  23. import android.app.Activity;  
  24. import android.os.Bundle;  
  25.   
  26. public class GLJNIActivity extends Activity {  
  27.     GLJNIView mView;  
  28.   
  29.     @Override  
  30.     protected void onCreate(Bundle icicle) {  
  31.         super.onCreate(icicle);  
  32.         mView = new GLJNIView(getApplication());  
  33.         setContentView(mView);  
  34.     }  
  35.   
  36.     @Override  
  37.     protected void onPause() {  
  38.         super.onPause();  
  39.         mView.onPause();  
  40.     }  
  41.   
  42.     @Override  
  43.     protected void onResume() {  
  44.         super.onResume();  
  45.         mView.onResume();  
  46.     }  
  47. }  
[GLJNIView.java]

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * Copyright (C) 2007 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *  
  16.  * author: mnorst@foxmail.com 
  17.  */  
  18.   
  19. package com.android.gljni;  
  20.   
  21. import java.io.IOException;  
  22. import java.io.InputStream;  
  23.   
  24. import javax.microedition.khronos.egl.EGLConfig;  
  25. import javax.microedition.khronos.opengles.GL10;  
  26.   
  27. import com.android.gljni.GLJNILib;  
  28. import com.android.gljnidemo06.R;  
  29.   
  30. import android.content.Context;  
  31. import android.graphics.Bitmap;  
  32. import android.graphics.BitmapFactory;  
  33. import android.opengl.GLSurfaceView;  
  34. import android.opengl.GLUtils;  
  35. import android.util.Log;  
  36.   
  37. /** 
  38.  * A simple GLSurfaceView sub-class that demonstrate how to perform 
  39.  * OpenGL ES 1.x rendering into a GL Surface. 
  40.  */  
  41. public class GLJNIView extends GLSurfaceView {  
  42.   
  43.     private static final String LOG_TAG = GLJNIView.class.getSimpleName();  
  44.   
  45.     private Renderer renderer;  
  46.   
  47.     public GLJNIView(Context context) {  
  48.         super(context);  
  49.   
  50.         // setEGLConfigChooser会对fps产生影响  
  51.         setEGLConfigChooser(8, 8, 8, 8, 16, 0);  
  52.   
  53.         renderer = new Renderer(context);  
  54.         setRenderer(renderer);  
  55.     }  
  56.   
  57.     private static class Renderer implements GLSurfaceView.Renderer {  
  58.         //用于纹理映射的绑定,并把绑定后的ID传递给C++代码,供其调用  
  59.         private int[] mTexture = new int[2];  
  60.         //用于加载Bitmap的context  
  61.         private Context mContext;  
  62.         public Renderer(Context ctx) {  
  63.             mContext = ctx;  
  64.         }  
  65.   
  66.         public void onDrawFrame(GL10 gl) {  
  67.             GLJNILib.step();  
  68.         }  
  69.   
  70.         public void onSurfaceChanged(GL10 gl, int width, int height) {  
  71.             GLJNILib.resize(width, height);  
  72.         }  
  73.   
  74.         public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
  75.             //用来绑定Bitmap纹理  
  76.             genTexture(gl, mContext);  
  77.             //调用本地setTexture方法,把纹理绑定的ID传递给C++代码,以供其调用  
  78.             GLJNILib.setTexture(mTexture);  
  79.             GLJNILib.init();  
  80.         }  
  81.           
  82.         /** 
  83.          * 加载Bitmap的方法, 
  84.          * 用来从res中加载Bitmap资源 
  85.          * */  
  86.         private Bitmap loadBitmap(Context context, int resourceId) {  
  87.             InputStream is = context.getResources().openRawResource(resourceId);  
  88.             Bitmap bitmap = null;  
  89.             try {  
  90.   
  91.                 // 利用BitmapFactory生成Bitmap  
  92.                 bitmap = BitmapFactory.decodeStream(is);  
  93.             } finally {  
  94.                 try {  
  95.   
  96.                     // 关闭流  
  97.                     is.close();  
  98.                     is = null;  
  99.                 } catch (IOException e) {  
  100.                     e.printStackTrace();  
  101.                 }  
  102.   
  103.             }  
  104.             return bitmap;  
  105.   
  106.         }  
  107.           
  108.         /** 
  109.          * 绑定Bitmap纹理 
  110.          * */  
  111.   
  112.         private void genTexture(GL10 gl, Context context) {  
  113.             //生成纹理  
  114.             gl.glGenTextures(2, mTexture, 0);  
  115.             //加载Bitmap  
  116.             Bitmap bitmap = loadBitmap(context, R.drawable.logo);  
  117.             if (bitmap != null) {  
  118.                 //如果bitmap加载成功,则生成此bitmap的纹理映射  
  119.                 gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexture[0]);  
  120.                 //设置纹理映射的属性  
  121.                 gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,  
  122.                         GL10.GL_NEAREST);  
  123.                 gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,  
  124.                         GL10.GL_NEAREST);  
  125.                 //生成纹理映射  
  126.                 GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);  
  127.                 //释放bitmap资源  
  128.                 bitmap.recycle();  
  129.             }  
  130.   
  131.         }  
  132.     }  
  133.   
  134. }  
[GLJNILib.java]

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * Copyright (C) 2007 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *  
  16.  * author: mnorst@foxmail.com 
  17.  */  
  18.   
  19. package com.android.gljni;  
  20.   
  21. //Wrapper for native library  
  22. public class GLJNILib {  
  23.       
  24.     static {  
  25.         System.loadLibrary("gljni");  
  26.     }  
  27.   
  28.     /** 
  29.      * @param width the current view width 
  30.      * @param height the current view height 
  31.      */  
  32.     public static native void resize(int width, int height);   
  33.       
  34.     /** 
  35.      * render  
  36.      */  
  37.     public static native void step();    
  38.       
  39.     /** 
  40.      * init 
  41.      */  
  42.     public static native void init();    
  43.       
  44.     /** 
  45.      * set the texture 
  46.      * @param texture   texture id 
  47.      */  
  48.     public static native void setTexture(int[] texture);  
  49. }  

[ [gl_code.cpp ]

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * Copyright (C) 2007 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *  
  16.  * author:  mnorst@foxmail.com 
  17.  * created: 2014/10/20 
  18.  * purpose: 纹理的使用 
  19.  */  
  20.   
  21. // OpenGL ES 1.x code  
  22.   
  23. #include <jni.h>  
  24. #include <android/log.h>  
  25.   
  26. #include <GLES/gl.h>  
  27. #include <GLES/glext.h>  
  28.   
  29. #include <stdio.h>  
  30. #include <stdlib.h>  
  31. #include <math.h>  
  32.   
  33. /************************************************************************/  
  34. /*                             定义                                     */  
  35. /************************************************************************/  
  36.   
  37. #define  LOG_TAG    "libgljni"  
  38. #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)  
  39. #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)  
  40.   
  41. //初始化纹理数组  
  42. GLuint *gTexture = 0;  
  43.   
  44. // 定义π  
  45. const GLfloat PI = 3.1415f;  
  46.   
  47. // 旋转角度  
  48. static GLfloat gAngle = 0.0f;   
  49.   
  50. // 顶点数组  
  51. const GLfloat gVertices[] = {  
  52.     0.0f, 1.0f, 0.0f,   // 上  
  53.     -1.0f,-1.0f, 0.0f,  // 左下  
  54.     1.0f,-1.0f, 0.0f,   // 右下  
  55. };  
  56.   
  57. const GLfloat gVerticesSquare[] = {  
  58.     -1.0f, -1.0f, 0.0f, // 左下  
  59.     1.0f, -1.0f, 0.0f,  // 右下  
  60.     -1.0f, 1.0f, 0.0f,  // 左上  
  61.     1.0f, 1.0f, 0.0f    // 右上  
  62. };  
  63.   
  64. // 纹理坐标  
  65. // 纹理坐标原点会因不同系统环境而有所不同。  
  66. // 比如在iOS以及Android上,纹理坐标原点(0, 0)是在左上角  
  67. // 而在OS X上,纹理坐标的原点是在左下角  
  68. const GLfloat gTextureCoord[] = {  
  69.     0.5f,0.0f,  
  70.     0.0f,1.0f,  
  71.     1.0f,1.0f,  
  72. };  
  73.   
  74. const GLfloat gTextureSquareCoord[] = {  
  75.     0.0f,1.0f,  
  76.     1.0f,1.0f,  
  77.     0.0f,0.0f,  
  78.     1.0f,0.0f,  
  79. };  
  80. /************************************************************************/  
  81. /*                             C++代码                                  */  
  82. /************************************************************************/  
  83.   
  84. static void printGLString(const char *name, GLenum s) {  
  85.     const char *v = (const char *) glGetString(s);  
  86.     LOGI("GL %s = %s\n", name, v);  
  87. }  
  88.   
  89. static void checkGlError(const char* op) {  
  90.     for (GLint error = glGetError(); error; error  
  91.         = glGetError()) {  
  92.             LOGI("after %s() glError (0x%x)\n", op, error);  
  93.     }  
  94. }  
  95.   
  96. bool init() {  
  97.     printGLString("Version", GL_VERSION);  
  98.     printGLString("Vendor", GL_VENDOR);  
  99.     printGLString("Renderer", GL_RENDERER);  
  100.     printGLString("Extensions", GL_EXTENSIONS);  
  101.   
  102.     // 启用阴影平滑  
  103.     glShadeModel(GL_SMOOTH);  
  104.   
  105.     // 黑色背景   
  106.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);     
  107.   
  108.     // 设置深度缓存     
  109.     glClearDepthf(1.0f);  
  110.   
  111.     // 启用深度测试  
  112.     glEnable(GL_DEPTH_TEST);      
  113.   
  114.     // 所作深度测试的类型      
  115.     glDepthFunc(GL_LEQUAL);   
  116.   
  117.     // 对透视进行修正    
  118.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    
  119.   
  120.     return true;  
  121. }  
  122.   
  123. static void _gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)  
  124. {  
  125.     GLfloat top = zNear * ((GLfloat) tan(fovy * PI / 360.0));  
  126.     GLfloat bottom = -top;  
  127.     GLfloat left = bottom * aspect;  
  128.     GLfloat right = top * aspect;  
  129.     glFrustumf(left, right, bottom, top, zNear, zFar);  
  130. }  
  131.   
  132. void resize(int width, int height)  
  133. {  
  134.     // 防止被零除  
  135.     if (height==0)                                
  136.     {  
  137.         height=1;  
  138.     }  
  139.   
  140.     // 重置当前的视口  
  141.     glViewport(0, 0, width, height);      
  142.     // 选择投影矩阵     
  143.     glMatrixMode(GL_PROJECTION);      
  144.     // 重置投影矩阵     
  145.     glLoadIdentity();                             
  146.   
  147.     // 设置视口的大小  
  148.     _gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);  
  149.   
  150.     // 选择模型观察矩阵  
  151.     glMatrixMode(GL_MODELVIEW);   
  152.   
  153.     // 重置模型观察矩阵  
  154.     glLoadIdentity();                             
  155. }  
  156.   
  157. void renderFrame() {  
  158.     // 清除屏幕及深度缓存  
  159.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    
  160.     // 设置背景颜色为黑色  
  161.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);  
  162.     // 重置当前的模型观察矩阵  
  163.     glLoadIdentity();         
  164.       
  165.     // 启用顶点数组  
  166.     glEnableClientState(GL_VERTEX_ARRAY);  
  167.     glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);  
  168.     // 纹理设置  
  169.     glEnable(GL_TEXTURE_2D);                                // 启用纹理映射  
  170.     glBindTexture(GL_TEXTURE_2D, gTexture[0]);              // 选择纹理  
  171.     glEnableClientState(GL_TEXTURE_COORD_ARRAY);            // 启用纹理坐标数组  
  172.   
  173.     // 绘制三角形  
  174.     glTranslatef(0.0f,2.0f,-10.0f);                         // 设置三角形位置  
  175.     glRotatef(gAngle,0.0f,1.0f,0.0f);                       // 旋转三角形  
  176.     glVertexPointer(3, GL_FLOAT, 0, gVertices);             // 指定顶点数组  
  177.     glTexCoordPointer(2, GL_FLOAT, 0, gTextureCoord);       // 设置纹理坐标  
  178.     glDrawArrays(GL_TRIANGLES, 0, 3);                       // 绘制三角形  
  179.   
  180.     // 绘制正方形  
  181.     glTranslatef(0.0f,-4.0f,0.0f);                          // 设置正方形位置  
  182.     glRotatef(-gAngle*2,0.0f,1.0f,0.0f);                    // 旋转正方形  
  183.     glVertexPointer(3, GL_FLOAT, 0, gVerticesSquare);       // 指定顶点数组  
  184.     glTexCoordPointer(2, GL_FLOAT, 0, gTextureSquareCoord); // 设置纹理坐标  
  185.     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);                  // 绘制正方形  
  186.   
  187.     // 关闭顶点数组  
  188.     glDisableClientState(GL_VERTEX_ARRAY);  
  189.     // 关闭纹理数组  
  190.     glDisableClientState(GL_TEXTURE_COORD_ARRAY);  
  191.     glDisable(GL_TEXTURE_2D);  
  192.   
  193.     // 增加旋转角度  
  194.     gAngle += 2.0f;  
  195. }  
  196.   
  197. /************************************************************************/  
  198. /*                          JNI代码                                     */  
  199. /************************************************************************/  
  200.   
  201. extern "C" {  
  202.     JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_resize(JNIEnv * env, jobject obj,  jint width, jint height);  
  203.     JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_step(JNIEnv * env, jobject obj);  
  204.     JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_init(JNIEnv * env, jobject obj);  
  205.     JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_setTexture(JNIEnv * env, jclass obj, jintArray tex);  
  206. };  
  207.   
  208. JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_resize(JNIEnv * env, jobject obj,  jint width, jint height)  
  209. {  
  210.     resize(width, height);  
  211. }  
  212.   
  213. JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_step(JNIEnv * env, jobject obj)  
  214. {  
  215.     renderFrame();  
  216. }  
  217.   
  218. JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_init(JNIEnv * env, jobject obj)  
  219. {  
  220.     init();  
  221. }  
  222.   
  223. JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_setTexture(JNIEnv * env, jclass obj, jintArray tex)  
  224. {  
  225.     gTexture = (GLuint *)env->GetIntArrayElements(tex,0);  
  226. }  

转载于:https://www.cnblogs.com/SunkingYang/p/11049180.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值