22 #include <GLES3/gl3.h>
33 using namespace AstcTextures;
50 const string resource_directory(
"/data/data/com.arm.malideveloper.openglessdk.astctextures/files/");
162 GLint attrib_location =
GL_CHECK(glGetAttribLocation(program, attrib_name));
164 if (attrib_location == -1)
166 LOGE(
"Cannot retrieve location of %s attribute.\n", attrib_name);
170 return attrib_location;
176 GLint uniform_location =
GL_CHECK(glGetUniformLocation(program, uniform_name));
178 if (uniform_location == -1)
180 LOGE(
"Cannot retrieve location of %s uniform.\n", uniform_name);
184 return uniform_location;
194 GL_CHECK(glShaderSource(shader, 1, &shader_source, NULL));
199 GL_CHECK(glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled));
201 if (compiled != GL_TRUE)
205 GL_CHECK(glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len));
209 char* log_buffer = NULL;
213 GL_CHECK(glGetShaderInfoLog(shader, info_len, NULL, log_buffer));
214 LOGE(
"Could not compile shader 0x%x:\n%s\n", shader_type, log_buffer);
237 GL_CHECK(glAttachShader(program, vertexShader));
238 GL_CHECK(glAttachShader(program, fragmentShader));
241 GLint linkStatus = GL_FALSE;
243 GL_CHECK(glGetProgramiv(program, GL_LINK_STATUS, &linkStatus));
245 if (linkStatus != GL_TRUE)
247 GLint buf_length = 0;
249 GL_CHECK(glGetProgramiv(program, GL_INFO_LOG_LENGTH, &buf_length));
253 char* log_buffer = NULL;
256 GL_CHECK(glGetProgramInfoLog(program, buf_length, NULL, log_buffer));
257 LOGE(
"Could not link program:\n%s\n", log_buffer);
269 LOGE(
"Error creating program object.");
292 if (!force_switch_texture)
315 GL_CHECK(glActiveTexture(GL_TEXTURE0));
317 GL_CHECK(glActiveTexture(GL_TEXTURE1));
319 GL_CHECK(glActiveTexture(GL_TEXTURE2));
331 unsigned char* compressed_data = NULL;
332 unsigned char* input_data = NULL;
335 unsigned int n_bytes_to_read = 0;
349 FILE* compressed_data_file = fopen(file_name,
"rb");
351 if (compressed_data_file == NULL)
353 LOGE(
"Could not open a file.\n");
357 LOGI(
"Loading texture [%s]\n", file_name);
360 fseek(compressed_data_file, 0, SEEK_END);
361 file_size = ftell(compressed_data_file);
362 rewind(compressed_data_file);
365 MALLOC_CHECK(
unsigned char*, input_data,
sizeof(
unsigned char) * file_size);
368 result = fread(input_data, 1, file_size, compressed_data_file);
370 if (result != file_size)
372 LOGE(
"Reading error [%s] ... FILE: %s LINE: %i\n", file_name, __FILE__, __LINE__);
380 xsize = astc_data_ptr->
xsize[0] + (astc_data_ptr->
xsize[1] << 8) + (astc_data_ptr->
xsize[2] << 16);
381 ysize = astc_data_ptr->
ysize[0] + (astc_data_ptr->
ysize[1] << 8) + (astc_data_ptr->
ysize[2] << 16);
382 zsize = astc_data_ptr->
zsize[0] + (astc_data_ptr->
zsize[1] << 8) + (astc_data_ptr->
zsize[2] << 16);
390 n_bytes_to_read = xblocks * yblocks * zblocks << 4;
394 GL_CHECK(glBindTexture(GL_TEXTURE_2D, to_id));
397 GL_CHECK(glCompressedTexImage2D(GL_TEXTURE_2D,
399 compressed_data_internal_format,
404 (
const GLvoid*)&astc_data_ptr[1]));
406 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
407 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
408 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT));
409 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT));
412 GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
415 fclose(compressed_data_file);
427 string cloud_and_gloss_texture_file_path;
428 string earth_color_texture_file_path;
429 string earth_night_texture_file_path;
480 const unsigned int n_sectors = 64;
481 const unsigned int n_rings = 64;
487 GLsizei sphere_vertices_size = 0;
488 GLsizei sphere_normals_size = 0;
489 GLsizei sphere_texcoords_size = 0;
498 GLsizei buffer_total_size = sphere_vertices_size + sphere_normals_size + sphere_texcoords_size;
503 GL_CHECK(glBufferData(GL_ARRAY_BUFFER, buffer_total_size, NULL, GL_STATIC_DRAW));
506 GL_CHECK(glBufferSubData(GL_ARRAY_BUFFER, buffer_offset, sphere_vertices_size, sphere_vertices));
508 buffer_offset += sphere_vertices_size;
510 GL_CHECK(glBufferSubData(GL_ARRAY_BUFFER, buffer_offset, sphere_normals_size, sphere_normals));
512 buffer_offset += sphere_normals_size;
514 GL_CHECK(glBufferSubData(GL_ARRAY_BUFFER, buffer_offset, sphere_texcoords_size, sphere_texcoords));
533 buffer_offset += sphere_vertices_size;
538 buffer_offset += sphere_normals_size;
562 const GLubyte* extensions =
GL_CHECK(glGetString(GL_EXTENSIONS));
564 if (strstr((
const char*) extensions,
"GL_KHR_texture_compression_astc_ldr") == NULL)
566 LOGI(
"OpenGL ES 3.0 implementation does not support GL_KHR_texture_compression_astc_ldr extension.\n");
577 GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
610 model_view_matrix[14] -= 2.5f + sinf(
current_time / 5.0
f) * 0.5f;
631 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...
#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
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...
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
#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
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_astctextures_NativeLibrary_uninit(JNIEnv *, jobject)
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_astctextures_NativeLibrary_init(JNIEnv *, jobject, jint width, jint height)
#define GL_COMPRESSED_RGBA_ASTC_10x8_KHR
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
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
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_astctextures_NativeLibrary_step(JNIEnv *, jobject)