Skip to content

Instantly share code, notes, and snippets.

@blackwolf12333
Created January 24, 2015 19:10
Show Gist options
  • Save blackwolf12333/be2cf91cd557a07efcf9 to your computer and use it in GitHub Desktop.
Save blackwolf12333/be2cf91cd557a07efcf9 to your computer and use it in GitHub Desktop.
#include "main.h"
#include <iostream>
#include <SOIL/SOIL.h>
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#define GLSL(src) "#version 150 core\n" #src
void checkGLError2(int line, std::string file) {
GLenum error = glGetError();
if(error != GL_NO_ERROR) {
printf("OpenGL error at line:%d in: %s\nError: %d\n", line, file.c_str(), error);
}
}
// Shader sources
const GLchar* vertexSource = GLSL(
in vec2 position;
in vec3 color;
in vec2 texcoord;
out vec3 Color;
out vec2 Texcoord;
void main() {
Color = color;
Texcoord = texcoord;
gl_Position = vec4(position, 0.0, 1.0);
}
);
const GLchar* fragmentSource = GLSL(
in vec3 Color;
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D tex;
void main() {
outColor = texture(tex, Texcoord) * vec4(Color, 0.0);
}
);
Main::Main() {}
void Main::initSDL() {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return;
}
this->window = SDL_CreateWindow("Hello World!", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
if (this->window == nullptr){
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return;
}
this->renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (this->renderer == nullptr){
SDL_DestroyWindow(this->window);
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return;
}
}
void Main::initGL() {
/* Request opengl 3.2 context. */
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
/* Turn on double buffering with a 24bit Z buffer.
* You may need to change this to 16 or 32 for your system */
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
glContext = SDL_GL_CreateContext(this->window);
SDL_GL_SetSwapInterval(0);
//Initialize clear color
glClearColor( 0.f, 0.f, 0.f, 1.f );
//Enable texturing
//glEnable( GL_TEXTURE_2D );
//Check for error
GLenum error = glGetError();
if(error != GL_NO_ERROR) {
printf("Error initializing OpenGL!\n");
}
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if(err!=GLEW_OK) {
//Problem: glewInit failed, something is seriously wrong.
printf("glewInit failed, aborting.\n");
}
printf("Some OpenGL info:\n");
printf("%s\n", glGetString(GL_VENDOR));
printf("%s\n", glGetString(GL_VERSION));
printf("%s\n", glGetString(GL_RENDERER));
}
void Main::initGameScene() {
//todo
}
void Main::checkShaderCompilerStatus(GLuint shader) {
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
char buffer[512];
glGetShaderInfoLog(shader, 512, NULL, buffer);
printf("%s\n", buffer);
} else {
printf("Shaders compiled.\n");
}
}
void Main::initShaders() {
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
checkShaderCompilerStatus(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
checkShaderCompilerStatus(fragmentShader);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), 0);
checkGLError2(__LINE__, __FILE__);
GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
checkGLError2(__LINE__, __FILE__);
GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(5 * sizeof(GLfloat)));
checkGLError2(__LINE__, __FILE__);
}
void Main::initVertices1() {
// Create Vertex Array Object
glGenVertexArrays(1, &vao1);
glBindVertexArray(vao1);
initShaders();
// Create a Vertex Buffer Object and copy the vertex data to it
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
float vertices[] = {
// Position Color Texcoords
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // Bottom-left
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
checkGLError2(__LINE__, __FILE__);
// Create an Element Buffer Object and copy the element data to it
GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
GLuint elements[] = {
0, 1, 2,
2, 3, 0
};
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements,
GL_STATIC_DRAW);
checkGLError2(__LINE__, __FILE__);
GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
int width = 0, height = 0;
unsigned char *image = SOIL_load_image("texture.png", &width, &height, 0, SOIL_LOAD_RGBA);
printf("stuff: %d\n%d\n\n", width, height);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SOIL_free_image_data(image);
checkGLError2(__LINE__, __FILE__);
glBindVertexArray(0);
checkGLError2(__LINE__, __FILE__);
}
void Main::initVertices2() {
// Create Vertex Array Object
glGenVertexArrays(1, &vao2);
glBindVertexArray(vao2);
initShaders();
// Create a Vertex Buffer Object and copy the vertex data to it
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
float vertices[] = {
// Position Color Texcoords
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // Bottom-left
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
checkGLError2(__LINE__, __FILE__);
// Create an Element Buffer Object and copy the element data to it
GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
GLuint elements[] = {
0, 1, 2,
2, 3, 0
};
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
checkGLError2(__LINE__, __FILE__);
GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture);
int width, height;
unsigned char *image = SOIL_load_image("texture.png", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SOIL_free_image_data(image);
checkGLError2(__LINE__, __FILE__);
glBindVertexArray(0);
checkGLError2(__LINE__, __FILE__);
}
void Main::loop() {
initShaders();
initVertices1();
initVertices2();
bool running = true;
SDL_Event e;
while (running) {
while (SDL_PollEvent(&e)){
//If user closes the window
if (e.type == SDL_QUIT){
running = false;
break;
}
//If user presses any key
if (e.type == SDL_KEYDOWN){
running = false;
break;
}
}
// Clear the screen to black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(vao1);
checkGLError2(__LINE__, __FILE__);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
checkGLError2(__LINE__, __FILE__);
glBindVertexArray(vao2);
checkGLError2(__LINE__, __FILE__);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
checkGLError2(__LINE__, __FILE__);
// Swap buffers
SDL_GL_SwapWindow(this->window);
}
}
void Main::cleanup() {
glDeleteProgram(shaderProgram);
glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);
//delete scene;
SDL_GL_DeleteContext(this->glContext);
SDL_DestroyRenderer(this->renderer);
SDL_DestroyWindow(this->window);
SDL_Quit();
}
int main()
{
Main *main = new Main();
main->initSDL();
main->initGL();
main->initGameScene();
main->loop();
main->cleanup();
}
#ifndef MAIN_H
#define MAIN_H
#include <SDL2/SDL.h>
#include "opengl.h"
class Main
{
public:
Main();
~Main();
void initSDL();
void initGL();
void initGameScene();
void loop();
void cleanup();
private:
SDL_Window *window;
SDL_Renderer *renderer;
SDL_GLContext glContext;
//Scene *scene; todo
GLuint vertexShader;
GLuint fragmentShader;
GLuint shaderProgram;
GLuint vao1;
GLuint vao2;
void initShaders();
void checkShaderCompilerStatus(GLuint shader);
void initVertices1();
void initVertices2();
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment