Last active
August 29, 2015 14:18
-
-
Save Overv/252aa62fb2641200e4b3 to your computer and use it in GitHub Desktop.
Triangle in OpenGL without vertex data
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
#include <SDL.h> | |
#define GLEW_STATIC | |
#include <GL/glew.h> | |
const char* vertShaderSrc = | |
"#version 440\n" | |
"void main() {" | |
"if (gl_VertexID == 0) gl_Position = vec4( 0.0, 0.5, 0, 1);" | |
"if (gl_VertexID == 1) gl_Position = vec4( 0.5, -0.5, 0, 1);" | |
"if (gl_VertexID == 2) gl_Position = vec4(-0.5, -0.5, 0, 1);" | |
"}"; | |
const char* fragShaderSrc = | |
"#version 440\n" | |
"out vec4 color;" | |
"void main() {" | |
"color = vec4(1, 0, 0, 1);" | |
"}"; | |
int main(int argc, char* argv[]) { | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4); | |
SDL_Window* window = SDL_CreateWindow("Triangle without vertex data", 100, 100, 800, 600, SDL_WINDOW_OPENGL); | |
SDL_GLContext context = SDL_GL_CreateContext(window); | |
glewExperimental = GL_TRUE; | |
glewInit(); | |
GLuint vao; | |
glGenVertexArrays(1, &vao); | |
glBindVertexArray(vao); | |
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER); | |
glShaderSource(vertShader, 1, &vertShaderSrc, nullptr); | |
glCompileShader(vertShader); | |
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(fragShader, 1, &fragShaderSrc, nullptr); | |
glCompileShader(fragShader); | |
GLuint program = glCreateProgram(); | |
glAttachShader(program, vertShader); | |
glAttachShader(program, fragShader); | |
glLinkProgram(program); | |
glUseProgram(program); | |
SDL_Event windowEvent; | |
while (true) { | |
if (SDL_PollEvent(&windowEvent)) { | |
if (windowEvent.type == SDL_QUIT) break; | |
} | |
glClearColor(0, 0, 0, 1); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
SDL_GL_SwapWindow(window); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment