OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
main.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2015-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include <jni.h>
22 #include <stdlib.h>
23 #include <time.h>
24 
25 #include <android/log.h>
26 #define LOG_TAG "ProceduralGeometry"
27 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
28 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
29 #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
30 
31 #define GL_GLEXT_PROTOTYPES
32 #define GL_GEOMETRY_SHADER GL_GEOMETRY_SHADER_EXT
33 #include <GLES3/gl3.h>
34 #include <GLES3/gl31.h>
35 #include <GLES2/gl2ext.h>
36 #include "geometry.cpp"
37 
38 #define BASE_ASSET_PATH "/data/data/com.arm.malideveloper.openglessdk.proceduralgeometry/files/"
39 #define TEXTURE_PATH(name) BASE_ASSET_PATH name
40 #define SHADER_PATH(name) BASE_ASSET_PATH name
41 #include "loader.cpp"
42 
43 #include <sys/time.h>
44 static timeval start_time;
45 static App app;
46 
47 const char *get_gl_error_msg(GLenum code)
48 {
49  switch (code)
50  {
51  case 0: return "NO_ERROR";
52  case 0x0500: return "INVALID_ENUM";
53  case 0x0501: return "INVALID_VALUE";
54  case 0x0502: return "INVALID_OPERATION";
55  case 0x0503: return "STACK_OVERFLOW";
56  case 0x0504: return "STACK_UNDERFLOW";
57  case 0x0505: return "OUT_OF_MEMORY";
58  case 0x0506: return "INVALID_FRAMEBUFFER_OPERATION";
59  default: return "UNKNOWN";
60  }
61 }
62 
64 {
65  GLenum error = glGetError();
66  if (error != GL_NO_ERROR)
67  {
68  LOGD("An OpenGL error occurred %s", get_gl_error_msg(error));
69  exit(1);
70  }
71 }
72 
73 extern "C"
74 {
76  {
77  LOGD("Init\n");
78  start_time.tv_sec = 0;
79  start_time.tv_usec = 0;
80  gettimeofday(&start_time, NULL);
81 
82  LOGD("Load assets\n");
83  load_assets(&app);
84  app_initialize(&app);
85  }
86 
87  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_proceduralgeometry_ProceduralGeometry_resize(JNIEnv* env, jobject obj, jint width, jint height)
88  {
89  app.window_width = width;
90  app.window_height = height;
91  app.elapsed_time = 0.0f;
92  glViewport(0, 0, width, height);
93  LOGD("Resizing %d %d\n", width, height);
94  }
95 
97  {
98  timeval now;
99  gettimeofday(&now, NULL);
100  float seconds = (now.tv_sec - start_time.tv_sec);
101  float milliseconds = (float(now.tv_usec - start_time.tv_usec)) / 1000000.0f;
102  float elapsed_time = seconds + milliseconds;
103  app.frame_time = elapsed_time - app.elapsed_time;
104  app.elapsed_time = elapsed_time;
105 
106  app_update_and_render(&app);
107  gl_check_error();
108 
109  LOGD("%.2f ms\n", app.frame_time * 1000.0f);
110  }
111 
112  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_proceduralgeometry_ProceduralGeometry_onpointerdown(JNIEnv* env, jobject obj, jfloat x, jfloat y)
113  {
114  app.pointer_x = x;
115  app.pointer_y = y;
116  app.pointer_down = true;
117  }
118 
119  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_proceduralgeometry_ProceduralGeometry_onpointerup(JNIEnv* env, jobject obj, jfloat x, jfloat y)
120  {
121  app.pointer_down = false;
122  if (y < 0.2f * app.window_height)
123  app.voxel_mode = 1.0f - app.voxel_mode;
124  }
125 };
void load_assets(App *app)
Definition: loader.cpp:175
bool pointer_down
Definition: geometry.h:46
void app_update_and_render(App *app)
Definition: geometry.cpp:297
void app_initialize(App *app)
Definition: geometry.cpp:248
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
void gl_check_error()
Definition: main.cpp:63
float frame_time
Definition: geometry.h:37
int window_width
Definition: geometry.h:35
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_proceduralgeometry_ProceduralGeometry_resize(JNIEnv *env, jobject obj, jint width, jint height)
Definition: main.cpp:87
static App app
Definition: main.cpp:45
float elapsed_time
Definition: geometry.h:38
int window_height
Definition: geometry.h:36
static timeval start_time
Definition: main.cpp:44
GLfloat GLfloat f
Definition: gl2ext.h:2707
const char * get_gl_error_msg(GLenum code)
Definition: main.cpp:34
float voxel_mode
Definition: geometry.h:53
float pointer_x
Definition: geometry.h:44
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_proceduralgeometry_ProceduralGeometry_step(JNIEnv *env, jobject obj)
Definition: main.cpp:96
float pointer_y
Definition: geometry.h:45
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
Definition: geometry.h:33
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_proceduralgeometry_ProceduralGeometry_init(JNIEnv *env, jobject obj)
Definition: main.cpp:75
precision highp float
Definition: hiz_cull.cs:37
GLint GLsizei width
Definition: gl2ext.h:179
#define LOGD(...)
Definition: main.cpp:29
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
GLint y
Definition: gl2ext.h:179
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_proceduralgeometry_ProceduralGeometry_onpointerdown(JNIEnv *env, jobject obj, jfloat x, jfloat y)
Definition: main.cpp:112
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_proceduralgeometry_ProceduralGeometry_onpointerup(JNIEnv *env, jobject obj, jfloat x, jfloat y)
Definition: main.cpp:119