Add base structure for openGL playground

This commit is contained in:
Mario Hüttel 2020-03-17 23:25:23 +01:00
commit ec17d18154
9 changed files with 372 additions and 0 deletions

76
.gitignore vendored Normal file
View File

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

20
CMakeLists.txt Normal file
View File

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

View File

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

View File

@ -0,0 +1,23 @@
#ifndef OPENGL_SHADER_H
#define OPENGL_SHADER_H
#include <string>
#include <epoxy/gl.h>
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

View File

@ -0,0 +1,23 @@
#ifndef SDL_MAINWINDOW_H
#define SDL_MAINWINDOW_H
#include <opengl-playground/mainwindow.hpp>
#include <SDL2/SDL.h>
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

29
src/main.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
#include <opengl-playground/sdlmainwindow.hpp>
#include <epoxy/gl.h>
#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;
}

11
src/mainwindow.cpp Normal file
View File

@ -0,0 +1,11 @@
#include <opengl-playground/mainwindow.hpp>
MainWindow::MainWindow()
{
}
void MainWindow::swapBuffer()
{
/* Do nothing */
}

138
src/openglshader.cpp Normal file
View File

@ -0,0 +1,138 @@
#include <opengl-playground/openglshader.hpp>
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;
}

36
src/sdlmainwindow.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <opengl-playground/sdlmainwindow.hpp>
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);
}