Compiling OpenGL shaders causes syntax errors - c++

I have two simple shaders (vertex and fragment)
Vertex Shader
#version 330 core
//=============================================
//---[Structs/Vertex Data]---------------------
//=============================================
layout(location = 0) in vec3 vertexPosition_modelspace;
//=============================================
//---[Variables]-------------------------------
//=============================================
uniform mat4 projection;
uniform mat4 view;
//=============================================
//---[Vertex Shader]---------------------------
//=============================================
void main()
{
gl_Position = projection * view * vertexPosition_modelspace;
}
Fragment Shader
#version 330 core
//=============================================
//---[Output Struct]---------------------------
//=============================================
out vec3 colour;
//=============================================
//---[Fragment Shader]-------------------------
//=============================================
void main()
{
colour = vec3(1, 0, 0);
}
I am trying to compile them and I get the following error
error C0000: syntax error, unexpected $undefined, expecting "::" at token "undefined"
I believe this may have something to do with the way I am reading the files. Here is an example for the vertex shader file
std::string VertexShaderCode = FindFileOrThrow(vertex_file_path);
std::ifstream vShaderFile(VertexShaderCode.c_str());
std::stringstream vShaderData;
vShaderData << vShaderFile.rdbuf();
vShaderFile.close();
The file is then compiled using
char const *VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);
How can I efficiently read in the files and avoid these errors?
EDIT:
Correct compile:
const std::string &shaderText = vShaderData.str();
GLint textLength = (GLint)shaderText.size();
const GLchar *pText = static_cast<const GLchar *>(shaderText.c_str());
glShaderSource(VertexShaderID, 1, &pText, &textLength);
glCompileShader(VertexShaderID);

VertexShaderCode is your filename, not the shader string. That's still in vShaderData. You need to copy the string out of vShaderData, and feed it into glShaderSource.

Related

OpenGL error thrown by fragment shader

I'm writing a 2D game in OpenTK, using OpenGL 4.4. Using colour and texture UV coordinates and a matrix I can succesfully draw textures between vertices with vertex shader:
public const string vertexShaderDefaultSrc =
#"
#version 330
uniform mat4 MVPMatrix;
layout (location = 0) in vec2 Position;
layout (location = 1) in vec2 Texture;
layout (location = 2) in vec4 Colour;
out vec2 InTexture;
out vec4 OutColour;
void main()
{
gl_Position = MVPMatrix * vec4(Position, 0, 1);
InTexture = Texture;
OutColour = Colour;
}";
and fragment shader:
public const string fragmentShaderDefaultSrc =
#"
#version 330
uniform sampler2D Sampler;
in vec2 InTexture;
in vec4 OutColour;
out vec4 OutFragColor;
void main()
{
OutFragColor = texture(Sampler, InTexture) * OutColour;
//Alpha test
if(OutFragColor.a <= 0)
discard;
}
";
BUT if I want to draw just a solid colour rather than a texture, I use this shader (with the same vertices, passing UV coords that won't be used):
public const string fragmentShaderSolidColourSrc =
#"
#version 330
uniform sampler2D Sampler;
in vec2 InTexture;
in vec4 OutColour;
out vec4 OutFragColor;
void main()
{
OutFragColor = OutColour;
//Alpha test
if(OutFragColor.a <= 0)
discard;
}
";
Now this works beautifully, but OpenGL reports an error - GL_INVALID_VALUE. It draws fine and everything seems to work, but ideally I would like OpenGL to be error free in that situation, so I can catch real errors. I would appreciate any help, and can share more detail of how the shader is compiled or used if that is helpful - what I don't understand is how the default shader can work but the solid colour doesn't.
I have tracked down the exact source of the errors in my render call (shader builds with no problems)
GL.EnableVertexAttribArray(shader.LocationPosition);
GL.VertexAttribPointer(shader.LocationPosition, 2, VertexAttribPointerType.Float, false, Stride, 0);
//-----everything up to here is fine
//this line throws an error
GL.EnableVertexAttribArray(shader.LocationTexture);
//as does this line
GL.VertexAttribPointer(shader.LocationTexture, 2, VertexAttribPointerType.Float, false, Stride, 8);
//this is all ok
GL.EnableVertexAttribArray(shader.LocationColour);
GL.VertexAttribPointer(shader.LocationColour, 4, VertexAttribPointerType.UnsignedByte, true, Stride, 16);
//ok
GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer);
GL.DrawArrays(DrawType, 0, Vertices.Length);
//ok
GL.DisableVertexAttribArray(shader.LocationPosition);
//this line throws error
GL.DisableVertexAttribArray(shader.LocationTexture);
//this is ok
GL.DisableVertexAttribArray(shader.LocationColour);
It appears to me after some tests (would be nice to have this verified) that if a variable such as the texture coordinates are not used by the shader the compiler gets rid of it, so a call to get it's location returns -1. Simply checking if locationTexture was -1 here and then not binding locationTexture etc if so resolved my issues.

