OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ETCAtlasAlpha.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 
35 #include <GLES2/gl2.h>
36 #include <GLES2/gl2ext.h>
37 
38 #include <string>
39 #include <sstream>
40 
41 #include <jni.h>
42 #include <android/log.h>
43 
44 #include "ETCAtlasAlpha.h"
45 #include "Shader.h"
46 #include "Texture.h"
47 #include "ETCHeader.h"
48 #include "AndroidPlatform.h"
49 
50 using std::stringstream;
51 using std::string;
52 using namespace MaliSDK;
53 string resourceDirectory = "/data/data/com.arm.malideveloper.openglessdk.etcatlasalpha/";
54 string textureFilename = "good_atlas_mip_";
55 string imageExtension = ".pkm";
56 
57 string vertexShaderFilename = "ETCAtlasAlpha_atlastex.vert";
58 string fragmentShaderFilename = "ETCAtlasAlpha_atlastex.frag";
59 
60 /* Texture variables. */
62 
63 /* Shader variables. */
65 GLint iLocPosition = -1;
66 GLint iLocTexCoord = -1;
67 GLint iLocSampler = -1;
68 
69 bool setupGraphics(int width, int height)
70 {
71  LOGD("setupGraphics(%d, %d)", width, height);
72 
73  /* Full paths to the shader and texture files */
74  string texturePath = resourceDirectory + textureFilename;
75  string vertexShaderPath = resourceDirectory + vertexShaderFilename;
76  string fragmentShaderPath = resourceDirectory + fragmentShaderFilename;
77 
78  /* Initialize OpenGL ES. */
79  /* Check which formats are supported. */
80  if (!Texture::isETCSupported(true))
81  {
82  LOGE("ETC1 not supported");
83  return false;
84  }
85 
86  /* Enable alpha blending. */
87  GL_CHECK(glEnable(GL_BLEND));
88  /* Should do src * (src alpha) + dest * (1-src alpha). */
89  GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
90 
91  /* Initialize textures. Using an atlas, load atlas and all mipmap levels from files. */
92  Texture::loadCompressedMipmaps(texturePath.c_str(), imageExtension.c_str(), &textureID);
93 
94  /* Process shaders. */
97  Shader::processShader(&vertexShaderID, vertexShaderPath.c_str(), GL_VERTEX_SHADER);
98  LOGD("vertexShaderID = %d", vertexShaderID);
99  Shader::processShader(&fragmentShaderID, fragmentShaderPath.c_str(), GL_FRAGMENT_SHADER);
100  LOGD("fragmentShaderID = %d", fragmentShaderID);
101 
102  programID = GL_CHECK(glCreateProgram());
103  if (!programID)
104  {
105  LOGE("Could not create program.");
106  return false;
107  }
108  GL_CHECK(glAttachShader(programID, vertexShaderID));
109  GL_CHECK(glAttachShader(programID, fragmentShaderID));
110  GL_CHECK(glLinkProgram(programID));
111  GL_CHECK(glUseProgram(programID));
112 
113  /* Vertex positions. */
114  iLocPosition = GL_CHECK(glGetAttribLocation(programID, "a_v4Position"));
115  if(iLocPosition == -1)
116  {
117  LOGE("Attribute not found: \"a_v4Position\"");
118  exit(1);
119  }
120  GL_CHECK(glEnableVertexAttribArray(iLocPosition));
121 
122  /* Texture coordinates. */
123  iLocTexCoord = GL_CHECK(glGetAttribLocation(programID, "a_v2TexCoord"));
124  if(iLocTexCoord == -1)
125  {
126  LOGD("Warning: Attribute not found: \"a_v2TexCoord\"");
127  }
128  else
129  {
130  GL_CHECK(glEnableVertexAttribArray(iLocTexCoord));
131  }
132 
133  /* Set the sampler to point at the 0th texture unit. */
134  iLocSampler = GL_CHECK(glGetUniformLocation(programID, "u_s2dTexture"));
135  if(iLocSampler == -1)
136  {
137  LOGD("Warning: Uniform not found: \"u_s2dTexture\"");
138  }
139  else
140  {
141  GL_CHECK(glUniform1i(iLocSampler, 0));
142  }
143 
144  /* Set clear screen color. */
145  GL_CHECK(glClearColor(0.125f, 0.25f, 0.5f, 1.0));
146 
147  return true;
148 }
149 
150 void renderFrame(void)
151 {
152  GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
153 
154  GL_CHECK(glUseProgram(programID));
155 
156  /* Pass the plane vertices to the shader. */
157  GL_CHECK(glVertexAttribPointer(iLocPosition, 3, GL_FLOAT, GL_FALSE, 0, vertices));
158 
159  GL_CHECK(glEnableVertexAttribArray(iLocPosition));
160 
161  if(iLocTexCoord != -1)
162  {
163  /* Pass the texture coordinates to the shader. */
164  GL_CHECK(glVertexAttribPointer(iLocTexCoord, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates));
165  GL_CHECK(glEnableVertexAttribArray(iLocTexCoord));
166  }
167 
168  GL_CHECK(glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices) / sizeof(GLubyte), GL_UNSIGNED_BYTE, indices));
169 }
170 
171 extern "C"
172 {
174  (JNIEnv *env, jclass jcls, jint width, jint height)
175  {
176  /* Make sure that all resource files are in place. */
179 
180  /* Load all "ATLAS" assets from 0 to 8 */
181  string texturePathFull = textureFilename + "0" + imageExtension;
182  int numberOfImages = 9;
183  for(int allImages = 0; allImages < numberOfImages; allImages++)
184  {
185  stringstream imageNumber;
186  imageNumber << allImages;
187  texturePathFull.replace(textureFilename.length(), 1, imageNumber.str());
188  AndroidPlatform::getAndroidAsset(env, resourceDirectory.c_str(), texturePathFull.c_str());
189  }
190 
191  setupGraphics(width, height);
192  }
193 
195  (JNIEnv *env, jclass jcls)
196  {
197  renderFrame();
198  }
199 
201  (JNIEnv *, jclass)
202  {
203 
204  }
205 }
GLuint textureID
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
const float vertices[]
Definition: Cube.h:30
GLuint fragmentShaderID
#define LOGD(...)
Definition: AstcTextures.h:28
void renderFrame(void)
GLsizei GLenum const void * indices
Definition: gl2ext.h:322
static void loadCompressedMipmaps(const char *filenameBase, const char *filenameSuffix, GLuint *textureID)
Load compressed mipmaps into memory.
Definition: Texture.cpp:232
GLfloat GLfloat f
Definition: gl2ext.h:2707
GLint iLocPosition
bool setupGraphics(int width, int height)
#define GL_CHECK(x)
Definition: AstcTextures.h:59
string fragmentShaderFilename
string textureFilename
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcatlasalpha_ETCAtlasAlpha_uninit(JNIEnv *, jclass)
static bool getAndroidAsset(JNIEnv *JNIEnvironment, const char destinationDirectory[], const char filename[])
Extract an asset file from the APK.
string resourceDirectory
GLint iLocSampler
GLuint vertexShaderID
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcatlasalpha_ETCAtlasAlpha_init(JNIEnv *env, jclass jcls, jint width, jint height)
#define LOGE(...)
Definition: AstcTextures.h:30
GLint GLsizei width
Definition: gl2ext.h:179
GLuint programID
static bool isETCSupported(bool verbose=false)
Reports whether or not ETC (Ericsson Texture Compression) is supported.
Definition: Texture.cpp:46
GLint iLocTexCoord
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
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcatlasalpha_ETCAtlasAlpha_step(JNIEnv *env, jclass jcls)
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
static const GLfloat textureCoordinates[]
Definition: ETCAtlasAlpha.h:97
string vertexShaderFilename
string imageExtension