gpt4 book ai didi

用于 Sampler2D 的 c++ OpenGL glGetUniformLocation 在 Raspberry PI 上返回 -1 但在 Windows 上工作

转载 作者:太空狗 更新时间:2023-10-29 23:18:02 25 4
gpt4 key购买 nike

我正在制作一个跨平台的 OpenGL 程序。但是,我遇到了一个问题,glGetUniformLocation 应该返回我的着色器程序中统一变量的位置,返回 -1,并且它只发生在 Linux(Raspbian 发行版,在 Raspberry PI 上运行)和 Windows 上相同的代码完美运行!这是我的代码:

Load Shader程序函数:

int shader, status;

programID = glCreateProgram();

// Load vertex shader
shader = LoadShaderFromString(GL_VERTEX_SHADER, Tools::GetFileAsString("VertexShader.glsl"), "Unable to compile vertex shader.\n");
glAttachShader(programID, shader);

// Load pixel shader
shader = LoadShaderFromString(GL_FRAGMENT_SHADER, Tools::GetFileAsString("FilterPixelShader.glsl"), "Unable to compile pixel shader.\n");
glAttachShader(programID, shader);

// Link the program
glLinkProgram(programID);
glGetProgramiv(programID, GL_LINK_STATUS, &status);

if (status == 0)
{
Log("Unable to link filter shader program.\n");
PrintProgramLog(programID);
Fail(); // Quits program
}

// returns -1 here!
frameTextureLocation = glGetUniformLocation(programID, "FrameTextureUnit");
if (frameTextureLocation == -1)
{
int errorCode = glGetError();
Log(string("Error retrieving variable frameTextureLocation from shader program: "));
Log((const char*)glewGetErrorString(errorCode))
Log("!\n");
Fail();
}

LoadShaderFromString:

int Shader::LoadShaderFromString(int type, const string& shaderSource, const string& errorMessage)
{
int shader, status;
const char* programSource;

shader = glCreateShader(type);
programSource = shaderSource.c_str();
glShaderSource(shader, 1, &programSource, nullptr);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);

if (status == 0)
{
Log(errorMessage);
PrintShaderLog(shader);
Fail();
}

return shader;
}

最后,着色器本身:

uniform sampler2D FrameTextureUnit; 
uniform sampler2D BackgroundTextureUnit;

#if __VERSION__ >= 130
// Texture coordinate
in vec2 texCoord;

// Final color
out vec4 gl_FragColor;
#else
// Texture coordinate
varying vec2 texCoord;
#endif

uniform float Tolerance; // Tolerance for color difference
uniform vec4 FilterColor; // Color of the filter

void main()
{
vec4 pixel = texture2D(FrameTextureUnit, texCoord);
vec4 background = texture2D(BackgroundTextureUnit, texCoord);

float difference = abs(background.x - pixel.x)
+ abs(background.y - pixel.y)
+ abs(background.z - pixel.z);

if (difference > Tolerance)
{
gl_FragColor = FilterColor;
}
else
{
// Y = 0.2126 R + 0.7152 G + 0.0722 B
float gray = pixel.x * 0.2126 + pixel.y * 0.7152 + pixel.z * 0.0722;
gl_FragColor = vec4(gray, gray, gray, 1);
}
}

有谁知道为什么会这样? :( 值得补充的是错误处理代码:

    int errorCode = glGetError();
Log(string("Error retrieving variable frameTextureLocation from shader program: "));
Log((const char*)glewGetErrorString(errorCode));

打印“从着色器程序检索变量 frameTextureLocation 时出错:无错误”。

最佳答案

始终指定 GLSL version在着色器的顶部,否则它默认为非常旧的版本。它必须是第一行。它还将消除内联版本检查的需要。

#version 150
// rest of shader here

关于用于 Sampler2D 的 c++ OpenGL glGetUniformLocation 在 Raspberry PI 上返回 -1 但在 Windows 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15364010/

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