我有一个小类,允许我加载着色器并在我的程序中使用它们。我能够编译着色器,但是当需要链接它们时,他们就是不想这样做。使用 glGetProgramInfoLog 我得到以下错误日志。我不明白为什么它告诉我没有定义程序,因为编译工作正常......
Vertex info
-----------
(0) : error C3001: no program defined
Fragment info
-------------
(0) : error C3001: no program defined
下面的代码执行 GLSL 链接:
program_ = glCreateProgramObjectARB();
if (vertex_) {
glAttachObjectARB(program_, vertex_);
}
if (fragment_) {
glAttachObjectARB(program_, fragment_);
}
glLinkProgramARB(program_);
vertex_ 和 fragment_ 是顶点和片段着色器,program_ 是 ProgramObjectARB。
我想知道为了使这个运行我需要做哪些必要的修改。
顶点着色器:
varying vec4 OCPosition; // surface positon in world's coordinates
varying vec4 ECposition; // surface position in eye coordinates
varying vec4 ECballCenter; // ball center in eye coordinates
uniform vec4 BallCenter; // ball center in modelling coordinates
void main()
{
OCPosition = gl_Vertex;
ECposition = gl_ModelViewMatrix * OCPosition;
ECballCenter = gl_ModelViewMatrix * BallCenter;
gl_Position = gl_ProjectionMatrix * ECposition;
}
片段着色器:
varying vec4 OCPosition; // surface position in world coordinates
varying vec4 ECposition; // surface position in eye coordinates
varying vec4 ECballCenter; // ball center in eye coordinates
uniform vec4 LightDir; // light direction, should be normalized
uniform vec4 HVector; // reflection vector for infinite light source
uniform vec4 SpecularColor;
uniform vec4 BaseColor, StripeColor;
uniform float StripeWidth; // = 0.3
uniform float FWidth; // = 0.005
void main()
{
vec3 normal; // Analytically computed normal
vec4 p; // Point in shader space
vec4 surfColor; // Computed color of the surface
float intensity; // Computed light intensity
vec4 distance; // Computed distance values
float inorout; // Counter for computing star pattern
p.xyz = normalize(OCPosition.xyz); // Calculate p
p.w = 1.0;
distance.y = StripeWidth - abs(p.z);
distance.xy = smoothstep(-FWidth, FWidth, distance.xy);
surfColor = mix(BaseColor, StripeColor, distance.y);
// normal = point on surface for sphere at (0,0,0)
normal = normalize(ECposition.xyz - ECballCenter.xyz);
// Per fragment diffuse lighting
intensity = 0.2; // ambient
intensity += 0.8 * clamp(dot(LightDir.xyz, normal), 0.0, 1.0);
surfColor *= intensity;
// Per fragment specular lighting
intensity = clamp(dot(HVector.xyz, normal), 0.0, 1.0);
intensity = pow(intensity, SpecularColor.a);
surfColor += SpecularColor * intensity;
gl_FragColor = surfColor;
}
编辑 - 解决方案 我用来读取源文件的方法似乎不正确。着色器可以编译,但在链接时,它不能正常工作。现在我使用这种方法来读取我的文件并且链接是正确的:
char *textFileRead(char *fn) {
FILE *fp;
char *content = NULL;
int count=0;
if (fn != NULL) {
fp = fopen(fn,"rt");
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > 0) {
content = (char *)malloc(sizeof(char) * (count+1));
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}