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