glGetUniformLocation在使用时在变换反馈期间返回-1

时间:2018-05-14 05:23:06

标签: opengl glsl transform-feedback

该函数返回-1,即使在顶点着色器中定义并使用了Uniform,我怀疑原因可能是out属性可能没有与目标缓冲区正确绑定(不确定是否是这种情况)。没有这个制服,我的大部分价值观将保持不变。

绘制变换反馈

/*code included in update*/
glUseProgram(feedbackShader->GetProgram());

glEnable(GL_RASTERIZER_DISCARD);
/*end of code included in update*/

glBindBuffer(GL_ARRAY_BUFFER, particleBuffer[isEvenBuffer]);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, transformBuffer[!isEvenBuffer]);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
glEnableVertexAttribArray(5);

glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, sizeof(Particle), 0); //Location
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Particle), (const GLvoid*)12); //Velocity
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Particle), (const GLvoid*)24); //InitLocation
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Particle), (const GLvoid*)36); //InitVelocity
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, sizeof(Particle), (const GLvoid*)48); //Lifetime
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Particle), (const GLvoid*)52); //InitLifetime

GLint uniformLocation = glGetUniformLocation(feedbackShader->GetProgram(), "time");

glUniform1f(uniformLocation, msec);

glBeginTransformFeedback(GL_POINTS);

glDrawTransformFeedback(GL_POINTS, transformBuffer[isEvenBuffer]);

glEndTransformFeedback();

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
glDisableVertexAttribArray(4);
glDisableVertexAttribArray(5);

顶点着色器

#version 410

in vec3 inLocation;
in vec3 inVelocity;
in vec3 inInitLocation;
in vec3 inInitVelocity;
in float inLifeTime;
in float inInitlifeTime;

out vec3 outLocation;
out vec3 outVelocity;
out vec3 outInitLocation;
out vec3 outInitVelocity;
out float outLifeTime;
out float outInitlifeTime;

uniform float time;

vec3 Gravity = vec3(0.0f,-0.98f,0.0f);
float dampeningFactor = 0.5;

void main()
{

  outLifeTime = inLifeTime - time;

  if(outLifeTime > 0.0f){
    outVelocity = (inVelocity + Gravity * time) * dampeningFactor;
    outLocation = inLocation + inVelocity * time;
  }else{
    outVelocity = inInitVelocity;
    outLocation = inInitLocation;
    outLifeTime = inInitlifeTime;
  }
  outInitVelocity = inInitVelocity;
  outInitLocation = inInitLocation;
  outInitlifeTime = inInitlifeTime;
}

更新 你们都要求提供一些额外的信息。

顶点着色器

#version 410

in vec3 inLocation;
in vec3 inVelocity;
in vec3 inInitLocation;
in vec3 inInitVelocity;
in float inLifeTime;
in float inInitlifeTime;

out vec3 outLocation;
out vec3 outVelocity;
out vec3 outInitLocation;
out vec3 outInitVelocity;
out float outLifeTime;
out float outInitlifeTime;

uniform float time;

vec3 Gravity = vec3(0.0f,-0.98f,0.0f);
float dampeningFactor = 0.5;

void main()
{

  outLifeTime = inLifeTime - time;

  if(outLifeTime > 0.0f){
    outVelocity = (inVelocity + Gravity * time) * dampeningFactor;
    outLocation = inLocation + inVelocity * time;
  }else{
    outVelocity = inInitVelocity;
    outLocation = inInitLocation;
    outLifeTime = inInitlifeTime;
  }
  outInitVelocity = inInitVelocity;
  outInitLocation = inInitLocation;
  outInitlifeTime = inInitlifeTime;
}

反馈变化(我是愚蠢的,一开始没有这个,虽然我的代码中仍存在相同的问题)

const GLchar* feedbackVaryings[] = {
    "outLocation",
    "outVelocity",
    "outInitLocation",
    "outInitVelocity",
    "outLifeTime",
    "outInitlifeTime"
};
glTransformFeedbackVaryings(feedbackShader->LinkProgram(), 6, feedbackVaryings, GL_INTERLEAVED_ATTRIBS);

我还在“绘制变换反馈部分”中添加了gluseProgram。

1 个答案:

答案 0 :(得分:1)

我假设说明

feedbackShader->LinkProgram()

链接程序。但是在链接程序之前必须指定变化的变换反馈变量。

请参阅OpenGL 4.6 API Core Profile Specification; 7.3.1.1 Naming Active Resources; page 104

  

活动资源列表的顺序依赖于实现   除TRANSFORM_FEEDBACK_VARYING之外的接口。如果变量在   使用TransformFeedbackVaryings命令指定了TRANSFORM_FEEDBACK_VARYING接口,在最后一次调用TransformFeedbackVaryings之前,活动资源列表将按最近一次调用LinkProgram中指定的变量顺序排列/ strong>即可。

这意味着,首先必须将已编译的顶点着色器对象附加到程序对象(glAttachShader)。然后,您必须指定变换反馈变化(glTransformFeedbackVaryings)。最后,您必须链接程序(glLinkProgram):

GLuint shader_obj;
GLuint program_obj;

.....

glAttachShader(program_obj, shader_obj);

const GLchar* feedbackVaryings[] = {
    "outLocation",
    "outVelocity",
    "outInitLocation",
    "outInitVelocity",
    "outLifeTime",
    "outInitlifeTime"
};
glTransformFeedbackVaryings(program_obj, 6, feedbackVaryings, GL_INTERLEAVED_ATTRIBS);

glLinkProgram(program_obj);


此外,我建议使用Layout Qualifier来定义顶点着色器inpout变量的属性索引。

e.g。

layout (location = 0) in vec3 inLocation;
layout (location = 1) in vec3 inVelocity;
layout (location = 2) in vec3 inInitLocation;
layout (location = 3) in vec3 inInitVelocity;
layout (location = 4) in float inLifeTime;
layout (location = 5) in float inInitlifeTime;

作为替代方案,属性索引可以在链接程序之后由glGetAttribLocation 确定,或者在链接程序之前由glBindAttribLocation 设置。< / p>