OpenGL glGetUniformLocation for Sampler2D在树莓派上返回-1,但在Windows
c++ OpenGL glGetUniformLocation for Sampler2D returns -1 on Raspberry PI but works on Windows
我正在制作一个跨平台的OpenGL程序。然而,我遇到了一个问题,其中glGetUniformLocation,它应该返回我的着色器程序中统一变量的位置,返回-1,它只发生在Linux上(树莓派发行版,在树莓派上运行),在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));
打印"从shader程序中检索变量frameTextureLocation出错:No Error ".
总是在你的着色器顶部指定GLSL版本,否则它默认是一个非常旧的版本。一定是第一行。它还将消除内联版本检查的需要。
#version 150
// rest of shader here
相关文章:
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 几个 Nt 函数在 Windows 7 x32 上返回STATUS_WAIT_0
- 尝试在 Windows 上重定向 stdout 和 stderr——_fileno(stdout) 返回 -2
- 从我的2d数组中的一列返回的字符串值被压缩为一个字符串(在Mac os上打开Windows txt文件)
- Qt5 哈希函数在 Windows 7 和 Windows10 上不会返回相同的哈希
- Windows 函数"GetCursorInfo"返回成功,但光标的句柄为 NULL
- C++ 即使数据可用,Windows recv() 也不会返回
- 如何检查 CNG Windows API 是否返回符合 FIPS 的算法实现
- C++ Windows 返回与退出进程
- repopenKeyex在Windows 8.1上返回2
- Windows句柄始终返回Invalid_handle_value
- 检查Windows激活状态返回错误的值
- 没有返回类型的静态函数可以通过Windows上的编译,但不能传递Linux上的编译
- 使用Outlook 2016:MAPISendMail在Windows应用程序中失败,并返回"MAPI_E_FAILURE"作为错误代码
- Windows HANDLE RAII 管理,如果返回布尔值而不是句柄怎么办?
- 为什么 CoCreateInstance 在某些 Windows 上返回REGDB_E_CLASSNOTREG?
- FCGX_GetParam REQUEST_METHOD可以在Windows中使用,但在Linux中返回NULL
- 创建 CTime @ 2am 不会返回正确的值(Windows CE,C++)
- 为什么返回Windows BOOL数据类型而不返回int