Overv / Open.GL

The source code and content of Open.GL.
http://open.gl/
1.07k stars 260 forks source link

Please use appropriate C++11 functionality. #55

Closed NicolBolas closed 7 years ago

NicolBolas commented 7 years ago

OpenGL tutorials have a peculiar place among programmers. Many neophyte programmers use them directly, often copying large parts into their projects and making only alterations that suit their needs.

As such, these tutorials are often where they get their first real exposure to the C++ language.

So I find myself dismayed when many newer OpenGL tutorials do things like this:

const GLchar* vertexSource =
    "#version 150 core\n"
    "in vec2 position;"
    "in vec3 color;"
    "out vec3 Color;"
    "void main()"
    "{"
    "    Color = color;"
    "    gl_Position = vec4(position, 0.0, 1.0);"
    "}";

C++11 added the raw string literal feature specifically for cases like this, where you're writing a literal that represents code that is in a different language.

New users need to be encouraged to use the language properly. OpenGL users ought to know that raw string literals exist in C++ and how to use them on shaders.

const GLchar* vertexSource =
R"glsl(#version 150 core
in vec2 position;
in vec3 color;
out vec3 Color;
void main()
{
    Color = color;
    gl_Position = vec4(position, 0.0, 1.0);
})glsl";

This is not a trivial matter of convenience; it affects how users debug their shaders. Your original version puts all of the shader code (save the #version declaration) on the same line. This means that, if the user makes a mistake, the error log will not help them, since it cannot point to the line where the error happened.

By contrast, the raw string literal version uses actual lines, which can in part help the user track down the specific problem.

I'm not asking that you litter your code with C++11-isms. I'm not saying that you should put auto and decltype and lambdas wherever you possibly can. But something like using raw string literals like this should be a no-brainer.

I can understand if you want to limit the compiler version to C++03 only.