Created
March 11, 2015 22:18
-
-
Save anlsh/8b630ba5e809827fd79c to your computer and use it in GitHub Desktop.
Could I have help diagnosing the problem in this?
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
#define GLEW_STATIC | |
#include <GL/glew.h> | |
#include "SDL2/SDL.h" | |
#include "SDL2/SDL_opengl.h" | |
#include "iostream" | |
const GLchar* vertexSource = | |
"#version 150 core\n" | |
"in vec2 position;" | |
"void main() {" | |
" gl_Position = vec4(position, 0.0, 1.0);" | |
"}"; | |
const GLchar* fragmentSource = | |
"#version 150 core\n" | |
"out vec4 outColor;" | |
"void main() {" | |
" outColor = vec4(1.0, 1.0, 1.0, 1.0);" | |
"}"; | |
int main() { | |
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); | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_Window* window = SDL_CreateWindow("triangle", 50, 50, 800, 600, SDL_WINDOW_OPENGL); | |
SDL_GLContext context = SDL_GL_CreateContext(window); | |
glewExperimental = GL_TRUE; | |
glewInit(); | |
GLuint vao; | |
glGenVertexArrays(1, &vao); | |
glBindVertexArray(vao); | |
GLuint vbo; | |
glGenBuffers(1, &vbo); | |
GLfloat vertices[] = {0.0f, 0.5f, .5f -.5f, -.5f, -.5f}; | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); | |
//compile vertex shader | |
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); | |
glShaderSource(vertexShader, 1, &vertexSource, NULL); | |
glCompileShader(vertexShader); | |
//Compile fragment shader | |
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(fragmentShader, 1, &fragmentSource, NULL); | |
glCompileShader(fragmentShader); | |
//Combine shaders into a program | |
GLuint shaderProgram = glCreateProgram(); | |
glAttachShader(shaderProgram, vertexShader); | |
glAttachShader(shaderProgram, fragmentShader); | |
glBindFragDataLocation(shaderProgram, 0, "outColor"); | |
glLinkProgram(shaderProgram); | |
glUseProgram(shaderProgram); | |
//retrieve a reference to the position input | |
GLint posAttrib = glGetAttribLocation(shaderProgram, "position"); | |
glEnableVertexAttribArray(posAttrib); | |
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); | |
SDL_Event event; | |
while (true) { | |
if (SDL_PollEvent(&event)) { | |
bool escape_pressed = event.type == SDL_KEYUP && | |
(event.key.keysym.sym == SDLK_ESCAPE); | |
if (event.type == SDL_QUIT || escape_pressed) break; | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
SDL_GL_SwapWindow(window); | |
} | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
SDL_GL_SwapWindow(window); | |
} | |
glDeleteProgram(shaderProgram); | |
glDeleteShader(fragmentShader); | |
glDeleteShader(vertexShader); | |
glDeleteBuffers(1, &vbo); | |
glDeleteVertexArrays(1, &vao); | |
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