gpt4 book ai didi

c++ - 没有有用信息的 GLSL 链接失败

转载 作者:行者123 更新时间:2023-11-28 00:27:22 25 4
gpt4 key购买 nike

此处类似标题问题:GLSL:shader linking fail (but no log)但就我而言,顶点着色器和片段着色器都非常简单,输入/输出变量匹配如下所列。

[编辑] 加载着色器的代码列在下方。由于 VS2010 不支持基于范围的 for 循环,部分代​​码是 #ifdef 的。但无论如何......我已经在 MinGW 32 位环境中尝试了代码并且它链接正常。

使用 VS2010 构建时,着色器链接成功并运行良好,但使用 NetBeans + MinGW-w64 失败并给出此日志消息:

Link info
---------
No shader objects attached.

会不会是与 MinGW-w64 OpenGL 库相关的东西?

这是我的顶点着色器,

#version 330

in vec4 vPosition;
in vec4 vColor;

out vec4 color;

void main()
{
color = vColor;
gl_Position = vPosition;
}

这是我的片段着色器。

#version 330

in vec4 color;
out vec4 fColor;

void main()
{
fColor = color;
}

负载着色器.h:

typedef struct {
GLenum type;
const char* filename;
GLuint shader;
} ShaderInfo;

主要.cpp:

vector<ShaderInfo> shaders;
ShaderInfo vert = {GL_VERTEX_SHADER, "SimpleVertexShader.vert"};
ShaderInfo frag = {GL_FRAGMENT_SHADER, "SimpleFragmentShader.frag"};
shaders.push_back(vert);
shaders.push_back(frag);

program = LoadShaders(shaders);

LoadShader.cpp - LoadShaders()

GLuint LoadShaders(vector<ShaderInfo> shaders)
{
if (shaders.empty()) return 0;

#if !defined(_MSC_VER) || 1600 < _MSC_VER
for (auto entry : shaders)
entry.shader = CreateShader(entry.type, entry.filename);
#else
for (vector<ShaderInfo>::iterator entry = shaders.begin(); entry != shaders.end(); ++entry)
entry->shader = CreateShader(entry->type, entry->filename);
#endif
// Create the program
return CreateProgram(shaders);
}

LoadShader.cpp - CreateShader()

GLuint CreateShader(GLenum shaderType, const char* shader_file_path)
{
// Create the shader
GLuint shaderID = glCreateShader(shaderType);
if (!shaderID)
return 0;

// Read the shader code from the file
std::string shaderCode;
std::ifstream shaderStream(shader_file_path, std::ios::in);
if(shaderStream.is_open())
{
std::string Line = "";
while(getline(shaderStream, Line))
shaderCode += "\n" + Line;
shaderStream.close();
}

// Compile the shader
printf("Compiling shader : %s\n", shader_file_path);
char const* sourcePointer = shaderCode.c_str();
glShaderSource(shaderID, 1, &sourcePointer , NULL);
glCompileShader(shaderID);

// Check the shader
GLint compiled;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLsizei len;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &len);

GLchar* log = new GLchar[len+1];
glGetShaderInfoLog(shaderID, len, &len, log);
std::cerr << "Shader compilation failed: " << log << std::endl;
delete [] log;

return 0;
}

return shaderID;
}

LoadShader.cpp - CreateProgram()

GLuint CreateProgram(vector<ShaderInfo> shaders)
{
// Create and link the program
fprintf(stdout, "Linking program\n");
GLuint programID = glCreateProgram();
if (!programID)
return 0;

// attach shaders and link the program
#if !defined(_MSC_VER) || 1600 < _MSC_VER
for (auto iter : shaders)
glAttachShader(programID, iter.shader);
#else
for (vector<ShaderInfo>::iterator iter = shaders.begin(); iter != shaders.end(); ++iter)
glAttachShader(programID, iter->shader);
#endif
glLinkProgram(programID);

// Check the program
GLint linked;
glGetProgramiv(programID, GL_LINK_STATUS, &linked);
if (!linked) {
GLsizei len;
glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &len);

GLchar* log = new GLchar[len+1];
glGetProgramInfoLog(programID, len, &len, log);
std::cerr << "Shader linking failed: " << log << std::endl;
delete [] log;

#if !defined(_MSC_VER) || 1600 < _MSC_VER
for (auto iter : shaders) {
glDeleteShader(iter.shader);
iter.shader = 0;
}
#else
for (vector<ShaderInfo>::iterator iter = shaders.begin(); iter != shaders.end(); ++iter) {
glDeleteShader(iter->shader);
iter->shader = 0;
}
#endif

return 0;
}

return programID;
}

最佳答案

我认为您在 LoadShaders 中有错误。 C++ 11 版本的循环:

for (auto entry : shaders)
entry.shader = CreateShader(entry.type, entry.filename);

获取着色器中每个条目的拷贝,因此 vector 中的元素不会更新。试试看:

for (auto &entry : shaders)
entry.shader = CreateShader(entry.type, entry.filename);

顺便说一句,如果你想支持旧的编译器,你还不如只使用旧版本的循环。

关于c++ - 没有有用信息的 GLSL 链接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24321873/

25 4 0