• Opengl ES 1.x NDK实例开发之三:多边形的旋转


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

    【实例讲解】


    本章在第二章的基础上讲解多边形的旋转

    上图中的三角形和四边形沿着Y轴旋转

    【实例源码】

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

  • 相关阅读:
    对this的浅解
    Java设计模式---工厂模式学习笔记
    MyBatis入门
    Java设计模式---单例模式学习笔记
    java设计模式--七大原则
    Servlet request常用方法
    maven中导入jdbc的坑
    Servlet生命周期
    JQuery选择器
    Test
  • 原文地址:https://www.cnblogs.com/SunkingYang/p/11049183.html
Copyright © 2020-2023  润新知