gpt4 book ai didi

c++ - OpenGL glUniform1ui() 抛出 GLError GL_INVALID_OPERATION

转载 作者:搜寻专家 更新时间:2023-10-31 01:33:49 25 4
gpt4 key购买 nike

我正在尝试在我的片段着色器中初始化一个 usampler2D

    uniform vec3 lightColor;

uniform usampler2D texSampler;///////////

uniform vec3 ambientLight;
uniform vec3 lightPosition;
uniform vec3 cameraPosition;


in Vertex{
vec4 rawPosition;
vec2 uv;
vec4 normal;
}vertexIn;

in InstanceData{
unsigned int textureUnitIndex;
}instance;
out vec4 color;

void main(){

vec3 normal = normalize(vec3(vertexIn.normal.x, vertexIn.normal.y, vertexIn.normal.z));
vec3 worldPosition = vec3(vertexIn.rawPosition.x, vertexIn.rawPosition.y, vertexIn.rawPosition.z);

vec3 lightVector = normalize(lightPosition - worldPosition);
vec3 cameraVector = normalize(cameraPosition - worldPosition);
vec3 halfVec = normalize(lightVector + cameraVector);

float lightAngle = clamp(dot(lightVector, normal), 0.0f, 1.0f);
float specAngle = clamp(dot(halfVec, normal), 0.0f, PI);
float specular = clamp(pow(specAngle, 128), 0.0f, 1.0f);
unsigned int index = instance.textureUnitIndex;

vec4 actColor = texture2D(texSampler, vec2(vertexIn.uv)); //////////////////////
vec4 diffuseLight = sqrt(actColor * vec4(lightColor, 1.0)) * lightAngle;
vec4 specularLight = 1.0f - (cos(vec4(lightColor, 1.0f) * specular));

color = actColor * (vec4(ambientLight, 1.0) + diffuseLight) + specularLight;// + vec4(ambientLight, 1.0f);
}

像这样

void ShaderProgram::addUniform(std::string uniformName, unsigned int uniformValue)
{
unsigned int loc = glGetUniformLocation(_programID, uniformName.c_str());
Graphic::checkOpenGLErrors("addUniform() - 1");
glUniform1ui(loc, (GLuint)uniformValue);
Graphic::checkOpenGLErrors("addUniform - 2");
}

loc 结果是 6 并且 uniformValue 等于 2 但 checkOpenGLErrors() 仍然抛出

“OpenGL 错误:GL_INVALID_OPERATION - 位于:addUniform - 2”

关于初始化采样器制服,您有什么需要了解的吗?因为这个函数在这个GLSL程序中通常不会抛出错误

谢谢

最佳答案

opaque types 上传制服时,您必须使用 glUniform1i1iv (包括采样器)。仅仅因为采样器使用无符号整数格式并不意味着统一调用会更改以匹配。

关于c++ - OpenGL glUniform1ui() 抛出 GLError GL_INVALID_OPERATION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40697354/

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