22 #include <GLES3/gl3.h>
33 using namespace AstcTextures;
50 const string resource_directory(
"/data/data/com.arm.malideveloper.openglessdk.astctextureslowprecision/files/");
165 GLint attrib_location =
GL_CHECK(glGetAttribLocation(program, attrib_name));
167 if (attrib_location == -1)
169 LOGE(
"Cannot retrieve location of %s attribute.\n", attrib_name);
173 return attrib_location;
179 GLint uniform_location =
GL_CHECK(glGetUniformLocation(program, uniform_name));
181 if (uniform_location == -1)
183 LOGE(
"Cannot retrieve location of %s uniform.\n", uniform_name);
187 return uniform_location;
197 GL_CHECK(glShaderSource(shader, 1, &shader_source, NULL));
202 GL_CHECK(glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled));
204 if (compiled != GL_TRUE)
208 GL_CHECK(glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len));
212 char* log_buffer = NULL;
216 GL_CHECK(glGetShaderInfoLog(shader, info_len, NULL, log_buffer));
217 LOGE(
"Could not compile shader 0x%x:\n%s\n", shader_type, log_buffer);
240 GL_CHECK(glAttachShader(program, vertexShader));
241 GL_CHECK(glAttachShader(program, fragmentShader));
244 GLint linkStatus = GL_FALSE;
246 GL_CHECK(glGetProgramiv(program, GL_LINK_STATUS, &linkStatus));
248 if (linkStatus != GL_TRUE)
250 GLint buf_length = 0;
252 GL_CHECK(glGetProgramiv(program, GL_INFO_LOG_LENGTH, &buf_length));
256 char* log_buffer = NULL;
259 GL_CHECK(glGetProgramInfoLog(program, buf_length, NULL, log_buffer));
260 LOGE(
"Could not link program:\n%s\n", log_buffer);
272 LOGE(
"Error creating program object.");
295 if (!force_switch_texture)
318 GL_CHECK(glActiveTexture(GL_TEXTURE0));
320 GL_CHECK(glActiveTexture(GL_TEXTURE1));
322 GL_CHECK(glActiveTexture(GL_TEXTURE2));
335 unsigned char* compressed_data = NULL;
336 unsigned char* input_data = NULL;
339 unsigned int n_bytes_to_read = 0;
353 FILE* compressed_data_file = fopen(file_name,
"rb");
355 if (compressed_data_file == NULL)
357 LOGE(
"Could not open a file.\n");
361 LOGI(
"Loading texture [%s]\n", file_name);
364 fseek(compressed_data_file, 0, SEEK_END);
365 file_size = ftell(compressed_data_file);
366 rewind(compressed_data_file);
369 MALLOC_CHECK(
unsigned char*, input_data,
sizeof(
unsigned char) * file_size);
372 result = fread(input_data, 1, file_size, compressed_data_file);
374 if (result != file_size)
376 LOGE(
"Reading error [%s] ... FILE: %s LINE: %i\n", file_name, __FILE__, __LINE__);
384 xsize = astc_data_ptr->
xsize[0] + (astc_data_ptr->
xsize[1] << 8) + (astc_data_ptr->
xsize[2] << 16);
385 ysize = astc_data_ptr->
ysize[0] + (astc_data_ptr->
ysize[1] << 8) + (astc_data_ptr->
ysize[2] << 16);
386 zsize = astc_data_ptr->
zsize[0] + (astc_data_ptr->
zsize[1] << 8) + (astc_data_ptr->
zsize[2] << 16);
394 n_bytes_to_read = xblocks * yblocks * zblocks << 4;
398 GL_CHECK(glBindTexture(GL_TEXTURE_2D, to_id));
401 GL_CHECK(glCompressedTexImage2D(GL_TEXTURE_2D,
403 compressed_data_internal_format,
408 (
const GLvoid*)&astc_data_ptr[1]));
410 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
411 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
412 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT));
413 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT));
420 decode_format = GL_RGBA16F;
426 GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
429 fclose(compressed_data_file);
441 string cloud_and_gloss_texture_file_path;
442 string earth_color_texture_file_path;
443 string earth_night_texture_file_path;
494 const unsigned int n_sectors = 64;
495 const unsigned int n_rings = 64;
501 GLsizei sphere_vertices_size = 0;
502 GLsizei sphere_normals_size = 0;
503 GLsizei sphere_texcoords_size = 0;
512 GLsizei buffer_total_size = sphere_vertices_size + sphere_normals_size + sphere_texcoords_size;
517 GL_CHECK(glBufferData(GL_ARRAY_BUFFER, buffer_total_size, NULL, GL_STATIC_DRAW));
520 GL_CHECK(glBufferSubData(GL_ARRAY_BUFFER, buffer_offset, sphere_vertices_size, sphere_vertices));
522 buffer_offset += sphere_vertices_size;
524 GL_CHECK(glBufferSubData(GL_ARRAY_BUFFER, buffer_offset, sphere_normals_size, sphere_normals));
526 buffer_offset += sphere_normals_size;
528 GL_CHECK(glBufferSubData(GL_ARRAY_BUFFER, buffer_offset, sphere_texcoords_size, sphere_texcoords));
547 buffer_offset += sphere_vertices_size;
552 buffer_offset += sphere_normals_size;
576 const GLubyte* extensions =
GL_CHECK(glGetString(GL_EXTENSIONS));
578 if (strstr((
const char*) extensions,
"GL_KHR_texture_compression_astc_ldr") == NULL)
580 LOGI(
"OpenGL ES 3.0 implementation does not support GL_KHR_texture_compression_astc_ldr extension.\n");
584 if (strstr((
const char*) extensions,
"GL_EXT_texture_compression_astc_decode_mode") == NULL)
587 LOGI(
"OpenGL ES 3.0 implementation does not support GL_EXT_texture_compression_astc_decode_mode extension.\n");
592 if (strstr((
const char*) extensions,
"GL_EXT_texture_compression_astc_decode_mode_rgb9e5") == NULL)
595 LOGI(
"OpenGL ES 3.0 implementation does not support GL_EXT_texture_compression_astc_decode_mode_rgb9e5 extension.\n");
607 GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
640 model_view_matrix[14] -= 2.5f + sinf(
current_time / 5.0
f) * 0.5f;
661 GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
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...
static bool astc_decode_mode_rgb9e5_supported
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR
#define GL_COMPRESSED_RGBA_ASTC_12x10_KHR
#define MALLOC_CHECK(ptr_type, ptr, size)
uniform float sphere_radius
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_astctextureslowprecision_NativeLibrary_step(JNIEnv *, jobject)
void setup_program(void)
This function sets up a program object that will be used for rendering, as well as retrieves attribut...
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR
static Matrix createRotationY(float angle)
Create and return a rotation matrix around the y-axis matrix.
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...
const GLenum earth_color_decode_format
Functions for drawing text in OpenGL ES.
const char * cloud_and_gloss_texture_file_path
const char earth_fragment_shader_source[]
GLint nighttime_texture_location
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_astctextureslowprecision_NativeLibrary_init(JNIEnv *, jobject, jint width, jint height)
#define ASTC_TEXTURE_SWITCH_INTERVAL
GLint GLsizei GLsizei height
static const int textureCharacterHeight
The height (in pixels) of the characters in the text texture.
void clear(void)
Removes the current string from the class.
GLint cloud_texture_location
#define GL_COMPRESSED_RGBA_ASTC_8x5_KHR
const string resource_directory("/data/data/com.arm.malideveloper.openglessdk.astctextures/files/")
GLuint earth_night_texture_id
unsigned short * getSphereIndices(int *n_indices)
Returns sphere indices.
GLuint earth_color_texture_id
Functions for manipulating matrices.
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...
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR
Matrix perspective_matrix
GLuint load_shader(GLenum shader_type, const char *shader_source)
Create shader object and compile its source code.
#define GL_COMPRESSED_RGBA_ASTC_10x10_KHR
unsigned int current_texture_set_id
float * getSphereNormalData(int *normal_data_size)
Returns normal coordinates.
const char * earth_color_texture_file_path
Provides a platform independent high resolution timer.
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR
#define GL_COMPRESSED_RGBA_ASTC_5x4_KHR
float * getSphereTexcoords(int *texcoords_size)
Returns texture coordinates.
const char * earth_night_texture_file_path
texture_set texture_ids[n_texture_ids]
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR
const float field_of_view
static const int textureCharacterWidth
The width (in pixels) of the characters in the text texture.
const char * compressed_texture_format_name
#define GL_COMPRESSED_RGBA_ASTC_10x5_KHR
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR
GLint daytime_texture_location
void render_frame(void)
Renders a single frame.
void load_textures(void)
Define 32 texture sets that the demo will switch between every 5 seconds.
float * getSphereVertexData(int *vertex_data_size)
Returns sphere vertices.
#define GL_COMPRESSED_RGBA_ASTC_8x6_KHR
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR
#define M_PI
The value of pi.
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR
SolidSphere * solid_sphere
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_astctextureslowprecision_NativeLibrary_uninit(JNIEnv *, jobject)
#define GL_COMPRESSED_RGBA_ASTC_10x8_KHR
const GLenum earth_night_decode_format
static Matrix createRotationX(float angle)
Create and return a rotation matrix around the x-axis matrix.
#define GL_COMPRESSED_RGBA_ASTC_12x12_KHR
void cleanup_graphics(void)
Perform graphics clean-up actions.
#define GL_COMPRESSED_RGBA_ASTC_8x8_KHR
const GLenum cloud_and_gloss_decode_format
void update_texture_bindings(bool force_switch_texture)
Update texture bindings and text presented by text renderer.
const GLenum compressed_data_internal_format
texture_set_info texture_sets_info[]
#define GL_COMPRESSED_RGBA_ASTC_6x6_KHR
GLint texture_coords_location
const char earth_vertex_shader_source[]
#define GL_COMPRESSED_RGBA_ASTC_6x5_KHR
float getTime()
Returns the time passed since object creation or since reset() was last called.
void reset()
Resets the timer to 0.0f.
void setup_graphics(int width, int height)
#define GL_COMPRESSED_RGBA_ASTC_5x5_KHR
void load_buffer_data(void)
Sets up a buffer object that will hold mesh data (vertex positions, normal vectors, textures UV coordinates).
static Matrix createRotationZ(float angle)
Create and return a rotation matrix around the z-axis matrix.
#define GL_COMPRESSED_RGBA_ASTC_10x6_KHR
static T * buffer_offset(T *buffer, size_t offset)
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR
void draw(void)
Draw the text to the screen.
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR
GLuint load_texture(const char *file_name, GLenum compressed_data_internal_format)
Define and retrieve compressed texture image.
static Matrix matrixPerspective(float FOV, float ratio, float zNear, float zFar)
Create and return a perspective projection matrix.
unsigned short * sphere_indices
GLuint cloud_and_gloss_texture_id
#define GL_COMPRESSED_RGBA_ASTC_4x4_KHR
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.
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR
static bool astc_decode_mode_supported
#define GL_TEXTURE_ASTC_DECODE_PRECISION_EXT