Add base structure for openGL playground

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

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);
}