1

该函数返回 -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。

4

1 回答 1

1

我假设指令

feedbackShader->LinkProgram()

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

请参阅OpenGL 4.6 API 核心配置文件规范;7.3.1.1 命名活动资源;第 104 页

除了 TRANSFORM_FEEDBACK_VARYING 之外,所有接口的活动资源列表的顺序都取决于实现。如果使用该命令指定了 TRANSFORM_FEEDBACK_VARYING 接口中的变量TransformFeedbackVaryings,则活动资源列表将按照最近一次调用中指定的变量顺序排列在TransformFeedbackVaryings最后一次调用之前LinkProgram

这意味着,首先您必须将已编译的顶点着色器对象附加到程序对象 ( 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来定义顶点共享器输入变量的属性索引。

例如

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

于 2018-05-15T04:57:55.593 回答