4

I'm using xcode to make a game with OpenGL. I have used GLUT to initialise a window. I have shaders that I wish to implement but when I try to compile them, I get two compile errors in the info log. My shaders look like this:

//FirstShader.vsh
#version 150 core

in vec3 position;

void main()
{
    gl_Position = vec4(position, 1.0);
}

//FirstShader.fsh
#version 150 core

out vec4 fragData;

void main()
{
    fragData = vec4(0.0, 0.0, 1.0, 1.0);
}         

I'm reading the file and compiling it with this code:

GLuint createShaderFromFile(const GLchar *path, GLenum shaderType){
GLuint shaderID = glCreateShader(shaderType);
std::ifstream fin;
fin.open(path);
if(!fin.is_open()){
    fin.close();
    std::cout << "Shader Not Found" << std:: endl;
    return 0;
}
std::string source((std::istreambuf_iterator<GLchar>(fin)),std::istreambuf_iterator<GLchar> ());

fin.close();

const GLchar* shaderSource = source.c_str();


glShaderSource(shaderID, 1, &shaderSource, NULL);

glCompileShader(shaderID);
GLint compileStatus;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compileStatus);

if (compileStatus != GL_TRUE) {
    std::cout << "Shader failed to compile" << std::endl;
    GLint infoLoglength;
    glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &infoLoglength);
    GLchar* infoLog = new GLchar[infoLoglength + 1];
    glGetShaderInfoLog(shaderID, infoLoglength, NULL, infoLog);
    std::cout << infoLog << std::endl;
    delete infoLog;
    return 0;
}


return shaderID;
}

Im getting these errors:

ERROR: 0:1: '' :  version '150' is not supported
ERROR: 0:1: '' : syntax error #version

My OpenGL version is 2.1 and my glsl version is 1.20. Does anybody know how I can fix this?

2
  • 3
    version '150' is not supported is quite a clear message, don't you think? What does glGetString(GL_VERSION) return, after you created the context? Commented Dec 14, 2013 at 14:35
  • If your GLSL Version is 1.2 then you clearly do not support version 1.5, as the error and @derhass says. Commented Dec 14, 2013 at 15:47

2 Answers 2

1

You can tell OSX to use a newer version of OpenGL by setting it in your pixel format when you create the context. Here is an example of how to set it up.

Or if you're using glut, I think you want glutInitContextVersion(x, y); where x and y are the major and minor version numbers. GLSL 1.5 is supported in OpenGL 3.2, I think. (And you might also want glutInitContextProfile(GLUT_CORE_PROFILE);, I think.)

Sign up to request clarification or add additional context in comments.

3 Comments

glutInitContextVersion(x,y); doesnt work. ive tried the context profile line of code but they both give errors.
@DavidGraovac I'm not real familiar with glut, so I'm not sure what to tell you. Have you tried setting the shader version to 1.2 and seeing what happens? I don't think there's anything in your shaders that requires 1.5 at the moment.
Note that glutInitContextVersion() and friends are not part of the official GLUT spec, but extensions by freeglut. Don't know which GLUT version DavidGraovac is using...
1

I re-wrote your shaders in a way that they will actually work in GLSL 1.20.

  • in must be replaced with attribute in a GLSL 1.20 vertex shader
  • out for fragment shader output is invalid, use gl_FragColor or gl_FragData [n] instead
  • Declaring a vertex attribute as vec3 and then doing something like vec4 (vtx, 1.0) is completely redundant
    • If you declare the attribute as vec4 and give it data using fewer than 4 components GLSL will automatically fill-in the attribute's missing components this way: vec4 (0.0, 0.0, 0.0, 1.0).

Fragment Shader:

#version 120

attribute vec4 position;

void main()
{
  gl_Position = position;
}

Vertex Shader:

#version 120

void main()
{
  gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
}

Of course this does not really solve your problem, because if you have a version of OS X 10.7 or newer, it supports OpenGL 3.2 and therefore GLSL 1.50 core. You need to request a core profile context for this to work, however - otherwise you will get OpenGL 2.1 and GLSL 1.20.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.