diff --git a/.gitignore b/.gitignore index 037d403..8294b6c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ install_manifest.txt matabstrix.html matabstrix.js +matabstrix.data diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f6000f..293b46a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/" project (Matabstrix) add_definitions ("-std=c++14 -Wall -Wextra") +set (CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--preload-file data") file (GLOB sources *.cpp) diff --git a/data/shaders/fragment.glsl b/data/shaders/fragment.glsl new file mode 100644 index 0000000..9d40f1b --- /dev/null +++ b/data/shaders/fragment.glsl @@ -0,0 +1,7 @@ +precision lowp float; + +varying vec4 f_color; + +void main(void) { + gl_FragColor = f_color; +} diff --git a/data/shaders/vertex.glsl b/data/shaders/vertex.glsl new file mode 100644 index 0000000..844b9bf --- /dev/null +++ b/data/shaders/vertex.glsl @@ -0,0 +1,11 @@ +attribute vec4 position; +attribute vec3 color; + +varying vec4 f_color; + +uniform mat4 mvp; + +void main(void) { + gl_Position = mvp * position; + f_color = vec4(color, 1.0); +} diff --git a/main.cpp b/main.cpp index a3e775f..cea63e8 100644 --- a/main.cpp +++ b/main.cpp @@ -10,7 +10,7 @@ #include #include -static GLuint load_shader(GLenum type, const char *source); +static GLuint load_shader(GLenum type, const char *filename); static GLuint build_program(); static GLuint create_array_buffer(GLuint index, GLsizeiptr size, const GLvoid *data, GLint count, GLenum type); @@ -31,28 +31,6 @@ static float delta_z = 0, delta_x = 0; static GLuint cube_id; static GLuint pyramid_id; -static const char vertex_shader_source[] = \ -"attribute vec4 position; \n"\ -"attribute vec3 color; \n"\ - -"varying vec4 f_color; \n"\ - -"uniform mat4 mvp; \n"\ - -"void main(void) { \n"\ -" gl_Position = mvp * position; \n"\ -" f_color = vec4(color, 1.0); \n"\ -"} \n"; - -static const char fragment_shader_source[] = \ -"precision lowp float; \n"\ - -"varying vec4 f_color; \n"\ - -"void main(void) { \n"\ -" gl_FragColor = f_color; \n"\ -"} \n"; - static GLfloat vertices[] = { // front -1.0, -1.0, 1.0, @@ -165,18 +143,33 @@ int main() emscripten_set_main_loop(iterate, 0, 1); } -GLuint load_shader(const GLenum type, const char *source) +GLuint load_shader(const GLenum type, const char *filename) { + FILE *file = fopen(filename, "r"); + + fseek(file, 0, SEEK_END); + const long size = ftell(file); + fseek(file, 0, SEEK_SET); + + char *source = (char*)malloc(size + 1); + fread(source, size, 1, file); + source[size] = 0; + + fclose(file); + const GLuint shader = glCreateShader(type); - glShaderSource(shader, 1, &source, nullptr); + glShaderSource(shader, 1, (const GLchar**)&source, nullptr); glCompileShader(shader); + + free(source); + return shader; } GLuint build_program() { - const GLuint vertex_shader = load_shader(GL_VERTEX_SHADER, vertex_shader_source); - const GLuint fragment_shader = load_shader(GL_FRAGMENT_SHADER, fragment_shader_source); + const GLuint vertex_shader = load_shader(GL_VERTEX_SHADER, "/data/shaders/vertex.glsl"); + const GLuint fragment_shader = load_shader(GL_FRAGMENT_SHADER, "/data/shaders/fragment.glsl"); const GLuint program = glCreateProgram(); glAttachShader(program, vertex_shader);