Created
February 14, 2016 00:16
-
-
Save versine/3f23d2971e8b44bd2810 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #define GLEW_STATIC | |
| #include <GL/glew.h> | |
| #include <SDL2/SDL.h> | |
| #include <SDL2/SDL_opengl.h> | |
| #include <iostream> | |
| #include <cmath> | |
| #include <stdio.h> | |
| #include <windows.h> | |
| #pragma comment(lib, "opengl32.lib") | |
| #pragma comment(lib, "glew32s.lib") | |
| #pragma comment(lib, "SDL2.lib") | |
| bool running = true; | |
| const int SCREEN_WIDTH = 800; | |
| const int SCREEN_HEIGHT = 600; | |
| const int OPENGL_MAJOR_VERSION = 3; | |
| const int OPENGL_MINOR_VERSION = 3; | |
| const SDL_GLprofile OPENGL_PROFILE = SDL_GLprofile::SDL_GL_CONTEXT_PROFILE_CORE; | |
| const GLchar* vertexShaderSource = "#version 330 core\n" | |
| "layout (location = 0) in vec3 position;\n" | |
| "void main()\n" | |
| "{\n" | |
| "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n" | |
| "}\0"; | |
| const GLchar* fragmentShaderSource = "#version 330 core\n" | |
| "out vec4 color;\n" | |
| "void main()\n" | |
| "{\n" | |
| "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" | |
| "}\n\0"; | |
| int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { | |
| if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
| std::cerr << "There was an error initing SDL2: " << SDL_GetError() << std::endl; | |
| return 1; | |
| } | |
| SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, OPENGL_MAJOR_VERSION); | |
| SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, OPENGL_MINOR_VERSION); | |
| SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, OPENGL_PROFILE); | |
| SDL_Window* window = SDL_CreateWindow("SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, | |
| SCREEN_HEIGHT, SDL_WINDOW_OPENGL); | |
| if (window == nullptr) { | |
| std::cerr << "There was an error creating the window: " << SDL_GetError() << std::endl; | |
| return 1; | |
| } | |
| SDL_GLContext context = SDL_GL_CreateContext(window); | |
| if (context == nullptr) { | |
| std::cerr << "There was an error creating OpenGL context: " << SDL_GetError() << std::endl; | |
| return 1; | |
| } | |
| const unsigned char *version = glGetString(GL_VERSION); | |
| if (version == nullptr) { | |
| std::cerr << "There was an error with OpenGL configuration:" << std::endl; | |
| return 1; | |
| } | |
| SDL_GL_MakeCurrent(window, context); | |
| glewExperimental = GL_TRUE; | |
| glewInit(); | |
| glViewport(0, 0, (GLsizei)SCREEN_WIDTH, (GLsizei)SCREEN_HEIGHT); | |
| GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); | |
| glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); | |
| glCompileShader(vertexShader); | |
| GLint success; | |
| GLchar infoLog[512]; | |
| glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); | |
| if (!success) | |
| { | |
| glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); | |
| std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; | |
| } | |
| GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); | |
| glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); | |
| glCompileShader(fragmentShader); | |
| glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); | |
| if (!success) | |
| { | |
| glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); | |
| std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; | |
| } | |
| GLuint shaderProgram = glCreateProgram(); | |
| glAttachShader(shaderProgram, vertexShader); | |
| glAttachShader(shaderProgram, fragmentShader); | |
| glLinkProgram(shaderProgram); | |
| glDeleteShader(vertexShader); | |
| glDeleteShader(fragmentShader); | |
| GLfloat vertices[] = { | |
| 0.5f, 0.5f, 0.0f, // Top Right | |
| 0.5f, -0.5f, 0.0f, // Bottom Right | |
| -0.5f, -0.5f, 0.0f, // Bottom Left | |
| -0.5f, 0.5f, 0.0f // Top Left | |
| }; | |
| GLuint indices[] = { // Note that we start from 0! | |
| 0, 1, 3, // First Triangle | |
| 1, 2, 3 // Second Triangle | |
| }; | |
| GLuint EBO, VAO, VBO; | |
| glGenVertexArrays(1, &VAO); | |
| glGenBuffers(1, &VBO); | |
| glGenBuffers(1, &EBO); | |
| glBindVertexArray(VAO); | |
| glBindBuffer(GL_ARRAY_BUFFER, VBO); | |
| glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); | |
| glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); | |
| glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); | |
| glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, static_cast < const void *>(0)); | |
| glEnableVertexAttribArray(0); | |
| glBindBuffer(GL_ARRAY_BUFFER, 0); | |
| glBindVertexArray(0); | |
| SDL_Event e; | |
| while (running) { | |
| while (SDL_PollEvent(&e)) { | |
| if (e.type == SDL_QUIT) { | |
| running = false; | |
| } | |
| } | |
| glClearColor(0.3f, 0.1f, 0.2f, 0.0f); | |
| glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
| glUseProgram(shaderProgram); | |
| glBindVertexArray(VAO); | |
| glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); | |
| glBindVertexArray(0); | |
| SDL_GL_SwapWindow(window); | |
| } | |
| glDeleteVertexArrays(1, &VAO); | |
| glDeleteBuffers(1, &VBO); | |
| glDeleteBuffers(1, &EBO); | |
| SDL_GL_DeleteContext(context); | |
| SDL_Quit(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment