Introduction to the development framework see: One of Opengl ES NDK Instance development: building a development framework
This chapter draws a rotated texture pyramid based on the sixth chapter (six: Texture mapping) of the Opengl ES 1.x NDK instance development, with the same principle as texture mapping, and it is important to note that the vertex arrays and texture arrays of the pyramids are fixed.
"Example Explanation"
"Instance Source"
[Gljniactivity.java]
/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. * * Author: [email protected] */package com.android.gljni;import java.text.decimalformat;import Com.android.gljni.gljniview;import Android.app.activity;import Android.os.bundle;import Android.os.Handler;import Android.os.message;import Android.view.viewgroup.layoutparams;import Android.widget.textview;public Class Gljniactivity extends Activity {Gljniview mView; TextView Mtextview; @Overrideprotected void OnCreate (Bundle icicle) {super.oncreate (icicle); mView = new Gljniview (Getapplication ()); Setcontentview (MView); Mview.sethandler (new Handler () {@Override public void Handlemessage (Message ms g) {super.handlemessage (msg); // ??? FPS mtextview.settext ("fps:" +msg.what); }}); Mtextview = new TextView (this), Mtextview.settext ("fps:0"); Addcontentview (Mtextview, New Layoutparams (L Ayoutparams.wrap_content, Layoutparams.wrap_content));} @Overrideprotected void OnPause () {super.onpause (); Mview.onpause ();} @Overrideprotected void Onresume () {super.onresume (); Mview.onresume ();}}
[Gljniview.java]
/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. * * Author: [email protected] */package com.android.gljni;import java.io.ioexception;import java.io.InputStream; Import Javax.microedition.khronos.egl.eglconfig;import Javax.microedition.khronos.opengles.gl10;import Com.android.gljni.gljnilib;import Com.android.gljnidemo08.r;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import ANDROID.OPengl. Glsurfaceview;import android.opengl.glutils;import android.os.handler;import android.util.log;/** * A Simple Glsurfaceview sub-class that demonstrate-perform * OpenGL ES 1.x rendering into a GL Surface. */public class Gljniview extends Glsurfaceview {private static final String Log_tag = GLJNIView.class.getSimpleName ();p RI Vate Renderer renderer;public Gljniview (context context) {super (context);// Seteglconfigchooser will have an impact on FPS Seteglconfigchooser (8, 8, 8, 8, 0), renderer = new renderer (context); Setrenderer ( renderer);} public void SetHandler (Handler Handler) {Renderer.sethandler (Handler);} private static class Renderer implements Glsurfaceview.renderer {//For texture map bindings and pass the bound ID to C + + code for it to call private int[] Mtexture = new int[2];//for loading bitmap contextprivate Context mcontext;//statistics fpsprivate Handler mhandler;private Long mstartmili;private long mendmili;private int mFps = 0;public Renderer (Context ctx) {mcontext = Ctx;mstartmili =system.curr Enttimemillis ();} public void SetHandler (HandlEr handler) {mhandler = handler;} public void Ondrawframe (GL10 gl) {gljnilib.step ();//The number of frames drawn in one minute to count Fpsmendmili = System.currenttimemillis (); if (Mendmili -Mstartmili >) {mhandler.sendemptymessagedelayed (mFps); Mstartmili = Mendmili;mfps = 0;} mfps++;} public void onsurfacechanged (GL10 gl, int width, int height) {gljnilib.resize (width, height);} public void onsurfacecreated (GL10 gl, EGLConfig config) {//used to bind bitmap texture gentexture (GL, mcontext);//Call local SetTexture method, Pass the ID of the texture binding to the C + + code for it to call Gljnilib.settexture (mtexture); Gljnilib.init ();} /** * Method of loading Bitmap, * used to load BITMAP resources from Res * */private Bitmap LoadBitmap (context context, int resourceId) {InputStream is = Co Ntext.getresources (). Openrawresource (ResourceId); Bitmap Bitmap = null;try {//Use Bitmapfactory to generate Bitmapbitmap = Bitmapfactory.decodestream (is);} finally {try {//close stream Is.clos E (); is = null;} catch (IOException e) {e.printstacktrace ();}} return bitmap;} /** * Bind bitmap texture * */private void Gentexture (GL10 GL, Context context) {//Generate texture Gl.glgentextures(2, mtexture, 0);//load Bitmapbitmap bitmap = LoadBitmap (context, R.drawable.logo); if (bitmap! = null) {//If bitmap load succeeds, The texture map of this bitmap is generated gl.glbindtexture (gl10.gl_texture_2d, mtexture[0]);//Set the properties of the texture map Gl.gltexparameterf (gl10.gl_texture_ Gl10.gl_texture_min_filter,gl10.gl_nearest) Gl.gltexparameterf (gl10.gl_texture_2d, GL10.GL_TEXTURE_MAG_ Filter,gl10.gl_nearest);//Generate Texture Map glutils.teximage2d (gl10.gl_texture_2d, 0, bitmap, 0);//Release bitmap Resources bitmap.recycle () ;}}}}
[Gljnilib.java]
/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. * * Author: [email protected] */package com.android.gljni;//wrapper for native Librarypublic class Gljnilib {static {system.loadlibrary ("Gljni");} /** * @param width The current view width * @param height The current view height */public static native void Resize (int width, int height); /** * Render */public static native void step (); /** * init */PublIC Static native void Init (); /** * Set the texture * @param texturetexture ID */public static native void SetTexture (int[] texture);}
[Gl_code.cpp]
/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. * * Author: [email protected] * created:2015/01/06 * Purpose: Rotated texture pyramid *///OpenGL ES 1.x code#include <jni.h># Include <android/log.h> #include <GLES/gl.h> #include <GLES/glext.h> #include <stdio.h># Include <stdlib.h> #include <math.h>/**************************************************************** * Definition *//************************************************************************/#define LOG_TAG " Libgljni "#define LOGI (...) __android_log_print (android_log_info,log_tag,__va_args__) #define LOGE (...) __android_log_print (android_log_error,log_tag,__va_args__)//Initialize texture array Gluint *gtexture = 0;//define πconst GLfloat PI = 3.1415f;//defining vertex coordinates # define POS 1.0f//defining vertex coordinates const GLFLOAT gvertices[] = {//Bottom face -1.0f,-1.0f,1.0f,1.0f,-1.0f,1.0f,1.0f,-1.0f , -1.0f,1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f,1.0f,//side 0.0f, 1.0f, 0.0f,-1.0f,-1.0f, 1.0f,1.0f,-1.0f, 1.0f , 0.0f, 1.0f, 0.0f,1.0f,-1.0f, 1.0f,1.0f,-1.0f, -1.0f,0.0f, 1.0f, 0.0f,1.0f,-1.0f, -1.0f,-1.0f,-1.0f, -1.0f,0.0f, 1.0f, 0.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f, 1.0f};//defines texture coordinates//texture coordinates origin will vary depending on the system environment. For example, on iOS and Android, the texture coordinates origin (0, 0) is in the upper-left corner//and on OS X, the origin of texture coordinates is in the lower-left corner of static glfloat texcoords[] = {///ground stitching into an entire texture 0.0f, 1.0f,1.0f, 1 .0f,1.0f, 0.0f,1.0f, 0.0f,0.0f, 0.0f,0.0f, 1.0f,0.5f, 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.5f, 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.5f, 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.5f, 0.0f,0.0f, 1.0f,1.0f, 1.0f,}; Rotation angle static Glfloat Gangle = 0.0f;/************************************************************************//* C + + code *//************************************************************* /static void printglstring (const char *name, Glenum s) {const char *V = (const char *) glgetstring (s); Logi ("GL%s =%s\n", name, v);} static void Checkglerror (const char* op) {for (Glint error = Glgeterror (); Error: Error = Glgeterror ()) {Logi ("after%s ()" Glerror (0x%x) \ n ", op, Error);}} BOOL Init () {printglstring ("Version", Gl_version);p rintglstring ("Vendor", Gl_vendor);p rintglstring ("Renderer", Gl_ RENDERER);p rintglstring ("Extensions", gl_extensions);//Enable Shadow smoothing Glshademodel (Gl_smooth);//Black background glclearcolor (0.0f, 0.0f, 0.0f, 0.0f);//Set Depth cache GLCLEARDEPTHF (1.0f);//enable depth test glenable (gl_depth_test);//Depth test type Gldepthfunc (gl_lequal) ;//correction of the perspective Glhint (Gl_perspective_correction_hINT, gl_nicest); return true;} static void _gluperspective (Glfloat Fovy, glfloat aspect, Glfloat znear, Glfloat zfar) {glfloat top = Znear * ((glfloat) TA N (Fovy * pi/360.0)); Glfloat bottom =-top; Glfloat left = bottom * aspect; Glfloat right = Top * ASPECT;GLFRUSTUMF (left, right, bottom, top, znear, Zfar);} void Resize (int width, int height) {//prevents being 0 except if (height==0) {height=1;} Resets the current viewport glviewport (0, 0, width, height),//Select projection matrix Glmatrixmode (gl_projection);//Reset projection matrix glloadidentity ();//Set Viewport size _ Gluperspective (45.0f, (glfloat) width/(glfloat) height,0.1f,100.0f);//select Model observation Matrix Glmatrixmode (Gl_modelview);// Reset Model Observation Matrix glloadidentity ();} void Renderframe () {//clear screen and depth cache Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);//Resets the current model observation matrix glloadidentity (); Gltranslatef (0,0,-10.0f); Glrotatef (gangle, 0, 1.0F, 0); Glrotatef (gangle, 0, 0, 1.0F);//enable vertex array glenableclientstate (Gl_vertex_array);//glenableclientstate (Gl_color_array); Glenableclientstate (Gl_texture_coord_array);//enable texture mapping glenable (gl_texture_2d); Select Texture GlbindtextUre (gl_texture_2d, gtexture[0]); Glvertexpointer (3,gl_float,0,gvertices); Gltexcoordpointer (2, gl_float, 0, texcoords); Gldrawarrays (GL_TRIANGLES, 0, 18);//close vertex array gldisableclientstate (Gl_vertex_array); gldisableclientstate (Gl_texture_coord_array);// Gldisableclientstate (Gl_olor_array); Gangle + = 2.f;} /************************************************************************//* JNI Code *//************************************************************************/extern "C" {JNIEXPORT vo ID jnicall java_com_android_gljni_gljnilib_resize (jnienv * env, Jobject obj, jint width, jint height); Jniexport void Jnicall java_com_android_gljni_gljnilib_step (jnienv * env, jobject obj); Jniexport void Jnicall java_com_android_gljni_gljnilib_init (jnienv * env, jobject obj); Jniexport void Jnicall java_com_android_gljni_gljnilib_settexture (jnienv * env, Jclass obj, Jintarray Tex);}; Jniexport void Jnicall java_com_android_gljni_gljnilib_resize (jnienv * en)V, jobject obj, jint width, jint height) {Resize (width, height);} Jniexport void Jnicall java_com_android_gljni_gljnilib_step (jnienv * env, Jobject obj) {renderframe ();} Jniexport void Jnicall java_com_android_gljni_gljnilib_init (jnienv * env, Jobject obj) {init ();} Jniexport void Jnicall java_com_android_gljni_gljnilib_settexture (jnienv * env, Jclass obj, Jintarray tex) {gTexture = (GL UINT *) env->getintarrayelements (tex,0);}
Opengl ES 1.x NDK Instance Development eight: rotated texture pyramid