Shader creation Opengl

I am using OpenGl 3 and the tutorial from BennyBox on Youtube.
Using this method:
static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& errorMessage){
GLint success = 0;
GLchar error [1024] = {0};
if(isProgram){
glGetProgramiv(shader, flag, &success);
}else{
glGetShaderiv(shader, flag, &success);
}
if(success == GL_FALSE){
if(isProgram){
glGetProgramInfoLog(shader, sizeof(error), NULL, error);
}else{
glGetShaderInfoLog(shader,sizeof(error), NULL, error);
}
std::cerr<< errorMessage<< ": " << error<< "'"<<std::endl;
}
}
I should be able to load shader file (fragment and vertex shaders). It works with basic shaders but when I try to modify them to that point:
#version 120
attribute vec3 position;
attribute vec2 texCoord;
varying vec2 texCoord;
void main(){
gl_Position = vec4(position, 1.0);
texCoord0 = texCoord;
}
The I get:
Error compiling shader!: 'ERROR: 0:6: 'texCoord' : redefinition
ERROR: 0:11: 'texCoord0' : undeclared identifier
ERROR: 0:11: 'assign' : cannot convert from 'attribute 2-component vector of float' to 'float'
and the fragment shader:
#version 120
uniform sampler2D diffuse;
varying vec2 texCoord0;
void main(){
gl_FragColor = texture2D(diffuse, texCoord0);
}
gives:
Unable to load shader: ./res/basicShader.fs
Error linking shader program: 'Attached vertex shader is not compiled.
I have the exact same code as the video, and it runs fine using basic coloring shader. I am on Visual Studio 2012.
There are two errors in your vertex shader.
First, you are declaring texCoord twice.
attribute vec2 texCoord;
varying vec2 texCoord;
Then you are trying to use a varying called texCoord0, without ever declaring it.
texCoord0 = texCoord;
Your vertex shader should look like this:
#version 120
attribute vec3 position;
attribute vec2 texCoord;
varying vec2 texCoord0;
void main(){
gl_Position = vec4(position, 1.0);
texCoord0 = texCoord;
}

GLSL Syntax error: "in" parse error

