Opengl ES 1.x NDK实例开发之二:颜色的使用

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

【实例讲解】


本章在第一章的基础上讲解颜色的使用。

本例中绘制了一个三角形和一个正方形,三角形用的是光滑着色,正方形用的是平面着色。使用Flat coloring(单调着色)给四边形涂上固定的一种颜色。使用Smooth coloring(平滑着色)将三角形的三个顶点的不同颜色混合在一起,创建漂亮的色彩混合。

[单一颜色渲染]

  1. glColor4f(1.0f, 0.0f, 0.0f, 1.0f);  
[顶点渲染混合]

  1. glEnableClientState(GL_COLOR_ARRAY);            // 启用颜色数组  
  2. glColorPointer(4, GL_FLOAT, 0, gColors);                // 指定颜色数组  
  3. glDisableClientState(GL_COLOR_ARRAY);           // 关闭颜色数组  
需注意的是,如果调用了glEnableClientState(GL_COLOR_ARRAY),则在代码中就要调用glColorPointer(4, GL_FLOAT, 0, gColors)来指定颜色数组,否则在glDrawArrays(GL_TRIANGLES, 0, 3)会因找不到颜色数组而报错

【实例源码】

[GLJNIActivity.java]

[java] view plaincopy在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]

[java] view plaincopy在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 javax.microedition.khronos.egl.EGLConfig;  
  22. import javax.microedition.khronos.opengles.GL10;  
  23.   
  24. import com.android.gljni.GLJNILib;  
  25.   
  26. import android.content.Context;  
  27. import android.opengl.GLSurfaceView;  
  28.   
  29. /** 
  30.  * A simple GLSurfaceView sub-class that demonstrate how to perform 
  31.  * OpenGL ES 1.x rendering into a GL Surface. 
  32.  */  
  33. public class GLJNIView extends GLSurfaceView {  
  34.   
  35.     private static final String LOG_TAG = GLJNIView.class.getSimpleName();  
  36.   
  37.     private Renderer renderer;  
  38.   
  39.     public GLJNIView(Context context) {  
  40.         super(context);  
  41.   
  42.         // setEGLConfigChooser会对fps产生影响  
  43.         setEGLConfigChooser(8888160);  
  44.   
  45.         renderer = new Renderer(context);  
  46.         setRenderer(renderer);  
  47.     }  
  48.   
  49.     private static class Renderer implements GLSurfaceView.Renderer {  
  50.   
  51.         public Renderer(Context ctx) {  
  52.   
  53.         }  
  54.   
  55.         public void onDrawFrame(GL10 gl) {  
  56.             GLJNILib.step();  
  57.         }  
  58.   
  59.         public void onSurfaceChanged(GL10 gl, int width, int height) {  
  60.             GLJNILib.resize(width, height);  
  61.         }  
  62.   
  63.         public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
  64.             GLJNILib.init();  
  65.         }  
  66.     }  
  67.   
  68. }  
[GLJNILib.java]

[java] view plaincopy在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.     public static native void step();    
  35.       
  36.     public static native void init();    
  37. }  


