From 9e6a51a8441cda410b8be8bc83c20db1a0851286 Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Mon, 28 Apr 2014 16:54:20 +0200 Subject: [PATCH] Uses moggle now --- .gitignore | 3 ++ .gitmodules | 3 ++ contrib/CMakeLists.txt | 1 + contrib/moggle | 1 + src/CMakeLists.txt | 2 +- src/main.cpp | 100 +++++++++++++++++++---------------------- 6 files changed, 54 insertions(+), 56 deletions(-) create mode 100644 .gitignore create mode 160000 contrib/moggle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e629a4a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build* +*.user + diff --git a/.gitmodules b/.gitmodules index c17295e..c9b52b1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "contrib/ImageStreams"] path = contrib/ImageStreams url = ../ImageStreams/ +[submodule "contrib/moggle"] + path = contrib/moggle + url = ../moggle/ diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt index 37bff4b..c9d20a4 100644 --- a/contrib/CMakeLists.txt +++ b/contrib/CMakeLists.txt @@ -2,3 +2,4 @@ add_subdirectory("NSAppWrapper") add_subdirectory("CLWrapper") add_subdirectory("ImageStreams") +add_subdirectory("moggle") diff --git a/contrib/moggle b/contrib/moggle new file mode 160000 index 0000000..bb8a9ab --- /dev/null +++ b/contrib/moggle @@ -0,0 +1 @@ +Subproject commit bb8a9ab41750803845dfc540b183d2647e2da8e8 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7039a1c..4a51e85 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,7 +3,7 @@ set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) file(GLOB sources "*.cpp") -set(libs ${JCLWrapper_LIBRARIES} ${JNSAppWrapper_LIBRARIES} ${ImageStreams_LIBRARIES}) +set(libs ${JCLWrapper_LIBRARIES} ${JNSAppWrapper_LIBRARIES} ${ImageStreams_LIBRARIES} moggle_xxx) message(STATUS "Linking against the following libraries: ${libs}") foreach(source ${sources}) diff --git a/src/main.cpp b/src/main.cpp index e6450a4..6cc90ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,11 @@ #include #include +#include +#include +#include +#include + #include #include @@ -16,12 +21,17 @@ using namespace std; #define BUFFER_OFFSET(i) ((char *)NULL + (i)) -const GLfloat quad[] = { +struct Vertex{ + moggle::vector3 position; + moggle::vector3 color; +}; + +const Vertex quad[] = { // x y z, r g b - 1, -1, 0, 1, 0, 0, - -1, -1, 0, 0, 1, 0, - 1, 1, 0, 0, 0, 1, - -1, 1, 0, 1, 1, 1 + {{1, -1, 0}, {1, 0, 0}}, + {{-1, -1, 0}, {0, 1, 0}}, + {{1, 1, 0}, {0, 0, 1}}, + {{-1, 1, 0}, {1, 1, 1}} }; static void check_shader(GLuint s){ @@ -43,70 +53,50 @@ string slurp(string filename) { } struct App { - float time; - GLuint program; - GLint uniform; - GLuint vao; - GLuint posBufferName; - - void initialize(){ - time = 0; - program = glCreateProgram(); + float time = 0; + moggle::shader_program program; + moggle::vao v; + moggle::vbo quad_vbo; - auto v_source_str = slurp("Fractal.vsh"); - auto v_source = v_source_str.c_str(); - GLuint v = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(v, 1, &v_source, NULL); - glCompileShader(v); - check_shader(v); + enum { + POS_ATTR, + COL_ATTR + }; - auto f_source_str = slurp("Fractal.fsh"); - auto f_source = f_source_str.c_str(); - GLuint f = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(f, 1, &f_source, NULL); - glCompileShader(f); - check_shader(f); - - glAttachShader(program, v); - glAttachShader(program, f); - - glBindAttribLocation(program, 0, "position"); - glBindAttribLocation(program, 1, "color"); - - glLinkProgram(program); + void initialize(){ + auto vs = moggle::shader::from_file(moggle::shader_type::vertex, "Fractal.vsh"); + auto fs = moggle::shader::from_file(moggle::shader_type::fragment, "Fractal.fsh"); - uniform = glGetUniformLocation(program, "rotation"); + program.attach(vs); + program.attach(fs); - glDetachShader(program, v); - glDeleteShader(v); - glDetachShader(program, f); - glDeleteShader(f); + program.bind_attribute(POS_ATTR, "position"); + program.bind_attribute(COL_ATTR, "color"); - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + program.link(); - glGenBuffers(1, &posBufferName); - glBindBuffer(GL_ARRAY_BUFFER, posBufferName); - glBufferData(GL_ARRAY_BUFFER, 4*6*4, quad, GL_STATIC_DRAW); + v.bind(); + quad_vbo.bind(GL_ARRAY_BUFFER); + quad_vbo.data(quad); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); + moggle::gl::enable_vertex_attribute_array(POS_ATTR); + moggle::gl::enable_vertex_attribute_array(COL_ATTR); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*4, BUFFER_OFFSET(0)); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*4, BUFFER_OFFSET(3*4)); + v.attribute(POS_ATTR, quad_vbo, &Vertex::position); + v.attribute(COL_ATTR, quad_vbo, &Vertex::color); } void draw(){ time += 1/60.0f; - glClearColor(0.65f, 0.65f, 0.65f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + moggle::gl::clear_color(0.65f, 0.65f, 0.65f, 1.0f); + moggle::gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glUseProgram(program); - glUniform1f(uniform, time); - glBindVertexArray(vao); + program.use(); + program.uniform("rotation").set(time); + v.bind(); - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + moggle::gl::draw_arrays(GL_TRIANGLE_STRIP, 0, 4); } }; @@ -195,7 +185,7 @@ int main() { app.create_window({ [&](ContextParameters){ a.initialize(); - b.initialize(); + // b.initialize(); }, [&](ContextParameters){ a.draw();