Android Graphics Tests 程序学习02---Opengl EGL炫彩实例Native&Java
转载:
注:文章都是通过阅读各位前辈总结的资料 Android 8.x && Linux(kernel 4.x)Qualcomm平台源码、加上自己的思考分析总结出来的,其中难免有理解不对的地方,欢迎大家批评指正。文章为个人学习、研究、欣赏之用,图文内容整理自互联网(◕‿◕),如有侵权,请联系删除,禁止转载(©Qualcomm ©Android @Linux 版权所有),谢谢。
首先感谢:
正是由于前人的分析和总结,帮助我节约了大量的时间和精力,再次感谢!!!
Google Pixel、Pixel XL 内核代码(==文章基于 Kernel-4.x==):
Kernel source for Pixel 2 (walleye) and Pixel 2 XL (taimen) - GitHub
AOSP 源码(==文章基于 Android 8.x==):
Android 系统全套源代码分享 (更新到 8.1.0_r1)
==源码(部分)==:
opengl
- android\frameworks\native\opengl\tests\gl2_basic\
- android\frameworks\native\opengl\tests\gl2_java\
- android\frameworks\native\opengl\tests\angeles\
(一)、gl2_basic
1.1、源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
#include <stdlib.h> #include <stdio.h> #include <time.h> #include <sched.h> #include <sys/resource.h> #include <EGL/egl.h> #include <GLES2/gl2.h> #include <GLES2/gl2ext.h> #include <utils/Timers.h> #include <WindowSurface.h> #include <EGLUtils.h> using namespace android; extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); static void printGLString(const char *name, GLenum s) { // fprintf(stderr, "printGLString %s, %d\n", name, s); const char *v = (const char *) glGetString(s); // int error = glGetError(); // fprintf(stderr, "glGetError() = %d, result of glGetString = %x\n", error, // (unsigned int) v); // if ((v < (const char*) 0) || (v > (const char*) 0x10000)) // fprintf(stderr, "GL %s = %s\n", name, v); // else // fprintf(stderr, "GL %s = (null) 0x%08x\n", name, (unsigned int) v); fprintf(stderr, "GL %s = %s\n", name, v); } static void printEGLString(EGLDisplay dpy, const char *name, GLenum s) { const char *v = (const char *) eglQueryString(dpy, s); const char* va = (const char*)eglQueryStringImplementationANDROID(dpy, s); fprintf(stderr, "GL %s = %s\nImplementationANDROID: %s\n", name, v, va); } static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) { if (returnVal != EGL_TRUE) { fprintf(stderr, "%s() returned %d\n", op, returnVal); } for (EGLint error = eglGetError(); error != EGL_SUCCESS; error = eglGetError()) { fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error), error); } } static void checkGlError(const char* op) { for (GLint error = glGetError(); error; error = glGetError()) { fprintf(stderr, "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_FragColor = 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); fprintf(stderr, "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_VERTEX_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); fprintf(stderr, "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) { gProgram = createProgram(gVertexShader, gFragmentShader); if (!gProgram) { return false; } gvPositionHandle = glGetAttribLocation(gProgram, "vPosition"); checkGlError("glGetAttribLocation"); fprintf(stderr, "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() { glClearColor(0.0f, 0.0f, 1.0f, 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"); } void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) { #define X(VAL) {VAL, #VAL} struct {EGLint attribute; const char* name;} names[] = { X(EGL_BUFFER_SIZE), X(EGL_ALPHA_SIZE), X(EGL_BLUE_SIZE), X(EGL_GREEN_SIZE), X(EGL_RED_SIZE), X(EGL_DEPTH_SIZE), X(EGL_STENCIL_SIZE), X(EGL_CONFIG_CAVEAT), X(EGL_CONFIG_ID), X(EGL_LEVEL), X(EGL_MAX_PBUFFER_HEIGHT), X(EGL_MAX_PBUFFER_PIXELS), X(EGL_MAX_PBUFFER_WIDTH), X(EGL_NATIVE_RENDERABLE), X(EGL_NATIVE_VISUAL_ID), X(EGL_NATIVE_VISUAL_TYPE), X(EGL_SAMPLES), X(EGL_SAMPLE_BUFFERS), X(EGL_SURFACE_TYPE), X(EGL_TRANSPARENT_TYPE), X(EGL_TRANSPARENT_RED_VALUE), X(EGL_TRANSPARENT_GREEN_VALUE), X(EGL_TRANSPARENT_BLUE_VALUE), X(EGL_BIND_TO_TEXTURE_RGB), X(EGL_BIND_TO_TEXTURE_RGBA), X(EGL_MIN_SWAP_INTERVAL), X(EGL_MAX_SWAP_INTERVAL), X(EGL_LUMINANCE_SIZE), X(EGL_ALPHA_MASK_SIZE), X(EGL_COLOR_BUFFER_TYPE), X(EGL_RENDERABLE_TYPE), X(EGL_CONFORMANT), }; #undef X for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) { EGLint value = -1; EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value); EGLint error = eglGetError(); if (returnVal && error == EGL_SUCCESS) { printf(" %s: ", names[j].name); printf("%d (0x%x)", value, value); } } printf("\n"); } int printEGLConfigurations(EGLDisplay dpy) { EGLint numConfig = 0; EGLint returnVal = eglGetConfigs(dpy, NULL, 0, &numConfig); checkEglError("eglGetConfigs", returnVal); if (!returnVal) { return false; } printf("Number of EGL configuration: %d\n", numConfig); EGLConfig* configs = (EGLConfig*) malloc(sizeof(EGLConfig) * numConfig); if (! configs) { printf("Could not allocate configs.\n"); return false; } returnVal = eglGetConfigs(dpy, configs, numConfig, &numConfig); checkEglError("eglGetConfigs", returnVal); if (!returnVal) { free(configs); return false; } for(int i = 0; i < numConfig; i++) { printf("Configuration %d\n", i); printEGLConfiguration(dpy, configs[i]); } free(configs); return true; } int main(int /*argc*/, char** /*argv*/) { EGLBoolean returnValue; EGLConfig myConfig = {0}; EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGLint s_configAttribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; EGLint majorVersion; EGLint minorVersion; EGLContext context; EGLSurface surface; EGLint w, h; EGLDisplay dpy; checkEglError("<init>"); dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); checkEglError("eglGetDisplay"); if (dpy == EGL_NO_DISPLAY) { printf("eglGetDisplay returned EGL_NO_DISPLAY.\n"); return 0; } returnValue = eglInitialize(dpy, &majorVersion, &minorVersion); checkEglError("eglInitialize", returnValue); fprintf(stderr, "EGL version %d.%d\n", majorVersion, minorVersion); if (returnValue != EGL_TRUE) { printf("eglInitialize failed\n"); return 0; } if (!printEGLConfigurations(dpy)) { printf("printEGLConfigurations failed\n"); return 0; } checkEglError("printEGLConfigurations"); WindowSurface windowSurface; EGLNativeWindowType window = windowSurface.getSurface(); returnValue = EGLUtils::selectConfigForNativeWindow(dpy, s_configAttribs, window, &myConfig); if (returnValue) { printf("EGLUtils::selectConfigForNativeWindow() returned %d", returnValue); return 0; } checkEglError("EGLUtils::selectConfigForNativeWindow"); printf("Chose this configuration:\n"); printEGLConfiguration(dpy, myConfig); surface = eglCreateWindowSurface(dpy, myConfig, window, NULL); checkEglError("eglCreateWindowSurface"); if (surface == EGL_NO_SURFACE) { printf("gelCreateWindowSurface failed.\n"); return 0; } context = eglCreateContext(dpy, myConfig, EGL_NO_CONTEXT, context_attribs); checkEglError("eglCreateContext"); if (context == EGL_NO_CONTEXT) { printf("eglCreateContext failed\n"); return 0; } returnValue = eglMakeCurrent(dpy, surface, surface, context); checkEglError("eglMakeCurrent", returnValue); if (returnValue != EGL_TRUE) { return 0; } eglQuerySurface(dpy, surface, EGL_WIDTH, &w); checkEglError("eglQuerySurface"); eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); checkEglError("eglQuerySurface"); GLint dim = w < h ? w : h; fprintf(stderr, "Window dimensions: %d x %d\n", w, h); printGLString("Version", GL_VERSION); printGLString("Vendor", GL_VENDOR); printGLString("Renderer", GL_RENDERER); printGLString("Extensions", GL_EXTENSIONS); printEGLString(dpy, "EGL Extensions", EGL_EXTENSIONS); if(!setupGraphics(w, h)) { fprintf(stderr, "Could not set up graphics.\n"); return 0; } for (;;) { renderFrame(); eglSwapBuffers(dpy, surface); checkEglError("eglSwapBuffers"); } return 0; } |
(二)、gl2_java
2.1、源码1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.android.gl2java; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.WindowManager; import java.io.File; public class GL2JavaActivity extends Activity { GL2JavaView mView; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); mView = new GL2JavaView(getApplication()); setContentView(mView); } @Override protected void onPause() { super.onPause(); mView.onPause(); } @Override protected void onResume() { super.onResume(); mView.onResume(); } } |
2.2、源码2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
package com.android.gl2java; import android.content.Context; import android.opengl.GLSurfaceView; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import javax.microedition.khronos.egl.EGL10; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.egl.EGLContext; import javax.microedition.khronos.egl.EGLDisplay; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLES20; /** * An implementation of SurfaceView that uses the dedicated surface for * displaying an OpenGL animation. This allows the animation to run in a * separate thread, without requiring that it be driven by the update mechanism * of the view hierarchy. * * The application-specific rendering code is delegated to a GLView.Renderer * instance. */ class GL2JavaView extends GLSurfaceView { private static String TAG = "GL2JavaView"; public GL2JavaView(Context context) { super(context); setEGLContextClientVersion(2); setRenderer(new Renderer()); } private static class Renderer implements GLSurfaceView.Renderer { public Renderer() { mTriangleVertices = ByteBuffer.allocateDirect(mTriangleVerticesData.length * 4) .order(ByteOrder.nativeOrder()).asFloatBuffer(); mTriangleVertices.put(mTriangleVerticesData).position(0); } public void onDrawFrame(GL10 gl) { GLES20.glClearColor(0.0f, 0.0f, 1.0f, 1.0f); GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); GLES20.glUseProgram(mProgram); checkGlError("glUseProgram"); GLES20.glVertexAttribPointer(mvPositionHandle, 2, GLES20.GL_FLOAT, false, 0, mTriangleVertices); checkGlError("glVertexAttribPointer"); GLES20.glEnableVertexAttribArray(mvPositionHandle); checkGlError("glEnableVertexAttribArray"); GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); checkGlError("glDrawArrays"); } public void onSurfaceChanged(GL10 gl, int width, int height) { GLES20.glViewport(0, 0, width, height); } public void onSurfaceCreated(GL10 gl, EGLConfig config) { mProgram = createProgram(mVertexShader, mFragmentShader); if (mProgram == 0) { return; } mvPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); checkGlError("glGetAttribLocation"); if (mvPositionHandle == -1) { throw new RuntimeException("Could not get attrib location for vPosition"); } } private int loadShader(int shaderType, String source) { int shader = GLES20.glCreateShader(shaderType); if (shader != 0) { GLES20.glShaderSource(shader, source); GLES20.glCompileShader(shader); int[] compiled = new int[1]; GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); if (compiled[0] == 0) { Log.e(TAG, "Could not compile shader " + shaderType + ":"); Log.e(TAG, GLES20.glGetShaderInfoLog(shader)); GLES20.glDeleteShader(shader); shader = 0; } } return shader; } private int createProgram(String vertexSource, String fragmentSource) { int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource); if (vertexShader == 0) { return 0; } int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource); if (pixelShader == 0) { return 0; } int program = GLES20.glCreateProgram(); if (program != 0) { GLES20.glAttachShader(program, vertexShader); checkGlError("glAttachShader"); GLES20.glAttachShader(program, pixelShader); checkGlError("glAttachShader"); GLES20.glLinkProgram(program); int[] linkStatus = new int[1]; GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0); if (linkStatus[0] != GLES20.GL_TRUE) { Log.e(TAG, "Could not link program: "); Log.e(TAG, GLES20.glGetProgramInfoLog(program)); GLES20.glDeleteProgram(program); program = 0; } } return program; } private void checkGlError(String op) { int error; while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) { Log.e(TAG, op + ": glError " + error); throw new RuntimeException(op + ": glError " + error); } } private final float[] mTriangleVerticesData = { 0.0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f }; private FloatBuffer mTriangleVertices; private final String mVertexShader = "attribute vec4 vPosition;\n" + "void main() {\n" + " gl_Position = vPosition;\n" + "}\n"; private final String mFragmentShader = "precision mediump float;\n" + "void main() {\n" + " gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n" + "}\n"; private int mProgram; private int mvPositionHandle; } } |
这个和前面例子1的界面效果是一摸一样的。
(三)、angeles
首先看看效果图,可以看到丰富的多彩世界的模型啦~
3.1、源码1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
\android\frameworks\native\opengl\tests\angeles\app-linux.cpp #include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <EGL/egl.h> #include <GLES/gl.h> #include <EGLUtils.h> #include <WindowSurface.h> using namespace android; #include "app.h" int gAppAlive = 1; static const char sAppName[] = "San Angeles Observation OpenGL ES version example (Linux)"; static int sWindowWidth = WINDOW_DEFAULT_WIDTH; static int sWindowHeight = WINDOW_DEFAULT_HEIGHT; static EGLDisplay sEglDisplay = EGL_NO_DISPLAY; static EGLContext sEglContext = EGL_NO_CONTEXT; static EGLSurface sEglSurface = EGL_NO_SURFACE; const char *egl_strerror(unsigned err) { switch(err){ case EGL_SUCCESS: return "SUCCESS"; case EGL_NOT_INITIALIZED: return "NOT INITIALIZED"; case EGL_BAD_ACCESS: return "BAD ACCESS"; case EGL_BAD_ALLOC: return "BAD ALLOC"; case EGL_BAD_ATTRIBUTE: return "BAD_ATTRIBUTE"; case EGL_BAD_CONFIG: return "BAD CONFIG"; case EGL_BAD_CONTEXT: return "BAD CONTEXT"; case EGL_BAD_CURRENT_SURFACE: return "BAD CURRENT SURFACE"; case EGL_BAD_DISPLAY: return "BAD DISPLAY"; case EGL_BAD_MATCH: return "BAD MATCH"; case EGL_BAD_NATIVE_PIXMAP: return "BAD NATIVE PIXMAP"; case EGL_BAD_NATIVE_WINDOW: return "BAD NATIVE WINDOW"; case EGL_BAD_PARAMETER: return "BAD PARAMETER"; case EGL_BAD_SURFACE: return "BAD_SURFACE"; // case EGL_CONTEXT_LOST: return "CONTEXT LOST"; default: return "UNKNOWN"; } } void egl_error(const char *name) { unsigned err = eglGetError(); if(err != EGL_SUCCESS) { fprintf(stderr,"%s(): egl error 0x%x (%s)\n", name, err, egl_strerror(err)); } } static void checkGLErrors() { GLenum error = glGetError(); if (error != GL_NO_ERROR) fprintf(stderr, "GL Error: 0x%04x\n", (int)error); } static void checkEGLErrors() { EGLint error = eglGetError(); // GLESonGL seems to be returning 0 when there is no errors? if (error && error != EGL_SUCCESS) fprintf(stderr, "EGL Error: 0x%04x\n", (int)error); } static int initGraphics(EGLint samples, const WindowSurface& windowSurface) { EGLint configAttribs[] = { EGL_DEPTH_SIZE, 16, EGL_SAMPLE_BUFFERS, samples ? 1 : 0, EGL_SAMPLES, samples, EGL_NONE }; EGLint majorVersion; EGLint minorVersion; EGLContext context; EGLConfig config; EGLSurface surface; EGLint w, h; EGLDisplay dpy; EGLNativeWindowType window = windowSurface.getSurface(); dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); eglInitialize(dpy, &majorVersion, &minorVersion); status_t err = EGLUtils::selectConfigForNativeWindow( dpy, configAttribs, window, &config); if (err) { fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n"); return 0; } surface = eglCreateWindowSurface(dpy, config, window, NULL); egl_error("eglCreateWindowSurface"); fprintf(stderr,"surface = %p\n", surface); context = eglCreateContext(dpy, config, NULL, NULL); egl_error("eglCreateContext"); fprintf(stderr,"context = %p\n", context); eglMakeCurrent(dpy, surface, surface, context); egl_error("eglMakeCurrent"); eglQuerySurface(dpy, surface, EGL_WIDTH, &sWindowWidth); eglQuerySurface(dpy, surface, EGL_HEIGHT, &sWindowHeight); sEglDisplay = dpy; sEglSurface = surface; sEglContext = context; if (samples == 0) { // GL_MULTISAMPLE is enabled by default glDisable(GL_MULTISAMPLE); } return EGL_TRUE; } static void deinitGraphics() { eglMakeCurrent(sEglDisplay, NULL, NULL, NULL); eglDestroyContext(sEglDisplay, sEglContext); eglDestroySurface(sEglDisplay, sEglSurface); eglTerminate(sEglDisplay); } int main(int argc, char *argv[]) { unsigned samples = 0; printf("usage: %s [samples]\n", argv[0]); if (argc == 2) { samples = atoi( argv[1] ); printf("Multisample enabled: GL_SAMPLES = %u\n", samples); } WindowSurface windowSurface; if (!initGraphics(samples, windowSurface)) { fprintf(stderr, "Graphics initialization failed.\n"); return EXIT_FAILURE; } appInit(); struct timeval timeTemp; int frameCount = 0; gettimeofday(&timeTemp, NULL); double totalTime = timeTemp.tv_usec/1000000.0 + timeTemp.tv_sec; while (gAppAlive) { struct timeval timeNow; gettimeofday(&timeNow, NULL); appRender(timeNow.tv_sec * 1000 + timeNow.tv_usec / 1000, sWindowWidth, sWindowHeight); checkGLErrors(); eglSwapBuffers(sEglDisplay, sEglSurface); checkEGLErrors(); frameCount++; } gettimeofday(&timeTemp, NULL); appDeinit(); deinitGraphics(); totalTime = (timeTemp.tv_usec/1000000.0 + timeTemp.tv_sec) - totalTime; printf("totalTime=%f s, frameCount=%d, %.2f fps\n", totalTime, frameCount, frameCount/totalTime); return EXIT_SUCCESS; } |
3.2、源码2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
\android\frameworks\native\opengl\tests\angeles\demo.c #include <stdlib.h> #include <math.h> #include <float.h> #include <assert.h> #include <GLES/gl.h> #include "app.h" #include "shapes.h" #include "cams.h" // Total run length is 20 * camera track base unit length (see cams.h). #define RUN_LENGTH (20 * CAMTRACK_LEN) #undef PI #define PI 3.1415926535897932f #define RANDOM_UINT_MAX 65535 static unsigned long sRandomSeed = 0; static void seedRandom(unsigned long seed) { sRandomSeed = seed; } static unsigned long randomUInt() { sRandomSeed = sRandomSeed * 0x343fd + 0x269ec3; return sRandomSeed >> 16; } // Capped conversion from float to fixed. static long floatToFixed(float value) { if (value < -32768) value = -32768; if (value > 32767) value = 32767; return (long)(value * 65536); } #define FIXED(value) floatToFixed(value) // Definition of one GL object in this demo. typedef struct { /* Vertex array and color array are enabled for all objects, so their * pointers must always be valid and non-NULL. Normal array is not * used by the ground plane, so when its pointer is NULL then normal * array usage is disabled. * * Vertex array is supposed to use GL_FIXED datatype and stride 0 * (i.e. tightly packed array). Color array is supposed to have 4 * components per color with GL_UNSIGNED_BYTE datatype and stride 0. * Normal array is supposed to use GL_FIXED datatype and stride 0. */ GLfixed *vertexArray; GLubyte *colorArray; GLfixed *normalArray; GLint vertexComponents; GLsizei count; } GLOBJECT; static long sStartTick = 0; static long sTick = 0; static int sCurrentCamTrack = 0; static long sCurrentCamTrackStartTick = 0; static long sNextCamTrackStartTick = 0x7fffffff; static GLOBJECT *sSuperShapeObjects[SUPERSHAPE_COUNT] = { NULL }; static GLOBJECT *sGroundPlane = NULL; typedef struct { float x, y, z; } VECTOR3; static void freeGLObject(GLOBJECT *object) { if (object == NULL) return; free(object->normalArray); free(object->colorArray); free(object->vertexArray); free(object); } static GLOBJECT * newGLObject(long vertices, int vertexComponents, int useNormalArray) { GLOBJECT *result; result = (GLOBJECT *)malloc(sizeof(GLOBJECT)); if (result == NULL) return NULL; result->count = vertices; result->vertexComponents = vertexComponents; result->vertexArray = (GLfixed *)malloc(vertices * vertexComponents * sizeof(GLfixed)); result->colorArray = (GLubyte *)malloc(vertices * 4 * sizeof(GLubyte)); if (useNormalArray) { result->normalArray = (GLfixed *)malloc(vertices * 3 * sizeof(GLfixed)); } else result->normalArray = NULL; if (result->vertexArray == NULL || result->colorArray == NULL || (useNormalArray && result->normalArray == NULL)) { freeGLObject(result); return NULL; } return result; } static void drawGLObject(GLOBJECT *object) { assert(object != NULL); glVertexPointer(object->vertexComponents, GL_FIXED, 0, object->vertexArray); glColorPointer(4, GL_UNSIGNED_BYTE, 0, object->colorArray); // Already done in initialization: //glEnableClientState(GL_VERTEX_ARRAY); //glEnableClientState(GL_COLOR_ARRAY); if (object->normalArray) { glNormalPointer(GL_FIXED, 0, object->normalArray); glEnableClientState(GL_NORMAL_ARRAY); } else glDisableClientState(GL_NORMAL_ARRAY); glDrawArrays(GL_TRIANGLES, 0, object->count); } static void vector3Sub(VECTOR3 *dest, VECTOR3 *v1, VECTOR3 *v2) { dest->x = v1->x - v2->x; dest->y = v1->y - v2->y; dest->z = v1->z - v2->z; } static void superShapeMap(VECTOR3 *point, float r1, float r2, float t, float p) { // sphere-mapping of supershape parameters point->x = (float)(cos(t) * cos(p) / r1 / r2); point->y = (float)(sin(t) * cos(p) / r1 / r2); point->z = (float)(sin(p) / r2); } static float ssFunc(const float t, const float *p) { return (float)(pow(pow(fabs(cos(p[0] * t / 4)) / p[1], p[4]) + pow(fabs(sin(p[0] * t / 4)) / p[2], p[5]), 1 / p[3])); } // Creates and returns a supershape object. // Based on Paul Bourke's POV-Ray implementation. // http://astronomy.swin.edu.au/~pbourke/povray/supershape/ static GLOBJECT * createSuperShape(const float *params) { const int resol1 = (int)params[SUPERSHAPE_PARAMS - 3]; const int resol2 = (int)params[SUPERSHAPE_PARAMS - 2]; // latitude 0 to pi/2 for no mirrored bottom // (latitudeBegin==0 for -pi/2 to pi/2 originally) const int latitudeBegin = resol2 / 4; const int latitudeEnd = resol2 / 2; // non-inclusive const int longitudeCount = resol1; const int latitudeCount = latitudeEnd - latitudeBegin; const long triangleCount = longitudeCount * latitudeCount * 2; const long vertices = triangleCount * 3; GLOBJECT *result; float baseColor[3]; int a, longitude, latitude; long currentVertex, currentQuad; result = newGLObject(vertices, 3, 1); if (result == NULL) return NULL; for (a = 0; a < 3; ++a) baseColor[a] = ((randomUInt() % 155) + 100) / 255.f; currentQuad = 0; currentVertex = 0; // longitude -pi to pi for (longitude = 0; longitude < longitudeCount; ++longitude) { // latitude 0 to pi/2 for (latitude = latitudeBegin; latitude < latitudeEnd; ++latitude) { float t1 = -PI + longitude * 2 * PI / resol1; float t2 = -PI + (longitude + 1) * 2 * PI / resol1; float p1 = -PI / 2 + latitude * 2 * PI / resol2; float p2 = -PI / 2 + (latitude + 1) * 2 * PI / resol2; float r0, r1, r2, r3; r0 = ssFunc(t1, params); r1 = ssFunc(p1, ¶ms[6]); r2 = ssFunc(t2, params); r3 = ssFunc(p2, ¶ms[6]); if (r0 != 0 && r1 != 0 && r2 != 0 && r3 != 0) { VECTOR3 pa, pb, pc, pd; VECTOR3 v1, v2, n; float ca; int i; //float lenSq, invLenSq; superShapeMap(&pa, r0, r1, t1, p1); superShapeMap(&pb, r2, r1, t2, p1); superShapeMap(&pc, r2, r3, t2, p2); superShapeMap(&pd, r0, r3, t1, p2); // kludge to set lower edge of the object to fixed level if (latitude == latitudeBegin + 1) pa.z = pb.z = 0; vector3Sub(&v1, &pb, &pa); vector3Sub(&v2, &pd, &pa); // Calculate normal with cross product. /* i j k i j * v1.x v1.y v1.z | v1.x v1.y * v2.x v2.y v2.z | v2.x v2.y */ n.x = v1.y * v2.z - v1.z * v2.y; n.y = v1.z * v2.x - v1.x * v2.z; n.z = v1.x * v2.y - v1.y * v2.x; /* Pre-normalization of the normals is disabled here because * they will be normalized anyway later due to automatic * normalization (GL_NORMALIZE). It is enabled because the * objects are scaled with glScale. */ /* lenSq = n.x * n.x + n.y * n.y + n.z * n.z; invLenSq = (float)(1 / sqrt(lenSq)); n.x *= invLenSq; n.y *= invLenSq; n.z *= invLenSq; */ ca = pa.z + 0.5f; for (i = currentVertex * 3; i < (currentVertex + 6) * 3; i += 3) { result->normalArray[i] = FIXED(n.x); result->normalArray[i + 1] = FIXED(n.y); result->normalArray[i + 2] = FIXED(n.z); } for (i = currentVertex * 4; i < (currentVertex + 6) * 4; i += 4) { int a, color[3]; for (a = 0; a < 3; ++a) { color[a] = (int)(ca * baseColor[a] * 255); if (color[a] > 255) color[a] = 255; } result->colorArray[i] = (GLubyte)color[0]; result->colorArray[i + 1] = (GLubyte)color[1]; result->colorArray[i + 2] = (GLubyte)color[2]; result->colorArray[i + 3] = 0; } result->vertexArray[currentVertex * 3] = FIXED(pa.x); result->vertexArray[currentVertex * 3 + 1] = FIXED(pa.y); result->vertexArray[currentVertex * 3 + 2] = FIXED(pa.z); ++currentVertex; result->vertexArray[currentVertex * 3] = FIXED(pb.x); result->vertexArray[currentVertex * 3 + 1] = FIXED(pb.y); result->vertexArray[currentVertex * 3 + 2] = FIXED(pb.z); ++currentVertex; result->vertexArray[currentVertex * 3] = FIXED(pd.x); result->vertexArray[currentVertex * 3 + 1] = FIXED(pd.y); result->vertexArray[currentVertex * 3 + 2] = FIXED(pd.z); ++currentVertex; result->vertexArray[currentVertex * 3] = FIXED(pb.x); result->vertexArray[currentVertex * 3 + 1] = FIXED(pb.y); result->vertexArray[currentVertex * 3 + 2] = FIXED(pb.z); ++currentVertex; result->vertexArray[currentVertex * 3] = FIXED(pc.x); result->vertexArray[currentVertex * 3 + 1] = FIXED(pc.y); result->vertexArray[currentVertex * 3 + 2] = FIXED(pc.z); ++currentVertex; result->vertexArray[currentVertex * 3] = FIXED(pd.x); result->vertexArray[currentVertex * 3 + 1] = FIXED(pd.y); result->vertexArray[currentVertex * 3 + 2] = FIXED(pd.z); ++currentVertex; } // r0 && r1 && r2 && r3 ++currentQuad; } // latitude } // longitude // Set number of vertices in object to the actual amount created. result->count = currentVertex; return result; } static GLOBJECT * createGroundPlane() { const int scale = 4; const int yBegin = -15, yEnd = 15; // ends are non-inclusive const int xBegin = -15, xEnd = 15; const long triangleCount = (yEnd - yBegin) * (xEnd - xBegin) * 2; const long vertices = triangleCount * 3; GLOBJECT *result; int x, y; long currentVertex, currentQuad; result = newGLObject(vertices, 2, 0); if (result == NULL) return NULL; currentQuad = 0; currentVertex = 0; for (y = yBegin; y < yEnd; ++y) { for (x = xBegin; x < xEnd; ++x) { GLubyte color; int i, a; color = (GLubyte)((randomUInt() & 0x5f) + 81); // 101 1111 for (i = currentVertex * 4; i < (currentVertex + 6) * 4; i += 4) { result->colorArray[i] = color; result->colorArray[i + 1] = color; result->colorArray[i + 2] = color; result->colorArray[i + 3] = 0; } // Axis bits for quad triangles: // x: 011100 (0x1c), y: 110001 (0x31) (clockwise) // x: 001110 (0x0e), y: 100011 (0x23) (counter-clockwise) for (a = 0; a < 6; ++a) { const int xm = x + ((0x1c >> a) & 1); const int ym = y + ((0x31 >> a) & 1); const float m = (float)(cos(xm * 2) * sin(ym * 4) * 0.75f); result->vertexArray[currentVertex * 2] = FIXED(xm * scale + m); result->vertexArray[currentVertex * 2 + 1] = FIXED(ym * scale + m); ++currentVertex; } ++currentQuad; } } return result; } static void drawGroundPlane() { glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_ZERO, GL_SRC_COLOR); glDisable(GL_LIGHTING); drawGLObject(sGroundPlane); glEnable(GL_LIGHTING); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); } static void drawFadeQuad() { static const GLfixed quadVertices[] = { -0x10000, -0x10000, 0x10000, -0x10000, -0x10000, 0x10000, 0x10000, -0x10000, 0x10000, 0x10000, -0x10000, 0x10000 }; const int beginFade = sTick - sCurrentCamTrackStartTick; const int endFade = sNextCamTrackStartTick - sTick; const int minFade = beginFade < endFade ? beginFade : endFade; if (minFade < 1024) { const GLfixed fadeColor = minFade << 6; glColor4x(fadeColor, fadeColor, fadeColor, 0); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_ZERO, GL_SRC_COLOR); glDisable(GL_LIGHTING); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glVertexPointer(2, GL_FIXED, 0, quadVertices); glDrawArrays(GL_TRIANGLES, 0, 6); glEnableClientState(GL_COLOR_ARRAY); glMatrixMode(GL_MODELVIEW); glEnable(GL_LIGHTING); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); } } // Called from the app framework. void appInit() { unsigned int a; glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glShadeModel(GL_FLAT); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); seedRandom(15); for (a = 0; a < SUPERSHAPE_COUNT; ++a) { sSuperShapeObjects[a] = createSuperShape(sSuperShapeParams[a]); assert(sSuperShapeObjects[a] != NULL); } sGroundPlane = createGroundPlane(); assert(sGroundPlane != NULL); } // Called from the app framework. void appDeinit() { unsigned int a; for (a = 0; a < SUPERSHAPE_COUNT; ++a) freeGLObject(sSuperShapeObjects[a]); freeGLObject(sGroundPlane); } static void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar) { GLfloat xmin, xmax, ymin, ymax; ymax = zNear * (GLfloat)tan(fovy * PI / 360); ymin = -ymax; xmin = ymin * aspect; xmax = ymax * aspect; glFrustumx((GLfixed)(xmin * 65536), (GLfixed)(xmax * 65536), (GLfixed)(ymin * 65536), (GLfixed)(ymax * 65536), (GLfixed)(zNear * 65536), (GLfixed)(zFar * 65536)); } static void prepareFrame(int width, int height) { glViewport(0, 0, width, height); glClearColorx((GLfixed)(0.1f * 65536), (GLfixed)(0.2f * 65536), (GLfixed)(0.3f * 65536), 0x10000); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, (float)width / height, 0.5f, 150); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } static void configureLightAndMaterial() { static GLfixed light0Position[] = { -0x40000, 0x10000, 0x10000, 0 }; static GLfixed light0Diffuse[] = { 0x10000, 0x6666, 0, 0x10000 }; static GLfixed light1Position[] = { 0x10000, -0x20000, -0x10000, 0 }; static GLfixed light1Diffuse[] = { 0x11eb, 0x23d7, 0x5999, 0x10000 }; static GLfixed light2Position[] = { -0x10000, 0, -0x40000, 0 }; static GLfixed light2Diffuse[] = { 0x11eb, 0x2b85, 0x23d7, 0x10000 }; static GLfixed materialSpecular[] = { 0x10000, 0x10000, 0x10000, 0x10000 }; glLightxv(GL_LIGHT0, GL_POSITION, light0Position); glLightxv(GL_LIGHT0, GL_DIFFUSE, light0Diffuse); glLightxv(GL_LIGHT1, GL_POSITION, light1Position); glLightxv(GL_LIGHT1, GL_DIFFUSE, light1Diffuse); glLightxv(GL_LIGHT2, GL_POSITION, light2Position); glLightxv(GL_LIGHT2, GL_DIFFUSE, light2Diffuse); glMaterialxv(GL_FRONT_AND_BACK, GL_SPECULAR, materialSpecular); glMaterialx(GL_FRONT_AND_BACK, GL_SHININESS, 60 << 16); glEnable(GL_COLOR_MATERIAL); } static void drawModels(float zScale) { const int translationScale = 9; int x, y; seedRandom(9); glScalex(1 << 16, 1 << 16, (GLfixed)(zScale * 65536)); for (y = -5; y <= 5; ++y) { for (x = -5; x <= 5; ++x) { float buildingScale; GLfixed fixedScale; int curShape = randomUInt() % SUPERSHAPE_COUNT; buildingScale = sSuperShapeParams[curShape][SUPERSHAPE_PARAMS - 1]; fixedScale = (GLfixed)(buildingScale * 65536); glPushMatrix(); glTranslatex((x * translationScale) * 65536, (y * translationScale) * 65536, 0); glRotatex((GLfixed)((randomUInt() % 360) << 16), 0, 0, 1 << 16); glScalex(fixedScale, fixedScale, fixedScale); drawGLObject(sSuperShapeObjects[curShape]); glPopMatrix(); } } for (x = -2; x <= 2; ++x) { const int shipScale100 = translationScale * 500; const int offs100 = x * shipScale100 + (sTick % shipScale100); float offs = offs100 * 0.01f; GLfixed fixedOffs = (GLfixed)(offs * 65536); glPushMatrix(); glTranslatex(fixedOffs, -4 * 65536, 2 << 16); drawGLObject(sSuperShapeObjects[SUPERSHAPE_COUNT - 1]); glPopMatrix(); glPushMatrix(); glTranslatex(-4 * 65536, fixedOffs, 4 << 16); glRotatex(90 << 16, 0, 0, 1 << 16); drawGLObject(sSuperShapeObjects[SUPERSHAPE_COUNT - 1]); glPopMatrix(); } } /* Following gluLookAt implementation is adapted from the * Mesa 3D Graphics library. http://www.mesa3d.org */ static void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez, GLfloat centerx, GLfloat centery, GLfloat centerz, GLfloat upx, GLfloat upy, GLfloat upz) { GLfloat m[16]; GLfloat x[3], y[3], z[3]; GLfloat mag; /* Make rotation matrix */ /* Z vector */ z[0] = eyex - centerx; z[1] = eyey - centery; z[2] = eyez - centerz; mag = (float)sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]); if (mag) { /* mpichler, 19950515 */ z[0] /= mag; z[1] /= mag; z[2] /= mag; } /* Y vector */ y[0] = upx; y[1] = upy; y[2] = upz; /* X vector = Y cross Z */ x[0] = y[1] * z[2] - y[2] * z[1]; x[1] = -y[0] * z[2] + y[2] * z[0]; x[2] = y[0] * z[1] - y[1] * z[0]; /* Recompute Y = Z cross X */ y[0] = z[1] * x[2] - z[2] * x[1]; y[1] = -z[0] * x[2] + z[2] * x[0]; y[2] = z[0] * x[1] - z[1] * x[0]; /* mpichler, 19950515 */ /* cross product gives area of parallelogram, which is < 1.0 for * non-perpendicular unit-length vectors; so normalize x, y here */ mag = (float)sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]); if (mag) { x[0] /= mag; x[1] /= mag; x[2] /= mag; } mag = (float)sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]); if (mag) { y[0] /= mag; y[1] /= mag; y[2] /= mag; } #define M(row,col) m[(col)*4+(row)] M(0, 0) = x[0]; M(0, 1) = x[1]; M(0, 2) = x[2]; M(0, 3) = 0.0; M(1, 0) = y[0]; M(1, 1) = y[1]; M(1, 2) = y[2]; M(1, 3) = 0.0; M(2, 0) = z[0]; M(2, 1) = z[1]; M(2, 2) = z[2]; M(2, 3) = 0.0; M(3, 0) = 0.0; M(3, 1) = 0.0; M(3, 2) = 0.0; M(3, 3) = 1.0; #undef M { int a; GLfixed fixedM[16]; for (a = 0; a < 16; ++a) fixedM[a] = (GLfixed)(m[a] * 65536); glMultMatrixx(fixedM); } /* Translate Eye to Origin */ glTranslatex((GLfixed)(-eyex * 65536), (GLfixed)(-eyey * 65536), (GLfixed)(-eyez * 65536)); } static void camTrack() { float lerp[5]; float eX, eY, eZ, cX, cY, cZ; float trackPos; CAMTRACK *cam; long currentCamTick; int a; if (sNextCamTrackStartTick <= sTick) { ++sCurrentCamTrack; sCurrentCamTrackStartTick = sNextCamTrackStartTick; } sNextCamTrackStartTick = sCurrentCamTrackStartTick + sCamTracks[sCurrentCamTrack].len * CAMTRACK_LEN; cam = &sCamTracks[sCurrentCamTrack]; currentCamTick = sTick - sCurrentCamTrackStartTick; trackPos = (float)currentCamTick / (CAMTRACK_LEN * cam->len); for (a = 0; a < 5; ++a) lerp[a] = (cam->src[a] + cam->dest[a] * trackPos) * 0.01f; if (cam->dist) { float dist = cam->dist * 0.1f; cX = lerp[0]; cY = lerp[1]; cZ = lerp[2]; eX = cX - (float)cos(lerp[3]) * dist; eY = cY - (float)sin(lerp[3]) * dist; eZ = cZ - lerp[4]; } else { eX = lerp[0]; eY = lerp[1]; eZ = lerp[2]; cX = eX + (float)cos(lerp[3]); cY = eY + (float)sin(lerp[3]); cZ = eZ + lerp[4]; } gluLookAt(eX, eY, eZ, cX, cY, cZ, 0, 0, 1); } // Called from the app framework. /* The tick is current time in milliseconds, width and height * are the image dimensions to be rendered. */ void appRender(long tick, int width, int height) { if (sStartTick == 0) sStartTick = tick; if (!gAppAlive) return; // Actual tick value is "blurred" a little bit. sTick = (sTick + tick - sStartTick) >> 1; // Terminate application after running through the demonstration once. if (sTick >= RUN_LENGTH) { gAppAlive = 0; return; } // Prepare OpenGL ES for rendering of the frame. prepareFrame(width, height); // Update the camera position and set the lookat. camTrack(); // Configure environment. configureLightAndMaterial(); // Draw the reflection by drawing models with negated Z-axis. glPushMatrix(); drawModels(-1); glPopMatrix(); // Blend the ground plane to the window. drawGroundPlane(); // Draw all the models normally. drawModels(1); // Draw fade quad over whole window (when changing cameras). drawFadeQuad(); } |
通过上面的示例可以看到OpenGL EGL炫彩的世界,并且C++和Java结合就能一起构建诸多复杂美丽的图形。
并且实现方式跟前一篇文章是相呼应的。
(四)、参考文档:
Thank you for reading, this article belongs to ๑Charles✦ˑ̫✦Vincent๑ copyright, if reproduced, please indicate the source:๑Charles✦ˑ̫✦Vincent๑(http://zhoujinjian.cc/2018/12/20/Android Graphics Tests 程序学习02—Opengl EGL炫彩实例Native&Java/)