OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AntiAlias.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2013-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 
33 #include <GLES2/gl2.h>
34 #include <GLES2/gl2ext.h>
35 
36 #include <cstdio>
37 #include <cstdlib>
38 #include <cmath>
39 #include <string>
40 
41 #include <jni.h>
42 #include <android/log.h>
43 #include <unistd.h>
44 
45 #include "AntiAlias.h"
46 #include "Text.h"
47 #include "Shader.h"
48 #include "Matrix.h"
49 #include "AndroidPlatform.h"
50 
51 using std::string;
52 using namespace MaliSDK;
53 
54 /* Asset directories and filenames. */
55 string resourceDirectory = "/data/data/com.arm.malideveloper.openglessdk.antialias/";
56 string vertexShaderFilename = "AntiAlias_triangle.vert";
57 string fragmentShaderFilename = "AntiAlias_triangle.frag";
58 
59 /* Shader variables. */
61 GLint iLocPosition = -1;
62 GLint iLocFillColor = -1;
63 GLint iLocProjection = -1;
64 
65 /* EGL variables. */
67 
68 /* A text object to draw text on the screen. */
70 
71 bool setupGraphics(int width, int height)
72 {
73  /* Full paths to the shader files */
74  string vertexShaderPath = resourceDirectory + vertexShaderFilename;
75  string fragmentShaderPath = resourceDirectory + fragmentShaderFilename;
76 
77  /* Initialize OpenGL ES. */
78  GL_CHECK(glEnable(GL_BLEND));
79  /* Should do src * (src alpha) + dest * (1-src alpha). */
80  GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
81 
82  /* Initialize the Text object and add some text. */
83  text = new Text(resourceDirectory.c_str(), width, height);
84  text->addString(0, 0, "Anti-aliased triangle", 255, 255, 0, 255);
85 
86  /* Process shaders. */
89  LOGI("setupGraphics(%d, %d)", width, height);
90  Shader::processShader(&vertexShaderID, vertexShaderPath.c_str(), GL_VERTEX_SHADER);
91  LOGI("vertexShaderID = %d", vertexShaderID);
92  Shader::processShader(&fragmentShaderID, fragmentShaderPath.c_str(), GL_FRAGMENT_SHADER);
93  LOGI("fragmentShaderID = %d", fragmentShaderID);
94 
95  /* Set up shaders. */
96  programID = GL_CHECK(glCreateProgram());
97  if (!programID)
98  {
99  LOGE("Could not create program.");
100  return false;
101  }
102  GL_CHECK(glAttachShader(programID, vertexShaderID));
103  GL_CHECK(glAttachShader(programID, fragmentShaderID));
104  GL_CHECK(glLinkProgram(programID));
105  GL_CHECK(glUseProgram(programID));
106  LOGI("Shaders in use...");
107 
108  /* Vertex positions. */
109  iLocPosition = GL_CHECK(glGetAttribLocation(programID, "a_v4Position"));
110  if(iLocPosition == -1)
111  {
112  LOGE("Attribute not found at %s:%i\n", __FILE__, __LINE__);
113  return false;
114  }
115  GL_CHECK(glEnableVertexAttribArray(iLocPosition));
116 
117  /* Fill colors. */
118  iLocFillColor = GL_CHECK(glGetAttribLocation(programID, "a_v4FillColor"));
119  if(iLocFillColor == -1)
120  {
121  LOGD("Warning: Uniform not found at %s:%i\n", __FILE__, __LINE__);
122  }
123  else
124  {
125  GL_CHECK(glEnableVertexAttribArray(iLocFillColor));
126  }
127 
128  /* Projection matrix. */
129  iLocProjection = GL_CHECK(glGetUniformLocation(programID, "u_m4Projection"));
130  if(iLocProjection == -1)
131  {
132  LOGD("Warning: Uniform not found at %s:%i\n", __FILE__, __LINE__);
133  }
134  else
135  {
136  GL_CHECK(glUniformMatrix4fv(iLocProjection, 1, GL_FALSE, Matrix::identityMatrix.getAsArray()));
137  }
138 
139  /* Set clear screen color. RGBA format, so opaque blue. */
140  GL_CHECK(glClearColor(0.0f, 0.0f, 1.0f, 1.0f));
141 
142  return true;
143 }
144 
145 void renderFrame(void)
146 {
147  /* Clear the screen on the EGL surface. */
148  GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
149 
150  GL_CHECK(glUseProgram(programID));
151 
152  /* Load EGL window-specific projection and modelview matrices. */
153  if(iLocProjection != -1)
154  {
155  GL_CHECK(glUniformMatrix4fv(iLocProjection, 1, GL_FALSE, Matrix::identityMatrix.getAsArray()));
156  }
157 
158  /* Set triangle vertex. */
159  GL_CHECK(glVertexAttribPointer(iLocPosition, 3, GL_FLOAT, GL_FALSE, 0, triangleVertices));
160  GL_CHECK(glEnableVertexAttribArray(iLocPosition));
161 
162  /* Set triangle colors. */
163  if(iLocFillColor != -1)
164  {
165  GL_CHECK(glVertexAttribPointer(iLocFillColor, 4, GL_FLOAT, GL_FALSE, 0, triangleColors));
166  GL_CHECK(glEnableVertexAttribArray(iLocFillColor));
167  }
168 
169  /* Draw the triangle. */
170  GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 3));
171 
172  /* Draw fonts. */
173  text->draw();
174 }
175 
176 extern "C"
177 {
178 
180  (JNIEnv *env, jclass jcls, jint width, jint height)
181  {
182  /* Make sure that all resource files are in place */
185 
186  setupGraphics(width, height);
187  }
188 
190  (JNIEnv *env, jclass jcls)
191  {
192  renderFrame();
193  }
194 
196  (JNIEnv *, jclass)
197  {
198  delete text;
199  }
200 }
#define LOGI(...)
Definition: AstcTextures.h:29
Functions for drawing text in OpenGL ES.
Definition: Text.h:44
static Matrix identityMatrix
The identity matrix.
Definition: Matrix.h:90
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_antialias_AntiAlias_uninit(JNIEnv *, jclass)
Definition: AntiAlias.cpp:196
void renderFrame(void)
Definition: AntiAlias.cpp:145
GLuint fragmentShaderID
const float triangleVertices[]
Definition: AntiAlias.h:29
#define LOGD(...)
Definition: AstcTextures.h:28
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_antialias_AntiAlias_init(JNIEnv *env, jclass jcls, jint width, jint height)
Definition: AntiAlias.cpp:180
const float triangleColors[]
Definition: AntiAlias.h:37
bool setupGraphics(int width, int height)
Definition: AntiAlias.cpp:71
string fragmentShaderFilename
Definition: AntiAlias.cpp:57
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
GLuint programID
Definition: AntiAlias.cpp:60
#define GL_CHECK(x)
Definition: AstcTextures.h:59
string resourceDirectory
Definition: AntiAlias.cpp:55
static bool getAndroidAsset(JNIEnv *JNIEnvironment, const char destinationDirectory[], const char filename[])
Extract an asset file from the APK.
int numberOfSamples
Definition: AntiAlias.cpp:66
GLuint vertexShaderID
#define LOGE(...)
Definition: AstcTextures.h:30
GLint GLsizei width
Definition: gl2ext.h:179
GLint iLocProjection
Definition: AntiAlias.cpp:63
static void processShader(GLuint *shader, const char *filename, GLint shaderType)
Create shader, load in source, compile, and dump debug as necessary.
Definition: Shader.cpp:29
void draw(void)
Draw the text to the screen.
Definition: Text.cpp:265
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
string vertexShaderFilename
Definition: AntiAlias.cpp:56
Text * text
Definition: AntiAlias.cpp:69
GLint iLocPosition
Definition: AntiAlias.cpp:61
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_antialias_AntiAlias_step(JNIEnv *env, jclass jcls)
Definition: AntiAlias.cpp:190
GLint iLocFillColor
Definition: AntiAlias.cpp:62