better project structure

This commit is contained in:
2018-12-10 20:18:46 -05:00
commit 8de34bc077
12 changed files with 391 additions and 0 deletions

150
.gitignore vendored Normal file
View File

@@ -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

10
.gitmodules vendored Normal file
View File

@@ -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

2
.idea/gl-template.iml generated Normal file
View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

18
CMakeLists.txt Normal file
View File

@@ -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)

16
framework/CMakeLists.txt Normal file
View File

@@ -0,0 +1,16 @@
project(framework)
add_library(framework
src/hello.cpp)
target_link_libraries(framework
glfw)
target_include_directories(framework
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE
src)
export(TARGETS framework glfw FILE FrameworkConfig.cmake)

10
framework/include/hello.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef GL_TEMPLATE_HELLO_H
#define GL_TEMPLATE_HELLO_H
#include <string>
void hello();
int showWindow(const std::string &title);
#endif //GL_TEMPLATE_HELLO_H

24
framework/src/hello.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include "hello.h"
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <string>
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;
}

9
glapp/CMakeLists.txt Normal file
View File

@@ -0,0 +1,9 @@
project(sample)
add_executable(sample
src/main.cpp)
target_link_libraries(sample
glad
glm
framework)

149
glapp/src/main.cpp Normal file
View File

@@ -0,0 +1,149 @@
#include <hello.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>
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<Vertex> 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);
}

1
vendor/glad vendored Submodule

Submodule vendor/glad added at 5bf3eda6da

1
vendor/glfw vendored Submodule

Submodule vendor/glfw added at f9923e9095

1
vendor/glm vendored Submodule

Submodule vendor/glm added at 437a131adb