c++ - OpenGL 着色器编译错误

标签 c++ opengl shader

我的着色器编译代码有点问题,即它们都注册为失败的编译并且没有收到日志。

这是着色器编译代码:

/* 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/

相关文章:

c++ - 如何在 C++ 头文件上使用 gdb?

c++ - 简单的 MFC 应用程序

c++ - 在 openGL 中打印一个红色三角形

c++ - “glGetTexLevelParameter”未在此范围内声明

c - 为什么我在创建着色器类型 35663 时遇到运行时错误

c++ - 如何在 VC++/MFC 应用程序中根据当前 DPI 设置缩放字体大小?

c++ - 格式化输出c++的问题

c++ - 如何清除opengl着色器缓存

javascript - 将 vec2 数组传递给 THREE.js 中的着色器

unity3d - 根据世界空间中的光线转换命中位置绘制到对象的纹理上