Android OpenGL ES code feature
Last Update:2016-06-28
Source: Internet
Author: User
<span id="Label3"> </p><pre class="brush:cpp;gutter:true;">/* 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. *///OpenGL ES 2.0 code#include <jni.h> #include <android/log.h> #include <GLES2/gl2.h> #include < gles2/gl2ext.h> #include <stdio.h> #include <stdlib.h> #include <math.h> #define LOG_TAG " Libgl2jni "#define Logi (...) __android_log_print (android_log_info,log_tag,__va_args__) #define LOGE (...) __android_log_print (android_log_error,log_tag,__va_args__) 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); }}static const char gvertexshader[] = "attribute vec4 vposition;\n" "void main () {\ n" "gl_position = vposition \ n ""}\n ", static const char gfragmentshader[] =" precision mediump float;\n "" void main () {\ n "" gl_fragcol or = Vec4 (0.0, 1.0, 0.0, 1.0); \ n ""}\n "; Gluint loadshader (glenum shadertype, const char* psource) {gluint shader = Glcreateshader (shadertype); If (shader) {glshadersource (shader, 1, &psource, NULL); Glcompileshader (shader); Glint compiled = 0; Glgetshaderiv (shader, gl_compile_status, &compiled); If (!compiled) {glint Infolen = 0; Glgetshaderiv (shader, Gl_info_log_length, &infolen); If (infolen) {char* buf = (char*) malloc (infolen); If (buf) {glgetshaderinfolog (shader, infolen, NULL, buf); LOGE ("Could not compile shader%d:\n%s\n", shadertype, buf); Free (buf); } Gldeleteshader (shader); Shader = 0; }}} return shader;} Gluint createprogram (const char* pvertexsource, const char* pfragmentsource) {gluint vertexshader = LoadShader (GL_VERT ex_shader, pvertexsource); If (!vertexshader) {return 0; } gluint pixelshader = Loadshader (gl_fragment_shader, pfragmentsource); If (!pixelshader) {return 0; } gluint program = Glcreateprogram (); If (program) {glattachshader (program, vertexshader); Checkglerror ("glattachshader"); Glattachshader (program, pixelshader); Checkglerror ("glattachshader"); Gllinkprogram (program); Glint Linkstatus = gl_false; GLGETPROGRAMIV (program, gl_link_status, &linkstatus); If (linkstatus! = Gl_true) {glint buflength = 0; GLGETPROGRAMIV (program, gl_info_log_length, &buflength); If (buflength) {char* buf = (char*) malloc (buflength); If (buf) {glgetprograminfolog (program, buflength, NULL, buf); LOGE ("Could not link program:\n%s\n", buf); Free (buf); }} Gldeleteprogram (program); program = 0; }} return program; Gluint gprogram; Gluint gvpositionhandle;bool setupgraphics (int w, int h) {printglstring ("Version", gl_version); Printglstring ("Vendor", gl_vendor); Printglstring ("Renderer", gl_renderer); Printglstring ("Extensions", gl_extensions); Logi ("setupgraphics (%d,%d)", w, h); Gprogram = Createprogram (gvertexshader, Gfragmentshader); If (!gprogram) {LOGE ("Could not to create program."); Return false; } gvpositionhandle = Glgetattriblocation (gprogram, "vposition"); Checkglerror ("glgetattriblocation"); Logi ("glgetattriblocation (\" vposition\ ") =%d\n", gvpositionhandle); Glviewport (0, 0, w, h); Checkglerror ("glviewport"); Return true;} Const Glfloat gtrianglevertices[] = {0.0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f};void renderframe () {static float Grey Grey + = 0.01f; If (grey > 1.0f) {grey = 0.0f; } glclearcolor (grey, grey, grey, 1.0f); Checkglerror ("glclearcolor"); Glclear (gl_depth_buffer_bit | gl_color_buffer_bit); Checkglerror ("glclear"); Gluseprogram (gprogram); Checkglerror ("gluseprogram"); Glvertexattribpointer (gvpositionhandle, 2, gl_float, gl_false, 0, gtrianglevertices); Checkglerror ("glvertexattribpointer"); Glenablevertexattribarray (gvpositionhandle); Checkglerror ("glenablevertexattribarray"); Gldrawarrays (gl_triangles, 0, 3); Checkglerror ("gldrawarrays");} extern "C" {jniexport void Jnicall java_com_android_gl2jni_gl2jnilib_init (jnienv * env, jobject obj, jint width, jint height); jniexport void Jnicall java_com_android_gl2jni_gl2jnilib_step (jnienv * env, jobject obj);}; jniexport void Jnicall java_com_android_gl2jni_gl2jnilib_init (jnienv * env, jobject obj, jint width, jint height) {set Upgraphics (width, height);} jniexport void Jnicall java_com_android_gl2jni_gl2jnilib_step (jnienv * env, jobject obj) {renderframe ();}</pre><p><p> </p></p><p><p>Android OpenGL ES code feature</p></p></span>