OpenGL变换反馈没有返回任何东西
Posted
技术标签:
【中文标题】OpenGL变换反馈没有返回任何东西【英文标题】:OpenGL Transform Feedback not returning anything 【发布时间】:2015-08-17 12:39:58 【问题描述】:我正在开展一个使用几何着色器和变换反馈创建几何的项目。
我目前正在尝试让几何着色器返回其输入(1 个三角形)而不更改/添加任何内容,但它不起作用。
如果我能得到任何帮助/建议,我将不胜感激。以下是我的部分代码:
转换反馈程序的创建:
//==========================================
// Create the Transform Program
//==========================================
int check = LoadShader("Shaders//transformVS.glsl", GL_VERTEX_SHADER, transformVS);
//TODO: check for fail
check = LoadShader("Shaders//transformGS.glsl", GL_GEOMETRY_SHADER, transformGS);
//TODO: check for fail
transformProgram = glCreateProgram();
glAttachShader(transformProgram, transformVS);
glAttachShader(transformProgram, transformGS);
glBindAttribLocation(transformProgram, 0, "position_in");
glBindAttribLocation(transformProgram, 1, "normal_in");
glBindAttribLocation(transformProgram, 2, "length_in");
static const char* varyings[] = "position_out", "normal_out", "length_out" ;
glTransformFeedbackVaryings(transformProgram, 3, varyings, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(transformProgram);
if (CheckProgram(transformProgram) == -1)
glDetachShader(transformProgram, transformVS);
glDetachShader(transformProgram, transformGS);
glDeleteShader(transformVS);
transformVS = 0;
glDeleteShader(transformGS);
transformGS = 0;
glDeleteProgram(transformProgram);
transformProgram = 0;
return -1;
error = glGetError();
VBO、VAO 和 TFO 的创建:
//=====================================
// Create VBOs
//=====================================
glGenBuffers(2, VBOID);
glBindBuffer(GL_ARRAY_BUFFER, VBOID[0]);
glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(TVertex_VNL), vertices, GL_DYNAMIC_COPY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
error = glGetError();
glBindBuffer(GL_ARRAY_BUFFER, VBOID[1]);
glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(TVertex_VNL), NULL, GL_DYNAMIC_COPY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
error = glGetError();
//=====================================
// Create VAOs
//=====================================
glGenVertexArrays(2, VAOID);
glBindVertexArray(VAOID[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOID[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TVertex_VNL), BUFFER_OFFSET(0)); //position
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(TVertex_VNL), BUFFER_OFFSET(sizeof(float) * 3)); //normal
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(TVertex_VNL), BUFFER_OFFSET(sizeof(float) * 6)); //length
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
error = glGetError();
glBindVertexArray(VAOID[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBOID[1]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TVertex_VNL), BUFFER_OFFSET(0)); //position
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(TVertex_VNL), BUFFER_OFFSET(sizeof(float) * 3)); //normal
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(TVertex_VNL), BUFFER_OFFSET(sizeof(float) * 6)); //length
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
error = glGetError();
//=====================================
// Create TFOs
//=====================================
glGenTransformFeedbacks(2, TFOID);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, TFOID[0]);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, VBOID[0]);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
error = glGetError();
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, TFOID[1]);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, VBOID[1]);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
error = glGetError();
渲染方法:
//=========================================
// Clear Screen
//=========================================
//Clear all the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
//========================================
// Transform Feedback
//========================================
glEnable(GL_RASTERIZER_DISCARD);
glUseProgram(transformProgram);
glBindVertexArray(VAOID[0]);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, TFOID[1]);
glBeginTransformFeedback(GL_TRIANGLES);
glDrawArrays(GL_TRIANGLES, 0, 3);
glEndTransformFeedback();
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
glDisable(GL_RASTERIZER_DISCARD);
//========================================
// Draw Triangle
//========================================
//Bind the shader that we want to use
glUseProgram(renderProgram);
//Setup all uniforms for your shader
glUniformMatrix4fv(renderMVP, 1, FALSE, &MVP[0][0]);
//Bind the VAO
glBindVertexArray(VAOID[1]);
glDrawArrays(GL_TRIANGLES, 0, 3);
//glDrawTransformFeedback(GL_TRIANGLES, TFOID[1]);
//========================================
// Swap Buffers
//========================================
glutSwapBuffers();
顶点着色器:
#version 330
in vec3 position_in;
in vec3 normal_in;
in float length_in;
out vec3 vs_position;
out vec3 vs_normal;
out float vs_length;
void main()
vs_position = position_in;
vs_normal = normal_in;
vs_length = length_in;
几何着色器:
#version 330
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
in vec3 vs_position[];
in vec3 vs_normal[];
in float vs_length[];
out vec3 position_out;
out vec3 normal_out;
out float length_out;
void main()
for(int i = 0; i < 3; i++)
position_out = vs_position[i];
normal_out = vs_normal[i];
length_out = vs_length[i];
EmitVertex();
EndPrimitive();
【问题讨论】:
在初始化期间,您创建了两个 TFO,但需要将第一个初始化两次。在渲染过程中,您只使用第二个。 @derhass 再次感谢您的回答。我也修复了那个,但它仍然无法正常工作。 TFO 初始化中是否缺少某些内容,例如告诉 opengl 编写不同的输出变量? 由于您使用交错输出,所有输出都将写入同一个缓冲区(除非您使用分隔符,但这是完全不同的事情,在这里不应该相关)。我不确定根本不使用片段着色器。由于您使用RASTERIZER_DISACRD
,因此可能没有必要,但我没有在规范中查找。不过,我认为向渲染方法添加一些错误检查不会有什么坏处。
【参考方案1】:
您的几何着色器没有发出任何顶点,因为从未输入过 for 循环体:
for(int i = 0; i >= 3; i++)
【讨论】:
感谢您的回答。我解决了这个问题,但三角形仍然没有被绘制出来:(以上是关于OpenGL变换反馈没有返回任何东西的主要内容,如果未能解决你的问题,请参考以下文章