commit ec17d181543a17325cfe5b51d4cc6bd1cd407268 Author: Mario Hüttel Date: Tue Mar 17 23:25:23 2020 +0100 Add base structure for openGL playground diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c675b52 --- /dev/null +++ b/.gitignore @@ -0,0 +1,76 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + +*.user +*.user* +*.buildconfig diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4ab37e4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +project(opengl-playground) + +#set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") + +cmake_minimum_required(VERSION 2.8) +find_package(PkgConfig REQUIRED) +pkg_check_modules(EPOXY REQUIRED epoxy) +pkg_check_modules(SDL2 REQUIRED sdl2) + +#find_package(OpenCL REQUIRED) + +include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include" ${EPOXY_INCLUDE_DIRS} ${SDL2_INCLUDE_DIRS}) + +aux_source_directory("src" SOURCES) +add_compile_options(-Wall) + +add_executable(${PROJECT_NAME} ${SOURCES}) +target_link_libraries(${PROJECT_NAME} m pthread ${EPOXY_LIBRARIES} ${SDL2_LIBRARIES}) +install (TARGETS ${PROJECT_NAME} DESTINATION bin) + diff --git a/include/opengl-playground/mainwindow.hpp b/include/opengl-playground/mainwindow.hpp new file mode 100644 index 0000000..bd6f3b4 --- /dev/null +++ b/include/opengl-playground/mainwindow.hpp @@ -0,0 +1,16 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + + +class MainWindow +{ + public: + MainWindow(); + virtual ~MainWindow() = default; + void virtual show() = 0; + void virtual activateGlContext() = 0; + void virtual swapBuffer(); + +}; + +#endif // MAINWINDOW_H diff --git a/include/opengl-playground/openglshader.hpp b/include/opengl-playground/openglshader.hpp new file mode 100644 index 0000000..8dafe4c --- /dev/null +++ b/include/opengl-playground/openglshader.hpp @@ -0,0 +1,23 @@ +#ifndef OPENGL_SHADER_H +#define OPENGL_SHADER_H + +#include +#include + +class OpenGlShaderProgram +{ + public: + OpenGlShaderProgram(std::string vertex_file, std::string geometry_file, std::string fragment_file); + ~OpenGlShaderProgram(); + int compile(); + void unload(); + int use(); + private: + bool compiled; + GLuint shader_program; + std::string vertex_file; + std::string geometry_file; + std::string fragment_file; +}; + +#endif // OPENGL_SHADER_H diff --git a/include/opengl-playground/sdlmainwindow.hpp b/include/opengl-playground/sdlmainwindow.hpp new file mode 100644 index 0000000..a54f0b9 --- /dev/null +++ b/include/opengl-playground/sdlmainwindow.hpp @@ -0,0 +1,23 @@ +#ifndef SDL_MAINWINDOW_H +#define SDL_MAINWINDOW_H + +#include +#include + +class SdlMainWindow : public MainWindow +{ + public: + SdlMainWindow(int height, int width); + ~SdlMainWindow(); + void show(); + void swapBuffer(); + void activateGlContext(); + private: + SDL_Window *window; + SDL_GLContext context; + int width; + int height; + +}; + +#endif // SDL_MAINWINDOW_H diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..2a0eb3b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 640 + +int main(int argc, char **argv) +{ + (void)argc; + (void)argv; + MainWindow *window; + + window = new SdlMainWindow(WINDOW_WIDTH, WINDOW_HEIGHT); + + window->show(); + window->activateGlContext(); + + /* Clear the image and set the background to a nice gray */ + glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); + glClearColor(0.1f, 0.1f, 0.1f, 0.f); + glClear(GL_COLOR_BUFFER_BIT); + + window->swapBuffer(); + + SDL_Delay(5000); + + delete window; +} diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp new file mode 100644 index 0000000..71f8853 --- /dev/null +++ b/src/mainwindow.cpp @@ -0,0 +1,11 @@ +#include + +MainWindow::MainWindow() +{ + +} + +void MainWindow::swapBuffer() +{ + /* Do nothing */ +} diff --git a/src/openglshader.cpp b/src/openglshader.cpp new file mode 100644 index 0000000..953db1d --- /dev/null +++ b/src/openglshader.cpp @@ -0,0 +1,138 @@ +#include + +OpenGlShaderProgram::OpenGlShaderProgram(std::string vertex_file, std::string geometry_file, std::string fragment_file) +{ + this->vertex_file = vertex_file; + this->geometry_file = geometry_file; + this->fragment_file = fragment_file; + this->compiled = false; +} + +char *load_shader_program_from_file(std::string file_name) +{ + return NULL; +} + +int OpenGlShaderProgram::compile() +{ + int ret_code = 0; + int success; + char info_log[512]; + unsigned int vertex_shader, geometry_shader, fragment_shader; + bool vertex_b = false, geo_b = false, frag_b = false; + char *src_code; + + if (this->compiled) { + glDeleteProgram(shader_program); + this->compiled = false; + } + + this->shader_program = glCreateProgram(); + + if (!vertex_file.empty()) { + vertex_b = true; + + /* Compile the vertex shader */ + vertex_shader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex_shader, 1, &src_code, NULL); + + glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success); + free(src_code); + + if (!success) { + glGetShaderInfoLog(vertex_shader, 512, NULL, info_log); + fprintf(stderr, "%s\n", info_log); + ret_code = -1; + goto delete_vertex; + } + + glAttachShader(this->shader_program, vertex_shader); + } + + if (!this->geometry_file.empty()) { + geo_b = true; + + /* Compile the vertex shader */ + geometry_shader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(geometry_shader, 1, &src_code, NULL); + + glGetShaderiv(geometry_shader, GL_COMPILE_STATUS, &success); + free(src_code); + + if (!success) { + glGetShaderInfoLog(geometry_shader, 512, NULL, info_log); + fprintf(stderr, "%s\n", info_log); + ret_code = -1; + goto delete_geo; + } + + glAttachShader(this->shader_program, geometry_shader); + + } + + if (!this->fragment_file.empty()) { + frag_b = true; + + /* Compile the vertex shader */ + fragment_shader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(fragment_shader, 1, &src_code, NULL); + + glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success); + free(src_code); + + if (!success) { + glGetShaderInfoLog(fragment_shader, 512, NULL, info_log); + fprintf(stderr, "%s\n", info_log); + ret_code = -1; + goto delete_frag; + } + + glAttachShader(this->shader_program, fragment_shader); + } + + glLinkProgram(this->shader_program); + + glGetShaderiv(this->shader_program, GL_LINK_STATUS, &success); + if (!success) { + glGetShaderInfoLog(this->shader_program, 512, NULL, info_log); + printf("%s\n", info_log); + ret_code = -2; + } + + this->compiled = true; + +delete_frag: + if (frag_b) + glDeleteShader(fragment_shader); +delete_geo: + if (geo_b) + glDeleteShader(geometry_shader); +delete_vertex: + if (vertex_b) + glDeleteShader(vertex_shader); + + if (ret_code) { + this->compiled = false; + glDeleteProgram(this->shader_program); + this->shader_program = 0; + } + return ret_code; +} + +void OpenGlShaderProgram::unload() +{ + if (this->compiled) { + glDeleteProgram(this->shader_program); + } + + this->compiled = false; +} + +int OpenGlShaderProgram::use() +{ + if (!this->compiled) + return -1; + + glUseProgram(this->shader_program); + return 0; +} diff --git a/src/sdlmainwindow.cpp b/src/sdlmainwindow.cpp new file mode 100644 index 0000000..0bf1282 --- /dev/null +++ b/src/sdlmainwindow.cpp @@ -0,0 +1,36 @@ +#include + +SdlMainWindow::SdlMainWindow(int height, int width) : MainWindow() +{ + this->width = width; + this->height = height; + this->window = NULL; + this->context = NULL; +} + +SdlMainWindow::~SdlMainWindow() +{ + if (this->window) + SDL_DestroyWindow(window); +} + +void SdlMainWindow::show() +{ + this->window = SDL_CreateWindow("OpenGL Playground", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + this->width, this->height, SDL_WINDOW_OPENGL); + return; +} + +void SdlMainWindow::swapBuffer() +{ + if (this->window) + SDL_GL_SwapWindow(this->window); +} + +void SdlMainWindow::activateGlContext() +{ + if (this->context == NULL) + this->context = SDL_GL_CreateContext(this->window); + else + SDL_GL_MakeCurrent(this->window, this->context); +}