VR SDK for Android 0.1.1 ARM Developer Center
loader.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 /* Copyright (c) 2015-2017, ARM Limited and Contributors
5  *
6  * SPDX-License-Identifier: MIT
7  *
8  * Permission is hereby granted, free of charge,
9  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation the rights to
11  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
17  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "loader.h"
25 
26 #define BASE_ASSET_PATH "/data/data/com.arm.developer.vrsdk.simplevr/files/"
27 #define SHADER_PATH(name) BASE_ASSET_PATH name
28 
29 char *read_file(const char *filename)
30 {
31  FILE *file = fopen(filename, "rb");
32  if (!file)
33  {
34  LOGE("Failed to open file %s\n", filename);
35  exit (1);
36  }
37  fseek(file, 0, SEEK_END);
38  long length = ftell(file);
39  fseek(file, 0, SEEK_SET);
40  char *data = (char *)calloc(length + 1, sizeof(char));
41  if (!data)
42  {
43  LOGE("Failed to allocate memory for file data %s\n", filename);
44  exit(1);
45  }
46  size_t read = fread(data, sizeof(char), length, file);
47  if (read != length)
48  {
49  LOGE("Failed to read whole file %s\n", filename);
50  exit(1);
51  }
52  data[length] = '\0';
53  fclose(file);
54  return data;
55 }
56 GLuint compile_shader(const char *source, GLenum type)
57 {
58  GLuint result = glCreateShader(type);
59  glShaderSource(result, 1, (const GLchar**)&source, NULL);
60  glCompileShader(result);
61  GLint status;
62  glGetShaderiv(result, GL_COMPILE_STATUS, &status);
63  if (status == GL_FALSE) {
64  GLint length;
65  glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length);
66  GLchar *info = new GLchar[length];
67  glGetShaderInfoLog(result, length, NULL, info);
68  LOGE("[COMPILE] %s\n", info);
69  delete[] info;
70  exit(1);
71  }
72  return result;
73 }
74 GLuint link_program(GLuint *shaders, int count)
75 {
76  GLuint program = glCreateProgram();
77  for (int i = 0; i < count; ++i)
78  glAttachShader(program, shaders[i]);
79  glLinkProgram(program);
80  for (int i = 0; i < count; ++i)
81  glDetachShader(program, shaders[i]);
82  GLint status;
83  glGetProgramiv(program, GL_LINK_STATUS, &status);
84  if (status == GL_FALSE) {
85  GLint length;
86  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
87  GLchar *info = new GLchar[length];
88  glGetProgramInfoLog(program, length, NULL, info);
89  LOGE("[LINK] %s\n", info);
90  delete[] info;
91  exit(1);
92  }
93  return program;
94 }
96 {
97  char *vs_src = read_file(SHADER_PATH("cube.vs"));
98  char *fs_src = read_file(SHADER_PATH("cube.fs"));
99  GLuint shaders[2];
100  shaders[0] = compile_shader(vs_src, GL_VERTEX_SHADER);
101  shaders[1] = compile_shader(fs_src, GL_FRAGMENT_SHADER);
102  app->program_cube = link_program(shaders, 2);
103  free(vs_src);
104  free(fs_src);
105 }
107 {
108  char *vs_src = read_file(SHADER_PATH("distort.vs"));
109  char *fs_src = read_file(SHADER_PATH("distort.fs"));
110  GLuint shaders[2];
111  shaders[0] = compile_shader(vs_src, GL_VERTEX_SHADER);
112  shaders[1] = compile_shader(fs_src, GL_FRAGMENT_SHADER);
113  app->program_distort = link_program(shaders, 2);
114  free(vs_src);
115  free(fs_src);
116 }
118 {
119  load_distort_shader(app);
120  load_cube_shader(app);
121 }
GLuint compile_shader(const char *source, GLenum type)
Definition: loader.cpp:56
GLuint program_distort
Definition: armvr.h:155
static App app
Definition: main.cpp:30
void load_distort_shader(App *app)
Definition: loader.cpp:106
char * read_file(const char *filename)
Definition: loader.cpp:29
void load_cube_shader(App *app)
Definition: loader.cpp:95
void load_assets(App *app)
Definition: loader.cpp:117
GLuint program_cube
Definition: armvr.h:166
typedef GLuint
Definition: armvr.cpp:53
GLuint link_program(GLuint *shaders, int count)
Definition: loader.cpp:74
typedef GLenum
Definition: armvr.cpp:53
Definition: armvr.h:148
#define SHADER_PATH(name)
Definition: loader.cpp:27
typedef GLint
Definition: armvr.cpp:53
#define LOGE(...)
Definition: armvr.h:88