OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Native.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 
47 #include <jni.h>
48 #include <android/log.h>
49 
50 #include <cstdlib>
51 #include <cmath>
52 #include <iostream>
53 #include <sstream>
54 #include <string>
55 
56 #include "Common.h"
57 #include "InstancedSolidTorus.h"
58 #include "Matrix.h"
59 #include "Shader.h"
60 #include "Torus.h"
61 #include "WireframeTorus.h"
62 
63 using namespace std;
64 using namespace MaliSDK;
65 
66 /* Asset directories and filenames */
67 const string resourceDirectory = "/data/data/com.arm.malideveloper.openglessdk.instancedTessellation/files/";
68 
69 /* Window properties. */
70 int windowWidth = 0;
71 int windowHeight = 0;
72 
73 /* Object managing OpenGL components which draw torus in wireframe. */
75 /* Object managing OpenGL components which draw instanced solid torus. */
77 
78 /* Rotation angles. */
79 static float angleX = 0.0f;
80 static float angleY = 0.0f;
81 static float angleZ = 0.0f;
82 
89 void setupGraphics(int width, int height)
90 {
91  /* Store window resolution. */
94 
96 
97  /* Distance between center of torus and center of its construction circle. */
98  const float torusRadius = 1.0f;
99  /* Radius of the construction circle. */
100  const float circleRadius = 0.4f;
101  /* Distance between solid torus and mesh. */
102  const float distance = 0.05f;
103 
104  /* Construct torus objects. */
105  /* [Generate WireframeTorus object] */
106  wireframeTorus = new WireframeTorus(torusRadius, circleRadius + distance);
107  /* [Generate WireframeTorus object] */
108  /* [Generate InstancedSolidTorus object] */
109  solidTorus = new InstancedSolidTorus(torusRadius, circleRadius);
110  /* [Generate InstancedSolidTorus object] */
111 
112  ASSERT(wireframeTorus != NULL, "Could not instantiate WireframeTorus class.");
113  ASSERT(solidTorus != NULL, "Could not instantiate InstancedSolidTorus class.");
114 
115  /* Configure projection matrix. */
116  Matrix projectionMatrix = Matrix::matrixPerspective(45.0f, float(windowWidth) / float(windowHeight), 0.1f, 100.0f);
117 
118  /* Set projection matrices for each of the torus objects. */
119  wireframeTorus->setProjectionMatrix(&projectionMatrix);
120  solidTorus->setProjectionMatrix(&projectionMatrix);
121 
122  /* Initialize OpenGL-ES. */
123  GL_CHECK(glEnable(GL_DEPTH_TEST));
124  GL_CHECK(glDepthFunc(GL_LEQUAL));
125 }
126 
131 {
132  /* [Update rotations angle] */
133  /* Increment rotation angles. */
134  angleX += 0.5;
135  angleY += 0.5;
136  angleZ += 0.5;
137 
138  if(angleX >= 360.0f) angleX = 0.0f;
139  if(angleY >= 360.0f) angleY = 0.0f;
140  if(angleZ >= 360.0f) angleZ = 0.0f;
141 
142  float rotationVector[] = {angleX, angleY, angleZ};
143  /* [Update rotations angle] */
144 
145  /* Clear the screen */
146  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
147 
148  /* Draw both toruses. */
149  /* [Draw wireframe model] */
150  wireframeTorus->draw(rotationVector);
151  /* [Draw wireframe model] */
152  /* [Draw solid model] */
153  solidTorus->draw(rotationVector);
154  /* [Draw solid model] */
155 }
156 
160 void uninit()
161 {
162  /* Delete torus objects. */
163  delete wireframeTorus;
164  delete solidTorus;
165 }
166 
167 extern "C"
168 {
170  jint width,
171  jint height);
174 };
175 
177  jint width,
178  jint height)
179 {
180  setupGraphics(width, height);
181 }
182 
184 {
185  renderFrame();
186 }
187 
189 {
190  uninit();
191 }
void setupGraphics(int width, int height)
Definition: Native.cpp:1256
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_instancedTessellation_NativeLibrary_step(JNIEnv *, jobject)
Definition: Native.cpp:183
Torus * wireframeTorus
Definition: Native.cpp:74
static float angleZ
Definition: Native.cpp:81
Torus * solidTorus
Definition: Native.cpp:76
#define GL_CHECK(x)
Definition: Native.cpp:64
float projectionMatrix[16]
Definition: Native.cpp:156
void setProjectionMatrix(MaliSDK::Matrix *projectionMatrix)
Pass the correctly defined projection matrix to the program related to the torus model.
Definition: Torus.cpp:67
int windowHeight
Definition: Native.cpp:574
const string resourceDirectory
Definition: Native.cpp:67
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
Functions for manipulating matrices.
Definition: Matrix.h:31
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_instancedTessellation_NativeLibrary_init(JNIEnv *, jobject, jint width, jint height)
Definition: Native.cpp:176
void matrixPerspective(float *matrix, float fieldOfView, float aspectRatio, float zNear, float zFar)
Create a perspective projection matrix and store the results in the first parameter.
Definition: Matrix.cpp:96
Class derived from the Torus abstract class. It manages drawing of a rotating wireframed unicolor tor...
void uninit()
Delete created objects and free allocated memory.
Definition: Native.cpp:1706
static void setResourceDirectory(std::string requiredResourceDirectory)
Set the resource directory for all tori.
Definition: Torus.cpp:105
Class derived form Torus abstract class. It manages drawing of a rotating solid torus, built from separate patches. Each patch is modelled as a Bezier surface approximating surface of a perfect torus. To satisfy the C1 continuity between neighbour patches, the number of circles creating the torus and also the number of points in each circle is restricted to 12. It allows us to divide both circles of torus ("big" and "small") into 4 quadrants and approximate each of it using bicubic Bezier curves. Control mesh vertices has to be distored, so the derivatives on the patch edges are equal and resulting image is round. That is why we cannot use the regular way to determine control points. The patches are in fact very dense square-shaped meshes, used as input attributes by vertex shader. The shader changes their shape on the basis of the distorted control mesh and places them next to each other, forming a round torus. The class, apart from inherited components, manages:
GLfloat GLfloat f
Definition: gl2ext.h:2707
void renderFrame(void)
Definition: Native.cpp:1536
Abstract class that draws torus on the screen. It stores generic data describing the drawn torus: ...
Definition: Torus.h:39
int windowWidth
Definition: Native.cpp:575
static float angleX
Definition: Native.cpp:79
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_instancedTessellation_NativeLibrary_uninit(JNIEnv *, jobject)
Definition: Native.cpp:188
virtual void draw(float *rotationVector)=0
Draw the torus model.
#define ASSERT(x, s)
Definition: common.h:45
GLint GLsizei width
Definition: gl2ext.h:179
static float angleY
Definition: Native.cpp:80
GLsizei GLsizei GLfloat distance
Definition: gl2ext.h:2507