7

我试图使用变换反馈从顶点着色器中检索数据以进行调试,因为我的透视矩阵似乎计算错误。我在变换反馈缓冲区中什么也没有。谁能帮我弄清楚我做错了哪一步?

以下是我设置转换反馈的代码片段:

//vertex to be passed into shader
const float vertexPos[] =
{
     0.75f, 0.75f, 0.0f, 1.0f,
     0.75f, -0.75f, 0.0f, 1.0f,
     -0.75f, -0.75f, 0.0f, 1.0f
};

//set up transform feedback
const char *variableName = "retrieve";//the variable to be retreived in shader
float dateFromShader[16];//receive the data from shader
GLuint tf_bo;//transform feedback buffer id

glTransformFeedbackVaryings(shaderProgram, 1, &variableName, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(shaderProgram);

glGenBuffers(1, &tf_bo);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, tf_bo);// Is this necessary to call glBindBuffer before glBindBufferBase?
     glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tf_bo);
     glBufferDate(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(float) * 16, NULL, GL_DYNAMIC_COPY);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);

//retrieve data from vertex shader
glEnable(GL_RASTERIZER_DICARD);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, tf_bo);
glBeginTransformFeedback(GL_TRIANGLES);
     glDrawArrays(GL_TRIANGLES, 0, 3);//
glEndTransformFeedback();

glBindBuffer(GL_ARRAY_BUFFER, tf_bo);
glGetBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * 16, dataFromShader);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisable(GL_RASTERIZER_DISCARD);

我的顶点着色器是这样的:

#version 420

layout(location = 0) in vec4 position;


uniform mat4 perspectiveMatrix;
out mat4 retrieve;

void main()
{
gl_Position = perspectiveMatrix * position;

retrieve = perspectiveMatrix;
}
4

2 回答 2

5
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tf_bo);

文档中所述,glBindBufferBase要求缓冲区对象具有数据存储(即:您已调用glBufferData它)。不能替代; glBindBufferBase_ _ 是一种具有不同用途的不同功能。glBindBuffer

接下来,您将捕获一个矩阵。问题是您正在绘制三个顶点,这意味着您将捕获三个顶点。您只为一个分配了足够的空间。这将导致未定义的行为。

最重要的是……您认为这将完成什么?您正在捕获直接上传的统一值。除非您认为 glUniformMatrix 的实现版本在某种程度上被破坏了,否则这对调试任何东西都没有多大帮助。

于 2012-11-29T16:13:45.457 回答
0
 glBufferDate(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(float) * 16, NULL, GL_DYNAMIC_COPY);

Is this line not supposed to be:

 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(float) * 16, NULL, GL_DYNAMIC_COPY);

Note the "glBufferData" not "glBufferDate". This will also support the fact that you are not getting any data in the buffer / 0 is being returned....

于 2020-02-03T12:27:47.883 回答