OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
occlusion.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2014-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 "common.hpp"
22 #include "scene.hpp"
23 #include "common.hpp"
24 #include <stdlib.h>
25 
26 #define GLES_VERSION 3
27 #include "Timer.h"
28 #include "Text.h"
29 
30 using namespace std;
31 
33 
34 static void render_text(Text &text, const char *method, float current_time)
35 {
36  // Enable alpha blending.
37  GL_CHECK(glEnable(GL_BLEND));
38  GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
39 
40  char method_string[128];
41  sprintf(method_string, "Method: %s (%4.1f / 10.0 s)", method, current_time);
42 
43  text.clear();
44  text.addString(300, surface_height - 20, method_string, 255, 255, 255, 255);
45 
46  text.addString(20, surface_height - 40, " Legend:", 255, 255, 255, 255);
47  text.addString(20, surface_height - 60, "Green tinted sphere: LOD 0", 255, 255, 0, 255);
48  text.addString(20, surface_height - 80, " Blue tinted sphere: LOD 1 - LOD 3", 255, 255, 0, 255);
49  text.addString(20, surface_height - 100, " Dark sphere: Occluded spheres", 255, 255, 0, 255);
50 
51  text.draw();
52  GL_CHECK(glDisable(GL_BLEND));
53 }
54 
55 Scene *scene = NULL;
56 Text *text = NULL;
57 
59 unsigned phase = 0;
60 float culling_timer = 0.0f;
61 
62 extern "C"
63 {
65  (JNIEnv *env, jclass jcls, jint width, jint height)
66  {
67  common_set_basedir("/data/data/com.arm.malideveloper.openglessdk.occlusionculling/files/");
68 
69  delete scene;
70  scene = new Scene;
73 
74  delete text;
75  text = new Text("/data/data/com.arm.malideveloper.openglessdk.occlusionculling/files/", width, height);
76 
77  timer.reset();
80  }
81 
83  (JNIEnv *env, jclass jcls)
84  {
85  float delta_time = timer.getInterval();
86 
87  // Render scene.
88  scene->move_camera(delta_time * 0.1f, 0.0f);
91 
92  static const char *methods[] = {
93  "Hierarchical-Z occlusion culling with level-of-detail",
94  "Hierarchical-Z occlusion culling without level-of-detail",
95  "No culling"
96  };
97  render_text(*text, methods[phase], culling_timer);
98 
99  // Don't need depth nor stencil buffers anymore. Just discard them so they are not written out to memory on Mali.
100  static const GLenum attachments[] = { GL_DEPTH, GL_STENCIL };
101  GL_CHECK(glInvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments));
102 
103  // Change the culling method over time.
104  culling_timer += delta_time;
105  if (culling_timer > 10.0f)
106  {
107  culling_timer = 0.0f;
108  phase = (phase + 1) % 3;
109 
110  switch (phase)
111  {
112  case 0:
114  break;
115  case 1:
117  break;
118  case 2:
120  break;
121  }
122  }
123 
124 
125  }
126 
128  (JNIEnv *, jclass)
129  {
130  delete scene;
131  scene = NULL;
132  delete text;
133  text = NULL;
134  }
135 }
136 
137 
void render(unsigned width, unsigned height)
Definition: scene.cpp:373
Functions for drawing text in OpenGL ES.
Definition: Text.h:44
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
float current_time
void update(float delta_time, unsigned width, unsigned height)
Definition: scene.cpp:282
Text * text
Definition: occlusion.cpp:56
Provides a platform independent high resolution timer.
Definition: Timer.h:37
void clear(void)
Removes the current string from the class.
Definition: Text.cpp:142
void move_camera(float delta_x, float delta_y)
Definition: scene.cpp:83
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionculling_OcclusionCulling_uninit(JNIEnv *, jclass)
Definition: occlusion.cpp:128
float culling_timer
Definition: occlusion.cpp:60
void common_set_basedir(const char *basedir)
Definition: common.cpp:332
int surface_width
Definition: occlusion.cpp:32
void set_culling_method(CullingMethod method)
Definition: scene.cpp:251
Scene * scene
Definition: occlusion.cpp:55
void reset()
Resets the timer to 0.0f.
Definition: Timer.cpp:100
Definition: scene.hpp:29
GLfloat GLfloat f
Definition: gl2ext.h:2707
void addString(int xPosition, int yPosition, const char *string, int red, int green, int blue, int alpha)
Add a std::string to be drawn to the screen.
Definition: Text.cpp:157
void set_show_redundant(bool enable)
Definition: scene.hpp:48
GLsizei const GLenum * attachments
Definition: gl2ext.h:1116
#define GL_CHECK(x)
Definition: AstcTextures.h:59
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionculling_OcclusionCulling_step(JNIEnv *env, jclass jcls)
Definition: occlusion.cpp:83
float Scene(vec3 p)
Definition: generate.cs:63
float getInterval()
Returns the time passed since getInterval() was last called.
Definition: Timer.cpp:117
static void render_text(Text &text, const char *method, float current_time)
Definition: occlusion.cpp:34
Timer timer
Definition: occlusion.cpp:58
int surface_height
Definition: occlusion.cpp:32
GLint GLsizei width
Definition: gl2ext.h:179
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
unsigned phase
Definition: occlusion.cpp:59
void draw(void)
Draw the text to the screen.
Definition: Text.cpp:265
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionculling_OcclusionCulling_init(JNIEnv *env, jclass jcls, jint width, jint height)
Definition: occlusion.cpp:65