Original Post
I was following some online tutorials about opengl and shader code and I encoutered a problem. They compiled their shader code from const char* variables but I wanted to find out how to do the same thing but reading the shaders from files. The problem I am having is this, when I run my program, I get no error messages from visual studio 2010 but in the console, I get this error message:
Error compiling shader type 35633: '0<1> : error c0000: syntax error, unexpected, $undefined at token ""
I ran the same shader code before when it was in a string and it worked just fine. Im reading the file with this function:
[source lang="cpp"]const char* readShaderFile(const std::string fileName)
{
std::ifstream shaderFile( fileName.c_str() );
// find the file size
shaderFile.seekg(0,std::ios::end);
std::streampos length = shaderFile.tellg();
shaderFile.seekg(0,std::ios::beg);
// read whole file into a vector:
std::vector buffer(length);
shaderFile.read(&buffer[0],length);
// convert to GLchar
const char* Shader = (std::string( buffer.begin(), buffer.end() )).c_str();
// return the shader string
return Shader;
}[/source]
and my shader code is here:
[source lang="cpp"]#version 330
in vec2 TexCoord0;
in vec3 Normal0;
out vec4 FragColor;
struct DirectionalLight
{
vec3 Color;
float AmbientIntensity;
float DiffuseIntensity;
vec3 Direction;
};
uniform DirectionalLight gDirectionalLight;
//uniform sampler2D gSampler;
void main()
{
vec4 AmbientColor = vec4(gDirectionalLight.Color, 1.0f) *
gDirectionalLight.AmbientIntensity;
float DiffuseFactor = dot(normalize(Normal0), -gDirectionalLight.Direction);
vec4 DiffuseColor;
if (DiffuseFactor > 0) {
DiffuseColor = vec4(gDirectionalLight.Color, 1.0f) *
gDirectionalLight.DiffuseIntensity *
DiffuseFactor;
}
else {
DiffuseColor = vec4(0, 0, 0, 0);
}
//FragColor = texture2D(gSampler, TexCoord0.xy) * (AmbientColor + DiffuseColor);
FragColor = vec4(1.0,0.0,0.0,1.0)*(AmbientColor + DiffuseColor);
}[/source]
Error compiling shader type 35633: '0<1> : error c0000: syntax error, unexpected, $undefined at token "
I ran the same shader code before when it was in a string and it worked just fine. Im reading the file with this function:
[source lang="cpp"]const char* readShaderFile(const std::string fileName)
{
std::ifstream shaderFile( fileName.c_str() );
// find the file size
shaderFile.seekg(0,std::ios::end);
std::streampos length = shaderFile.tellg();
shaderFile.seekg(0,std::ios::beg);
// read whole file into a vector:
std::vector
shaderFile.read(&buffer[0],length);
// convert to GLchar
const char* Shader = (std::string( buffer.begin(), buffer.end() )).c_str();
// return the shader string
return Shader;
}[/source]
and my shader code is here:
[source lang="cpp"]#version 330
in vec2 TexCoord0;
in vec3 Normal0;
out vec4 FragColor;
struct DirectionalLight
{
vec3 Color;
float AmbientIntensity;
float DiffuseIntensity;
vec3 Direction;
};
uniform DirectionalLight gDirectionalLight;
//uniform sampler2D gSampler;
void main()
{
vec4 AmbientColor = vec4(gDirectionalLight.Color, 1.0f) *
gDirectionalLight.AmbientIntensity;
float DiffuseFactor = dot(normalize(Normal0), -gDirectionalLight.Direction);
vec4 DiffuseColor;
if (DiffuseFactor > 0) {
DiffuseColor = vec4(gDirectionalLight.Color, 1.0f) *
gDirectionalLight.DiffuseIntensity *
DiffuseFactor;
}
else {
DiffuseColor = vec4(0, 0, 0, 0);
}
//FragColor = texture2D(gSampler, TexCoord0.xy) * (AmbientColor + DiffuseColor);
FragColor = vec4(1.0,0.0,0.0,1.0)*(AmbientColor + DiffuseColor);
}[/source]