OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Triangle.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 
28 #include <GLES2/gl2.h>
29 #include <GLES2/gl2ext.h>
30 
31 #include <jni.h>
32 #include <android/log.h>
33 
34 #include <cstdio>
35 #include <cstdlib>
36 #include <cmath>
37 
38 #include "AndroidPlatform.h"
39 #include "Triangle.h"
40 #include "Text.h"
41 #include "Shader.h"
42 #include "Timer.h"
43 
44 using std::string;
45 using namespace MaliSDK;
46 
47 /* Asset directories and filenames. */
48 string resourceDirectory = "/data/data/com.arm.malideveloper.openglessdk.triangle/";
49 string vertexShaderFilename = "Triangle_triangle.vert";
50 string fragmentShaderFilename = "Triangle_triangle.frag";
51 
52 /* Shader variables. */
54 GLint iLocPosition = -1;
55 GLint iLocFillColor = -1;
56 
57 /* A text object to draw text on the screen. */
59 
60 bool setupGraphics(int width, int height)
61 {
62  LOGD("setupGraphics(%d, %d)", width, height);
63 
64  /* Full paths to the shader files */
65  string vertexShaderPath = resourceDirectory + vertexShaderFilename;
66  string fragmentShaderPath = resourceDirectory + fragmentShaderFilename;
67 
70 
71  GL_CHECK(glEnable(GL_DEPTH_TEST));
72  GL_CHECK(glDepthFunc(GL_LEQUAL));
73 
74 
75  /* Initialize OpenGL ES. */
76  GL_CHECK(glEnable(GL_BLEND));
77  /* Should do: src * (src alpha) + dest * (1-src alpha). */
78  GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
79 
80  /* Initialize the Text object and add some text. */
81  text = new Text(resourceDirectory.c_str(), width, height);
82  text->addString(0, 0, "Simple triangle", 255, 255, 0, 255);
83 
84  /* Process shaders. */
85  Shader::processShader(&vertexShaderID, vertexShaderPath.c_str(), GL_VERTEX_SHADER);
86  LOGD("vertexShaderID = %d", vertexShaderID);
87  Shader::processShader(&fragmentShaderID, fragmentShaderPath.c_str(), GL_FRAGMENT_SHADER);
88  LOGD("fragmentShaderID = %d", fragmentShaderID);
89 
90  programID = GL_CHECK(glCreateProgram());
91  if (programID == 0)
92  {
93  LOGE("Could not create program.");
94  return false;
95  }
96 
97  GL_CHECK(glAttachShader(programID, vertexShaderID));
98  GL_CHECK(glAttachShader(programID, fragmentShaderID));
99  GL_CHECK(glLinkProgram(programID));
100  GL_CHECK(glUseProgram(programID));
101 
102  /* Positions. */
103  GL_CHECK(iLocPosition = glGetAttribLocation(programID, "a_v4Position"));
104  LOGD("glGetAttribLocation(\"a_v4Position\") = %d\n", iLocPosition);
105 
106  /* Fill colors. */
107  GL_CHECK(iLocFillColor = glGetAttribLocation(programID, "a_v4FillColor"));
108  LOGD("glGetAttribLocation(\"a_v4FillColor\") = %d\n", iLocFillColor);
109 
110  GL_CHECK(glViewport(0, 0, width, height));
111 
112  /* Set clear screen color. */
113  GL_CHECK(glClearColor(0.0f, 0.0f, 1.0f, 1.0f));
114  GL_CHECK(glClearDepthf(1.0f));
115  return true;
116 }
117 
118 void renderFrame(void)
119 {
120  GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
121 
122  GL_CHECK(glUseProgram(programID));
123 
124  /* Pass the triangle vertex positions to the shader */
125  GL_CHECK(glVertexAttribPointer(iLocPosition, 3, GL_FLOAT, GL_FALSE, 0, triangleVertices));
126 
127  GL_CHECK(glEnableVertexAttribArray(iLocPosition));
128 
129  if(iLocFillColor != -1)
130  {
131  /* Pass the vertex colours to the shader */
132  GL_CHECK(glVertexAttribPointer(iLocFillColor, 4, GL_FLOAT, GL_FALSE, 0, triangleColors));
133  GL_CHECK(glEnableVertexAttribArray(iLocFillColor));
134  }
135 
136  GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 3));
137 
138  /* Draw fonts. */
139  text->draw();
140 }
141 
142 extern "C"
143 {
145  (JNIEnv *env, jclass jcls, jint width, jint height)
146  {
147  /* Make sure that all resource files are in place. */
150 
151  setupGraphics(width, height);
152  }
153 
155  (JNIEnv *env, jclass jcls)
156  {
157  renderFrame();
158  }
159 
161  (JNIEnv *, jclass)
162  {
163  delete text;
164  }
165 }
GLuint programID
Definition: Triangle.cpp:53
string fragmentShaderFilename
Definition: Triangle.cpp:50
Text * text
Definition: Triangle.cpp:58
Functions for drawing text in OpenGL ES.
Definition: Text.h:44
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
GLuint fragmentShaderID
const float triangleVertices[]
Definition: AntiAlias.h:29
#define LOGD(...)
Definition: AstcTextures.h:28
GLint iLocFillColor
Definition: Triangle.cpp:55
const float triangleColors[]
Definition: AntiAlias.h:37
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_triangle_Triangle_uninit(JNIEnv *, jclass)
Definition: Triangle.cpp:161
GLint iLocPosition
Definition: Triangle.cpp:54
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
#define GL_CHECK(x)
Definition: AstcTextures.h:59
string resourceDirectory
Definition: Triangle.cpp:48
static bool getAndroidAsset(JNIEnv *JNIEnvironment, const char destinationDirectory[], const char filename[])
Extract an asset file from the APK.
string vertexShaderFilename
Definition: Triangle.cpp:49
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_triangle_Triangle_init(JNIEnv *env, jclass jcls, jint width, jint height)
Definition: Triangle.cpp:145
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_triangle_Triangle_step(JNIEnv *env, jclass jcls)
Definition: Triangle.cpp:155
GLuint vertexShaderID
#define LOGE(...)
Definition: AstcTextures.h:30
GLint GLsizei width
Definition: gl2ext.h:179
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
bool setupGraphics(int width, int height)
Definition: Triangle.cpp:60
void renderFrame(void)
Definition: Triangle.cpp:118