gpt4 book ai didi

opengl - 无法链接已编译的着色器 (GLSL)

转载 作者:行者123 更新时间:2023-12-01 07:32:05 27 4
gpt4 key购买 nike

我有一个小类,它允许我加载着色器并在我的程序中使用它们。我能够编译着色器,但是当需要链接它们时,他们只是不想这样做。使用 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;
}

最佳答案

来自 Nvidia 驱动程序中 GLSL 编译器的特定错误代码意味着它在您的着色器中找不到名为“main”的函数(在这种情况下是顶点或片段)

最常见的原因是您实际上并没有在您认为的情况下将程序放入驱动程序中 - 您传入的字符串尚未初始化,或者您传入的长度为 0,而你实际上并没有编译任何东西。

编辑

我最好的猜测是 glCreateShader 或 glSetShaderSource 调用有一些微妙的错误。大多数情况下,错误仅在您尝试链接着色器(而不是编译)时出现,因为这是驱动程序第一次知道它有完整的着色器,而不仅仅是部分、不完整的着色器,需要另一个着色器链接才能完成.

关于opengl - 无法链接已编译的着色器 (GLSL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1938466/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com