diff --git a/README.md b/README.md index 37b7bcdc7..1f6cdbe37 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,14 @@ auto shader = glCreateShader(GL_COMPUTE_SHADER); // ... ``` +*glbinding* is compatible with OpenGL-compatible windowing toolkits and we provide example integrations for the following ones within the examples: + +* [GLFW](https://github.com/cginternals/glbinding/tree/master/source/examples/cubescape-glfw) +* [GTK 3](https://github.com/cginternals/glbinding/tree/master/source/examples/cubescape-gtk3) +* [GTK 4](https://github.com/cginternals/glbinding/tree/master/source/examples/cubescape-gtk4) +* [Qt 5](https://github.com/cginternals/glbinding/tree/master/source/examples/cubescape-qt) +* [SDL](https://github.com/cginternals/glbinding/tree/master/source/examples/cubescape-sdl) + # Resources * [Tools](https://github.com/cginternals/glbinding/wiki/tools) diff --git a/source/examples/CMakeLists.txt b/source/examples/CMakeLists.txt index fd026e098..afcc5c734 100644 --- a/source/examples/CMakeLists.txt +++ b/source/examples/CMakeLists.txt @@ -3,14 +3,28 @@ # Examples # +# General Features add_subdirectory("callbacks") add_subdirectory("comparison") add_subdirectory("multi-context") -add_subdirectory("cubescape") + +# +# Cubescape +# + +# Shared Static Libraries +add_subdirectory("cubescape-shared-gl") +add_subdirectory("cubescape-shared-gles") + +# OpenGL APIs +add_subdirectory("cubescape-gl") add_subdirectory("cubescape-gles") -add_subdirectory("cubescape-log") + +# Integrations +add_subdirectory("cubescape-glfw") add_subdirectory("cubescape-wgl") add_subdirectory("cubescape-qt") add_subdirectory("cubescape-sdl") add_subdirectory("cubescape-gtk3") add_subdirectory("cubescape-gtk4") +add_subdirectory("cubescape-log") diff --git a/source/examples/cubescape/CMakeLists.txt b/source/examples/cubescape-gl/CMakeLists.txt similarity index 75% rename from source/examples/cubescape/CMakeLists.txt rename to source/examples/cubescape-gl/CMakeLists.txt index 332f7e474..637e8f1e1 100644 --- a/source/examples/cubescape/CMakeLists.txt +++ b/source/examples/cubescape-gl/CMakeLists.txt @@ -4,7 +4,6 @@ # find_package(glfw3 QUIET) -find_package(cpplocate QUIET) # @@ -12,7 +11,7 @@ find_package(cpplocate QUIET) # # Target name -set(target cubescape) +set(target cubescape-gl) # Exit here if required dependencies are not met if (NOT glfw3_FOUND) @@ -20,12 +19,6 @@ if (NOT glfw3_FOUND) return() endif() -if (NOT cpplocate_FOUND) - message(STATUS "Example ${target}: using static data path (cpplocate not found)") -else() - message(STATUS "Example ${target}") -endif() - # # Sources @@ -33,12 +26,6 @@ endif() set(sources main.cpp - CubeScape.cpp - CubeScape.h - glutils.cpp - glutils.h - RawFile.cpp - RawFile.h ) @@ -90,7 +77,7 @@ target_link_libraries(${target} glfw ${META_PROJECT_NAME}::glbinding ${META_PROJECT_NAME}::glbinding-aux - $<$:cpplocate::cpplocate> + ${META_PROJECT_NAME}::cubescape-shared-gl ) @@ -102,7 +89,6 @@ target_compile_definitions(${target} PRIVATE ${DEFAULT_COMPILE_DEFINITIONS} GLFW_INCLUDE_NONE - $<$:cpplocate_FOUND> ) diff --git a/source/examples/cubescape/main.cpp b/source/examples/cubescape-gl/main.cpp similarity index 99% rename from source/examples/cubescape/main.cpp rename to source/examples/cubescape-gl/main.cpp index bed0d37c8..1ae777b99 100644 --- a/source/examples/cubescape/main.cpp +++ b/source/examples/cubescape-gl/main.cpp @@ -17,7 +17,7 @@ #include #include -#include "CubeScape.h" +#include using namespace gl; diff --git a/source/examples/cubescape-gles/CMakeLists.txt b/source/examples/cubescape-gles/CMakeLists.txt index 85cacdfce..999e430eb 100644 --- a/source/examples/cubescape-gles/CMakeLists.txt +++ b/source/examples/cubescape-gles/CMakeLists.txt @@ -4,7 +4,6 @@ # find_package(glfw3 QUIET) -find_package(cpplocate QUIET) # @@ -20,12 +19,6 @@ if (NOT glfw3_FOUND) return() endif() -if (NOT cpplocate_FOUND) - message(STATUS "Example ${target}: using static data path (cpplocate not found)") -else() - message(STATUS "Example ${target}") -endif() - # # Sources @@ -33,12 +26,6 @@ endif() set(sources main.cpp - CubeScape.cpp - CubeScape.h - glutils.cpp - glutils.h - RawFile.cpp - RawFile.h ) @@ -89,7 +76,7 @@ target_link_libraries(${target} glfw ${META_PROJECT_NAME}::glbinding ${META_PROJECT_NAME}::glbinding-aux - $<$:cpplocate::cpplocate> + ${META_PROJECT_NAME}::cubescape-shared-gles ) @@ -101,7 +88,6 @@ target_compile_definitions(${target} PRIVATE ${DEFAULT_COMPILE_DEFINITIONS} GLFW_INCLUDE_NONE - $<$:cpplocate_FOUND> ) diff --git a/source/examples/cubescape-gles/main.cpp b/source/examples/cubescape-gles/main.cpp index a853ef437..bd5d98079 100644 --- a/source/examples/cubescape-gles/main.cpp +++ b/source/examples/cubescape-gles/main.cpp @@ -15,7 +15,7 @@ #include #include -#include "CubeScape.h" +#include using namespace gl; diff --git a/source/examples/cubescape-glfw/CMakeLists.txt b/source/examples/cubescape-glfw/CMakeLists.txt new file mode 100644 index 000000000..5809f84ed --- /dev/null +++ b/source/examples/cubescape-glfw/CMakeLists.txt @@ -0,0 +1,135 @@ + +# +# External dependencies +# + +find_package(glfw3 QUIET) + + +# +# Executable name and options +# + +# Target name +set(target cubescape-glfw) + +# Exit here if required dependencies are not met +if (NOT glfw3_FOUND) + message("Example ${target} skipped: glfw3 not found") + return() +endif() + + +# +# Sources +# + +set(sources + main.cpp +) + + +# +# Create executable +# + +# Build executable +add_executable(${target} + MACOSX_BUNDLE + ${sources} +) + +# Create namespaced alias +add_executable(${META_PROJECT_NAME}::${target} ALIAS ${target}) + + +# +# Project options +# + +set_target_properties(${target} + PROPERTIES + ${DEFAULT_PROJECT_OPTIONS} + INSTALL_RPATH "${EXECUTABLE_INSTALL_RPATH}" + FOLDER "${IDE_FOLDER}" +) + + +# +# Include directories +# + +target_include_directories(${target} + PRIVATE + ${DEFAULT_INCLUDE_DIRECTORIES} + ${PROJECT_BINARY_DIR}/source/include + SYSTEM +) + + +# +# Libraries +# + +target_link_libraries(${target} + PRIVATE + ${DEFAULT_LIBRARIES} + glfw + ${META_PROJECT_NAME}::glbinding + ${META_PROJECT_NAME}::glbinding-aux + ${META_PROJECT_NAME}::cubescape-shared-gl +) + + +# +# Compile definitions +# + +target_compile_definitions(${target} + PRIVATE + ${DEFAULT_COMPILE_DEFINITIONS} + GLFW_INCLUDE_NONE +) + + +# +# Compile options +# + +target_compile_options(${target} + PRIVATE + ${DEFAULT_COMPILE_OPTIONS_PRIVATE} + PUBLIC + ${DEFAULT_COMPILE_OPTIONS_PUBLIC} +) + + +# +# Linker options +# + +target_link_libraries(${target} + PRIVATE + ${DEFAULT_LINKER_OPTIONS} +) + + +# +# Target Health +# + +perform_health_checks( + ${target} + ${sources} +) + + +# +# Deployment +# + +# Executable +install(TARGETS ${target} + RUNTIME DESTINATION ${INSTALL_EXAMPLES} COMPONENT examples_glfw + BUNDLE DESTINATION ${INSTALL_EXAMPLES} COMPONENT examples_glfw +) diff --git a/source/examples/cubescape-glfw/main.cpp b/source/examples/cubescape-glfw/main.cpp new file mode 100644 index 000000000..1ae777b99 --- /dev/null +++ b/source/examples/cubescape-glfw/main.cpp @@ -0,0 +1,130 @@ + +#include + +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include + + +using namespace gl; +using namespace glbinding; + + +namespace +{ + CubeScape * cubescape(nullptr); +} + + +void error(int errnum, const char * errmsg) +{ + std::cerr << errnum << ": " << errmsg << std::endl; +} + + +void framebuffer_size_callback(GLFWwindow * /*window*/, int width, int height) +{ + cubescape->resize(width, height); +} + +void key_callback(GLFWwindow * window, int key, int /*scancode*/, int action, int /*mods*/) +{ + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) + glfwSetWindowShouldClose(window, 1); + + bool numCubesChanged = false; + + if (key == GLFW_KEY_I && (action == GLFW_PRESS || action == GLFW_REPEAT)) + { + cubescape->setNumCubes(cubescape->numCubes() + 1); + numCubesChanged = true; + } + + if (key == GLFW_KEY_D && (action == GLFW_PRESS || action == GLFW_REPEAT)) + { + cubescape->setNumCubes(cubescape->numCubes() - 1); + numCubesChanged = true; + } + + if (numCubesChanged) + { + const int n = cubescape->numCubes(); + std::cout << "#cubes = " << n << " * " << n << " = " << n * n << std::endl; + } +} + + +int main(int, char *[]) +{ + glfwSetErrorCallback(error); + + if (!glfwInit()) + return 1; + + glfwDefaultWindowHints(); + +#ifdef SYSTEM_DARWIN + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); +#endif + + GLFWwindow * window = glfwCreateWindow(640, 480, "", nullptr, nullptr); + if (!window) + { + glfwTerminate(); + return -1; + } + + glfwSetKeyCallback(window, key_callback); + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + + glfwMakeContextCurrent(window); + + glbinding::initialize(glfwGetProcAddress, false); // only resolve functions that are actually used (lazy) + glbinding::aux::enableGetErrorCallback(); + + // print some gl infos (query) + + std::cout << std::endl + << "OpenGL Version: " << aux::ContextInfo::version() << std::endl + << "OpenGL Vendor: " << aux::ContextInfo::vendor() << std::endl + << "OpenGL Renderer: " << aux::ContextInfo::renderer() << std::endl; + + std::cout << std::endl + << "Press i or d to either increase or decrease number of cubes." << std::endl << std::endl; + + + cubescape = new CubeScape(); + + int width, height; glfwGetFramebufferSize(window, &width, &height); + cubescape->resize(width, height); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + cubescape->draw(); + glfwSwapBuffers(window); + } + + delete cubescape; + cubescape = nullptr; + + glfwTerminate(); + return 0; +} diff --git a/source/examples/cubescape-gtk3/CMakeLists.txt b/source/examples/cubescape-gtk3/CMakeLists.txt index 8b00ea96a..dc4d3a46a 100644 --- a/source/examples/cubescape-gtk3/CMakeLists.txt +++ b/source/examples/cubescape-gtk3/CMakeLists.txt @@ -4,7 +4,6 @@ # find_package(GTK3 QUIET) -find_package(cpplocate QUIET) # @@ -20,12 +19,6 @@ if (NOT TARGET GTK3::GTK3) return() endif() -if (NOT cpplocate_FOUND) - message(STATUS "Example ${target}: using static data path (cpplocate not found)") -else() - message(STATUS "Example ${target}") -endif() - # # Sources @@ -33,12 +26,6 @@ endif() set(sources main.cpp - CubeScape.cpp - CubeScape.h - glutils.cpp - glutils.h - RawFile.cpp - RawFile.h ) @@ -90,7 +77,7 @@ target_link_libraries(${target} GTK3::GTK3 ${META_PROJECT_NAME}::glbinding ${META_PROJECT_NAME}::glbinding-aux - $<$:cpplocate::cpplocate> + ${META_PROJECT_NAME}::cubescape-shared-gl ) @@ -101,7 +88,6 @@ target_link_libraries(${target} target_compile_definitions(${target} PRIVATE ${DEFAULT_COMPILE_DEFINITIONS} - $<$:cpplocate_FOUND> ) diff --git a/source/examples/cubescape-gtk3/CubeScape.cpp b/source/examples/cubescape-gtk3/CubeScape.cpp deleted file mode 100644 index 185be1686..000000000 --- a/source/examples/cubescape-gtk3/CubeScape.cpp +++ /dev/null @@ -1,265 +0,0 @@ - -#include "CubeScape.h" - -#include -#include -#include -#include - -#ifdef cpplocate_FOUND -#include -#endif - -#include - -#include "glutils.h" -#include "RawFile.h" - -using namespace gl; - -namespace -{ - - -bool readFile(const std::string & filePath, std::string & content) -{ - // http://insanecoding.blogspot.de/2011/11/how-to-read-in-file-in-c.html - - std::ifstream in(filePath, std::ios::in | std::ios::binary); - - if (!in) - return false; - - content = std::string(std::istreambuf_iterator(in), std::istreambuf_iterator()); - return true; -} - -std::string determineDataPath() -{ -#ifdef cpplocate_FOUND - std::string path = cpplocate::locatePath("data/cubescape", "share/glbinding/cubescape", reinterpret_cast(&gl::glCreateShader)); - if (path.empty()) path = "./data"; - else path = path + "/data"; -#else - const std::string path = "./data"; -#endif - - return path; -} - - -} // namespace - - -CubeScape::CubeScape() -: m_initialized(false) -, a_vertex(-1) -, u_transform(-1) -, u_time(-1) -, u_numcubes(-1) -, m_vao(0) -, m_indices(0) -, m_vertices(0) -, m_program(0) -, m_a(0.f) -, m_numcubes(16) -{ - // Get data path - std::string dataPath = determineDataPath(); - - GLuint vs = glCreateShader(GL_VERTEX_SHADER); - GLuint gs = glCreateShader(GL_GEOMETRY_SHADER); - GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); - - std::string vertexSource; - std::string geometrySource; - std::string fragmentSource; - - auto success = readFile(dataPath + "/cubescape/cubescape.vert", vertexSource); - success &= readFile(dataPath + "/cubescape/cubescape.geom", geometrySource); - success &= readFile(dataPath + "/cubescape/cubescape.frag", fragmentSource); - - if (!success) - { - std::cerr << "Could not load shaders in " << dataPath + "/cubescape/cubescape.*" << "." << std::endl; - return; - } - - const char * vertSource = vertexSource.c_str(); - const char * geomSource = geometrySource.c_str(); - const char * fragSource = fragmentSource.c_str(); - - glShaderSource(vs, 1, &vertSource, nullptr); - glCompileShader(vs); - compile_info(vs); - - glShaderSource(gs, 1, &geomSource, nullptr); - glCompileShader(gs); - compile_info(gs); - - glShaderSource(fs, 1, &fragSource, nullptr); - glCompileShader(fs); - compile_info(fs); - - m_program = glCreateProgram(); - - glAttachShader(m_program, vs); - glAttachShader(m_program, gs); - glAttachShader(m_program, fs); - - glLinkProgram(m_program); - link_info(m_program); - - // create textures - - glGenTextures(2, m_textures); - - glBindTexture(GL_TEXTURE_2D, m_textures[0]); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - { - RawFile terrain(dataPath + "/cubescape/terrain.64.64.r.ub.raw"); - if (!terrain.isValid()) - std::cout << "warning: loading texture from " << terrain.filePath() << " failed."; - - glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 64, 64, 0, GL_RED, GL_UNSIGNED_BYTE, terrain.data()); - } - - glBindTexture(GL_TEXTURE_2D, m_textures[1]); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - { - RawFile patches(dataPath + "/cubescape/patches.64.16.rgb.ub.raw"); - if (!patches.isValid()) - std::cout << "warning: loading texture from " << patches.filePath() << " failed."; - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 64, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, patches.data()); - } - - - // create cube - - static const GLfloat vertices_data[24] = - { - -1.f, -1.f, -1.f, // 0 - -1.f, -1.f, 1.f, // 1 - -1.f, 1.f, -1.f, // 2 - -1.f, 1.f, 1.f, // 3 - 1.f, -1.f, -1.f, // 4 - 1.f, -1.f, 1.f, // 5 - 1.f, 1.f, -1.f, // 6 - 1.f, 1.f, 1.f // 7 - }; - - static const GLubyte indices_data[18] = { - 2, 3, 6, 0, 1, 2, 1, 5, 3, 5, 4, 7, 4, 0, 6, 5, 1, 4 }; - - glGenVertexArrays(1, &m_vao); - glBindVertexArray(m_vao); - - glGenBuffers(1, &m_vertices); - glBindBuffer(GL_ARRAY_BUFFER, m_vertices); - glBufferData(GL_ARRAY_BUFFER, (8 * 3) * sizeof(float), vertices_data, GL_STATIC_DRAW); - - glGenBuffers(1, &m_indices); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indices); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, (6 * 3) * sizeof(GLubyte), indices_data, GL_STATIC_DRAW); - - // setup uniforms - - a_vertex = glGetAttribLocation(m_program, "a_vertex"); - glEnableVertexAttribArray(static_cast(a_vertex)); - - glVertexAttribPointer(static_cast(a_vertex), 3, GL_FLOAT, GL_FALSE, 0, nullptr); - - u_transform = glGetUniformLocation(m_program, "modelViewProjection"); - u_time = glGetUniformLocation(m_program, "time"); - u_numcubes = glGetUniformLocation(m_program, "numcubes"); - - m_time = clock::now(); - - GLint terrain = glGetUniformLocation(m_program, "terrain"); - GLint patches = glGetUniformLocation(m_program, "patches"); - - // since only single program and single data is used, bind only once - - glEnable(GL_DEPTH_TEST); - - glClearColor(0.f, 0.f, 0.f, 1.0f); - - glUseProgram(m_program); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_textures[0]); - glUniform1i(terrain, 0); - - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_textures[1]); - glUniform1i(patches, 1); - - // view - - m_view = mat4::lookAt(0.f, 0.8f,-2.0f, 0.f, -1.2f, 0.f, 0.f, 1.f, 0.f); - - m_initialized = true; -} - -CubeScape::~CubeScape() -{ - if (!m_initialized) - { - return; - } - - glDeleteBuffers(1, &m_vertices); - glDeleteBuffers(1, &m_indices); - - glDeleteProgram(m_program); -} - -void CubeScape::setNumCubes(int _numCubes) -{ - m_numcubes = std::min(4096, std::max(1, _numCubes)); -} -int CubeScape::numCubes() const -{ - return m_numcubes; -} - -void CubeScape::resize(int width, int height) -{ - m_projection = mat4::perspective(40.f, static_cast(width) / static_cast(height), 1.f, 4.f); - - glViewport(0, 0, width, height); -} - -void CubeScape::draw() -{ - if (!m_initialized) - { - return; - } - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - auto ms = std::chrono::duration_cast(clock::now() - m_time); - float t = static_cast(ms.count()) * 1e-3f; - - const mat4 transform = m_projection * m_view * mat4::rotate(t * 0.1f, 0.f, 1.f, 0.f); - - glUniformMatrix4fv(u_transform, 1, GL_FALSE, &transform[0]); - glUniform1f(u_time, t); - glUniform1i(u_numcubes, m_numcubes); - - glDrawElementsInstanced(GL_TRIANGLES, 18, GL_UNSIGNED_BYTE, 0, m_numcubes * m_numcubes); -} diff --git a/source/examples/cubescape-gtk3/CubeScape.h b/source/examples/cubescape-gtk3/CubeScape.h deleted file mode 100644 index 52aacb467..000000000 --- a/source/examples/cubescape-gtk3/CubeScape.h +++ /dev/null @@ -1,47 +0,0 @@ - -#pragma once - -#include - -#include - -#include "glutils.h" - - -class CubeScape -{ -public: - CubeScape(); - ~CubeScape(); - - void resize(int width, int height); - void draw(); - - void setNumCubes(int numCubes); - int numCubes() const; - -protected: - bool m_initialized; - - gl::GLint a_vertex; - gl::GLint u_transform; - gl::GLint u_time; - gl::GLint u_numcubes; - - gl::GLuint m_vao; - gl::GLuint m_indices; - gl::GLuint m_vertices; - - gl::GLuint m_program; - - gl::GLuint m_textures[2]; - - float m_a; - int m_numcubes; - - mat4 m_view; - mat4 m_projection; - - using clock = std::chrono::system_clock; - clock::time_point m_time; -}; diff --git a/source/examples/cubescape-gtk3/main.cpp b/source/examples/cubescape-gtk3/main.cpp index 9cb5f9f3f..64669241f 100644 --- a/source/examples/cubescape-gtk3/main.cpp +++ b/source/examples/cubescape-gtk3/main.cpp @@ -18,7 +18,7 @@ #include #include -#include "CubeScape.h" +#include using namespace gl; diff --git a/source/examples/cubescape-gtk4/CMakeLists.txt b/source/examples/cubescape-gtk4/CMakeLists.txt index 951fc71a0..826d8d917 100644 --- a/source/examples/cubescape-gtk4/CMakeLists.txt +++ b/source/examples/cubescape-gtk4/CMakeLists.txt @@ -4,7 +4,6 @@ # find_package(GTK4 QUIET) -find_package(cpplocate QUIET) # @@ -20,12 +19,6 @@ if (NOT TARGET GTK4::GTK4) return() endif() -if (NOT cpplocate_FOUND) - message(STATUS "Example ${target}: using static data path (cpplocate not found)") -else() - message(STATUS "Example ${target}") -endif() - # # Sources @@ -90,7 +83,7 @@ target_link_libraries(${target} GTK4::GTK4 ${META_PROJECT_NAME}::glbinding ${META_PROJECT_NAME}::glbinding-aux - $<$:cpplocate::cpplocate> + ${META_PROJECT_NAME}::cubescape-shared-gl ) @@ -101,7 +94,6 @@ target_link_libraries(${target} target_compile_definitions(${target} PRIVATE ${DEFAULT_COMPILE_DEFINITIONS} - $<$:cpplocate_FOUND> ) diff --git a/source/examples/cubescape-gtk4/RawFile.cpp b/source/examples/cubescape-gtk4/RawFile.cpp deleted file mode 100644 index e4de8cb82..000000000 --- a/source/examples/cubescape-gtk4/RawFile.cpp +++ /dev/null @@ -1,59 +0,0 @@ - -#include "RawFile.h" - -#include -#include - -RawFile::RawFile(const std::string & _filePath) -: m_filePath(_filePath) -, m_valid(false) -{ - m_valid = readFile(); -} - -RawFile::~RawFile() -{ -} - -bool RawFile::isValid() const -{ - return m_valid; -} - -const char * RawFile::data() const -{ - return m_data.data(); -} - -size_t RawFile::size() const -{ - return m_data.size(); -} - -bool RawFile::readFile() -{ - std::ifstream ifs(m_filePath, std::ios::in | std::ios::binary); - - if (!ifs) - { - std::cerr << "Reading from file \"" << m_filePath << "\" failed." << std::endl; - return false; - } - - readRawData(ifs); - - ifs.close(); - - return true; -} - -void RawFile::readRawData(std::ifstream & ifs) -{ - ifs.seekg(0, std::ios::end); - - const size_t _size = static_cast(ifs.tellg()); - m_data.resize(_size); - - ifs.seekg(0, std::ios::beg); - ifs.read(m_data.data(), static_cast(_size)); -} diff --git a/source/examples/cubescape-gtk4/RawFile.h b/source/examples/cubescape-gtk4/RawFile.h deleted file mode 100644 index 8d4bb396e..000000000 --- a/source/examples/cubescape-gtk4/RawFile.h +++ /dev/null @@ -1,32 +0,0 @@ - -#pragma once - -#include -#include -#include -#include - -class RawFile -{ -public: - RawFile(const std::string & filePath); - virtual ~RawFile(); - - RawFile & operator=(const RawFile &) = delete; - - const char * data() const; - size_t size() const; - - bool isValid() const; - inline const std::string & filePath() const { return m_filePath; } - -protected: - bool readFile(); - void readRawData(std::ifstream & ifs); - -protected: - const std::string m_filePath; - std::vector m_data; - - bool m_valid; -}; diff --git a/source/examples/cubescape-gtk4/glutils.cpp b/source/examples/cubescape-gtk4/glutils.cpp deleted file mode 100644 index 99aed1878..000000000 --- a/source/examples/cubescape-gtk4/glutils.cpp +++ /dev/null @@ -1,273 +0,0 @@ - -#include "glutils.h" - -#include -#include - - -#include - - -using namespace gl32core; - - -void compile_info(const GLuint shader) -{ - GLint status(0); - glGetShaderiv(shader, GL_COMPILE_STATUS, &status); - - if (1 != status) - { - GLint maxLength(0); - GLint logLength(0); - - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); - - GLchar * log = new GLchar[maxLength]; - glGetShaderInfoLog(shader, maxLength, &logLength, log); - - std::cout << "Compiling shader failed." << std::endl - << log << std::endl; - } -} - -void link_info(const GLuint program) -{ - GLint status(0); - glGetProgramiv(program, GL_LINK_STATUS, &status); - - if (1 != status) - { - GLint maxLength(0); - GLint logLength(0); - - glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength); - - GLchar * log = new GLchar[maxLength]; - glGetProgramInfoLog(program, maxLength, &logLength, log); - - std::cout << "Linking program failed." << std::endl - << log << std::endl; - } -} - - -mat4::mat4() -{ - for (int i = 0; i < 16; ++i) - m[i] = 0.f; - - m[0] = m[5] = m[10] = m[15] = 1.f; -} - - - mat4 operator*(const mat4 & a, const mat4 & b) -{ - mat4 m; - - m[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12] * b[ 3]; - m[ 4] = a[ 0] * b[ 4] + a[ 4] * b[ 5] + a[ 8] * b[ 6] + a[12] * b[ 7]; - m[ 8] = a[ 0] * b[ 8] + a[ 4] * b[ 9] + a[ 8] * b[10] + a[12] * b[11]; - m[12] = a[ 0] * b[12] + a[ 4] * b[13] + a[ 8] * b[14] + a[12] * b[15]; - - m[ 1] = a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2] + a[13] * b[ 3]; - m[ 5] = a[ 1] * b[ 4] + a[ 5] * b[ 5] + a[ 9] * b[ 6] + a[13] * b[ 7]; - m[ 9] = a[ 1] * b[ 8] + a[ 5] * b[ 9] + a[ 9] * b[10] + a[13] * b[11]; - m[13] = a[ 1] * b[12] + a[ 5] * b[13] + a[ 9] * b[14] + a[13] * b[15]; - - m[ 2] = a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2] + a[14] * b[ 3]; - m[ 6] = a[ 2] * b[ 4] + a[ 6] * b[ 5] + a[10] * b[ 6] + a[14] * b[ 7]; - m[10] = a[ 2] * b[ 8] + a[ 6] * b[ 9] + a[10] * b[10] + a[14] * b[11]; - m[14] = a[ 2] * b[12] + a[ 6] * b[13] + a[10] * b[14] + a[14] * b[15]; - - m[ 3] = a[ 3] * b[ 0] + a[ 7] * b[ 1] + a[11] * b[ 2] + a[15] * b[ 3]; - m[ 7] = a[ 3] * b[ 4] + a[ 7] * b[ 5] + a[11] * b[ 6] + a[15] * b[ 7]; - m[11] = a[ 3] * b[ 8] + a[ 7] * b[ 9] + a[11] * b[10] + a[15] * b[11]; - m[15] = a[ 3] * b[12] + a[ 7] * b[13] + a[11] * b[14] + a[15] * b[15]; - - return m; -} - -mat4 mat4::perspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar) -{ - mat4 m; - - //const float zRange = zNear - zFar; - const float f = 1.f / static_cast(tan(fovy * 0.5f * 0.01745329251994329576923690768489f)); - - m[ 0] = f / aspect; - m[ 5] = f; - - m[10] = -(zFar + zNear) / (zFar - zNear); - m[14] = -(2.f * zFar * zNear) / (zFar - zNear); - - m[11] = -1.f; - m[15] = 0.f; - - return m; -} - -mat4 mat4::lookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez - , GLfloat centerx, GLfloat centery, GLfloat centerz, GLfloat upx, GLfloat upy, GLfloat upz) -{ - vec3 e(eyex, eyey, eyez); - - vec3 f(centerx - eyex, centery - eyey, centerz - eyez); - f.normalize(); - - vec3 u(upx, upy, upz); - u.normalize(); - - vec3 s = crossp(f, u); - u = crossp(s, f); - - mat4 m; - - m[ 0] = s[0]; - m[ 4] = s[1]; - m[ 8] = s[2]; - - m[ 1] = u[0]; - m[ 5] = u[1]; - m[ 9] = u[2]; - - m[ 2] = -f[0]; - m[ 6] = -f[1]; - m[10] = -f[2]; - - m[12] = -dotp(s, e); - m[13] = -dotp(u, e); - m[14] = dotp(f, e); - - return m; -} - -mat4 mat4::translate(GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - m[12] = x; - m[13] = y; - m[14] = z; - - return m; -} - -mat4 mat4::scale(GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - m[ 0] = x; - m[ 5] = y; - m[10] = z; - - return m; -} - -mat4 mat4::rotate(GLfloat a, GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - GLfloat l = 1.f / static_cast(sqrt(x * x + y * y + z * z)); - - x *= l; - y *= l; - z *= l; - - const GLfloat c = static_cast(cos(a)); - const GLfloat s = static_cast(sin(a)); - - const GLfloat d = 1.f - c; - - m[ 0] = x * x * d + c; - m[ 4] = x * y * d - z * s; - m[ 8] = x * z * d + y * s; - - m[ 1] = y * x * d + z * s; - m[ 5] = y * y * d + c; - m[ 9] = y * z * d - x * s; - - m[ 2] = z * x * d - y * s; - m[ 6] = z * y * d + x * s; - m[10] = z * z * d + c; - - return m; -} - -vec3::vec3() -{ - v[0] = v[1] = v[2] = 0.f; -} - -vec3::vec3(GLfloat x, GLfloat y, GLfloat z) -{ - v[0] = x; - v[1] = y; - v[2] = z; -} - -vec3 & vec3::operator+(const vec3 & rhs) -{ - v[0] += rhs[0]; - v[1] += rhs[1]; - v[2] += rhs[2]; - - return *this; -} - -vec3 & vec3::operator-(const vec3 & rhs) -{ - v[0] -= rhs[0]; - v[1] -= rhs[1]; - v[2] -= rhs[2]; - - return *this; -} - -vec3 & vec3::operator*(const vec3 & rhs) -{ - v[0] *= rhs[0]; - v[1] *= rhs[1]; - v[2] *= rhs[2]; - - return *this; -} - -vec3 & vec3::operator/(const vec3 & rhs) -{ - v[0] /= rhs[0]; - v[1] /= rhs[1]; - v[2] /= rhs[2]; - - return *this; -} - -GLfloat vec3::length() -{ - return static_cast(sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])); -} - -void vec3::normalize() -{ - const GLfloat s = 1.f / length(); - - v[0] *= s; - v[1] *= s; - v[2] *= s; -} - -vec3 crossp(const vec3 & a, const vec3 & b) -{ - vec3 v; - - v[0] = a[1] * b[2] - a[2] * b[1]; - v[1] = a[2] * b[0] - a[0] * b[2]; - v[2] = a[0] * b[1] - a[1] * b[0]; - - return v; -} - -GLfloat dotp(const vec3 & a, const vec3 & b) -{ - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; -} diff --git a/source/examples/cubescape-gtk4/glutils.h b/source/examples/cubescape-gtk4/glutils.h deleted file mode 100644 index 147971d95..000000000 --- a/source/examples/cubescape-gtk4/glutils.h +++ /dev/null @@ -1,50 +0,0 @@ - -#pragma once - -#include - -void compile_info(const gl32core::GLuint shader); -void link_info(const gl32core::GLuint program); - -struct mat4 -{ - mat4(); - - inline gl::GLfloat & operator[](const int i) { return m[i]; } - inline const gl::GLfloat & operator[](const int i) const { return m[i]; } - - static mat4 lookAt(gl::GLfloat eyex, gl::GLfloat eyey, gl::GLfloat eyez - , gl::GLfloat centerx, gl::GLfloat centery, gl::GLfloat centerz, gl::GLfloat upx, gl::GLfloat upy, gl::GLfloat upz); - - static mat4 perspective(gl::GLfloat fovy, gl::GLfloat aspect, gl::GLfloat zNear, gl::GLfloat zFar); - - static mat4 translate(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - static mat4 scale(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - static mat4 rotate(gl::GLfloat angle, gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - - gl::GLfloat m[16]; -}; - -mat4 operator*(const mat4 & a, const mat4 & b); - -struct vec3 -{ - vec3(); - vec3(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - - inline gl::GLfloat & operator[](const int i) { return v[i]; } - inline const gl::GLfloat & operator[](const int i) const { return v[i]; } - - vec3 & operator+(const vec3 & rhs); - vec3 & operator-(const vec3 & rhs); - vec3 & operator*(const vec3 & rhs); - vec3 & operator/(const vec3 & rhs); - - gl::GLfloat length(); - void normalize(); - - gl::GLfloat v[3]; -}; - -vec3 crossp(const vec3 & a, const vec3 & b); -gl::GLfloat dotp(const vec3 & a, const vec3 & b); diff --git a/source/examples/cubescape-gtk4/main.cpp b/source/examples/cubescape-gtk4/main.cpp index fdd3de420..766422d05 100644 --- a/source/examples/cubescape-gtk4/main.cpp +++ b/source/examples/cubescape-gtk4/main.cpp @@ -17,7 +17,7 @@ #include #include -#include "CubeScape.h" +#include using namespace gl; diff --git a/source/examples/cubescape-log/CMakeLists.txt b/source/examples/cubescape-log/CMakeLists.txt index 8f1f20e1d..2b9dd9cdb 100644 --- a/source/examples/cubescape-log/CMakeLists.txt +++ b/source/examples/cubescape-log/CMakeLists.txt @@ -4,7 +4,6 @@ # find_package(glfw3 QUIET) -find_package(cpplocate QUIET) # @@ -20,28 +19,13 @@ if (NOT glfw3_FOUND) return() endif() -if (NOT cpplocate_FOUND) - message(STATUS "Example ${target}: using static data path (cpplocate not found)") -else() - message(STATUS "Example ${target}") -endif() - # # Sources # -set(cubscape_path ../cubescape) - set(sources main.cpp - - ${cubscape_path}/CubeScape.cpp - ${cubscape_path}/CubeScape.h - ${cubscape_path}/glutils.cpp - ${cubscape_path}/glutils.h - ${cubscape_path}/RawFile.cpp - ${cubscape_path}/RawFile.h ) @@ -92,7 +76,7 @@ target_link_libraries(${target} glfw ${META_PROJECT_NAME}::glbinding ${META_PROJECT_NAME}::glbinding-aux - $<$:cpplocate::cpplocate> + ${META_PROJECT_NAME}::cubescape-shared-gl ) @@ -104,7 +88,6 @@ target_compile_definitions(${target} PRIVATE ${DEFAULT_COMPILE_DEFINITIONS} GLFW_INCLUDE_NONE - $<$:cpplocate_FOUND> ) diff --git a/source/examples/cubescape-log/main.cpp b/source/examples/cubescape-log/main.cpp index 4dedac3c1..8c5117397 100644 --- a/source/examples/cubescape-log/main.cpp +++ b/source/examples/cubescape-log/main.cpp @@ -1,6 +1,10 @@ #include +#include +#include +#include + #include #include @@ -16,13 +20,8 @@ #include #include -#include "../cubescape/CubeScape.h" - -#include "../cubescape/glutils.h" - -#include -#include -#include +#include +#include using namespace gl; using namespace glbinding; diff --git a/source/examples/cubescape-qt/CMakeLists.txt b/source/examples/cubescape-qt/CMakeLists.txt index 852cd456a..dde0d145e 100644 --- a/source/examples/cubescape-qt/CMakeLists.txt +++ b/source/examples/cubescape-qt/CMakeLists.txt @@ -6,7 +6,6 @@ find_package(Qt5Core 5.1 QUIET) find_package(Qt5Gui 5.1 QUIET) find_package(Qt5Widgets 5.1 QUIET) -find_package(cpplocate QUIET) # Enable automoc @@ -38,19 +37,10 @@ if(APPLE) endif() -if (NOT cpplocate_FOUND) - message(STATUS "Example ${target}: using static data path (cpplocate not found)") -else() - message(STATUS "Example ${target}") -endif() - - # # Sources # -set(cubscape_path ../cubescape) - set(sources main.cpp @@ -61,13 +51,6 @@ set(sources Viewer.cpp Viewer.h Viewer.ui - - ${cubscape_path}/CubeScape.cpp - ${cubscape_path}/CubeScape.h - ${cubscape_path}/glutils.cpp - ${cubscape_path}/glutils.h - ${cubscape_path}/RawFile.cpp - ${cubscape_path}/RawFile.h ) @@ -119,10 +102,10 @@ target_link_libraries(${target} ${DEFAULT_LIBRARIES} ${META_PROJECT_NAME}::glbinding ${META_PROJECT_NAME}::glbinding-aux + ${META_PROJECT_NAME}::cubescape-shared-gl Qt5::Core Qt5::Gui Qt5::Widgets - "$<$:cpplocate::cpplocate>" "$<$:${OPENGL_gl_LIBRARY}>" ) @@ -134,7 +117,6 @@ target_link_libraries(${target} target_compile_definitions(${target} PRIVATE ${DEFAULT_COMPILE_DEFINITIONS} - $<$:cpplocate_FOUND> ) diff --git a/source/examples/cubescape-qt/Painter.cpp b/source/examples/cubescape-qt/Painter.cpp index aeb2dd9d9..27c87acdc 100644 --- a/source/examples/cubescape-qt/Painter.cpp +++ b/source/examples/cubescape-qt/Painter.cpp @@ -5,7 +5,7 @@ #include -#include "../cubescape/CubeScape.h" +#include Painter::Painter() diff --git a/source/examples/cubescape-sdl/CMakeLists.txt b/source/examples/cubescape-sdl/CMakeLists.txt index fe11a45bf..a927035a9 100644 --- a/source/examples/cubescape-sdl/CMakeLists.txt +++ b/source/examples/cubescape-sdl/CMakeLists.txt @@ -4,7 +4,6 @@ # find_package(SDL2 QUIET) -find_package(cpplocate QUIET) # @@ -20,12 +19,6 @@ if (NOT TARGET SDL2::SDL2) return() endif() -if (NOT cpplocate_FOUND) - message(STATUS "Example ${target}: using static data path (cpplocate not found)") -else() - message(STATUS "Example ${target}") -endif() - # # Sources @@ -33,12 +26,6 @@ endif() set(sources main.cpp - CubeScape.cpp - CubeScape.h - glutils.cpp - glutils.h - RawFile.cpp - RawFile.h ) @@ -91,7 +78,7 @@ target_link_libraries(${target} SDL2::SDL2main ${META_PROJECT_NAME}::glbinding ${META_PROJECT_NAME}::glbinding-aux - $<$:cpplocate::cpplocate> + ${META_PROJECT_NAME}::cubescape-shared-gl ) @@ -102,7 +89,6 @@ target_link_libraries(${target} target_compile_definitions(${target} PRIVATE ${DEFAULT_COMPILE_DEFINITIONS} - $<$:cpplocate_FOUND> ) diff --git a/source/examples/cubescape-sdl/CubeScape.cpp b/source/examples/cubescape-sdl/CubeScape.cpp deleted file mode 100644 index 185be1686..000000000 --- a/source/examples/cubescape-sdl/CubeScape.cpp +++ /dev/null @@ -1,265 +0,0 @@ - -#include "CubeScape.h" - -#include -#include -#include -#include - -#ifdef cpplocate_FOUND -#include -#endif - -#include - -#include "glutils.h" -#include "RawFile.h" - -using namespace gl; - -namespace -{ - - -bool readFile(const std::string & filePath, std::string & content) -{ - // http://insanecoding.blogspot.de/2011/11/how-to-read-in-file-in-c.html - - std::ifstream in(filePath, std::ios::in | std::ios::binary); - - if (!in) - return false; - - content = std::string(std::istreambuf_iterator(in), std::istreambuf_iterator()); - return true; -} - -std::string determineDataPath() -{ -#ifdef cpplocate_FOUND - std::string path = cpplocate::locatePath("data/cubescape", "share/glbinding/cubescape", reinterpret_cast(&gl::glCreateShader)); - if (path.empty()) path = "./data"; - else path = path + "/data"; -#else - const std::string path = "./data"; -#endif - - return path; -} - - -} // namespace - - -CubeScape::CubeScape() -: m_initialized(false) -, a_vertex(-1) -, u_transform(-1) -, u_time(-1) -, u_numcubes(-1) -, m_vao(0) -, m_indices(0) -, m_vertices(0) -, m_program(0) -, m_a(0.f) -, m_numcubes(16) -{ - // Get data path - std::string dataPath = determineDataPath(); - - GLuint vs = glCreateShader(GL_VERTEX_SHADER); - GLuint gs = glCreateShader(GL_GEOMETRY_SHADER); - GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); - - std::string vertexSource; - std::string geometrySource; - std::string fragmentSource; - - auto success = readFile(dataPath + "/cubescape/cubescape.vert", vertexSource); - success &= readFile(dataPath + "/cubescape/cubescape.geom", geometrySource); - success &= readFile(dataPath + "/cubescape/cubescape.frag", fragmentSource); - - if (!success) - { - std::cerr << "Could not load shaders in " << dataPath + "/cubescape/cubescape.*" << "." << std::endl; - return; - } - - const char * vertSource = vertexSource.c_str(); - const char * geomSource = geometrySource.c_str(); - const char * fragSource = fragmentSource.c_str(); - - glShaderSource(vs, 1, &vertSource, nullptr); - glCompileShader(vs); - compile_info(vs); - - glShaderSource(gs, 1, &geomSource, nullptr); - glCompileShader(gs); - compile_info(gs); - - glShaderSource(fs, 1, &fragSource, nullptr); - glCompileShader(fs); - compile_info(fs); - - m_program = glCreateProgram(); - - glAttachShader(m_program, vs); - glAttachShader(m_program, gs); - glAttachShader(m_program, fs); - - glLinkProgram(m_program); - link_info(m_program); - - // create textures - - glGenTextures(2, m_textures); - - glBindTexture(GL_TEXTURE_2D, m_textures[0]); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - { - RawFile terrain(dataPath + "/cubescape/terrain.64.64.r.ub.raw"); - if (!terrain.isValid()) - std::cout << "warning: loading texture from " << terrain.filePath() << " failed."; - - glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 64, 64, 0, GL_RED, GL_UNSIGNED_BYTE, terrain.data()); - } - - glBindTexture(GL_TEXTURE_2D, m_textures[1]); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - { - RawFile patches(dataPath + "/cubescape/patches.64.16.rgb.ub.raw"); - if (!patches.isValid()) - std::cout << "warning: loading texture from " << patches.filePath() << " failed."; - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 64, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, patches.data()); - } - - - // create cube - - static const GLfloat vertices_data[24] = - { - -1.f, -1.f, -1.f, // 0 - -1.f, -1.f, 1.f, // 1 - -1.f, 1.f, -1.f, // 2 - -1.f, 1.f, 1.f, // 3 - 1.f, -1.f, -1.f, // 4 - 1.f, -1.f, 1.f, // 5 - 1.f, 1.f, -1.f, // 6 - 1.f, 1.f, 1.f // 7 - }; - - static const GLubyte indices_data[18] = { - 2, 3, 6, 0, 1, 2, 1, 5, 3, 5, 4, 7, 4, 0, 6, 5, 1, 4 }; - - glGenVertexArrays(1, &m_vao); - glBindVertexArray(m_vao); - - glGenBuffers(1, &m_vertices); - glBindBuffer(GL_ARRAY_BUFFER, m_vertices); - glBufferData(GL_ARRAY_BUFFER, (8 * 3) * sizeof(float), vertices_data, GL_STATIC_DRAW); - - glGenBuffers(1, &m_indices); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indices); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, (6 * 3) * sizeof(GLubyte), indices_data, GL_STATIC_DRAW); - - // setup uniforms - - a_vertex = glGetAttribLocation(m_program, "a_vertex"); - glEnableVertexAttribArray(static_cast(a_vertex)); - - glVertexAttribPointer(static_cast(a_vertex), 3, GL_FLOAT, GL_FALSE, 0, nullptr); - - u_transform = glGetUniformLocation(m_program, "modelViewProjection"); - u_time = glGetUniformLocation(m_program, "time"); - u_numcubes = glGetUniformLocation(m_program, "numcubes"); - - m_time = clock::now(); - - GLint terrain = glGetUniformLocation(m_program, "terrain"); - GLint patches = glGetUniformLocation(m_program, "patches"); - - // since only single program and single data is used, bind only once - - glEnable(GL_DEPTH_TEST); - - glClearColor(0.f, 0.f, 0.f, 1.0f); - - glUseProgram(m_program); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_textures[0]); - glUniform1i(terrain, 0); - - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_textures[1]); - glUniform1i(patches, 1); - - // view - - m_view = mat4::lookAt(0.f, 0.8f,-2.0f, 0.f, -1.2f, 0.f, 0.f, 1.f, 0.f); - - m_initialized = true; -} - -CubeScape::~CubeScape() -{ - if (!m_initialized) - { - return; - } - - glDeleteBuffers(1, &m_vertices); - glDeleteBuffers(1, &m_indices); - - glDeleteProgram(m_program); -} - -void CubeScape::setNumCubes(int _numCubes) -{ - m_numcubes = std::min(4096, std::max(1, _numCubes)); -} -int CubeScape::numCubes() const -{ - return m_numcubes; -} - -void CubeScape::resize(int width, int height) -{ - m_projection = mat4::perspective(40.f, static_cast(width) / static_cast(height), 1.f, 4.f); - - glViewport(0, 0, width, height); -} - -void CubeScape::draw() -{ - if (!m_initialized) - { - return; - } - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - auto ms = std::chrono::duration_cast(clock::now() - m_time); - float t = static_cast(ms.count()) * 1e-3f; - - const mat4 transform = m_projection * m_view * mat4::rotate(t * 0.1f, 0.f, 1.f, 0.f); - - glUniformMatrix4fv(u_transform, 1, GL_FALSE, &transform[0]); - glUniform1f(u_time, t); - glUniform1i(u_numcubes, m_numcubes); - - glDrawElementsInstanced(GL_TRIANGLES, 18, GL_UNSIGNED_BYTE, 0, m_numcubes * m_numcubes); -} diff --git a/source/examples/cubescape-sdl/CubeScape.h b/source/examples/cubescape-sdl/CubeScape.h deleted file mode 100644 index 52aacb467..000000000 --- a/source/examples/cubescape-sdl/CubeScape.h +++ /dev/null @@ -1,47 +0,0 @@ - -#pragma once - -#include - -#include - -#include "glutils.h" - - -class CubeScape -{ -public: - CubeScape(); - ~CubeScape(); - - void resize(int width, int height); - void draw(); - - void setNumCubes(int numCubes); - int numCubes() const; - -protected: - bool m_initialized; - - gl::GLint a_vertex; - gl::GLint u_transform; - gl::GLint u_time; - gl::GLint u_numcubes; - - gl::GLuint m_vao; - gl::GLuint m_indices; - gl::GLuint m_vertices; - - gl::GLuint m_program; - - gl::GLuint m_textures[2]; - - float m_a; - int m_numcubes; - - mat4 m_view; - mat4 m_projection; - - using clock = std::chrono::system_clock; - clock::time_point m_time; -}; diff --git a/source/examples/cubescape-sdl/RawFile.cpp b/source/examples/cubescape-sdl/RawFile.cpp deleted file mode 100644 index e4de8cb82..000000000 --- a/source/examples/cubescape-sdl/RawFile.cpp +++ /dev/null @@ -1,59 +0,0 @@ - -#include "RawFile.h" - -#include -#include - -RawFile::RawFile(const std::string & _filePath) -: m_filePath(_filePath) -, m_valid(false) -{ - m_valid = readFile(); -} - -RawFile::~RawFile() -{ -} - -bool RawFile::isValid() const -{ - return m_valid; -} - -const char * RawFile::data() const -{ - return m_data.data(); -} - -size_t RawFile::size() const -{ - return m_data.size(); -} - -bool RawFile::readFile() -{ - std::ifstream ifs(m_filePath, std::ios::in | std::ios::binary); - - if (!ifs) - { - std::cerr << "Reading from file \"" << m_filePath << "\" failed." << std::endl; - return false; - } - - readRawData(ifs); - - ifs.close(); - - return true; -} - -void RawFile::readRawData(std::ifstream & ifs) -{ - ifs.seekg(0, std::ios::end); - - const size_t _size = static_cast(ifs.tellg()); - m_data.resize(_size); - - ifs.seekg(0, std::ios::beg); - ifs.read(m_data.data(), static_cast(_size)); -} diff --git a/source/examples/cubescape-sdl/RawFile.h b/source/examples/cubescape-sdl/RawFile.h deleted file mode 100644 index 8d4bb396e..000000000 --- a/source/examples/cubescape-sdl/RawFile.h +++ /dev/null @@ -1,32 +0,0 @@ - -#pragma once - -#include -#include -#include -#include - -class RawFile -{ -public: - RawFile(const std::string & filePath); - virtual ~RawFile(); - - RawFile & operator=(const RawFile &) = delete; - - const char * data() const; - size_t size() const; - - bool isValid() const; - inline const std::string & filePath() const { return m_filePath; } - -protected: - bool readFile(); - void readRawData(std::ifstream & ifs); - -protected: - const std::string m_filePath; - std::vector m_data; - - bool m_valid; -}; diff --git a/source/examples/cubescape-sdl/glutils.cpp b/source/examples/cubescape-sdl/glutils.cpp deleted file mode 100644 index 99aed1878..000000000 --- a/source/examples/cubescape-sdl/glutils.cpp +++ /dev/null @@ -1,273 +0,0 @@ - -#include "glutils.h" - -#include -#include - - -#include - - -using namespace gl32core; - - -void compile_info(const GLuint shader) -{ - GLint status(0); - glGetShaderiv(shader, GL_COMPILE_STATUS, &status); - - if (1 != status) - { - GLint maxLength(0); - GLint logLength(0); - - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); - - GLchar * log = new GLchar[maxLength]; - glGetShaderInfoLog(shader, maxLength, &logLength, log); - - std::cout << "Compiling shader failed." << std::endl - << log << std::endl; - } -} - -void link_info(const GLuint program) -{ - GLint status(0); - glGetProgramiv(program, GL_LINK_STATUS, &status); - - if (1 != status) - { - GLint maxLength(0); - GLint logLength(0); - - glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength); - - GLchar * log = new GLchar[maxLength]; - glGetProgramInfoLog(program, maxLength, &logLength, log); - - std::cout << "Linking program failed." << std::endl - << log << std::endl; - } -} - - -mat4::mat4() -{ - for (int i = 0; i < 16; ++i) - m[i] = 0.f; - - m[0] = m[5] = m[10] = m[15] = 1.f; -} - - - mat4 operator*(const mat4 & a, const mat4 & b) -{ - mat4 m; - - m[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12] * b[ 3]; - m[ 4] = a[ 0] * b[ 4] + a[ 4] * b[ 5] + a[ 8] * b[ 6] + a[12] * b[ 7]; - m[ 8] = a[ 0] * b[ 8] + a[ 4] * b[ 9] + a[ 8] * b[10] + a[12] * b[11]; - m[12] = a[ 0] * b[12] + a[ 4] * b[13] + a[ 8] * b[14] + a[12] * b[15]; - - m[ 1] = a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2] + a[13] * b[ 3]; - m[ 5] = a[ 1] * b[ 4] + a[ 5] * b[ 5] + a[ 9] * b[ 6] + a[13] * b[ 7]; - m[ 9] = a[ 1] * b[ 8] + a[ 5] * b[ 9] + a[ 9] * b[10] + a[13] * b[11]; - m[13] = a[ 1] * b[12] + a[ 5] * b[13] + a[ 9] * b[14] + a[13] * b[15]; - - m[ 2] = a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2] + a[14] * b[ 3]; - m[ 6] = a[ 2] * b[ 4] + a[ 6] * b[ 5] + a[10] * b[ 6] + a[14] * b[ 7]; - m[10] = a[ 2] * b[ 8] + a[ 6] * b[ 9] + a[10] * b[10] + a[14] * b[11]; - m[14] = a[ 2] * b[12] + a[ 6] * b[13] + a[10] * b[14] + a[14] * b[15]; - - m[ 3] = a[ 3] * b[ 0] + a[ 7] * b[ 1] + a[11] * b[ 2] + a[15] * b[ 3]; - m[ 7] = a[ 3] * b[ 4] + a[ 7] * b[ 5] + a[11] * b[ 6] + a[15] * b[ 7]; - m[11] = a[ 3] * b[ 8] + a[ 7] * b[ 9] + a[11] * b[10] + a[15] * b[11]; - m[15] = a[ 3] * b[12] + a[ 7] * b[13] + a[11] * b[14] + a[15] * b[15]; - - return m; -} - -mat4 mat4::perspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar) -{ - mat4 m; - - //const float zRange = zNear - zFar; - const float f = 1.f / static_cast(tan(fovy * 0.5f * 0.01745329251994329576923690768489f)); - - m[ 0] = f / aspect; - m[ 5] = f; - - m[10] = -(zFar + zNear) / (zFar - zNear); - m[14] = -(2.f * zFar * zNear) / (zFar - zNear); - - m[11] = -1.f; - m[15] = 0.f; - - return m; -} - -mat4 mat4::lookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez - , GLfloat centerx, GLfloat centery, GLfloat centerz, GLfloat upx, GLfloat upy, GLfloat upz) -{ - vec3 e(eyex, eyey, eyez); - - vec3 f(centerx - eyex, centery - eyey, centerz - eyez); - f.normalize(); - - vec3 u(upx, upy, upz); - u.normalize(); - - vec3 s = crossp(f, u); - u = crossp(s, f); - - mat4 m; - - m[ 0] = s[0]; - m[ 4] = s[1]; - m[ 8] = s[2]; - - m[ 1] = u[0]; - m[ 5] = u[1]; - m[ 9] = u[2]; - - m[ 2] = -f[0]; - m[ 6] = -f[1]; - m[10] = -f[2]; - - m[12] = -dotp(s, e); - m[13] = -dotp(u, e); - m[14] = dotp(f, e); - - return m; -} - -mat4 mat4::translate(GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - m[12] = x; - m[13] = y; - m[14] = z; - - return m; -} - -mat4 mat4::scale(GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - m[ 0] = x; - m[ 5] = y; - m[10] = z; - - return m; -} - -mat4 mat4::rotate(GLfloat a, GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - GLfloat l = 1.f / static_cast(sqrt(x * x + y * y + z * z)); - - x *= l; - y *= l; - z *= l; - - const GLfloat c = static_cast(cos(a)); - const GLfloat s = static_cast(sin(a)); - - const GLfloat d = 1.f - c; - - m[ 0] = x * x * d + c; - m[ 4] = x * y * d - z * s; - m[ 8] = x * z * d + y * s; - - m[ 1] = y * x * d + z * s; - m[ 5] = y * y * d + c; - m[ 9] = y * z * d - x * s; - - m[ 2] = z * x * d - y * s; - m[ 6] = z * y * d + x * s; - m[10] = z * z * d + c; - - return m; -} - -vec3::vec3() -{ - v[0] = v[1] = v[2] = 0.f; -} - -vec3::vec3(GLfloat x, GLfloat y, GLfloat z) -{ - v[0] = x; - v[1] = y; - v[2] = z; -} - -vec3 & vec3::operator+(const vec3 & rhs) -{ - v[0] += rhs[0]; - v[1] += rhs[1]; - v[2] += rhs[2]; - - return *this; -} - -vec3 & vec3::operator-(const vec3 & rhs) -{ - v[0] -= rhs[0]; - v[1] -= rhs[1]; - v[2] -= rhs[2]; - - return *this; -} - -vec3 & vec3::operator*(const vec3 & rhs) -{ - v[0] *= rhs[0]; - v[1] *= rhs[1]; - v[2] *= rhs[2]; - - return *this; -} - -vec3 & vec3::operator/(const vec3 & rhs) -{ - v[0] /= rhs[0]; - v[1] /= rhs[1]; - v[2] /= rhs[2]; - - return *this; -} - -GLfloat vec3::length() -{ - return static_cast(sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])); -} - -void vec3::normalize() -{ - const GLfloat s = 1.f / length(); - - v[0] *= s; - v[1] *= s; - v[2] *= s; -} - -vec3 crossp(const vec3 & a, const vec3 & b) -{ - vec3 v; - - v[0] = a[1] * b[2] - a[2] * b[1]; - v[1] = a[2] * b[0] - a[0] * b[2]; - v[2] = a[0] * b[1] - a[1] * b[0]; - - return v; -} - -GLfloat dotp(const vec3 & a, const vec3 & b) -{ - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; -} diff --git a/source/examples/cubescape-sdl/glutils.h b/source/examples/cubescape-sdl/glutils.h deleted file mode 100644 index 147971d95..000000000 --- a/source/examples/cubescape-sdl/glutils.h +++ /dev/null @@ -1,50 +0,0 @@ - -#pragma once - -#include - -void compile_info(const gl32core::GLuint shader); -void link_info(const gl32core::GLuint program); - -struct mat4 -{ - mat4(); - - inline gl::GLfloat & operator[](const int i) { return m[i]; } - inline const gl::GLfloat & operator[](const int i) const { return m[i]; } - - static mat4 lookAt(gl::GLfloat eyex, gl::GLfloat eyey, gl::GLfloat eyez - , gl::GLfloat centerx, gl::GLfloat centery, gl::GLfloat centerz, gl::GLfloat upx, gl::GLfloat upy, gl::GLfloat upz); - - static mat4 perspective(gl::GLfloat fovy, gl::GLfloat aspect, gl::GLfloat zNear, gl::GLfloat zFar); - - static mat4 translate(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - static mat4 scale(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - static mat4 rotate(gl::GLfloat angle, gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - - gl::GLfloat m[16]; -}; - -mat4 operator*(const mat4 & a, const mat4 & b); - -struct vec3 -{ - vec3(); - vec3(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - - inline gl::GLfloat & operator[](const int i) { return v[i]; } - inline const gl::GLfloat & operator[](const int i) const { return v[i]; } - - vec3 & operator+(const vec3 & rhs); - vec3 & operator-(const vec3 & rhs); - vec3 & operator*(const vec3 & rhs); - vec3 & operator/(const vec3 & rhs); - - gl::GLfloat length(); - void normalize(); - - gl::GLfloat v[3]; -}; - -vec3 crossp(const vec3 & a, const vec3 & b); -gl::GLfloat dotp(const vec3 & a, const vec3 & b); diff --git a/source/examples/cubescape-sdl/main.cpp b/source/examples/cubescape-sdl/main.cpp index 8d32c6ee8..6d1a95860 100644 --- a/source/examples/cubescape-sdl/main.cpp +++ b/source/examples/cubescape-sdl/main.cpp @@ -17,7 +17,7 @@ #include #include -#include "CubeScape.h" +#include using namespace gl; diff --git a/source/examples/cubescape-shared-gl/CMakeLists.txt b/source/examples/cubescape-shared-gl/CMakeLists.txt new file mode 100644 index 000000000..e4300f887 --- /dev/null +++ b/source/examples/cubescape-shared-gl/CMakeLists.txt @@ -0,0 +1,130 @@ + +# +# External dependencies +# + +find_package(cpplocate QUIET) + + +# +# Executable name and options +# + +# Target name +set(target cubescape-shared-gl) + +if (NOT cpplocate_FOUND) + message(STATUS "Example Lib ${target}: using static data path (cpplocate not found)") +else() + message(STATUS "Example Lib ${target}") +endif() + + +# +# Sources +# + +set(sources + CubeScape.cpp + CubeScape.h + glutils.cpp + glutils.h + RawFile.cpp + RawFile.h +) + + +# +# Create library +# + +# Build library +add_library(${target} STATIC + ${sources} +) + +# Create namespaced alias +add_library(${META_PROJECT_NAME}::${target} ALIAS ${target}) + + +# +# Project options +# + +set_target_properties(${target} + PROPERTIES + ${DEFAULT_PROJECT_OPTIONS} + FOLDER "${IDE_FOLDER}" +) + + +# +# Include directories +# + +target_include_directories(${target} + PRIVATE + ${DEFAULT_INCLUDE_DIRECTORIES} + ${PROJECT_BINARY_DIR}/source/include + + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} + + SYSTEM +) + + +# +# Libraries +# + +target_link_libraries(${target} + PRIVATE + ${DEFAULT_LIBRARIES} + ${META_PROJECT_NAME}::glbinding + ${META_PROJECT_NAME}::glbinding-aux + $<$:cpplocate::cpplocate> +) + + +# +# Compile definitions +# + +target_compile_definitions(${target} + PRIVATE + ${DEFAULT_COMPILE_DEFINITIONS} + $<$:cpplocate_FOUND> +) + + +# +# Compile options +# + +target_compile_options(${target} + PRIVATE + ${DEFAULT_COMPILE_OPTIONS_PRIVATE} + PUBLIC + ${DEFAULT_COMPILE_OPTIONS_PUBLIC} +) + + +# +# Linker options +# + +target_link_libraries(${target} + PRIVATE + ${DEFAULT_LINKER_OPTIONS} +) + + +# +# Target Health +# + +perform_health_checks( + ${target} + ${sources} +) diff --git a/source/examples/cubescape-gtk4/CubeScape.cpp b/source/examples/cubescape-shared-gl/CubeScape.cpp similarity index 100% rename from source/examples/cubescape-gtk4/CubeScape.cpp rename to source/examples/cubescape-shared-gl/CubeScape.cpp diff --git a/source/examples/cubescape-gtk4/CubeScape.h b/source/examples/cubescape-shared-gl/CubeScape.h similarity index 100% rename from source/examples/cubescape-gtk4/CubeScape.h rename to source/examples/cubescape-shared-gl/CubeScape.h diff --git a/source/examples/cubescape-gles/RawFile.cpp b/source/examples/cubescape-shared-gl/RawFile.cpp similarity index 100% rename from source/examples/cubescape-gles/RawFile.cpp rename to source/examples/cubescape-shared-gl/RawFile.cpp diff --git a/source/examples/cubescape-gles/RawFile.h b/source/examples/cubescape-shared-gl/RawFile.h similarity index 100% rename from source/examples/cubescape-gles/RawFile.h rename to source/examples/cubescape-shared-gl/RawFile.h diff --git a/source/examples/cubescape-gtk3/glutils.cpp b/source/examples/cubescape-shared-gl/glutils.cpp similarity index 100% rename from source/examples/cubescape-gtk3/glutils.cpp rename to source/examples/cubescape-shared-gl/glutils.cpp diff --git a/source/examples/cubescape-gtk3/glutils.h b/source/examples/cubescape-shared-gl/glutils.h similarity index 100% rename from source/examples/cubescape-gtk3/glutils.h rename to source/examples/cubescape-shared-gl/glutils.h diff --git a/source/examples/cubescape-shared-gles/CMakeLists.txt b/source/examples/cubescape-shared-gles/CMakeLists.txt new file mode 100644 index 000000000..a4dd69e46 --- /dev/null +++ b/source/examples/cubescape-shared-gles/CMakeLists.txt @@ -0,0 +1,130 @@ + +# +# External dependencies +# + +find_package(cpplocate QUIET) + + +# +# Executable name and options +# + +# Target name +set(target cubescape-shared-gles) + +if (NOT cpplocate_FOUND) + message(STATUS "Example Lib ${target}: using static data path (cpplocate not found)") +else() + message(STATUS "Example Lib ${target}") +endif() + + +# +# Sources +# + +set(sources + CubeScape.cpp + CubeScape.h + glutils.cpp + glutils.h + RawFile.cpp + RawFile.h +) + + +# +# Create library +# + +# Build library +add_library(${target} STATIC + ${sources} +) + +# Create namespaced alias +add_library(${META_PROJECT_NAME}::${target} ALIAS ${target}) + + +# +# Project options +# + +set_target_properties(${target} + PROPERTIES + ${DEFAULT_PROJECT_OPTIONS} + FOLDER "${IDE_FOLDER}" +) + + +# +# Include directories +# + +target_include_directories(${target} + PRIVATE + ${DEFAULT_INCLUDE_DIRECTORIES} + ${PROJECT_BINARY_DIR}/source/include + + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} + + SYSTEM +) + + +# +# Libraries +# + +target_link_libraries(${target} + PRIVATE + ${DEFAULT_LIBRARIES} + ${META_PROJECT_NAME}::glbinding + ${META_PROJECT_NAME}::glbinding-aux + $<$:cpplocate::cpplocate> +) + + +# +# Compile definitions +# + +target_compile_definitions(${target} + PRIVATE + ${DEFAULT_COMPILE_DEFINITIONS} + $<$:cpplocate_FOUND> +) + + +# +# Compile options +# + +target_compile_options(${target} + PRIVATE + ${DEFAULT_COMPILE_OPTIONS_PRIVATE} + PUBLIC + ${DEFAULT_COMPILE_OPTIONS_PUBLIC} +) + + +# +# Linker options +# + +target_link_libraries(${target} + PRIVATE + ${DEFAULT_LINKER_OPTIONS} +) + + +# +# Target Health +# + +perform_health_checks( + ${target} + ${sources} +) diff --git a/source/examples/cubescape-gles/CubeScape.cpp b/source/examples/cubescape-shared-gles/CubeScape.cpp similarity index 100% rename from source/examples/cubescape-gles/CubeScape.cpp rename to source/examples/cubescape-shared-gles/CubeScape.cpp diff --git a/source/examples/cubescape-gles/CubeScape.h b/source/examples/cubescape-shared-gles/CubeScape.h similarity index 100% rename from source/examples/cubescape-gles/CubeScape.h rename to source/examples/cubescape-shared-gles/CubeScape.h diff --git a/source/examples/cubescape-gtk3/RawFile.cpp b/source/examples/cubescape-shared-gles/RawFile.cpp similarity index 100% rename from source/examples/cubescape-gtk3/RawFile.cpp rename to source/examples/cubescape-shared-gles/RawFile.cpp diff --git a/source/examples/cubescape-gtk3/RawFile.h b/source/examples/cubescape-shared-gles/RawFile.h similarity index 100% rename from source/examples/cubescape-gtk3/RawFile.h rename to source/examples/cubescape-shared-gles/RawFile.h diff --git a/source/examples/cubescape-gles/glutils.cpp b/source/examples/cubescape-shared-gles/glutils.cpp similarity index 100% rename from source/examples/cubescape-gles/glutils.cpp rename to source/examples/cubescape-shared-gles/glutils.cpp diff --git a/source/examples/cubescape-gles/glutils.h b/source/examples/cubescape-shared-gles/glutils.h similarity index 100% rename from source/examples/cubescape-gles/glutils.h rename to source/examples/cubescape-shared-gles/glutils.h diff --git a/source/examples/cubescape-wgl/CMakeLists.txt b/source/examples/cubescape-wgl/CMakeLists.txt index 481c5f6f4..01e3809fe 100644 --- a/source/examples/cubescape-wgl/CMakeLists.txt +++ b/source/examples/cubescape-wgl/CMakeLists.txt @@ -4,7 +4,6 @@ # find_package(glfw3 QUIET) -find_package(cpplocate QUIET) find_package(OpenGL QUIET) @@ -31,12 +30,6 @@ if (NOT glfw3_FOUND) return() endif() -if (NOT cpplocate_FOUND) - message(STATUS "Example ${target}: using static data path (cpplocate not found)") -else() - message(STATUS "Example ${target}") -endif() - # # Sources @@ -44,12 +37,6 @@ endif() set(sources main.cpp - CubeScape.cpp - CubeScape.h - glutils.cpp - glutils.h - RawFile.cpp - RawFile.h ) @@ -59,7 +46,6 @@ set(sources # Build executable add_executable(${target} - MACOSX_BUNDLE ${sources} ) @@ -102,7 +88,7 @@ target_link_libraries(${target} glfw ${META_PROJECT_NAME}::glbinding ${META_PROJECT_NAME}::glbinding-aux - $<$:cpplocate::cpplocate> + ${META_PROJECT_NAME}::cubescape-shared-gl ) @@ -114,7 +100,6 @@ target_compile_definitions(${target} PRIVATE ${DEFAULT_COMPILE_DEFINITIONS} GLFW_INCLUDE_NONE - $<$:cpplocate_FOUND> ) diff --git a/source/examples/cubescape-wgl/CubeScape.cpp b/source/examples/cubescape-wgl/CubeScape.cpp deleted file mode 100644 index 185be1686..000000000 --- a/source/examples/cubescape-wgl/CubeScape.cpp +++ /dev/null @@ -1,265 +0,0 @@ - -#include "CubeScape.h" - -#include -#include -#include -#include - -#ifdef cpplocate_FOUND -#include -#endif - -#include - -#include "glutils.h" -#include "RawFile.h" - -using namespace gl; - -namespace -{ - - -bool readFile(const std::string & filePath, std::string & content) -{ - // http://insanecoding.blogspot.de/2011/11/how-to-read-in-file-in-c.html - - std::ifstream in(filePath, std::ios::in | std::ios::binary); - - if (!in) - return false; - - content = std::string(std::istreambuf_iterator(in), std::istreambuf_iterator()); - return true; -} - -std::string determineDataPath() -{ -#ifdef cpplocate_FOUND - std::string path = cpplocate::locatePath("data/cubescape", "share/glbinding/cubescape", reinterpret_cast(&gl::glCreateShader)); - if (path.empty()) path = "./data"; - else path = path + "/data"; -#else - const std::string path = "./data"; -#endif - - return path; -} - - -} // namespace - - -CubeScape::CubeScape() -: m_initialized(false) -, a_vertex(-1) -, u_transform(-1) -, u_time(-1) -, u_numcubes(-1) -, m_vao(0) -, m_indices(0) -, m_vertices(0) -, m_program(0) -, m_a(0.f) -, m_numcubes(16) -{ - // Get data path - std::string dataPath = determineDataPath(); - - GLuint vs = glCreateShader(GL_VERTEX_SHADER); - GLuint gs = glCreateShader(GL_GEOMETRY_SHADER); - GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); - - std::string vertexSource; - std::string geometrySource; - std::string fragmentSource; - - auto success = readFile(dataPath + "/cubescape/cubescape.vert", vertexSource); - success &= readFile(dataPath + "/cubescape/cubescape.geom", geometrySource); - success &= readFile(dataPath + "/cubescape/cubescape.frag", fragmentSource); - - if (!success) - { - std::cerr << "Could not load shaders in " << dataPath + "/cubescape/cubescape.*" << "." << std::endl; - return; - } - - const char * vertSource = vertexSource.c_str(); - const char * geomSource = geometrySource.c_str(); - const char * fragSource = fragmentSource.c_str(); - - glShaderSource(vs, 1, &vertSource, nullptr); - glCompileShader(vs); - compile_info(vs); - - glShaderSource(gs, 1, &geomSource, nullptr); - glCompileShader(gs); - compile_info(gs); - - glShaderSource(fs, 1, &fragSource, nullptr); - glCompileShader(fs); - compile_info(fs); - - m_program = glCreateProgram(); - - glAttachShader(m_program, vs); - glAttachShader(m_program, gs); - glAttachShader(m_program, fs); - - glLinkProgram(m_program); - link_info(m_program); - - // create textures - - glGenTextures(2, m_textures); - - glBindTexture(GL_TEXTURE_2D, m_textures[0]); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - { - RawFile terrain(dataPath + "/cubescape/terrain.64.64.r.ub.raw"); - if (!terrain.isValid()) - std::cout << "warning: loading texture from " << terrain.filePath() << " failed."; - - glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 64, 64, 0, GL_RED, GL_UNSIGNED_BYTE, terrain.data()); - } - - glBindTexture(GL_TEXTURE_2D, m_textures[1]); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - { - RawFile patches(dataPath + "/cubescape/patches.64.16.rgb.ub.raw"); - if (!patches.isValid()) - std::cout << "warning: loading texture from " << patches.filePath() << " failed."; - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 64, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, patches.data()); - } - - - // create cube - - static const GLfloat vertices_data[24] = - { - -1.f, -1.f, -1.f, // 0 - -1.f, -1.f, 1.f, // 1 - -1.f, 1.f, -1.f, // 2 - -1.f, 1.f, 1.f, // 3 - 1.f, -1.f, -1.f, // 4 - 1.f, -1.f, 1.f, // 5 - 1.f, 1.f, -1.f, // 6 - 1.f, 1.f, 1.f // 7 - }; - - static const GLubyte indices_data[18] = { - 2, 3, 6, 0, 1, 2, 1, 5, 3, 5, 4, 7, 4, 0, 6, 5, 1, 4 }; - - glGenVertexArrays(1, &m_vao); - glBindVertexArray(m_vao); - - glGenBuffers(1, &m_vertices); - glBindBuffer(GL_ARRAY_BUFFER, m_vertices); - glBufferData(GL_ARRAY_BUFFER, (8 * 3) * sizeof(float), vertices_data, GL_STATIC_DRAW); - - glGenBuffers(1, &m_indices); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indices); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, (6 * 3) * sizeof(GLubyte), indices_data, GL_STATIC_DRAW); - - // setup uniforms - - a_vertex = glGetAttribLocation(m_program, "a_vertex"); - glEnableVertexAttribArray(static_cast(a_vertex)); - - glVertexAttribPointer(static_cast(a_vertex), 3, GL_FLOAT, GL_FALSE, 0, nullptr); - - u_transform = glGetUniformLocation(m_program, "modelViewProjection"); - u_time = glGetUniformLocation(m_program, "time"); - u_numcubes = glGetUniformLocation(m_program, "numcubes"); - - m_time = clock::now(); - - GLint terrain = glGetUniformLocation(m_program, "terrain"); - GLint patches = glGetUniformLocation(m_program, "patches"); - - // since only single program and single data is used, bind only once - - glEnable(GL_DEPTH_TEST); - - glClearColor(0.f, 0.f, 0.f, 1.0f); - - glUseProgram(m_program); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_textures[0]); - glUniform1i(terrain, 0); - - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_textures[1]); - glUniform1i(patches, 1); - - // view - - m_view = mat4::lookAt(0.f, 0.8f,-2.0f, 0.f, -1.2f, 0.f, 0.f, 1.f, 0.f); - - m_initialized = true; -} - -CubeScape::~CubeScape() -{ - if (!m_initialized) - { - return; - } - - glDeleteBuffers(1, &m_vertices); - glDeleteBuffers(1, &m_indices); - - glDeleteProgram(m_program); -} - -void CubeScape::setNumCubes(int _numCubes) -{ - m_numcubes = std::min(4096, std::max(1, _numCubes)); -} -int CubeScape::numCubes() const -{ - return m_numcubes; -} - -void CubeScape::resize(int width, int height) -{ - m_projection = mat4::perspective(40.f, static_cast(width) / static_cast(height), 1.f, 4.f); - - glViewport(0, 0, width, height); -} - -void CubeScape::draw() -{ - if (!m_initialized) - { - return; - } - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - auto ms = std::chrono::duration_cast(clock::now() - m_time); - float t = static_cast(ms.count()) * 1e-3f; - - const mat4 transform = m_projection * m_view * mat4::rotate(t * 0.1f, 0.f, 1.f, 0.f); - - glUniformMatrix4fv(u_transform, 1, GL_FALSE, &transform[0]); - glUniform1f(u_time, t); - glUniform1i(u_numcubes, m_numcubes); - - glDrawElementsInstanced(GL_TRIANGLES, 18, GL_UNSIGNED_BYTE, 0, m_numcubes * m_numcubes); -} diff --git a/source/examples/cubescape-wgl/CubeScape.h b/source/examples/cubescape-wgl/CubeScape.h deleted file mode 100644 index 52aacb467..000000000 --- a/source/examples/cubescape-wgl/CubeScape.h +++ /dev/null @@ -1,47 +0,0 @@ - -#pragma once - -#include - -#include - -#include "glutils.h" - - -class CubeScape -{ -public: - CubeScape(); - ~CubeScape(); - - void resize(int width, int height); - void draw(); - - void setNumCubes(int numCubes); - int numCubes() const; - -protected: - bool m_initialized; - - gl::GLint a_vertex; - gl::GLint u_transform; - gl::GLint u_time; - gl::GLint u_numcubes; - - gl::GLuint m_vao; - gl::GLuint m_indices; - gl::GLuint m_vertices; - - gl::GLuint m_program; - - gl::GLuint m_textures[2]; - - float m_a; - int m_numcubes; - - mat4 m_view; - mat4 m_projection; - - using clock = std::chrono::system_clock; - clock::time_point m_time; -}; diff --git a/source/examples/cubescape-wgl/RawFile.cpp b/source/examples/cubescape-wgl/RawFile.cpp deleted file mode 100644 index e4de8cb82..000000000 --- a/source/examples/cubescape-wgl/RawFile.cpp +++ /dev/null @@ -1,59 +0,0 @@ - -#include "RawFile.h" - -#include -#include - -RawFile::RawFile(const std::string & _filePath) -: m_filePath(_filePath) -, m_valid(false) -{ - m_valid = readFile(); -} - -RawFile::~RawFile() -{ -} - -bool RawFile::isValid() const -{ - return m_valid; -} - -const char * RawFile::data() const -{ - return m_data.data(); -} - -size_t RawFile::size() const -{ - return m_data.size(); -} - -bool RawFile::readFile() -{ - std::ifstream ifs(m_filePath, std::ios::in | std::ios::binary); - - if (!ifs) - { - std::cerr << "Reading from file \"" << m_filePath << "\" failed." << std::endl; - return false; - } - - readRawData(ifs); - - ifs.close(); - - return true; -} - -void RawFile::readRawData(std::ifstream & ifs) -{ - ifs.seekg(0, std::ios::end); - - const size_t _size = static_cast(ifs.tellg()); - m_data.resize(_size); - - ifs.seekg(0, std::ios::beg); - ifs.read(m_data.data(), static_cast(_size)); -} diff --git a/source/examples/cubescape-wgl/RawFile.h b/source/examples/cubescape-wgl/RawFile.h deleted file mode 100644 index 8d4bb396e..000000000 --- a/source/examples/cubescape-wgl/RawFile.h +++ /dev/null @@ -1,32 +0,0 @@ - -#pragma once - -#include -#include -#include -#include - -class RawFile -{ -public: - RawFile(const std::string & filePath); - virtual ~RawFile(); - - RawFile & operator=(const RawFile &) = delete; - - const char * data() const; - size_t size() const; - - bool isValid() const; - inline const std::string & filePath() const { return m_filePath; } - -protected: - bool readFile(); - void readRawData(std::ifstream & ifs); - -protected: - const std::string m_filePath; - std::vector m_data; - - bool m_valid; -}; diff --git a/source/examples/cubescape-wgl/glutils.cpp b/source/examples/cubescape-wgl/glutils.cpp deleted file mode 100644 index 99aed1878..000000000 --- a/source/examples/cubescape-wgl/glutils.cpp +++ /dev/null @@ -1,273 +0,0 @@ - -#include "glutils.h" - -#include -#include - - -#include - - -using namespace gl32core; - - -void compile_info(const GLuint shader) -{ - GLint status(0); - glGetShaderiv(shader, GL_COMPILE_STATUS, &status); - - if (1 != status) - { - GLint maxLength(0); - GLint logLength(0); - - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); - - GLchar * log = new GLchar[maxLength]; - glGetShaderInfoLog(shader, maxLength, &logLength, log); - - std::cout << "Compiling shader failed." << std::endl - << log << std::endl; - } -} - -void link_info(const GLuint program) -{ - GLint status(0); - glGetProgramiv(program, GL_LINK_STATUS, &status); - - if (1 != status) - { - GLint maxLength(0); - GLint logLength(0); - - glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength); - - GLchar * log = new GLchar[maxLength]; - glGetProgramInfoLog(program, maxLength, &logLength, log); - - std::cout << "Linking program failed." << std::endl - << log << std::endl; - } -} - - -mat4::mat4() -{ - for (int i = 0; i < 16; ++i) - m[i] = 0.f; - - m[0] = m[5] = m[10] = m[15] = 1.f; -} - - - mat4 operator*(const mat4 & a, const mat4 & b) -{ - mat4 m; - - m[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12] * b[ 3]; - m[ 4] = a[ 0] * b[ 4] + a[ 4] * b[ 5] + a[ 8] * b[ 6] + a[12] * b[ 7]; - m[ 8] = a[ 0] * b[ 8] + a[ 4] * b[ 9] + a[ 8] * b[10] + a[12] * b[11]; - m[12] = a[ 0] * b[12] + a[ 4] * b[13] + a[ 8] * b[14] + a[12] * b[15]; - - m[ 1] = a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2] + a[13] * b[ 3]; - m[ 5] = a[ 1] * b[ 4] + a[ 5] * b[ 5] + a[ 9] * b[ 6] + a[13] * b[ 7]; - m[ 9] = a[ 1] * b[ 8] + a[ 5] * b[ 9] + a[ 9] * b[10] + a[13] * b[11]; - m[13] = a[ 1] * b[12] + a[ 5] * b[13] + a[ 9] * b[14] + a[13] * b[15]; - - m[ 2] = a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2] + a[14] * b[ 3]; - m[ 6] = a[ 2] * b[ 4] + a[ 6] * b[ 5] + a[10] * b[ 6] + a[14] * b[ 7]; - m[10] = a[ 2] * b[ 8] + a[ 6] * b[ 9] + a[10] * b[10] + a[14] * b[11]; - m[14] = a[ 2] * b[12] + a[ 6] * b[13] + a[10] * b[14] + a[14] * b[15]; - - m[ 3] = a[ 3] * b[ 0] + a[ 7] * b[ 1] + a[11] * b[ 2] + a[15] * b[ 3]; - m[ 7] = a[ 3] * b[ 4] + a[ 7] * b[ 5] + a[11] * b[ 6] + a[15] * b[ 7]; - m[11] = a[ 3] * b[ 8] + a[ 7] * b[ 9] + a[11] * b[10] + a[15] * b[11]; - m[15] = a[ 3] * b[12] + a[ 7] * b[13] + a[11] * b[14] + a[15] * b[15]; - - return m; -} - -mat4 mat4::perspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar) -{ - mat4 m; - - //const float zRange = zNear - zFar; - const float f = 1.f / static_cast(tan(fovy * 0.5f * 0.01745329251994329576923690768489f)); - - m[ 0] = f / aspect; - m[ 5] = f; - - m[10] = -(zFar + zNear) / (zFar - zNear); - m[14] = -(2.f * zFar * zNear) / (zFar - zNear); - - m[11] = -1.f; - m[15] = 0.f; - - return m; -} - -mat4 mat4::lookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez - , GLfloat centerx, GLfloat centery, GLfloat centerz, GLfloat upx, GLfloat upy, GLfloat upz) -{ - vec3 e(eyex, eyey, eyez); - - vec3 f(centerx - eyex, centery - eyey, centerz - eyez); - f.normalize(); - - vec3 u(upx, upy, upz); - u.normalize(); - - vec3 s = crossp(f, u); - u = crossp(s, f); - - mat4 m; - - m[ 0] = s[0]; - m[ 4] = s[1]; - m[ 8] = s[2]; - - m[ 1] = u[0]; - m[ 5] = u[1]; - m[ 9] = u[2]; - - m[ 2] = -f[0]; - m[ 6] = -f[1]; - m[10] = -f[2]; - - m[12] = -dotp(s, e); - m[13] = -dotp(u, e); - m[14] = dotp(f, e); - - return m; -} - -mat4 mat4::translate(GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - m[12] = x; - m[13] = y; - m[14] = z; - - return m; -} - -mat4 mat4::scale(GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - m[ 0] = x; - m[ 5] = y; - m[10] = z; - - return m; -} - -mat4 mat4::rotate(GLfloat a, GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - GLfloat l = 1.f / static_cast(sqrt(x * x + y * y + z * z)); - - x *= l; - y *= l; - z *= l; - - const GLfloat c = static_cast(cos(a)); - const GLfloat s = static_cast(sin(a)); - - const GLfloat d = 1.f - c; - - m[ 0] = x * x * d + c; - m[ 4] = x * y * d - z * s; - m[ 8] = x * z * d + y * s; - - m[ 1] = y * x * d + z * s; - m[ 5] = y * y * d + c; - m[ 9] = y * z * d - x * s; - - m[ 2] = z * x * d - y * s; - m[ 6] = z * y * d + x * s; - m[10] = z * z * d + c; - - return m; -} - -vec3::vec3() -{ - v[0] = v[1] = v[2] = 0.f; -} - -vec3::vec3(GLfloat x, GLfloat y, GLfloat z) -{ - v[0] = x; - v[1] = y; - v[2] = z; -} - -vec3 & vec3::operator+(const vec3 & rhs) -{ - v[0] += rhs[0]; - v[1] += rhs[1]; - v[2] += rhs[2]; - - return *this; -} - -vec3 & vec3::operator-(const vec3 & rhs) -{ - v[0] -= rhs[0]; - v[1] -= rhs[1]; - v[2] -= rhs[2]; - - return *this; -} - -vec3 & vec3::operator*(const vec3 & rhs) -{ - v[0] *= rhs[0]; - v[1] *= rhs[1]; - v[2] *= rhs[2]; - - return *this; -} - -vec3 & vec3::operator/(const vec3 & rhs) -{ - v[0] /= rhs[0]; - v[1] /= rhs[1]; - v[2] /= rhs[2]; - - return *this; -} - -GLfloat vec3::length() -{ - return static_cast(sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])); -} - -void vec3::normalize() -{ - const GLfloat s = 1.f / length(); - - v[0] *= s; - v[1] *= s; - v[2] *= s; -} - -vec3 crossp(const vec3 & a, const vec3 & b) -{ - vec3 v; - - v[0] = a[1] * b[2] - a[2] * b[1]; - v[1] = a[2] * b[0] - a[0] * b[2]; - v[2] = a[0] * b[1] - a[1] * b[0]; - - return v; -} - -GLfloat dotp(const vec3 & a, const vec3 & b) -{ - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; -} diff --git a/source/examples/cubescape-wgl/glutils.h b/source/examples/cubescape-wgl/glutils.h deleted file mode 100644 index 147971d95..000000000 --- a/source/examples/cubescape-wgl/glutils.h +++ /dev/null @@ -1,50 +0,0 @@ - -#pragma once - -#include - -void compile_info(const gl32core::GLuint shader); -void link_info(const gl32core::GLuint program); - -struct mat4 -{ - mat4(); - - inline gl::GLfloat & operator[](const int i) { return m[i]; } - inline const gl::GLfloat & operator[](const int i) const { return m[i]; } - - static mat4 lookAt(gl::GLfloat eyex, gl::GLfloat eyey, gl::GLfloat eyez - , gl::GLfloat centerx, gl::GLfloat centery, gl::GLfloat centerz, gl::GLfloat upx, gl::GLfloat upy, gl::GLfloat upz); - - static mat4 perspective(gl::GLfloat fovy, gl::GLfloat aspect, gl::GLfloat zNear, gl::GLfloat zFar); - - static mat4 translate(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - static mat4 scale(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - static mat4 rotate(gl::GLfloat angle, gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - - gl::GLfloat m[16]; -}; - -mat4 operator*(const mat4 & a, const mat4 & b); - -struct vec3 -{ - vec3(); - vec3(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - - inline gl::GLfloat & operator[](const int i) { return v[i]; } - inline const gl::GLfloat & operator[](const int i) const { return v[i]; } - - vec3 & operator+(const vec3 & rhs); - vec3 & operator-(const vec3 & rhs); - vec3 & operator*(const vec3 & rhs); - vec3 & operator/(const vec3 & rhs); - - gl::GLfloat length(); - void normalize(); - - gl::GLfloat v[3]; -}; - -vec3 crossp(const vec3 & a, const vec3 & b); -gl::GLfloat dotp(const vec3 & a, const vec3 & b); diff --git a/source/examples/cubescape-wgl/main.cpp b/source/examples/cubescape-wgl/main.cpp index 9b4ef2340..d66a58109 100644 --- a/source/examples/cubescape-wgl/main.cpp +++ b/source/examples/cubescape-wgl/main.cpp @@ -17,7 +17,7 @@ #include #include -#include "CubeScape.h" +#include using namespace gl; diff --git a/source/examples/cubescape/CubeScape.cpp b/source/examples/cubescape/CubeScape.cpp deleted file mode 100644 index 185be1686..000000000 --- a/source/examples/cubescape/CubeScape.cpp +++ /dev/null @@ -1,265 +0,0 @@ - -#include "CubeScape.h" - -#include -#include -#include -#include - -#ifdef cpplocate_FOUND -#include -#endif - -#include - -#include "glutils.h" -#include "RawFile.h" - -using namespace gl; - -namespace -{ - - -bool readFile(const std::string & filePath, std::string & content) -{ - // http://insanecoding.blogspot.de/2011/11/how-to-read-in-file-in-c.html - - std::ifstream in(filePath, std::ios::in | std::ios::binary); - - if (!in) - return false; - - content = std::string(std::istreambuf_iterator(in), std::istreambuf_iterator()); - return true; -} - -std::string determineDataPath() -{ -#ifdef cpplocate_FOUND - std::string path = cpplocate::locatePath("data/cubescape", "share/glbinding/cubescape", reinterpret_cast(&gl::glCreateShader)); - if (path.empty()) path = "./data"; - else path = path + "/data"; -#else - const std::string path = "./data"; -#endif - - return path; -} - - -} // namespace - - -CubeScape::CubeScape() -: m_initialized(false) -, a_vertex(-1) -, u_transform(-1) -, u_time(-1) -, u_numcubes(-1) -, m_vao(0) -, m_indices(0) -, m_vertices(0) -, m_program(0) -, m_a(0.f) -, m_numcubes(16) -{ - // Get data path - std::string dataPath = determineDataPath(); - - GLuint vs = glCreateShader(GL_VERTEX_SHADER); - GLuint gs = glCreateShader(GL_GEOMETRY_SHADER); - GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); - - std::string vertexSource; - std::string geometrySource; - std::string fragmentSource; - - auto success = readFile(dataPath + "/cubescape/cubescape.vert", vertexSource); - success &= readFile(dataPath + "/cubescape/cubescape.geom", geometrySource); - success &= readFile(dataPath + "/cubescape/cubescape.frag", fragmentSource); - - if (!success) - { - std::cerr << "Could not load shaders in " << dataPath + "/cubescape/cubescape.*" << "." << std::endl; - return; - } - - const char * vertSource = vertexSource.c_str(); - const char * geomSource = geometrySource.c_str(); - const char * fragSource = fragmentSource.c_str(); - - glShaderSource(vs, 1, &vertSource, nullptr); - glCompileShader(vs); - compile_info(vs); - - glShaderSource(gs, 1, &geomSource, nullptr); - glCompileShader(gs); - compile_info(gs); - - glShaderSource(fs, 1, &fragSource, nullptr); - glCompileShader(fs); - compile_info(fs); - - m_program = glCreateProgram(); - - glAttachShader(m_program, vs); - glAttachShader(m_program, gs); - glAttachShader(m_program, fs); - - glLinkProgram(m_program); - link_info(m_program); - - // create textures - - glGenTextures(2, m_textures); - - glBindTexture(GL_TEXTURE_2D, m_textures[0]); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - { - RawFile terrain(dataPath + "/cubescape/terrain.64.64.r.ub.raw"); - if (!terrain.isValid()) - std::cout << "warning: loading texture from " << terrain.filePath() << " failed."; - - glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 64, 64, 0, GL_RED, GL_UNSIGNED_BYTE, terrain.data()); - } - - glBindTexture(GL_TEXTURE_2D, m_textures[1]); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - { - RawFile patches(dataPath + "/cubescape/patches.64.16.rgb.ub.raw"); - if (!patches.isValid()) - std::cout << "warning: loading texture from " << patches.filePath() << " failed."; - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 64, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, patches.data()); - } - - - // create cube - - static const GLfloat vertices_data[24] = - { - -1.f, -1.f, -1.f, // 0 - -1.f, -1.f, 1.f, // 1 - -1.f, 1.f, -1.f, // 2 - -1.f, 1.f, 1.f, // 3 - 1.f, -1.f, -1.f, // 4 - 1.f, -1.f, 1.f, // 5 - 1.f, 1.f, -1.f, // 6 - 1.f, 1.f, 1.f // 7 - }; - - static const GLubyte indices_data[18] = { - 2, 3, 6, 0, 1, 2, 1, 5, 3, 5, 4, 7, 4, 0, 6, 5, 1, 4 }; - - glGenVertexArrays(1, &m_vao); - glBindVertexArray(m_vao); - - glGenBuffers(1, &m_vertices); - glBindBuffer(GL_ARRAY_BUFFER, m_vertices); - glBufferData(GL_ARRAY_BUFFER, (8 * 3) * sizeof(float), vertices_data, GL_STATIC_DRAW); - - glGenBuffers(1, &m_indices); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indices); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, (6 * 3) * sizeof(GLubyte), indices_data, GL_STATIC_DRAW); - - // setup uniforms - - a_vertex = glGetAttribLocation(m_program, "a_vertex"); - glEnableVertexAttribArray(static_cast(a_vertex)); - - glVertexAttribPointer(static_cast(a_vertex), 3, GL_FLOAT, GL_FALSE, 0, nullptr); - - u_transform = glGetUniformLocation(m_program, "modelViewProjection"); - u_time = glGetUniformLocation(m_program, "time"); - u_numcubes = glGetUniformLocation(m_program, "numcubes"); - - m_time = clock::now(); - - GLint terrain = glGetUniformLocation(m_program, "terrain"); - GLint patches = glGetUniformLocation(m_program, "patches"); - - // since only single program and single data is used, bind only once - - glEnable(GL_DEPTH_TEST); - - glClearColor(0.f, 0.f, 0.f, 1.0f); - - glUseProgram(m_program); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_textures[0]); - glUniform1i(terrain, 0); - - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_textures[1]); - glUniform1i(patches, 1); - - // view - - m_view = mat4::lookAt(0.f, 0.8f,-2.0f, 0.f, -1.2f, 0.f, 0.f, 1.f, 0.f); - - m_initialized = true; -} - -CubeScape::~CubeScape() -{ - if (!m_initialized) - { - return; - } - - glDeleteBuffers(1, &m_vertices); - glDeleteBuffers(1, &m_indices); - - glDeleteProgram(m_program); -} - -void CubeScape::setNumCubes(int _numCubes) -{ - m_numcubes = std::min(4096, std::max(1, _numCubes)); -} -int CubeScape::numCubes() const -{ - return m_numcubes; -} - -void CubeScape::resize(int width, int height) -{ - m_projection = mat4::perspective(40.f, static_cast(width) / static_cast(height), 1.f, 4.f); - - glViewport(0, 0, width, height); -} - -void CubeScape::draw() -{ - if (!m_initialized) - { - return; - } - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - auto ms = std::chrono::duration_cast(clock::now() - m_time); - float t = static_cast(ms.count()) * 1e-3f; - - const mat4 transform = m_projection * m_view * mat4::rotate(t * 0.1f, 0.f, 1.f, 0.f); - - glUniformMatrix4fv(u_transform, 1, GL_FALSE, &transform[0]); - glUniform1f(u_time, t); - glUniform1i(u_numcubes, m_numcubes); - - glDrawElementsInstanced(GL_TRIANGLES, 18, GL_UNSIGNED_BYTE, 0, m_numcubes * m_numcubes); -} diff --git a/source/examples/cubescape/CubeScape.h b/source/examples/cubescape/CubeScape.h deleted file mode 100644 index 52aacb467..000000000 --- a/source/examples/cubescape/CubeScape.h +++ /dev/null @@ -1,47 +0,0 @@ - -#pragma once - -#include - -#include - -#include "glutils.h" - - -class CubeScape -{ -public: - CubeScape(); - ~CubeScape(); - - void resize(int width, int height); - void draw(); - - void setNumCubes(int numCubes); - int numCubes() const; - -protected: - bool m_initialized; - - gl::GLint a_vertex; - gl::GLint u_transform; - gl::GLint u_time; - gl::GLint u_numcubes; - - gl::GLuint m_vao; - gl::GLuint m_indices; - gl::GLuint m_vertices; - - gl::GLuint m_program; - - gl::GLuint m_textures[2]; - - float m_a; - int m_numcubes; - - mat4 m_view; - mat4 m_projection; - - using clock = std::chrono::system_clock; - clock::time_point m_time; -}; diff --git a/source/examples/cubescape/RawFile.cpp b/source/examples/cubescape/RawFile.cpp deleted file mode 100644 index e4de8cb82..000000000 --- a/source/examples/cubescape/RawFile.cpp +++ /dev/null @@ -1,59 +0,0 @@ - -#include "RawFile.h" - -#include -#include - -RawFile::RawFile(const std::string & _filePath) -: m_filePath(_filePath) -, m_valid(false) -{ - m_valid = readFile(); -} - -RawFile::~RawFile() -{ -} - -bool RawFile::isValid() const -{ - return m_valid; -} - -const char * RawFile::data() const -{ - return m_data.data(); -} - -size_t RawFile::size() const -{ - return m_data.size(); -} - -bool RawFile::readFile() -{ - std::ifstream ifs(m_filePath, std::ios::in | std::ios::binary); - - if (!ifs) - { - std::cerr << "Reading from file \"" << m_filePath << "\" failed." << std::endl; - return false; - } - - readRawData(ifs); - - ifs.close(); - - return true; -} - -void RawFile::readRawData(std::ifstream & ifs) -{ - ifs.seekg(0, std::ios::end); - - const size_t _size = static_cast(ifs.tellg()); - m_data.resize(_size); - - ifs.seekg(0, std::ios::beg); - ifs.read(m_data.data(), static_cast(_size)); -} diff --git a/source/examples/cubescape/RawFile.h b/source/examples/cubescape/RawFile.h deleted file mode 100644 index 8d4bb396e..000000000 --- a/source/examples/cubescape/RawFile.h +++ /dev/null @@ -1,32 +0,0 @@ - -#pragma once - -#include -#include -#include -#include - -class RawFile -{ -public: - RawFile(const std::string & filePath); - virtual ~RawFile(); - - RawFile & operator=(const RawFile &) = delete; - - const char * data() const; - size_t size() const; - - bool isValid() const; - inline const std::string & filePath() const { return m_filePath; } - -protected: - bool readFile(); - void readRawData(std::ifstream & ifs); - -protected: - const std::string m_filePath; - std::vector m_data; - - bool m_valid; -}; diff --git a/source/examples/cubescape/glutils.cpp b/source/examples/cubescape/glutils.cpp deleted file mode 100644 index 99aed1878..000000000 --- a/source/examples/cubescape/glutils.cpp +++ /dev/null @@ -1,273 +0,0 @@ - -#include "glutils.h" - -#include -#include - - -#include - - -using namespace gl32core; - - -void compile_info(const GLuint shader) -{ - GLint status(0); - glGetShaderiv(shader, GL_COMPILE_STATUS, &status); - - if (1 != status) - { - GLint maxLength(0); - GLint logLength(0); - - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); - - GLchar * log = new GLchar[maxLength]; - glGetShaderInfoLog(shader, maxLength, &logLength, log); - - std::cout << "Compiling shader failed." << std::endl - << log << std::endl; - } -} - -void link_info(const GLuint program) -{ - GLint status(0); - glGetProgramiv(program, GL_LINK_STATUS, &status); - - if (1 != status) - { - GLint maxLength(0); - GLint logLength(0); - - glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength); - - GLchar * log = new GLchar[maxLength]; - glGetProgramInfoLog(program, maxLength, &logLength, log); - - std::cout << "Linking program failed." << std::endl - << log << std::endl; - } -} - - -mat4::mat4() -{ - for (int i = 0; i < 16; ++i) - m[i] = 0.f; - - m[0] = m[5] = m[10] = m[15] = 1.f; -} - - - mat4 operator*(const mat4 & a, const mat4 & b) -{ - mat4 m; - - m[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12] * b[ 3]; - m[ 4] = a[ 0] * b[ 4] + a[ 4] * b[ 5] + a[ 8] * b[ 6] + a[12] * b[ 7]; - m[ 8] = a[ 0] * b[ 8] + a[ 4] * b[ 9] + a[ 8] * b[10] + a[12] * b[11]; - m[12] = a[ 0] * b[12] + a[ 4] * b[13] + a[ 8] * b[14] + a[12] * b[15]; - - m[ 1] = a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2] + a[13] * b[ 3]; - m[ 5] = a[ 1] * b[ 4] + a[ 5] * b[ 5] + a[ 9] * b[ 6] + a[13] * b[ 7]; - m[ 9] = a[ 1] * b[ 8] + a[ 5] * b[ 9] + a[ 9] * b[10] + a[13] * b[11]; - m[13] = a[ 1] * b[12] + a[ 5] * b[13] + a[ 9] * b[14] + a[13] * b[15]; - - m[ 2] = a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2] + a[14] * b[ 3]; - m[ 6] = a[ 2] * b[ 4] + a[ 6] * b[ 5] + a[10] * b[ 6] + a[14] * b[ 7]; - m[10] = a[ 2] * b[ 8] + a[ 6] * b[ 9] + a[10] * b[10] + a[14] * b[11]; - m[14] = a[ 2] * b[12] + a[ 6] * b[13] + a[10] * b[14] + a[14] * b[15]; - - m[ 3] = a[ 3] * b[ 0] + a[ 7] * b[ 1] + a[11] * b[ 2] + a[15] * b[ 3]; - m[ 7] = a[ 3] * b[ 4] + a[ 7] * b[ 5] + a[11] * b[ 6] + a[15] * b[ 7]; - m[11] = a[ 3] * b[ 8] + a[ 7] * b[ 9] + a[11] * b[10] + a[15] * b[11]; - m[15] = a[ 3] * b[12] + a[ 7] * b[13] + a[11] * b[14] + a[15] * b[15]; - - return m; -} - -mat4 mat4::perspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar) -{ - mat4 m; - - //const float zRange = zNear - zFar; - const float f = 1.f / static_cast(tan(fovy * 0.5f * 0.01745329251994329576923690768489f)); - - m[ 0] = f / aspect; - m[ 5] = f; - - m[10] = -(zFar + zNear) / (zFar - zNear); - m[14] = -(2.f * zFar * zNear) / (zFar - zNear); - - m[11] = -1.f; - m[15] = 0.f; - - return m; -} - -mat4 mat4::lookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez - , GLfloat centerx, GLfloat centery, GLfloat centerz, GLfloat upx, GLfloat upy, GLfloat upz) -{ - vec3 e(eyex, eyey, eyez); - - vec3 f(centerx - eyex, centery - eyey, centerz - eyez); - f.normalize(); - - vec3 u(upx, upy, upz); - u.normalize(); - - vec3 s = crossp(f, u); - u = crossp(s, f); - - mat4 m; - - m[ 0] = s[0]; - m[ 4] = s[1]; - m[ 8] = s[2]; - - m[ 1] = u[0]; - m[ 5] = u[1]; - m[ 9] = u[2]; - - m[ 2] = -f[0]; - m[ 6] = -f[1]; - m[10] = -f[2]; - - m[12] = -dotp(s, e); - m[13] = -dotp(u, e); - m[14] = dotp(f, e); - - return m; -} - -mat4 mat4::translate(GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - m[12] = x; - m[13] = y; - m[14] = z; - - return m; -} - -mat4 mat4::scale(GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - m[ 0] = x; - m[ 5] = y; - m[10] = z; - - return m; -} - -mat4 mat4::rotate(GLfloat a, GLfloat x, GLfloat y, GLfloat z) -{ - mat4 m; - - GLfloat l = 1.f / static_cast(sqrt(x * x + y * y + z * z)); - - x *= l; - y *= l; - z *= l; - - const GLfloat c = static_cast(cos(a)); - const GLfloat s = static_cast(sin(a)); - - const GLfloat d = 1.f - c; - - m[ 0] = x * x * d + c; - m[ 4] = x * y * d - z * s; - m[ 8] = x * z * d + y * s; - - m[ 1] = y * x * d + z * s; - m[ 5] = y * y * d + c; - m[ 9] = y * z * d - x * s; - - m[ 2] = z * x * d - y * s; - m[ 6] = z * y * d + x * s; - m[10] = z * z * d + c; - - return m; -} - -vec3::vec3() -{ - v[0] = v[1] = v[2] = 0.f; -} - -vec3::vec3(GLfloat x, GLfloat y, GLfloat z) -{ - v[0] = x; - v[1] = y; - v[2] = z; -} - -vec3 & vec3::operator+(const vec3 & rhs) -{ - v[0] += rhs[0]; - v[1] += rhs[1]; - v[2] += rhs[2]; - - return *this; -} - -vec3 & vec3::operator-(const vec3 & rhs) -{ - v[0] -= rhs[0]; - v[1] -= rhs[1]; - v[2] -= rhs[2]; - - return *this; -} - -vec3 & vec3::operator*(const vec3 & rhs) -{ - v[0] *= rhs[0]; - v[1] *= rhs[1]; - v[2] *= rhs[2]; - - return *this; -} - -vec3 & vec3::operator/(const vec3 & rhs) -{ - v[0] /= rhs[0]; - v[1] /= rhs[1]; - v[2] /= rhs[2]; - - return *this; -} - -GLfloat vec3::length() -{ - return static_cast(sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])); -} - -void vec3::normalize() -{ - const GLfloat s = 1.f / length(); - - v[0] *= s; - v[1] *= s; - v[2] *= s; -} - -vec3 crossp(const vec3 & a, const vec3 & b) -{ - vec3 v; - - v[0] = a[1] * b[2] - a[2] * b[1]; - v[1] = a[2] * b[0] - a[0] * b[2]; - v[2] = a[0] * b[1] - a[1] * b[0]; - - return v; -} - -GLfloat dotp(const vec3 & a, const vec3 & b) -{ - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; -} diff --git a/source/examples/cubescape/glutils.h b/source/examples/cubescape/glutils.h deleted file mode 100644 index 147971d95..000000000 --- a/source/examples/cubescape/glutils.h +++ /dev/null @@ -1,50 +0,0 @@ - -#pragma once - -#include - -void compile_info(const gl32core::GLuint shader); -void link_info(const gl32core::GLuint program); - -struct mat4 -{ - mat4(); - - inline gl::GLfloat & operator[](const int i) { return m[i]; } - inline const gl::GLfloat & operator[](const int i) const { return m[i]; } - - static mat4 lookAt(gl::GLfloat eyex, gl::GLfloat eyey, gl::GLfloat eyez - , gl::GLfloat centerx, gl::GLfloat centery, gl::GLfloat centerz, gl::GLfloat upx, gl::GLfloat upy, gl::GLfloat upz); - - static mat4 perspective(gl::GLfloat fovy, gl::GLfloat aspect, gl::GLfloat zNear, gl::GLfloat zFar); - - static mat4 translate(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - static mat4 scale(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - static mat4 rotate(gl::GLfloat angle, gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - - gl::GLfloat m[16]; -}; - -mat4 operator*(const mat4 & a, const mat4 & b); - -struct vec3 -{ - vec3(); - vec3(gl::GLfloat x, gl::GLfloat y, gl::GLfloat z); - - inline gl::GLfloat & operator[](const int i) { return v[i]; } - inline const gl::GLfloat & operator[](const int i) const { return v[i]; } - - vec3 & operator+(const vec3 & rhs); - vec3 & operator-(const vec3 & rhs); - vec3 & operator*(const vec3 & rhs); - vec3 & operator/(const vec3 & rhs); - - gl::GLfloat length(); - void normalize(); - - gl::GLfloat v[3]; -}; - -vec3 crossp(const vec3 & a, const vec3 & b); -gl::GLfloat dotp(const vec3 & a, const vec3 & b);