簡體   English   中英

OpenGL-獲取着色器進行編譯的問題

[英]OpenGL - Problems getting shaders to compile

最終隔離了我的着色器無法適應它無法編譯的問題。

這是我的着色器加載例程。 第一部分讀取着色器:

void GLSLShader::LoadFromFile(GLenum whichShader, const string filename)
{   
    ifstream fp;

    // Attempt to open the shader
    fp.open(filename.c_str(), ios_base::in);

    // If the file exists, load it
    if(fp) 
    {
        // Copy the shader into the buffer
        string buffer(std::istreambuf_iterator<char>(fp), (std::istreambuf_iterator<char>()));

        // Debug output to show full text of shader
        errorLog.writeSuccess("Shader debug: %s", buffer.c_str());

        LoadFromString(whichShader, buffer);
    } 
    else 
    {
        errorLog.writeError("Could not load the shader %s", filename.c_str());
    }
}

將其加載到字符串中后,將其發送以進行加載:

void GLSLShader::LoadFromString(GLenum type, const string source) 
{
    // Create the shader
    GLuint shader = glCreateShader(type);

    // Convert the string
    const char * ptmp = source.c_str();
    glShaderSource(shader, 1, &ptmp, NULL);

    // Compile the shader
    glCompileShader(shader);

    // Check to see if the shader has loaded
    GLint status;
    glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
    if (status == GL_FALSE) {
        GLint infoLogLength;
        glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
        GLchar *infoLog= new GLchar[infoLogLength];
        glGetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
        errorLog.writeError("could not compile: %s", infoLog);
        delete [] infoLog;
    }
    _shaders[_totalShaders++]=shader;
}

我收到調試輸出“無法編譯:”,並且似乎錯誤緩沖區為空。 在過去三天里,這一直使我發瘋。 有人看到我的錯誤嗎?

這是非常簡單的着色器:

#version 330

layout (location = 0) in vec4 position;
layout (location = 1) in vec4 color;

smooth out vec4 theColor;

void main()
{
    gl_Position = position;
    theColor = color;
}



#version 330

smooth in vec4 theColor;

out vec4 outputColor;

void main()
{
    outputColor = theColor;
}

更新

由於某種原因,似乎在內存中着色器的末尾添加了廢話。 我不知道為什么。 我嘗試將其讀入char *和字符串。 這是輸出:

<-!-> Shader: #version 330

layout (location = 0) in vec4 position;
layout (location = 1) in vec4 color;

smooth out vec4 theColor;

void main()
{
    gl_Position = position;
    theColor = color;
}
n
<-!-> Shader: #version 330

smooth in vec4 theColor;

out vec4 outputColor;

void main()
{
    outputColor = theColor;
}
nts/Resources

請注意,第一個着色器的末尾為“ n”,第二個着色器的末尾為“ nts / Resources”。 知道為什么嗎?

另一個更新

最后的廢話是由結尾處的多余行引起的。 我刪除了它,然后又回到了輸出正確的着色器文本的狀態。 仍然沒有想到編譯。

我在這里茫然。 超級聖經有大量未優化的庫,我不需要。 Mac似乎沒有該死的不錯的着色器編譯器。 我真的不介意它有多簡單。 它只需要使用着色器即可。 有人有例子嗎?

您在調用glShaderSource未提供源數組中每個字符串的長度。 因此,假定提供的字符串以空字節('\\ 0')結尾。 您將文件讀入內存的方式不會以其他空字節終止着色器源。 因此GLSL編譯器將在着色器源代碼的末尾讀取到隨機存儲器中,並在其中找到...垃圾。

解決方案:添加終止的空字節,或提供着色器文本長度參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM