OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AstcTextures.h
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 
21 #ifndef ASTC_TEXTURES_H
22 #define ASTC_TEXTURES_H
23 
24 #include <android/log.h>
25 #include <GLES3/gl3.h>
26 
27 #define LOG_TAG "libNative"
28 #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
29 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
30 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
31 #define LOGF(...) __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, __VA_ARGS__)
32 
33 #define MALLOC_CHECK(ptr_type, ptr, size) \
34 { \
35  ptr = (ptr_type) malloc(size); \
36  if (ptr == NULL) \
37  { \
38  LOGF("Memory allocation error FILE: %s LINE: %i\n", __FILE__, __LINE__); \
39  exit(EXIT_FAILURE); \
40  } \
41 }
42 
43 #define REALLOC_CHECK(ptr_type, ptr, size) \
44 { \
45  ptr = (ptr_type) realloc(ptr, size); \
46  if (ptr == NULL) \
47  { \
48  LOGF("Memory allocation error FILE: %s LINE: %i\n", __FILE__, __LINE__); \
49  exit(EXIT_FAILURE); \
50  } \
51 }
52 
53 #define FREE_CHECK(ptr) \
54 { \
55  free((void*) ptr); \
56  ptr = NULL; \
57 }
58 
59 #define GL_CHECK(x) \
60  x; \
61  { \
62  GLenum glError = glGetError(); \
63  if(glError != GL_NO_ERROR) \
64  { \
65  LOGE("glGetError() = %i (%#.8x) at %s:%i\n", glError, glError, __FILE__, __LINE__); \
66  exit(EXIT_FAILURE); \
67  } \
68  }
69 
70 /* ASTC texture compression internal formats. */
71 #define GL_COMPRESSED_RGBA_ASTC_4x4_KHR (0x93B0)
72 #define GL_COMPRESSED_RGBA_ASTC_5x4_KHR (0x93B1)
73 #define GL_COMPRESSED_RGBA_ASTC_5x5_KHR (0x93B2)
74 #define GL_COMPRESSED_RGBA_ASTC_6x5_KHR (0x93B3)
75 #define GL_COMPRESSED_RGBA_ASTC_6x6_KHR (0x93B4)
76 #define GL_COMPRESSED_RGBA_ASTC_8x5_KHR (0x93B5)
77 #define GL_COMPRESSED_RGBA_ASTC_8x6_KHR (0x93B6)
78 #define GL_COMPRESSED_RGBA_ASTC_8x8_KHR (0x93B7)
79 #define GL_COMPRESSED_RGBA_ASTC_10x5_KHR (0x93B8)
80 #define GL_COMPRESSED_RGBA_ASTC_10x6_KHR (0x93B9)
81 #define GL_COMPRESSED_RGBA_ASTC_10x8_KHR (0x93BA)
82 #define GL_COMPRESSED_RGBA_ASTC_10x10_KHR (0x93BB)
83 #define GL_COMPRESSED_RGBA_ASTC_12x10_KHR (0x93BC)
84 #define GL_COMPRESSED_RGBA_ASTC_12x12_KHR (0x93BD)
85 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR (0x93D0)
86 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR (0x93D1)
87 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR (0x93D2)
88 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR (0x93D3)
89 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR (0x93D4)
90 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR (0x93D5)
91 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR (0x93D6)
92 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR (0x93D7)
93 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR (0x93D8)
94 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR (0x93D9)
95 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR (0x93DA)
96 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR (0x93DB)
97 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR (0x93DC)
98 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR (0x93DD)
99 
100 /* EXT_texture_compression_astc_decode_mode */
101 #define GL_TEXTURE_ASTC_DECODE_PRECISION_EXT (0x8F69)
102 
103 /* Time period for each texture set to be displayed. */
104 #define ASTC_TEXTURE_SWITCH_INTERVAL (5) /* sec */
105 
106 /* Angular rates around several axes. */
107 #define X_ROTATION_SPEED (5)
108 #define Y_ROTATION_SPEED (4)
109 #define Z_ROTATION_SPEED (3)
110 
111 /* Vertex shader source code. */
113 {
114  "#version 300 es\n"
115  "in vec4 av4position;\n"
116  "in vec3 vv3normal;\n"
117  "in vec2 vv3tex2dcoord;\n"
118  "uniform mat4 mv;\n"
119  "uniform mat4 mvp;\n"
120  "out vec2 tex2dcoord;\n"
121  "out vec3 normal;\n"
122  "out vec3 light;\n"
123  "out vec3 view;\n"
124  "void main() {\n"
125  " vec3 light_position = vec3(15.0, 0.0, 0.0);\n"
126  " vec4 P = mv * av4position;\n"
127  " normal = mat3(mv) * vv3normal;\n"
128  " light = light_position - P.xyz;\n"
129  " view = -P.xyz;\n"
130  " tex2dcoord = vv3tex2dcoord;\n"
131  " gl_Position = mvp * av4position;\n"
132  "}\n"
133 };
134 
135 /* Fragment shader source code. */
137 {
138  "#version 300 es\n"
139  "precision mediump float;\n"
140  "uniform sampler2D cloud_texture;\n"
141  "uniform sampler2D daytime_texture;\n"
142  "uniform sampler2D nighttime_texture;\n"
143  "in vec2 tex2dcoord;\n"
144  "in vec3 normal;\n"
145  "in vec3 light;\n"
146  "in vec3 view;\n"
147  "out vec4 color;\n"
148  "void main() {\n"
149  " vec3 diffuse_albedo = vec3(2.0, 2.0, 3.0);\n"
150  " vec3 specular_albedo = vec3(0.1);\n"
151  " float specular_power = 16.0;\n"
152  " vec3 Normal = normalize(normal);\n"
153  " vec3 Light = normalize(light);\n"
154  " vec3 View = normalize(view);\n"
155  " vec3 reflected_light = reflect(-Light, Normal);\n"
156  " vec3 diffuse = max(dot(Normal, Light), 0.0) * diffuse_albedo;\n"
157  " vec3 specular = pow(max(dot(reflected_light, View), 0.0), specular_power) * specular_albedo;\n"
158  " vec2 clouds = texture(cloud_texture, tex2dcoord).rg;\n"
159  " vec3 daytime = (texture(daytime_texture, tex2dcoord).rgb * diffuse + specular * clouds.g) * (1.0 - clouds.r) + clouds.r * diffuse;\n"
160  " vec3 nighttime = texture(nighttime_texture, tex2dcoord).rgb * (1.0 - clouds.r) * 2.0;\n"
161  " color = vec4(mix(nighttime, daytime, 0.5), 1.0);\n"
162  "}\n"
163 };
164 
172 GLuint load_shader(GLenum shader_type, const char* shader_source);
173 
182 GLuint create_program(const char* vertex_source, const char* fragment_source);
183 
184 /* ASTC header declaration. */
185 typedef struct
186 {
187  unsigned char magic[4];
188  unsigned char blockdim_x;
189  unsigned char blockdim_y;
190  unsigned char blockdim_z;
191  unsigned char xsize[3]; /* x-size = xsize[0] + xsize[1] + xsize[2] */
192  unsigned char ysize[3]; /* x-size, y-size and z-size are given in texels */
193  unsigned char zsize[3]; /* block count is inferred */
194 } astc_header;
195 
196 /* Contains information about texture set bindings. */
197 typedef struct texture_set
198 {
199  /* Bindings for each texture unit. */
203 
204  /* Name of compression algorithm. */
205  const char* name;
206 } texture_set;
207 
208 
209 /* Contains information about texture set files. */
210 typedef struct texture_set_info
211 {
212  /* Texture internal format. */
214 
215  /* Paths to texture images for one texture set. */
216  /* Also assign decode format for each texture. */
219 
221  const char* earth_color_texture_file_path;
222 
224  const char* earth_night_texture_file_path;
225 
226  /* Name of compression algorithm. */
227  const char* compressed_texture_format_name;
229 
237 GLint get_and_check_attrib_location(GLuint program, const GLchar* attrib_name);
238 
246 GLint get_and_check_uniform_location(GLuint program, const GLchar* uniform_name);
247 
248 #endif /* ASTC_TEXTURES_H */
GLuint create_program(const char *vertex_source, const char *fragment_source)
Create program object, attach vertex and fragment shader to it. Link program object and check whether...
const GLenum earth_color_decode_format
Definition: AstcTextures.h:220
const char * cloud_and_gloss_texture_file_path
Definition: AstcTextures.h:213
const char earth_fragment_shader_source[]
Definition: AstcTextures.h:133
struct texture_set_info texture_set_info
GLint get_and_check_attrib_location(GLuint program, const GLchar *attrib_name)
Invoke glGetAttribLocation(), if it has returned a positive value. Otherwise, print a message and exi...
GLuint load_shader(GLenum shader_type, const char *shader_source)
Create shader object and compile its source code.
GLuint earth_night_texture_id
Definition: AstcTextures.h:199
GLuint earth_color_texture_id
Definition: AstcTextures.h:198
const char * name
Definition: AstcTextures.h:202
const char * earth_color_texture_file_path
Definition: AstcTextures.h:214
struct texture_set texture_set
const char * earth_night_texture_file_path
Definition: AstcTextures.h:215
const char * compressed_texture_format_name
Definition: AstcTextures.h:218
const GLenum earth_night_decode_format
Definition: AstcTextures.h:223
const GLenum cloud_and_gloss_decode_format
Definition: AstcTextures.h:217
const GLenum compressed_data_internal_format
Definition: AstcTextures.h:210
const char earth_vertex_shader_source[]
Definition: AstcTextures.h:109
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
GLuint program
Definition: gl2ext.h:1475
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
GLint get_and_check_uniform_location(GLuint program, const GLchar *uniform_name)
Invoke glGetUniformLocation, if it has returned a positive value. Otherwise, print a message and exit...
GLuint cloud_and_gloss_texture_id
Definition: AstcTextures.h:197