alistair
3 years ago
commit
72340d8a16
7 changed files with 410 additions and 0 deletions
@ -0,0 +1,23 @@
@@ -0,0 +1,23 @@
|
||||
cmake_minimum_required(VERSION 3.18) |
||||
|
||||
# set the project name and version |
||||
project(GLTEST VERSION 0.0.1) |
||||
|
||||
# Let's ensure -std=c++xx instead of -std=g++xx |
||||
set(CMAKE_CXX_EXTENSIONS OFF) |
||||
set(CMAKE_BUILD_TYPE "Debug") |
||||
|
||||
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") |
||||
|
||||
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address") |
||||
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address") |
||||
|
||||
|
||||
add_subdirectory(src) |
||||
|
||||
|
||||
# specify the C++ standard |
||||
set(CMAKE_CXX_STANDARD 17) |
||||
set(CMAKE_CXX_STANDARD_REQUIRED True) |
||||
|
||||
|
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
|
||||
|
||||
find_package(SDL2 REQUIRED) |
||||
find_package(OpenGL REQUIRED) |
||||
find_package(GLEW REQUIRED) |
||||
|
||||
include_directories(${SDL2_INCLUDE_DIRS}) |
||||
include_directories(${OPENGL_INCLUDE_DIR}) |
||||
include_directories(${GLEW_INCLUDE_DIRS}) |
||||
|
||||
file(GLOB testgamefiles |
||||
"*.h" |
||||
"*.cpp" |
||||
) |
||||
|
||||
add_executable(testgame ${testgamefiles}) |
||||
target_link_libraries(testgame ${GLEW_LIBRARIES}) |
||||
target_link_libraries(testgame ${OPENGL_LIBRARIES}) |
||||
target_link_libraries(testgame ${SDL2_LIBRARIES}) |
||||
|
||||
|
||||
add_custom_command( |
||||
TARGET testgame POST_BUILD |
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory |
||||
${CMAKE_SOURCE_DIR}/src/shaders $<TARGET_FILE_DIR:testgame>/shaders |
||||
COMMENT "Copying shaders" VERBATIM |
||||
) |
@ -0,0 +1,246 @@
@@ -0,0 +1,246 @@
|
||||
#include <SDL2/SDL.h> |
||||
//Using SDL, SDL OpenGL, standard IO, and, strings
|
||||
|
||||
#include <GL/glew.h> |
||||
#include <SDL2/SDL_opengl.h> |
||||
#include <GL/glu.h> |
||||
#include <string> |
||||
#include <iostream> |
||||
#include "shaders.h" |
||||
#include <stdio.h> |
||||
|
||||
class SDLGLGLWindow { |
||||
|
||||
GLuint gProgramID = 0; |
||||
GLint gVertexPos2DLocation = -1; |
||||
GLuint gVBO = 0; |
||||
GLuint gIBO = 0; |
||||
|
||||
public: |
||||
int screen_width; |
||||
int screen_height; |
||||
SDL_Window *gWindow; |
||||
|
||||
|
||||
SDLGLGLWindow(int width, int height) { |
||||
screen_width = width; |
||||
screen_height = height; |
||||
|
||||
if (!create_gl_window()) { |
||||
exit(1); |
||||
} |
||||
} |
||||
|
||||
int create_gl_window() { |
||||
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 ); |
||||
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 ); |
||||
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK,
|
||||
SDL_GL_CONTEXT_PROFILE_CORE ); |
||||
|
||||
|
||||
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height,
|
||||
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN ); |
||||
|
||||
auto gContext = SDL_GL_CreateContext( gWindow ); |
||||
if( gContext == NULL ) { |
||||
std::cerr << "OpenGL context could not be created! SDL Error: "
|
||||
<< SDL_GetError() << std::endl ; |
||||
return false; |
||||
} |
||||
glewExperimental = GL_TRUE;
|
||||
GLenum glewError = glewInit(); |
||||
if( glewError != GLEW_OK ) |
||||
{ |
||||
std::cerr << "Error initializing GLEW! "
|
||||
<< glewGetErrorString( glewError ) << std::endl; |
||||
} |
||||
|
||||
//Use Vsync
|
||||
if( SDL_GL_SetSwapInterval( 1 ) < 0 ) |
||||
{ |
||||
std::cerr << "Warning: Unable to set VSync! SDL Error: "
|
||||
<< SDL_GetError() << std::endl; |
||||
} |
||||
|
||||
//Initialize OpenGL
|
||||
if( !initGL() ) |
||||
{ |
||||
std::cerr << "Unable to initialie OpenGL!" << std::endl; |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
|
||||
void printProgramLog( GLuint program ) |
||||
{ |
||||
//Make sure name is shader
|
||||
if( glIsProgram( program ) ) |
||||
{ |
||||
//Program log length
|
||||
int infoLogLength = 0; |
||||
int maxLength = infoLogLength; |
||||
|
||||
//Get info string length
|
||||
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &maxLength ); |
||||
|
||||
//Allocate string
|
||||
char* infoLog = new char[ maxLength ]; |
||||
|
||||
//Get info log
|
||||
glGetProgramInfoLog( program, maxLength, &infoLogLength, infoLog ); |
||||
if( infoLogLength > 0 ) |
||||
{ |
||||
//Print Log
|
||||
printf( "%s\n", infoLog ); |
||||
} |
||||
|
||||
//Deallocate string
|
||||
delete[] infoLog; |
||||
} |
||||
else |
||||
{ |
||||
printf( "Name %d is not a program\n", program ); |
||||
} |
||||
} |
||||
|
||||
int initGL() { |
||||
//Success flag
|
||||
bool success = true; |
||||
//Generate program
|
||||
gProgramID = glCreateProgram(); |
||||
|
||||
Shader vertex {GL_VERTEX_SHADER, gProgramID, "src/shaders/test.vert"}; |
||||
Shader fragment {GL_FRAGMENT_SHADER, gProgramID, "src/shaders/test.frag"}; |
||||
|
||||
//Link program
|
||||
glLinkProgram( gProgramID ); |
||||
|
||||
//Check for errors
|
||||
GLint programSuccess = GL_TRUE; |
||||
glGetProgramiv( gProgramID, GL_LINK_STATUS, &programSuccess ); |
||||
if( programSuccess != GL_TRUE ) |
||||
{ |
||||
printf( "Error linking program %d!\n", gProgramID ); |
||||
printProgramLog(gProgramID); |
||||
return false; |
||||
} |
||||
|
||||
gVertexPos2DLocation = glGetAttribLocation( gProgramID, "LVertexPos2D" ); |
||||
if( gVertexPos2DLocation == -1 ) |
||||
{ |
||||
printf( "LVertexPos2D is not a valid glsl program variable!\n" ); |
||||
return false; |
||||
} |
||||
|
||||
|
||||
//Initialize clear color
|
||||
glClearColor( 0.f, 0.f, 0.f, 1.f ); |
||||
|
||||
//VBO data
|
||||
GLfloat vertexData[] = |
||||
{ |
||||
-0.5f, -0.5f, |
||||
0.5f, -0.5f, |
||||
0.5f, 0.5f, |
||||
-0.5f, 0.5f |
||||
}; |
||||
|
||||
//IBO data
|
||||
GLuint indexData[] = { 0, 1, 2, 3 }; |
||||
|
||||
//Create VBO
|
||||
glGenBuffers( 1, &gVBO ); |
||||
glBindBuffer( GL_ARRAY_BUFFER, gVBO ); |
||||
glBufferData( GL_ARRAY_BUFFER, 2 * 4 * sizeof(GLfloat), vertexData, GL_STATIC_DRAW ); |
||||
|
||||
//Create IBO
|
||||
glGenBuffers( 1, &gIBO ); |
||||
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, gIBO ); |
||||
glBufferData( GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLuint), indexData, GL_STATIC_DRAW ); |
||||
|
||||
return success; |
||||
} |
||||
|
||||
|
||||
|
||||
void render() |
||||
{ |
||||
//Clear color buffer
|
||||
glClear( GL_COLOR_BUFFER_BIT ); |
||||
|
||||
//Bind program
|
||||
glUseProgram( gProgramID ); |
||||
|
||||
//Enable vertex position
|
||||
glEnableVertexAttribArray( gVertexPos2DLocation ); |
||||
|
||||
//Set vertex data
|
||||
glBindBuffer( GL_ARRAY_BUFFER, gVBO ); |
||||
glVertexAttribPointer( gVertexPos2DLocation, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), NULL ); |
||||
|
||||
//Set index data and render
|
||||
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, gIBO ); |
||||
glDrawElements( GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, NULL ); |
||||
|
||||
//Disable vertex position
|
||||
glDisableVertexAttribArray( gVertexPos2DLocation ); |
||||
|
||||
//Unbind program
|
||||
glUseProgram( NULL ); |
||||
} |
||||
|
||||
|
||||
void
|
||||
swap_window()
|
||||
{ |
||||
SDL_GL_SwapWindow(gWindow); |
||||
} |
||||
|
||||
~SDLGLGLWindow() { |
||||
//Deallocate program
|
||||
glDeleteProgram( gProgramID ); |
||||
|
||||
//Destroy window
|
||||
SDL_DestroyWindow( gWindow ); |
||||
gWindow = NULL; |
||||
|
||||
//Quit SDL subsystems
|
||||
SDL_Quit(); |
||||
} |
||||
}; |
||||
|
||||
int main(int argc, char **argv) { |
||||
SDLGLGLWindow cont {640, 480}; |
||||
|
||||
bool quit = false; |
||||
|
||||
//Event handler
|
||||
SDL_Event e; |
||||
|
||||
//Enable text input
|
||||
SDL_StartTextInput(); |
||||
|
||||
//While application is running
|
||||
while( !quit ) |
||||
{ |
||||
//Handle events on queue
|
||||
while( SDL_PollEvent( &e ) != 0 ) |
||||
{ |
||||
//User requests quit
|
||||
if( e.type == SDL_QUIT ) |
||||
{ |
||||
quit = true; |
||||
} |
||||
} |
||||
|
||||
//Render quad
|
||||
|
||||
cont.render(); |
||||
//Update screen
|
||||
cont.swap_window(); |
||||
} |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
#include "shaders.h" |
||||
#include <fstream> |
||||
#include <vector> |
||||
|
||||
|
||||
bool Shader::handle_error()
|
||||
{ |
||||
GLint fShaderCompiled = GL_FALSE; |
||||
glGetShaderiv( shader, GL_COMPILE_STATUS, &fShaderCompiled ); |
||||
if( fShaderCompiled == GL_TRUE ) { |
||||
return false; |
||||
} |
||||
|
||||
int maxLength = 0; |
||||
|
||||
//Get info string length
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); |
||||
|
||||
std::vector<GLchar> err_log(maxLength); |
||||
//Get info log
|
||||
glGetShaderInfoLog( shader, maxLength, &maxLength, &err_log[0]); |
||||
|
||||
std::cerr << (char *)(&err_log[0]) << std::endl; |
||||
|
||||
return true; |
||||
} |
||||
|
||||
Shader::Shader(GLuint type, std::string filename) |
||||
: type{type}
|
||||
{ |
||||
|
||||
shader = glCreateShader(type); |
||||
|
||||
// read source
|
||||
std::ifstream source_file(filename, std::fstream::in); |
||||
if (source_file.is_open()) { |
||||
source = std::string(std::istreambuf_iterator<char>(source_file),
|
||||
std::istreambuf_iterator<char>()); |
||||
} |
||||
|
||||
if (!source_file.is_open() || source.size() == 0) { |
||||
throw std::exception(); |
||||
} |
||||
|
||||
|
||||
const char *cstr = source.c_str(); |
||||
glShaderSource(shader, 1, &cstr, NULL); |
||||
|
||||
|
||||
glCompileShader(shader); |
||||
|
||||
if (handle_error()) { |
||||
// compilation failed
|
||||
glDeleteShader(shader); |
||||
} |
||||
} |
||||
|
||||
Shader::Shader(GLuint type, GLuint programID, std::string filename):
|
||||
Shader(type, filename)
|
||||
{ |
||||
this->program = programID; |
||||
attach(programID); |
||||
} |
||||
|
||||
void Shader::attach(GLuint program) { |
||||
glAttachShader(program, shader); |
||||
} |
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
|
||||
#ifndef G_SHADERSH |
||||
#define G_SHADERSH |
||||
|
||||
#include <SDL2/SDL.h> |
||||
//Using SDL, SDL OpenGL, standard IO, and, strings
|
||||
|
||||
#include <GL/glew.h> |
||||
#include <SDL2/SDL_opengl.h> |
||||
#include <GL/glu.h> |
||||
#include <string> |
||||
#include <iostream> |
||||
|
||||
class Shader { |
||||
|
||||
GLuint program = 0; |
||||
std::string source; |
||||
bool handle_error(); |
||||
|
||||
public: |
||||
GLuint shader; |
||||
GLuint type; |
||||
|
||||
Shader(GLuint type, std::string filename); |
||||
|
||||
Shader(GLuint type, GLuint programID, std::string filename); |
||||
|
||||
void attach(GLuint programID); |
||||
|
||||
}; |
||||
#endif |
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
#version 140 |
||||
|
||||
out vec4 LFragment; |
||||
|
||||
void main() |
||||
{ |
||||
LFragment = vec4( 1.0, 1.0, 1.0, 1.0 ); |
||||
} |
Loading…
Reference in new issue