alistair
3 years ago
9 changed files with 141 additions and 615 deletions
@ -1,254 +0,0 @@
@@ -1,254 +0,0 @@
|
||||
#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 <stdio.h> |
||||
|
||||
#include "shaders.h" |
||||
|
||||
|
||||
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 ); |
||||
} |
||||
} |
||||
|
||||
|
||||
class SDLGLGLWindow { |
||||
|
||||
GLuint gProgramID = 0; |
||||
GLint gVertexPos2DLocation = -1; |
||||
/* array buffers */ |
||||
GLuint gVBO = 0; |
||||
GLuint gIBO = 0; |
||||
GLuint gVAO = 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; |
||||
} |
||||
|
||||
|
||||
|
||||
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; |
||||
} |
||||
|
||||
|
||||
|
||||
//VBO data |
||||
GLfloat vertexData[] = |
||||
{ |
||||
-0.5f, -0.5f, |
||||
0.5f, -0.5f, |
||||
0.5f, 0.5f, |
||||
-0.5f, 0.2f |
||||
}; |
||||
|
||||
//IBO data |
||||
GLuint indexData[] = { 0, 1, 2, 3 }; |
||||
|
||||
//Create VBO |
||||
glGenBuffers( 1, &gVBO ); |
||||
glBindBuffer( GL_ARRAY_BUFFER, gVBO ); |
||||
glBufferData( GL_ARRAY_BUFFER, sizeof vertexData, vertexData, GL_STATIC_DRAW ); |
||||
|
||||
|
||||
//Create IBO |
||||
glGenBuffers( 1, &gIBO ); |
||||
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, gIBO ); |
||||
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof indexData, indexData, GL_STATIC_DRAW ); |
||||
|
||||
return success; |
||||
} |
||||
|
||||
|
||||
|
||||
void render() |
||||
{ |
||||
//Initialize clear color |
||||
glClearColor( 0.4f, 0.2f, 0.1f, 1.f ); |
||||
//Clear color buffer |
||||
glClear( GL_COLOR_BUFFER_BIT ); |
||||
|
||||
//Bind program |
||||
glUseProgram( gProgramID ); |
||||
|
||||
|
||||
//Set vertex data |
||||
glBindBuffer( GL_ARRAY_BUFFER, gVBO ); |
||||
glVertexAttribPointer( gVertexPos2DLocation, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), NULL); |
||||
|
||||
//Enable vertex position |
||||
glEnableVertexAttribArray( gVertexPos2DLocation ); |
||||
|
||||
//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; |
||||
} |
@ -1,267 +0,0 @@
@@ -1,267 +0,0 @@
|
||||
#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 <stdio.h> |
||||
|
||||
#include "shaders.h" |
||||
|
||||
|
||||
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 ); |
||||
} |
||||
} |
||||
|
||||
|
||||
class SDLGLGLWindow { |
||||
|
||||
GLuint gProgramID = 0; |
||||
GLint gVertexPos2DLocation = -1; |
||||
/* array buffers */ |
||||
GLuint gVBO = 0; |
||||
GLuint gIBO = 0; |
||||
GLuint gVAO = 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; |
||||
} |
||||
|
||||
|
||||
|
||||
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 }; |
||||
|
||||
glGenVertexArrays(1, &gVAO); |
||||
glGenBuffers( 1, &gVBO ); |
||||
|
||||
glBindVertexArray(gVAO); |
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, gVBO); |
||||
glBufferData( GL_ARRAY_BUFFER, sizeof vertexData, vertexData, GL_STATIC_DRAW ); |
||||
|
||||
|
||||
glVertexAttribPointer( gVertexPos2DLocation, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), NULL); |
||||
glEnableVertexAttribArray( gVAO); |
||||
|
||||
// unbind |
||||
glBindBuffer(GL_ARRAY_BUFFER, 0); |
||||
glBindVertexArray(0); |
||||
|
||||
//Create IBO |
||||
glGenBuffers( 1, &gIBO ); |
||||
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, gIBO ); |
||||
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof indexData, indexData, GL_STATIC_DRAW ); |
||||
|
||||
printProgramLog(gProgramID); |
||||
return success; |
||||
} |
||||
|
||||
|
||||
|
||||
void render() |
||||
{ |
||||
//Clear color buffer |
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); |
||||
|
||||
//Bind program |
||||
glUseProgram( gProgramID ); |
||||
|
||||
|
||||
//Set vertex data |
||||
glBindVertexArray(gVAO); |
||||
|
||||
// glBindBuffer( GL_ARRAY_BUFFER, gVBO ); |
||||
|
||||
//Enable vertex position |
||||
|
||||
//Set index data and render |
||||
|
||||
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, gIBO ); |
||||
glDrawElements( GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, NULL ); |
||||
// glDrawArrays(GL_TRIANGLES, 0, 3); |
||||
|
||||
//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; |
||||
} |
Loading…
Reference in new issue