shader could not compile

Hi there I am a beginner to OpenGL and had been trying to write a program that displays a few basic shapes on the screen but somehow only a window without the expected shapes showed up. After tracing it I realised the vertex shader and fragment shader failed to compile. Any help? much appreciated!

Here’s my shader source:


const GLchar* vertexSource =
"#version 150 core
"
"in vec3 position;"
"void main() {"
"   gl_Position = vec4(position, 1.0);"
"}";

const GLchar* fragmentSource =
"#version 150 core
"
"out vec4 outColor;"
"void main() {"
"   outColor = vec4(1.0, 1.0, 1.0, 1.0);"
"}";

and here’s how I create and compile the shaders


GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);

Try putting more newlines at the end of each line in the string. If that doesn’t work, fetch the compile logs for each of the shaders with


int log_len = 0;
glGetShaderiv(vertShader, GL_INFO_LOG_LENGTH, &log_len);

char *log_text = new char[log_len+1];
glGetShaderInfoLog(vertShader, log_len, &log_len, log_text);

Hi malexander,

Thanks for the reply, I had altered my code to reading in the shaders glsl file instead, and checked using the compile log, it gives the error message:

ERROR: 0:2: ‘’ : syntax error #version

I had done a lot of searches online and many had said that this problem is caused by the shader source having no null-terminator in the end. so I had tried to add a null terminator like this


    std::string VertexShaderCode;
    std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
    if(VertexShaderStream.is_open())
    {
        std::string Line = "";
        while(getline(VertexShaderStream, Line))
            VertexShaderCode += "
" + Line;
        VertexShaderCode += "\0"; //<-- (THE ADDED LINE FOR NULL TERMINATOR)
        VertexShaderStream.close();
    }

but it still give me the same error. Any ideas?

Thanks

printf("%s %s %s
", glGetString(GL_RENDERER), glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION));