From 8de34bc077e46235a5d50ff23b0ae3828dbd0868 Mon Sep 17 00:00:00 2001 From: allem Date: Mon, 10 Dec 2018 20:18:46 -0500 Subject: [PATCH] better project structure --- .gitignore | 150 ++++++++++++++++++++++++++++++++++++++ .gitmodules | 10 +++ .idea/gl-template.iml | 2 + CMakeLists.txt | 18 +++++ framework/CMakeLists.txt | 16 ++++ framework/include/hello.h | 10 +++ framework/src/hello.cpp | 24 ++++++ glapp/CMakeLists.txt | 9 +++ glapp/src/main.cpp | 149 +++++++++++++++++++++++++++++++++++++ vendor/glad | 1 + vendor/glfw | 1 + vendor/glm | 1 + 12 files changed, 391 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 .idea/gl-template.iml create mode 100644 CMakeLists.txt create mode 100644 framework/CMakeLists.txt create mode 100644 framework/include/hello.h create mode 100644 framework/src/hello.cpp create mode 100644 glapp/CMakeLists.txt create mode 100644 glapp/src/main.cpp create mode 160000 vendor/glad create mode 160000 vendor/glfw create mode 160000 vendor/glm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aed3007 --- /dev/null +++ b/.gitignore @@ -0,0 +1,150 @@ +# Created by .ignore support plugin (hsz.mobi) +### C++ template +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/*.xml +.idea/**/dictionaries + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ +cmake-build-release/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests +### C template +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf +### CMake template +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake + +.name diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..4b4c1d6 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,10 @@ +[submodule "vendor/glfw"] + path = vendor/glfw + url = https://github.com/glfw/glfw.git +[submodule "vendor/glad"] + path = vendor/glad + url = https://github.com/Dav1dde/glad.git + branch = c +[submodule "vendor/glm"] + path = vendor/glm + url = https://github.com/g-truc/glm.git diff --git a/.idea/gl-template.iml b/.idea/gl-template.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/gl-template.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..11e5d8a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.13) +project(gl_template) + +set(CMAKE_CXX_STANDARD 17) + +option(GLFW_BUILD_DOCS OFF) +option(GLFW_BUILD_EXAMPLES OFF) +option(GLFW_BUILD_TESTS OFF) +add_subdirectory(vendor/glfw) + +option(GLM_TEST_ENABLE OFF) +add_subdirectory(vendor/glm) + +add_library(glad vendor/glad/src/glad.c) +target_include_directories(glad PUBLIC vendor/glad/include) + +add_subdirectory(framework) +add_subdirectory(glapp) diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt new file mode 100644 index 0000000..93a07fd --- /dev/null +++ b/framework/CMakeLists.txt @@ -0,0 +1,16 @@ +project(framework) + +add_library(framework + src/hello.cpp) + +target_link_libraries(framework + glfw) + +target_include_directories(framework + PUBLIC + $ + $ + PRIVATE + src) + +export(TARGETS framework glfw FILE FrameworkConfig.cmake) \ No newline at end of file diff --git a/framework/include/hello.h b/framework/include/hello.h new file mode 100644 index 0000000..f928c68 --- /dev/null +++ b/framework/include/hello.h @@ -0,0 +1,10 @@ +#ifndef GL_TEMPLATE_HELLO_H +#define GL_TEMPLATE_HELLO_H + +#include + +void hello(); + +int showWindow(const std::string &title); + +#endif //GL_TEMPLATE_HELLO_H diff --git a/framework/src/hello.cpp b/framework/src/hello.cpp new file mode 100644 index 0000000..c9c20d4 --- /dev/null +++ b/framework/src/hello.cpp @@ -0,0 +1,24 @@ +#include "hello.h" + +#include + +#include +#include + +void hello() { + printf("Hello, World!\n"); +} + +int showWindow(const std::string &title) { + if (!glfwInit()) return EXIT_FAILURE; + + GLFWwindow *window = glfwCreateWindow(1280, 720, title.c_str(), nullptr, nullptr); + + while (!glfwWindowShouldClose(window)) { + glfwPollEvents(); + } + + glfwDestroyWindow(window); + glfwTerminate(); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/glapp/CMakeLists.txt b/glapp/CMakeLists.txt new file mode 100644 index 0000000..da2ecf3 --- /dev/null +++ b/glapp/CMakeLists.txt @@ -0,0 +1,9 @@ +project(sample) + +add_executable(sample + src/main.cpp) + +target_link_libraries(sample + glad + glm + framework) diff --git a/glapp/src/main.cpp b/glapp/src/main.cpp new file mode 100644 index 0000000..4cdb5c5 --- /dev/null +++ b/glapp/src/main.cpp @@ -0,0 +1,149 @@ +#include + +#include +#include + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +struct Vertex { + glm::vec2 pos; + glm::vec3 col; +}; +static const char *vertex_shader_text = + "uniform mat4 pvm;\n" + "attribute vec3 vCol;\n" + "attribute vec2 vPos;\n" + "varying vec3 color;\n" + "void main()\n" + "{\n" + " gl_Position = pvm * vec4(vPos, 0.0, 1.0);\n" + " color = vCol;\n" + "}\n"; + + +static const char *fragment_shader_text = + "varying vec3 color;\n" + "void main()\n" + "{\n" + " gl_FragColor = vec4(color, 1.0);\n" + "}\n"; + + +static void error_callback(int error, const char *description) { + fprintf(stderr, "Error: %s\n", description); +} + + +static void key_callback(GLFWwindow *window, int key, int scan_code, int action, int mods) { + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) + glfwSetWindowShouldClose(window, GLFW_TRUE); +} + + +int main() { + hello(); + + std::vector vertices = { + {glm::vec2(+0.5f, +0.5f), glm::vec3(1, 0, 0)}, + {glm::vec2(+0.5f, -0.5f), glm::vec3(0, 1, 0)}, + {glm::vec2(-0.5f, -0.5f), glm::vec3(0, 0, 1)}, + {glm::vec2(-0.5f, +0.5f), glm::vec3(1, 1, 1)}, + }; + + const int i_width = 1280, i_height = 720; + + GLFWwindow *window; + GLFWmonitor *monitor; + const GLFWvidmode *mode; + GLuint vertex_buffer, vertex_shader, fragment_shader, program; + GLuint pvm_location, vpos_location, vcol_location; + + glfwSetErrorCallback(error_callback); + + if (!glfwInit()) { + exit(EXIT_FAILURE); + } + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + + window = glfwCreateWindow(i_width, i_height, "Hello Triangle", nullptr, nullptr); + if (!window) { + glfwTerminate(); + exit(EXIT_FAILURE); + } + monitor = glfwGetPrimaryMonitor(); + mode = glfwGetVideoMode(monitor); + glfwSetWindowPos(window, (mode->width - i_width) / 2, (mode->height - i_height) / 2); + + glfwSetKeyCallback(window, key_callback); + glfwMakeContextCurrent(window); + gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); + glfwSwapInterval(1); + + // NOTE: OpenGL error checks have been omitted for brevity + glGenBuffers(1, &vertex_buffer); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices.front(), GL_STATIC_DRAW); + + vertex_shader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex_shader, 1, &vertex_shader_text, nullptr); + glCompileShader(vertex_shader); + + fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment_shader, 1, &fragment_shader_text, nullptr); + glCompileShader(fragment_shader); + + program = glCreateProgram(); + glAttachShader(program, vertex_shader); + glAttachShader(program, fragment_shader); + glLinkProgram(program); + + pvm_location = (GLuint) glGetUniformLocation(program, "pvm"); + vpos_location = (GLuint) glGetAttribLocation(program, "vPos"); + vcol_location = (GLuint) glGetAttribLocation(program, "vCol"); + + glEnableVertexAttribArray(vpos_location); + glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) nullptr); + + glEnableVertexAttribArray(vcol_location); + glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) (sizeof(float) * 2)); + + while (!glfwWindowShouldClose(window)) { + int width, height; + float ratio; + + glfwGetFramebufferSize(window, &width, &height); + ratio = (float) width / height; + glViewport(0, 0, width, height); + + glClear(GL_COLOR_BUFFER_BIT); + + + auto pvm = glm::ortho(-ratio, ratio, -1.f, 1.f); + pvm = glm::rotate(pvm, (float) glfwGetTime(), glm::vec3(0.f, 0.f, 1.f)); + pvm = glm::scale(pvm, glm::vec3(0.9f)); + + glUseProgram(program); + glUniformMatrix4fv(pvm_location, 1, GL_FALSE, glm::value_ptr(pvm)); + + glDrawArrays(GL_QUADS, 0, (GLsizei) vertices.size()); + + glfwSwapBuffers(window); + glfwPollEvents(); + } + + glfwDestroyWindow(window); + glfwTerminate(); + exit(EXIT_SUCCESS); +} diff --git a/vendor/glad b/vendor/glad new file mode 160000 index 0000000..5bf3eda --- /dev/null +++ b/vendor/glad @@ -0,0 +1 @@ +Subproject commit 5bf3eda6da606324999775b88a90ed572202be93 diff --git a/vendor/glfw b/vendor/glfw new file mode 160000 index 0000000..f9923e9 --- /dev/null +++ b/vendor/glfw @@ -0,0 +1 @@ +Subproject commit f9923e90958e726aaabc86d83fb3681216d76067 diff --git a/vendor/glm b/vendor/glm new file mode 160000 index 0000000..437a131 --- /dev/null +++ b/vendor/glm @@ -0,0 +1 @@ +Subproject commit 437a131adb3205b20abb5432eb705b36abb8b51f