Essentially, I initialize my shaders like so:
//Graphics variables
GLenum errorChk;
GLuint vsId[2],fsId[2],prgId[2]; //First entry is for shadow, second is for main
GLchar vsTxt[2][0x4000] = {0},fsTxt[2][0x4000] = {0};
GLint status[1];
GLchar infoLog[1024] = {0};
GLuint vaoId;
GLuint vboId[8];
GLuint texId[5];
GLuint fbufId;
GLuint zTex;
//////////////////////////////////////////
//CODE HAS BEEN OMITTED HERE FOR CLARITY//
//////////////////////////////////////////
void InitShaders() {
//Read shader text into buffers
std::ifstream in("Assets\\Shader\\ShadowMap.txvs",std::ifstream::binary);
in.seekg(0,in.end);
int len = in.tellg();
in.seekg(0,in.beg);
in.read(vsTxt[0],len);
in.close();
in.open("Assets\\Shader\\ShadowMap.txfs",std::ifstream::binary);
in.seekg(0,in.end);
len = in.tellg();
in.seekg(0,in.beg);
in.read(fsTxt[0],len);
in.close();
in.open("Assets\\Shader\\FakePBR.txvs",std::ifstream::binary);
in.seekg(0,in.end);
len = in.tellg();
in.seekg(0,in.beg);
in.read(vsTxt[1],len);
in.close();
in.open("Assets\\Shader\\FakePBR.txfs",std::ifstream::binary);
in.seekg(0,in.end);
len = in.tellg();
in.seekg(0,in.beg);
in.read(fsTxt[1],len);
in.close();
printf("VERTEX SHADER 1\n===============\n%s\n\n",vsTxt[0]);
printf("FRAGMENT SHADER 1\n=================\n%s\n\n",fsTxt[0]);
printf("VERTEX SHADER 2\n===============\n%s\n\n",vsTxt[1]);
printf("FRAGMENT SHADER 2\n=================\n%s\n\n",fsTxt[1]);
//Compile and link shaders
errorChk = glGetError();
for(int i=0; i<2; i++) {
vsId[i] = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vsId[i],1,(const GLchar**)&vsTxt[i],NULL);
glCompileShader(vsId[i]);
fsId[i] = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fsId[i],1,(const GLchar**)&fsTxt[i],NULL);
glCompileShader(fsId[i]);
prgId[i] = glCreateProgram();
glAttachShader(prgId[i],vsId[i]);
glAttachShader(prgId[i],fsId[i]);
glLinkProgram(prgId[i]);
glUseProgram(prgId[i]);
errorChk = glGetError();
if(errorChk!=GL_NO_ERROR) {
fprintf(stderr,"ERROR GE10: Could not create shaders (index %i): \n%s\n\n",i,gluErrorString(errorChk));
glGetShaderiv(vsId[i],GL_COMPILE_STATUS,status);
if((*status)!=GL_TRUE) {
glGetShaderInfoLog(vsId[i],1024,NULL,infoLog);
fprintf(stderr,"ERROR GE11: Vertex shader compile error: \n%s\n\n",infoLog);
}
glGetShaderiv(fsId[i],GL_COMPILE_STATUS,status);
if((*status)!=GL_TRUE) {
glGetShaderInfoLog(fsId[i],1024,NULL,infoLog);
fprintf(stderr,"ERROR GE12: Fragment shader compile error: \n%s\n\n",infoLog);
}
glGetProgramiv(prgId[i],GL_LINK_STATUS,status);
if((*status)!=GL_TRUE) {
glGetShaderInfoLog(prgId[i],1024,NULL,infoLog);
fprintf(stderr,"ERROR GE13: Program link error: \n%s\n\n",infoLog);
}
exit(-1);
}
}
//Init uniform pointers
errorChk = glGetError();
glUseProgram(prgId[1]);
glUniform1i(glGetUniformLocation(prgId[1],"u_Albedo"),0);
glUniform1i(glGetUniformLocation(prgId[1],"u_NormalMap"),1);
glUniform1i(glGetUniformLocation(prgId[1],"u_Roughness"),2);
glUniform1i(glGetUniformLocation(prgId[1],"u_Metalness"),3);
glUniform1i(glGetUniformLocation(prgId[1],"u_Emission"),4);
errorChk = glGetError();
if(errorChk!=GL_NO_ERROR) {
fprintf(stderr,"ERROR GE14: Could not create uniforms: \n%s\n\n",gluErrorString(errorChk));
exit(-1);
}
//Init array buffers
glGenVertexArrays(1,&vaoId);
glBindVertexArray(vaoId);
glGenBuffers(8,&vboId[0]);
glGenTextures(5,texId);
errorChk = glGetError();
if(errorChk!=GL_NO_ERROR) {
fprintf(stderr,"ERROR GE15: Could not create vertex buffers: \n%s\n\n",gluErrorString(errorChk));
exit(-1);
}
//Init other shader data
vsd.u_ShadowBias = glm::mat4(0.5,0.0,0.0,0.0,
0.0,0.5,0.0,0.0,
0.0,0.0,0.5,0.0,
0.5,0.5,0.5,1.0);
}
However, I end up with the following error after running this (yes, the shader text is loaded correctly):
VERTEX SHADER 1
===============
#version 330 core
layout(location=0) in vec3 a_Position;
layout(location=6) in ivec4 a_Joints;
layout(location=7) in vec4 a_Weights;
layout(std140) uniform u_VertexShaderData
{
mat4 u_ShadowBias;
mat4 u_Model;
mat4 u_View;
mat4 u_Projection;
mat4 u_Bones[60];
};
void main()
{
vec4 o_Position = vec4(0.0);
for(int i=0; i<4; i++)
{
mat4 m_Bone = u_Bones[a_Joints[i]];
o_Position += (m_Bone*vec4(a_Position,1.0))*a_Weights[i];
}
gl_Position = u_Projection*u_View*u_Model*o_Position;
}
FRAGMENT SHADER 1
=================
#version 330 core
out float o_FragDepth;
void main()
{
o_FragDepth = gl_FragCoord.z;
}
VERTEX SHADER 2
===============
#version 330 core
layout(location=0) in vec3 a_Position;
layout(location=1) in vec3 a_Normal;
layout(location=2) in vec4 a_Tangent;
layout(location=6) in ivec4 a_Joints;
layout(location=7) in vec4 a_Weights;
layout(std140) uniform u_VertexShaderData
{
mat4 u_ShadowBias;
mat4 u_Model;
mat4 u_View;
mat4 u_Projection;
mat4 u_Bones[60];
};
out vec4 o_Position;
out vec4 o_Normal;
out vec4 o_Tangent;
out vec4 o_Bitangent;
out vec4 o_ShadowCoord;
void main()
{
vec4 v_Position = vec4(a_Position,1.0);
vec4 v_Normal = vec4(a_Normal,0.0);
vec4 v_Tangent = vec4(a_Tangent,0.0);
vec4 v_Bitangent = vec4(cross(a_Normal,a_Tangent),0.0);
o_Position = vec4(0.0);
o_Normal = vec4(0.0);
o_Tangent = vec4(0.0);
o_Bitangent = vec4(0.0);
for(int i=0; i<4; i++)
{
mat4 m_Bone = u_Bones[a_Joints[i]];
o_Position += (m_Bone*v_Position)*a_Weights[i];
o_Normal += (m_Bone*v_Normal)*a_Weights[i];
o_Tangent += (m_Bone*v_Tangent)*a_Weights[i];
o_Bitangent += (m_Bone*v_Bitangent)*a_Weights[i];
}
mat4 m_Position = u_View*u_Model;
mat4 m_Normal = transpose(inverse(m_Position));
mat4 m_Position = u_Projection*m_Position;
o_Normal = normalize(m_Normal*o_Normal);
o_Tangent = normalize(m_Normal*o_Tangent)*a_Tangent.w;
o_Bitangent = normalize(m_Normal*o_Bitangent);
gl_Position = m_Position*o_Position;
o_ShadowCoord = u_ShadowBias*gl_Position;
}
FRAGMENT SHADER 2
=================
#version 330 core
layout(location=4) in vec2 a_UV;
in vec4 o_Position;
in vec4 o_Normal;
in vec4 o_Tangent;
in vec4 o_Bitangent;
in vec4 o_ShadowCoord;
const float u_EnvSize = 9.0;
const float u_IrrLod = 6.0;
uniform vec4 u_LightDirection;
uniform sampler2DShadow u_ShadowMap;
uniform samplerCube u_Radiance;
uniform sampler2D u_Albedo;
uniform sampler2D u_NormalMap;
uniform sampler2D u_Roughness;
uniform sampler2D u_Metalness;
uniform sampler2D u_Emission;
out vec4 o_FragColor;
vec3 sampleHDR(vec4 c)
{
return vec3(c.rgb*exp2((c.a*255.0)-128.0));
}
float sampleShadow()
{
return 1.0;
//float bias = 0.005*tan(acos(dot(o_Normal,normalize(-u_LightDirection))));
//bias = clamp(bias,0.0,0.01);
//return 0.5+0.5*step(o_ShadowCoord.z-bias/o_ShadowCoord.w,texture(u_ShadowMap,o_ShadowCoord.xy));
}
void main()
{
vec4 v_Albedo = texture(u_Albedo,a_UV);
vec4 v_NormalMap = texture(u_NormalMap,a_UV);
vec4 v_Normal = (o_Tangent*v_NormalMap.r);
v_Normal += (o_Bitangent*v_NormalMap.g);
float f_NormalMapB = sqrt(1.0-dot(v_NormalMap.rg,v_NormalMap.rg));
v_Normal = normalize(v_Normal+(o_Normal*f_NormalMapB))*o_Tangent.w;
vec4 v_Reflect = reflect(v_Normal,vec3(0.0,0.0,-1.0,0.0));
float f_Roughness = u_EnvSize*texture(u_Roughness,a_UV).r;
float f_Metalness = texture(u_Metalness,a_UV).r;
vec3 v_Emission = texture(u_Metalness,a_UV).rgb;
float f_Emission = (0.3*v_Emission.r)+(0.6*v_Emission.g)+(0.1*v_Emission.b);
vec3 v_Specular = vec3(1.0);
//vec3 v_Specular = vec3(max(0.0,pow(dot(v_Reflect,vec4(0.577,0.577,0.577,0.0)),4.0+(44.0*f_Roughness))));
//vec3 v_Specular = sampleHDR(textureLod(u_Radiance,v_Reflect,f_Roughness));
vec3 v_Diffuse = vec3(0.5);
//vec3 v_Diffuse = vec3(0.5*max(0.0,dot(v_Normal,vec4(0.577,0.577,0.577,0.0))));
//vec3 v_Diffuse = sampleHDR(textureLod(u_Radiance,v_Normal,u_IrrLod));
float f_Shadow = sampleShadow();
float f_Fresnel = 0.04+0.96*pow(max(0.0,1.0-dot(v_Normal,vec4(0.0,0.0,-1.0,0.0))),5.0);
vec3 v_Metal0 = v_Albedo.rgb*v_Diffuse;
vec3 v_Metal1 = v_Albedo.rgb*v_Specular;
vec3 resultC = (f_Shadow*mix(mix(v_Metal0,v_Metal1,f_Metalness),v_Specular,f_Fresnel))+v_Emission;
float resultA = mix(v_Albedo.a,1.0,f_Fresnel)+f_Emission;
o_FragColor = vec4(resultC,resultA);
}
ERROR GE10: Could not create shaders (index 0):
invalid operation
ERROR GE11: Vertex shader compile error:
ERROR: 0:1: '' : syntax error: #version is mandatory and should be set before any other token
ERROR GE12: Fragment shader compile error:
ERROR: 0:1: '' : syntax error: #version is mandatory and should be set before any other token
ERROR GE13: Program link error:
ERROR: 0:1: '' : syntax error: #version is mandatory and should be set before any other token
What is wrong, and how can I fix it? I’m running on an Intel HD 3000 GPU, and Windows 10 x64 OS.