I'm trying to use shaders in my program but i'm getting very strange error...
Vertex shader failed to compile with the following error
ERROR: 0:6: error(#132) Syntax error: "in" parse error
ERROR: error(#273) 1 compilation errors. No code generated
I thought the problem was with file reading, but after trying many ways of that it's still not working.
So here is my code:
bool ShaderProgram::LoadShaderFile(const char* shader_path, GLuint& shader_id)
{
ifstream oFileStream(shader_path);
if(oFileStream)
{
// Load shader code
string sShaderSource;
sShaderSource.assign((istreambuf_iterator<char> (oFileStream) ), istreambuf_iterator<char> () );
// Forward shader code to OGL
const GLchar* chShaderSource = sShaderSource.c_str() + '\0';
printf("%s", chShaderSource);
glShaderSource(shader_id, 1, (const GLchar**) &chShaderSource, NULL);
return true;
}
else
return false;
}
And my shaders:
// shader.vs
// Vertex Shader
#version 330
in vec3 vVertex
in vec3 vColor
smooth out vec4 vVaryingColor;
void main()
{
vVaryingColor = vec4(vColor, 1.0);
gl_Position = vec4(vVertex, 1.0);
}
// shader.fs
// Fragment Shader
#version 330
smooth in vec4 vVeryingColor;
out vec4 vVaryingColor;
void main()
{
vFragColor = vVaryingColor;
}
You are missing the semicolons at the end of the in lines.
You have:
in vec3 vVertex
in vec3 vColor
You should have:
in vec3 vVertex;
in vec3 vColor;

Cannot Compile Vertex Shader: Unexpected $end at token "<EOF>"

I'm having some issues with compiling my vertex shaders under OpenGL. I have a pretty standard vertex shader:
#version 330
layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
out vec4 ex_Color;
void main(void)
{
gl_Position = in_Position;
ex_Color = in_Color;
}
and my shader loading function looks like:
string temp = LoadFile(vShaderPath);
const char* vShaderString = temp.c_str();
const char* vShaderPathC = vShaderPath.c_str();
fprintf(stderr, "File: %s \nContents: %s\n", vShaderPathC, vShaderString);
temp = LoadFile(fShaderPath);
const char* fShaderString = temp.c_str();
vShaderHandle = glCreateShader(GL_VERTEX_SHADER);
fShaderHandle = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShaderHandle, 1, &vShaderString, NULL);
glShaderSource(fShaderHandle, 1, &fShaderString, NULL);
GLint compiled;
glCompileShader(vShaderHandle);
glCompileShader(fShaderHandle);
glGetShaderiv(vShaderHandle, GL_COMPILE_STATUS, &compiled);
if(compiled == false)
{
fprintf(stderr, "ERROR: Vertex shader not compiled properly.\n");
GLint blen = 0;
GLsizei slen = 0;
glGetShaderiv(vShaderHandle, GL_INFO_LOG_LENGTH , &blen);
if (blen > 1)
{
GLchar* compiler_log = new GLchar[blen];
glGetInfoLogARB(vShaderHandle, blen, &slen, compiler_log);
fprintf(stderr, "compiler log:\n %s", compiler_log);
delete [] compiler_log;
}
}
but when I run my program, I get an output of:
INFO: OpenGL Version: 3.3.0 NVIDIA 310.19
File: vShader.v.glsl
Contents: #version 330
layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
void main(void)
{
gl_Position = in_Position;
ex_Color = in_Color;
}
ERROR: Vertex shader not compiled properly.
compiler log:
(0) : error C0000: syntax error, unexpected $end at token "<EOF>"
Loadfile is defined:
string ShaderEffect::LoadFile(string path)
{
ifstream in(path.c_str(), ios::in);
if(in.is_open())
{
string contents;
in.seekg(0, ios::end);
contents.resize(in.tellg());
in.seekg(0, ios::beg);
in.read(&contents[0], contents.size());
in.close();
return contents;
}
else
throw "Problem reading file!";
}
I know the glsl code itself is not the problem because I have hard coded the string in:
const char * testvShader = {
"#version 330\n"\
"layout(location=0) in vec4 in_Position;\n"\
"layout(location=1) in vec4 in_Color;\n"\
"out vec4 ex_Color;"
"void main()\n"\
"{\n"\
" gl_Position = in_Position;\n"\
" ex_Color = in_Color;\n"\
"}"};
and when I switch &vShaderString to &testvShader the program runs fine. But I don't see how the loading can be the problem, because the fragment shader loads the same way and compiles and runs perfectly fine, and I print the file to the console before compiling it and it looks fine. I'm at my wits end, I can't figure out what the issue is.
P.S. I'm running on Fedora, if that matters.
You call c_str() on temp and store the result in a variable, and then you modify temp and use the saved result of c_str(). This doesn't work because the result of c_str() becomes invalid if you modify the string.
This compiler error is usually caused by trailing garbage. The most simple remedy is telling OpenGL which length the source string has exactly.
Replace your glShaderSource calls with
const char* vShaderString = temp.c_str();
int vertex_shader_string_length = temp.length();
/* ... */
const char* fShaderString = temp.c_str();
int fragment_shader_string_length = temp.length();
glShaderSource(vShaderHandle, 1, &vShaderString, &vertex_shader_string_length);
glShaderSource(fShaderHandle, 1, &fShaderString, &fragment_shader_string_length);

OpenGL - Problems getting shaders to compile

Finally isolated the issue of my shader not being able to be used to it failing to compile.
Here is my shader loading routine. The first part reads in the shader:
void GLSLShader::LoadFromFile(GLenum whichShader, const string filename)
{
ifstream fp;
// Attempt to open the shader
fp.open(filename.c_str(), ios_base::in);
// If the file exists, load it
if(fp)
{
// Copy the shader into the buffer
string buffer(std::istreambuf_iterator<char>(fp), (std::istreambuf_iterator<char>()));
// Debug output to show full text of shader
errorLog.writeSuccess("Shader debug: %s", buffer.c_str());
LoadFromString(whichShader, buffer);
}
else
{
errorLog.writeError("Could not load the shader %s", filename.c_str());
}
}
After it has loaded it into a string, it sends it to be loaded:
void GLSLShader::LoadFromString(GLenum type, const string source)
{
// Create the shader
GLuint shader = glCreateShader(type);
// Convert the string
const char * ptmp = source.c_str();
glShaderSource(shader, 1, &ptmp, NULL);
// Compile the shader
glCompileShader(shader);
// Check to see if the shader has loaded
GLint status;
glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
GLint infoLogLength;
glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar *infoLog= new GLchar[infoLogLength];
glGetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
errorLog.writeError("could not compile: %s", infoLog);
delete [] infoLog;
}
_shaders[_totalShaders++]=shader;
}
I am getting the debug output "could not compile: " and it seems that the error buffer is empty. This has been driving me crazy for the past 3 days. Does anyone see my error?
Here are the very simple shaders:
#version 330
layout (location = 0) in vec4 position;
layout (location = 1) in vec4 color;
smooth out vec4 theColor;
void main()
{
gl_Position = position;
theColor = color;
}
#version 330
smooth in vec4 theColor;
out vec4 outputColor;
void main()
{
outputColor = theColor;
}
UPDATE
It looks like for some reason there is crap being added to the end of the shader in memory. I am not sure why. I have tried reading it in to a char* and a string. Here is the output:
<-!-> Shader: #version 330
layout (location = 0) in vec4 position;
layout (location = 1) in vec4 color;
smooth out vec4 theColor;
void main()
{
gl_Position = position;
theColor = color;
}
n
<-!-> Shader: #version 330
smooth in vec4 theColor;
out vec4 outputColor;
void main()
{
outputColor = theColor;
}
nts/Resources
Notice the 'n' at the end of the first shader and the 'nts/Resources' at the end of the second one. Any idea why?
ANOTHER UPDATE
The crap at the end was caused by an extra line at the end. I removed it and it is back to outputting the correct shader text. Still no luck thought with compiling.
I am at a loss here. The Superbible has their extensive libraries that are unoptimized and I don't need. There doesn't seem to be a damn decent shader compiler for Mac. I really don't mind how simple it is. It just needs to work with shaders. Does anyone have an example?
You're calling glShaderSource without supplying the length of each string in the sources array. Hence the string supplied is assumed to be terminated by a null byte ('\0'). The way you're reading the file into memory does not terminate the shader source with an additional null byte. So the GLSL compiler will read beyond the end of the shader source into random memory, where it finds… garbage.
Solution: Either add a terminating null byte, or supply the shader text lengths parameter.