[gl_code.cpp]

  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/09/26 
  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. const GLfloat PI = 3.1415f;  
  43.   
  44. // 顶点数组  
  45. const GLfloat gVertices[] = {  
  46.     0.0f, 1.0f, 0.0f,   // 上  
  47.     -1.0f,-1.0f, 0.0f,  // 左下  
  48.     1.0f,-1.0f, 0.0f,   // 右下  
  49. };  
  50.   
  51. const GLfloat gVerticesSquare[] = {  
  52.     -1.0f, -1.0f, 0.0f, // 左下  
  53.     1.0f, -1.0f, 0.0f,  // 右下  
  54.     -1.0f, 1.0f, 0.0f,  // 左上  
  55.     1.0f, 1.0f, 0.0f    // 右上  
  56. };  
  57.   
  58. // 颜色数组  
  59. const GLfloat gColors[] = {  
  60.     1.0f,0.0f,0.0f, 1.0f,  
  61.     0.0f,1.0f,0.0f, 1.0f,  
  62.     0.0f,0.0f,1.0f, 1.0f,  
  63. };  
  64.   
  65. /************************************************************************/  
  66. /*                             C++代码                                  */  
  67. /************************************************************************/  
  68.   
  69. static void printGLString(const char *name, GLenum s) {  
  70.     const char *v = (const char *) glGetString(s);  
  71.     LOGI("GL %s = %s\n", name, v);  
  72. }  
  73.   
  74. static void checkGlError(const char* op) {  
  75.     for (GLint error = glGetError(); error; error  
  76.         = glGetError()) {  
  77.             LOGI("after %s() glError (0x%x)\n", op, error);  
  78.     }  
  79. }  
  80.   
  81. bool init() {  
  82.     printGLString("Version", GL_VERSION);  
  83.     printGLString("Vendor", GL_VENDOR);  
  84.     printGLString("Renderer", GL_RENDERER);  
  85.     printGLString("Extensions", GL_EXTENSIONS);  
  86.   
  87.     // 启用阴影平滑  
  88.     glShadeModel(GL_SMOOTH);  
  89.   
  90.     // 黑色背景   
  91.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);     
  92.   
  93.     // 设置深度缓存     
  94.     glClearDepthf(1.0f);  
  95.   
  96.     // 启用深度测试  
  97.     glEnable(GL_DEPTH_TEST);      
  98.   
  99.     // 所作深度测试的类型      
  100.     glDepthFunc(GL_LEQUAL);   
  101.   
  102.     // 对透视进行修正    
  103.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    
  104.   
  105.     return true;  
  106. }  
  107.   
  108. static void _gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)  
  109. {  
  110.     GLfloat top = zNear * ((GLfloat) tan(fovy * PI / 360.0));  
  111.     GLfloat bottom = -top;  
  112.     GLfloat left = bottom * aspect;  
  113.     GLfloat right = top * aspect;  
  114.     glFrustumf(left, right, bottom, top, zNear, zFar);  
  115. }  
  116.   
  117. void resize(int width, int height)  
  118. {  
  119.     // 防止被零除  
  120.     if (height==0)                                
  121.     {  
  122.         height=1;  
  123.     }  
  124.   
  125.     // 重置当前的视口  
  126.     glViewport(0, 0, width, height);      
  127.     // 选择投影矩阵     
  128.     glMatrixMode(GL_PROJECTION);      
  129.     // 重置投影矩阵     
  130.     glLoadIdentity();                             
  131.   
  132.     GLfloat ratio = (GLfloat)width/(GLfloat)height;  
  133.   
  134.     // 设置视口的大小  
  135.     _gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);  
  136.   
  137.     // 选择模型观察矩阵  
  138.     glMatrixMode(GL_MODELVIEW);   
  139.   
  140.     // 重置模型观察矩阵  
  141.     glLoadIdentity();                             
  142. }  
  143.   
  144. void renderFrame() {  
  145.     // 清除屏幕及深度缓存  
  146.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    
  147.     // 设置背景颜色为黑色  
  148.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);  
  149.     // 重置当前的模型观察矩阵  
  150.     glLoadIdentity();         
  151.       
  152.     // 启用顶点数组  
  153.     glEnableClientState(GL_VERTEX_ARRAY);  
  154.   
  155.     // 绘制三角形  
  156.     glTranslatef(0.0f,2.0f,-10.0f);                     // 设置三角形位置  
  157.     glEnableClientState(GL_COLOR_ARRAY);                // 启用颜色数组  
  158.     glColorPointer(4, GL_FLOAT, 0, gColors);            // 指定颜色数组  
  159.     glVertexPointer(3, GL_FLOAT, 0, gVertices);         // 指定顶点数组  
  160.     glDrawArrays(GL_TRIANGLES, 0, 3);                   // 绘制三角形  
  161.     glDisableClientState(GL_COLOR_ARRAY);               // 关闭颜色数组  
  162.       
  163.     // 绘制正方形  
  164.     glTranslatef(0.0f,-4.0f,0.0f);                      // 设置正方形位置  
  165.     glColor4f(1.0f, 0.0f, 0.0f, 1.0f);                  // 设置颜色为红色  
  166.     glVertexPointer(3, GL_FLOAT, 0, gVerticesSquare);   // 指定顶点数组  
  167.     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);              // 绘制正方形  
  168.       
  169.     // 关闭顶点数组  
  170.     glDisableClientState(GL_VERTEX_ARRAY);  
  171. }  
  172.   
  173. /************************************************************************/  
  174. /*                          JNI代码                                     */  
  175. /************************************************************************/  
  176.   
  177. extern "C" {  
  178.     JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_resize(JNIEnv * env, jobject obj,  jint width, jint height);  
  179.     JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_step(JNIEnv * env, jobject obj);  
  180.     JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_init(JNIEnv * env, jobject obj);  
  181. };  
  182.   
  183. JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_resize(JNIEnv * env, jobject obj,  jint width, jint height)  
  184. {  
  185.     resize(width, height);  
  186. }  
  187.   
  188. JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_step(JNIEnv * env, jobject obj)  
  189. {  
  190.     renderFrame();  
  191. }  
  192.   
  193. JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_init(JNIEnv * env, jobject obj)  
  194. {  
  195.     init();  
  196. }  


[Android.mk]

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. LOCAL_PATH:= $(call my-dir)    
  2.     
  3. include $(CLEAR_VARS)    
  4.     
  5. LOCAL_MODULE    :libgljni    
  6. LOCAL_CFLAGS    := -Werror  
  7. LOCAL_SRC_FILES :gl_code.cpp    
  8. LOCAL_LDLIBS    := -llog -lGLESv1_CM    
  9.     
  10. include $(BUILD_SHARED_LIBRARY)    

posted @ 2015-08-25 13:26  SunkingYang  阅读(226)  评论(0编辑  收藏  举报