OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
loader.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2015-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 #define STB_IMAGE_IMPLEMENTATION
22 #include "stb_image.h"
23 
24 GLuint load_texture(const char *filename)
25 {
26  int width, height, channels;
27  unsigned char *pixels = stbi_load(filename, &width, &height, &channels, 4);
28 
29  if (!pixels)
30  {
31  LOGE("Failed to load texture %s\n", filename);
32  exit(1);
33  }
34 
35  GLuint result = 0;
36  glGenTextures(1, &result);
37  glBindTexture(GL_TEXTURE_2D, result);
38  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
39  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
40  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
41  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
42  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
43  glBindTexture(GL_TEXTURE_2D, 0);
44  stbi_image_free(pixels);
45  return result;
46 }
47 
48 char *read_file(const char *filename)
49 {
50  FILE *file = fopen(filename, "rb");
51  if (!file)
52  {
53  LOGE("Failed to open file %s\n", filename);
54  exit (1);
55  }
56  fseek(file, 0, SEEK_END);
57  long length = ftell(file);
58  fseek(file, 0, SEEK_SET);
59  char *data = (char *)calloc(length + 1, sizeof(char));
60  if (!data)
61  {
62  LOGE("Failed to allocate memory for file data %s\n", filename);
63  exit(1);
64  }
65  size_t read = fread(data, sizeof(char), length, file);
66  if (read != length)
67  {
68  LOGE("Failed to read whole file %s\n", filename);
69  exit(1);
70  }
71  data[length] = '\0';
72  fclose(file);
73  return data;
74 }
75 
77 {
78  GLuint result = glCreateShader(type);
79  glShaderSource(result, 1, (const GLchar**)&source, NULL);
80  glCompileShader(result);
81  GLint status;
82  glGetShaderiv(result, GL_COMPILE_STATUS, &status);
83  if (status == GL_FALSE) {
84  GLint length;
85  glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length);
86  GLchar *info = new GLchar[length];
87  glGetShaderInfoLog(result, length, NULL, info);
88  LOGE("[COMPILE] %s\n%s\n", source, info);
89  delete[] info;
90  exit(1);
91  }
92  return result;
93 }
94 
96 {
97  GLuint program = glCreateProgram();
98  for (int i = 0; i < count; ++i)
99  glAttachShader(program, shaders[i]);
100 
101  glLinkProgram(program);
102 
103  for (int i = 0; i < count; ++i)
104  glDetachShader(program, shaders[i]);
105 
106  GLint status;
107  glGetProgramiv(program, GL_LINK_STATUS, &status);
108  if (status == GL_FALSE) {
109  GLint length;
110  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
111  GLchar *info = new GLchar[length];
112  glGetProgramInfoLog(program, length, NULL, info);
113  LOGE("[LINK] %s\n", info);
114  delete[] info;
115  exit(1);
116  }
117  return program;
118 }
119 
121 {
122  char *vs_src = read_file(SHADER_PATH("backdrop.vs"));
123  char *fs_src = read_file(SHADER_PATH("backdrop.fs"));
124 
125  GLuint shaders[2];
126  shaders[0] = compile_shader(vs_src, GL_VERTEX_SHADER);
127  shaders[1] = compile_shader(fs_src, GL_FRAGMENT_SHADER);
128  app->program_backdrop = link_program(shaders, 2);
129 
130  LOGD("%s\n", vs_src);
131  LOGD("%s\n", fs_src);
132 
133  free(vs_src);
134  free(fs_src);
135 }
136 
138 {
139  char *vs_src = read_file(SHADER_PATH("geometry.vs"));
140  char *fs_src = read_file(SHADER_PATH("geometry.fs"));
141  char *gs_src = read_file(SHADER_PATH("geometry.gs"));
142 
143  GLuint shaders[] = {
144  compile_shader(vs_src, GL_VERTEX_SHADER),
145  compile_shader(fs_src, GL_FRAGMENT_SHADER),
147  };
148  app->program_geometry = link_program(shaders, 3);
149 
150  free(vs_src);
151  free(fs_src);
152  free(gs_src);
153 }
154 
156 {
157  char *cs_src = read_file(SHADER_PATH("centroid.cs"));
158 
159  GLuint shaders[] = { compile_shader(cs_src, GL_COMPUTE_SHADER) };
160  app->program_centroid = link_program(shaders, 1);
161 
162  free(cs_src);
163 }
164 
166 {
167  char *cs_src = read_file(SHADER_PATH("generate.cs"));
168 
169  GLuint shaders[] = { compile_shader(cs_src, GL_COMPUTE_SHADER) };
170  app->program_generate = link_program(shaders, 1);
171 
172  free(cs_src);
173 }
174 
176 {
181 
182  app->tex_material = load_texture(TEXTURE_PATH("texture11.jpg"));
183 }
void load_assets(App *app)
Definition: loader.cpp:175
GLuint program_geometry
Definition: geometry.h:56
GLuint load_texture(const char *filename)
Definition: loader.cpp:24
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum const void * pixels
Definition: gl2ext.h:572
void load_centroid_shader(App *app)
Definition: loader.cpp:155
#define SHADER_PATH(name)
Definition: main.cpp:40
char * read_file(const char *filename)
Definition: loader.cpp:48
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
void load_generate_shader(App *app)
Definition: loader.cpp:165
GLuint program_backdrop
Definition: geometry.h:65
GLuint program_centroid
Definition: geometry.h:69
#define TEXTURE_PATH(name)
Definition: main.cpp:39
#define LOGD(...)
Definition: AstcTextures.h:28
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
Definition: gl2ext.h:179
GLenum GLenum GLsizei count
Definition: gl2ext.h:133
void load_backdrop_shader(App *app)
Definition: loader.cpp:120
GLsizei GLsizei GLchar * source
Definition: gl2ext.h:877
ClipmapApplication * app
Definition: main.cpp:47
GLuint compile_shader(const char *source, GLenum type)
Definition: loader.cpp:76
STBIDEF stbi_uc * stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
GLuint link_program(GLuint *shaders, int count)
Definition: loader.cpp:95
GLenum type
Definition: gl2ext.h:133
STBIDEF void stbi_image_free(void *retval_from_stbi_load)
GLenum GLuint GLenum GLsizei length
Definition: gl2ext.h:134
GLuint tex_material
Definition: geometry.h:92
Definition: geometry.h:33
#define LOGE(...)
Definition: AstcTextures.h:30
GLint GLsizei width
Definition: gl2ext.h:179
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
GLuint program
Definition: gl2ext.h:1475
GLuint program_generate
Definition: geometry.h:73
void load_geometry_shader(App *app)
Definition: loader.cpp:137
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
#define GL_GEOMETRY_SHADER
Definition: main.cpp:32