PDA

View Full Version : shader could not compile



scoder
11-11-2013, 09:09 AM
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\n"
"in vec3 position;"
"void main() {"
" gl_Position = vec4(position, 1.0);"
"}";

const GLchar* fragmentSource =
"#version 150 core\n"
"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);

malexander
11-11-2013, 05:09 PM
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);

scoder
11-13-2013, 04:38 AM
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 += "\n" + Line;
VertexShaderCode += "\0"; //<-- (THE ADDED LINE FOR NULL TERMINATOR)
VertexShaderStream.close();
}


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

Thanks

arekkusu
11-13-2013, 08:51 AM
Any ideas?

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