我的着色器编译代码有点问题,即它们都注册为失败的编译并且没有收到日志。
这是着色器编译代码:
/* Make the shader */
Uint size;
GLchar* file;
loadFileRaw(filePath, file, &size);
const char * pFile = file;
const GLint pSize = size;
newCashe.shader = glCreateShader(shaderType);
glShaderSource(newCashe.shader, 1, &pFile, &pSize);
glCompileShader(newCashe.shader);
GLint shaderCompiled;
glGetShaderiv(newCashe.shader, GL_COMPILE_STATUS, &shaderCompiled);
if(shaderCompiled == GL_FALSE)
{
ReportFiler->makeReport("ShaderCasher.cpp", "loadShader()", "Shader did not compile", "The shader " + filePath + " failed to compile, reporting the error - " + OpenGLServices::getShaderLog(newCashe.shader));
}
这些是支持函数:
bool loadFileRaw(string fileName, char* data, Uint* size)
{
if (fileName != "")
{
FILE *file = fopen(fileName.c_str(), "rt");
if (file != NULL)
{
fseek(file, 0, SEEK_END);
*size = ftell(file);
rewind(file);
if (*size > 0)
{
data = (char*)malloc(sizeof(char) * (*size + 1));
*size = fread(data, sizeof(char), *size, file);
data[*size] = '\0';
}
fclose(file);
}
}
return data;
}
string OpenGLServices::getShaderLog(GLuint obj)
{
int infologLength = 0;
int charsWritten = 0;
char *infoLog;
glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 0)
{
infoLog = (char *)malloc(infologLength);
glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
string log = infoLog;
free(infoLog);
return log;
}
return "<Blank Log>";
}
以及我正在加载的着色器:
void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
void main(void)
{
gl_Position = ftransform();
}
简而言之,我明白了
From: ShaderCasher.cpp, In: loadShader(), Subject: Shader did not compile
Message: The shader Data/Shaders/Standard/standard.vs failed to compile, reporting the error - <Blank Log>
对于我编译的每个着色器。
我试过用硬编码字符串替换读取的文件,但我得到了同样的错误,所以我编译它们的方式一定有问题。我已经运行并编译了带有着色器的示例程序,所以我怀疑我的驱动程序是问题所在,但无论如何我使用的是 Nvidia 8600m GT。
有人能帮忙吗?
最佳答案
在loadFileRaw
, 你通过 char *data
按值(value)。然后你分配给它:
data = (char*)malloc(sizeof(char) * (*size + 1));
所以你的电话
loadFileRaw(filePath, file, &size);
不改变file
的值!改file
, 通过指针传入:
loadFileRaw(filePath, &file, &size);
然后将您的功能更改为
bool loadFileRaw(string fileName, char** data, Uint* size) { ... }
在其中,
*data = (char*)malloc(sizeof(char) * (*size + 1));
等(对于 data
的其余引用)。
也就是说,既然您使用的是 C++,请考虑使用 std::vector<char>
或 std::string
用于您的内存管理。为了让你开始,
{
std::vector<char> data;
data.resize(100); // now it has 100 entries, initialized to zero
silly_C_function_that_takes_a_char_pointer_and_a_size(&data[0], 100); // OK
} // presto, memory freed
此外,您可以考虑使用 std::ifstream
用于读取文件,而不是使用 FILE *
.
关于c++ - OpenGL 着色器编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2923303/