// Compile with // c++ -std=c++11 ./shared_constant_names.cc -Wall $(pkg-config --cflags --libs sdl2 gl) -o shared_constant_names #define GL_GLEXT_PROTOTYPES 1 #include #include #include #include #include const GLchar * const vertex_shader_source = R"foo(#version 330 in vec2 in_position; void main() { const float zs[4] = float[4](0, 0, 0, 0); gl_Position = vec4(in_position, zs[int(in_position.x) % 4], 1.0); } )foo"; const GLchar * const fragment_shader_source = R"foo(#version 330 void main() { const vec4 colours[2] = vec4[2](vec4(1, 0, 0, 0), vec4(0, 1, 0, 0)); gl_FragColor = colours[int(gl_FragCoord.x) % 2]; } )foo"; const GLfloat vertex_positions[][2] = {{-0.4f, -0.4f}, {-0.4f, 0.4f}, {0.4f, 0.4f}}; int main() { SDL_Init(SDL_INIT_VIDEO); SDL_Window * const window = SDL_CreateWindow( "test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 256, 256, SDL_WINDOW_OPENGL); if (!window) { std::cerr << "Could not create window: " << SDL_GetError() << '\n'; return 1; } SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); SDL_GLContext gl_context = SDL_GL_CreateContext(window); glClear(GL_COLOR_BUFFER_BIT); GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions), vertex_positions, GL_STATIC_DRAW); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0); const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader, 1, const_cast(&vertex_shader_source), 0); glCompileShader(vertex_shader); { GLint is_compiled; glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &is_compiled); assert(is_compiled == GL_TRUE); } const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, const_cast(&fragment_shader_source), 0); glCompileShader(fragment_shader); { GLint is_compiled; glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &is_compiled); assert(is_compiled == GL_TRUE); } GLuint shader_program = glCreateProgram(); glAttachShader(shader_program, vertex_shader); glAttachShader(shader_program, fragment_shader); glBindAttribLocation(shader_program, 0, "in_position"); glLinkProgram(shader_program); { GLint is_linked; glGetProgramiv(shader_program, GL_LINK_STATUS, &is_linked); assert(is_linked == GL_TRUE); } glUseProgram(shader_program); glDrawArrays(GL_TRIANGLES, 0, 3); SDL_GL_DeleteContext(gl_context); SDL_DestroyWindow(window); }