Last active
August 29, 2015 13:56
-
-
Save dmikis/8953030 to your computer and use it in GitHub Desktop.
OpenGL 3.2 + SDL2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <fstream> | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
#include <gl3.h> | |
#define SDL_MAIN_HANDLED | |
#include <SDL.h> | |
namespace gfx { | |
const char * WINDOW_TITLE = "GFX"; | |
const int WINDOW_WIDTH = 640; // px | |
const int WINDOW_HEIGHT = 480; // px | |
const float VERTICES_POSITIONS[] = { | |
-1.0f, 1.0f, 0.0f, 1.0f, | |
-1.0f, -1.0f, 0.0f, 1.0f, | |
1.0f, 1.0f, 0.0f, 1.0f, | |
1.0f, -1.0f, 0.0f, 1.0f | |
}; | |
const float VERTICES_COLORS[] = { | |
1.0f, 0.0f, 0.0f, | |
0.0f, 1.0f, 0.0f, | |
0.0f, 0.0f, 1.0f, | |
1.0f, 1.0f, 1.0f | |
}; | |
#define GFX_UNUSED(x) ((void) (x)) | |
std::string readShaderSource(const std::string & filename); | |
int main(int argc, char * argv[]) | |
{ | |
GFX_UNUSED(argc); | |
GFX_UNUSED(argv); | |
SDL_SetMainReady(); | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
SDL_Window * window = SDL_CreateWindow( | |
WINDOW_TITLE, | |
SDL_WINDOWPOS_UNDEFINED, | |
SDL_WINDOWPOS_UNDEFINED, | |
WINDOW_WIDTH, | |
WINDOW_HEIGHT, | |
SDL_WINDOW_OPENGL | |
); | |
SDL_GLContext glcontext = SDL_GL_CreateContext(window); | |
std::cout << | |
"GL version: " << glGetString(GL_VERSION) << '\n' << | |
"GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << '\n' << | |
"Renderer: " << glGetString(GL_RENDERER) << '\n'; | |
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); | |
glClearColor(0, 0, 0, 1); | |
GLuint vertexPositionBuffer; | |
glGenBuffers(1, &vertexPositionBuffer); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBuffer); | |
glBufferData( | |
GL_ARRAY_BUFFER, | |
sizeof(float) * 16, | |
(const GLvoid *) VERTICES_POSITIONS, | |
GL_STATIC_DRAW | |
); | |
GLuint vertexColorBuffer; | |
glGenBuffers(1, &vertexColorBuffer); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexColorBuffer); | |
glBufferData( | |
GL_ARRAY_BUFFER, | |
sizeof(float) * 16, | |
(const GLvoid *) VERTICES_COLORS, | |
GL_STATIC_DRAW | |
); | |
GLuint program = glCreateProgram(); | |
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); | |
std::string vertexShaderSource = readShaderSource("./test.vert"); | |
const char * vertexShaderSourceCStr = vertexShaderSource.data(); | |
GLint vertexShaderSourceLength = vertexShaderSource.size(); | |
glShaderSource( | |
vertexShader, | |
1, | |
&vertexShaderSourceCStr, | |
&vertexShaderSourceLength | |
); | |
glCompileShader(vertexShader); | |
GLint vertexShaderCompileStatus; | |
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vertexShaderCompileStatus); | |
if (vertexShaderCompileStatus != GL_TRUE) { | |
GLint vertexShaderInfoLogLength; | |
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &vertexShaderInfoLogLength); | |
char * vertexShaderInfoLog = static_cast<char *>(std::malloc(vertexShaderInfoLogLength)); | |
glGetShaderInfoLog(vertexShader, vertexShaderInfoLogLength, NULL, vertexShaderInfoLog); | |
std::cout << vertexShaderInfoLog << '\n'; | |
std::free(vertexShaderInfoLog); | |
} | |
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); | |
std::string fragmentShaderSource = readShaderSource("./test.frag"); | |
const char * fragmentShaderSourceCStr = fragmentShaderSource.data(); | |
GLint fragmentShaderSourceLength = fragmentShaderSource.size(); | |
glShaderSource( | |
fragmentShader, | |
1, | |
&fragmentShaderSourceCStr, | |
&fragmentShaderSourceLength | |
); | |
glCompileShader(fragmentShader); | |
GLint fragmentShaderCompileStatus; | |
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentShaderCompileStatus); | |
if (fragmentShaderCompileStatus != GL_TRUE) { | |
GLint fragmentShaderInfoLogLength; | |
glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &fragmentShaderInfoLogLength); | |
char * fragmentShaderInfoLog = static_cast<char *>(std::malloc(fragmentShaderInfoLogLength)); | |
glGetShaderInfoLog(fragmentShader, fragmentShaderInfoLogLength, NULL, fragmentShaderInfoLog); | |
std::cout << fragmentShaderInfoLog << '\n'; | |
std::free(fragmentShaderInfoLog); | |
} | |
glAttachShader(program, vertexShader); | |
glAttachShader(program, fragmentShader); | |
glLinkProgram(program); | |
GLint programLinkStatus; | |
glGetProgramiv(program, GL_LINK_STATUS, &programLinkStatus); | |
if (programLinkStatus != GL_TRUE) { | |
GLint programInfoLogLength; | |
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &programInfoLogLength); | |
char * programInfoLog = static_cast<char *>(std::malloc(programInfoLogLength)); | |
glGetProgramInfoLog(program, programInfoLogLength, NULL, programInfoLog); | |
std::cout << | |
"Program isn't linked!\n" << | |
programInfoLog << '\n'; | |
std::free(programInfoLog); | |
} | |
GLuint vertexArrayObj; | |
glGenVertexArrays(1, &vertexArrayObj); | |
glBindVertexArray(vertexArrayObj); | |
glValidateProgram(program); | |
GLint programValidateStatus; | |
glGetProgramiv(program, GL_VALIDATE_STATUS, &programValidateStatus); | |
if (programValidateStatus != GL_TRUE) { | |
GLint programInfoLogLength; | |
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &programInfoLogLength); | |
char * programInfoLog = static_cast<char *>(std::malloc(programInfoLogLength)); | |
glGetProgramInfoLog(program, programInfoLogLength, NULL, programInfoLog); | |
std::cout << | |
"Program isn't valid!\n" << | |
programInfoLog << '\n'; | |
std::free(programInfoLog); | |
} | |
GLint vertexPositionAttrib = glGetAttribLocation(program, "vertexPosition"); | |
GLint vertexColorAttrib = glGetAttribLocation(program, "vertexColor"); | |
bool running = true; | |
while (running) { | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) { | |
switch (event.type) { | |
case SDL_WINDOWEVENT: | |
{ | |
switch (event.window.event) { | |
case SDL_WINDOWEVENT_CLOSE: | |
running = false; | |
break; | |
} | |
} | |
break; | |
} | |
} | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glUseProgram(program); | |
glEnableVertexAttribArray(vertexPositionAttrib); | |
glEnableVertexAttribArray(vertexColorAttrib); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBuffer); | |
glVertexAttribPointer( | |
vertexPositionAttrib, | |
4, | |
GL_FLOAT, | |
GL_TRUE, | |
0, | |
0 | |
); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexColorAttrib); | |
glVertexAttribPointer( | |
vertexColorAttrib, | |
3, | |
GL_FLOAT, | |
GL_TRUE, | |
0, | |
0 | |
); | |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);\ | |
glFinish(); | |
glFlush(); | |
SDL_GL_SwapWindow(window); | |
} | |
SDL_GL_DeleteContext(glcontext); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} | |
std::string readShaderSource(const std::string & filename) { | |
std::fstream file(filename.c_str()); | |
std::stringstream buffer; | |
std::string tmp; | |
while (std::getline(file, tmp)) { | |
buffer << tmp << '\n'; | |
} | |
return buffer.str(); | |
} | |
} // namespace gfx | |
int main(int argc, char * argv[]) { | |
return gfx::main(argc, argv); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SYSTEM = $(shell uname) | |
ifeq ($(SYSTEM),Darwin) | |
CLANG = /opt/local/bin/clang++-mp-3.3 | |
else | |
CLANG = $(shell which clang) | |
endif | |
CXX = $(CLANG) | |
CC = $(CLANG) | |
PKGCOFNIG = $(shell which pkg-config) | |
$(info $(PKGCOFNIG)) | |
LIBS = sdl2 | |
ifneq ($(SYSTEM),Darwin) | |
LIBS += gl | |
endif | |
CXXFLAGS += -Wall -Wextra -std=c++11 $(shell $(PKGCOFNIG) --cflags $(LIBS)) | |
LDFLAGS += $(shell $(PKGCOFNIG) --libs $(LIBS)) | |
ifeq ($(SYSTEM),Darwin) | |
CXXFLAGS += -I/System/Library/Frameworks/OpenGL.framework/Headers | |
LDFLAGS += -L/System/Library/Frameworks/OpenGL.framework/Libraries -lGL | |
endif | |
OBJS = $(patsubst %.cpp,%.o,$(wildcard *.cpp)) | |
BIN = gfx | |
$(BIN): $(OBJS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 150 | |
precision highp float; | |
noperspective in vec3 color; | |
out vec4 fragColor; | |
void main(void) | |
{ | |
fragColor = vec4(color, 1.0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 150 | |
precision highp float; | |
in vec4 vertexPosition; | |
in vec3 vertexColor; | |
noperspective out vec3 color; | |
void main(void) | |
{ | |
gl_Position = vertexPosition; | |
color = vertexColor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment