变换反馈的完整设置(openGL)
GLSL 1.50、openGL 3.3。
我最近一直在尝试让我的转换反馈发挥作用,但没有成功。在 glBeginTranformFeedback() 之后我仍然收到错误,并且由于我还没有找到任何完整的工作代码,我已经用我找到的一些代码和文档积累了我的知识,它现在应该运行良好,但我遗漏了一些东西。因此,如果有人获得完整的代码(缓冲区初始化、设置、更新、渲染、读回),那肯定会有帮助,如果你不知道但知道发生了什么,你可以看看我的代码。我排除了一些基准测试、窗口处理及其创建:
int main()
{
bool fullsize = false, paused = false;
std::string caption = "Tester";
GLuint dataVAO,speedUpdateVBO,dataVBO;
std::vector<vector3f> dataW;
// Create the main rendering window
init(); //just some camera commands
UniShader shader; //my shader class keeps everything together
shader.init();
shader.addShader("test.vert");
shader.addShader("test.frag");
shader.newAttributeVariable("speed");
shader.newFeedbackVarying("sp");
shader.linkShader();
shader.use();
//init some data
dataW.push_back(vector3f(0,1,0));
//creating VAO
glGenVertexArrays(1,&dataVAO);
glBindVertexArray(dataVAO);
//creating VBO
glGenBuffers(1,&dataVBO);
glBindBuffer(GL_ARRAY_BUFFER,dataVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vector3f), 0, GL_DYNAMIC_DRAW);
glVertexAttribPointer(shader.getAttributeIndex("speed"), 3, GL_FLOAT, GL_FALSE, 0, 0);
glGenBuffers(1, &speedUpdateVBO);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, speedUpdateVBO);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(vector3f), NULL, GL_DYNAMIC_COPY);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, speedUpdateVBO);
glBindVertexArray(0);
while (App.IsOpened())
{
App.SetActive();
benchP = Clock.GetElapsedTime();
//update calls
if(!paused)
//update
benchU = Clock.GetElapsedTime();
//render calls
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.6f,0.7f,0.7f);
GLuint query;
GLuint count = 0;
glGenQueries(1, &query);
glEnableVertexAttribArray(shader.getAttributeIndex("speed"));
glBindVertexArray(dataVAO);
glBindBuffer(GL_ARRAY_BUFFER,dataVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vector3f)*dataW.size(), &dataW[0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, speedUpdateVBO);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(vector3f)*dataW.size(), NULL, GL_DYNAMIC_COPY);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, speedUpdateVBO);
glEnable(GL_RASTERIZER_DISCARD);
glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, query);
printOglError(); //Until this everything OK, I think
glBeginTransformFeedback(GL_POINTS);
printOglError(); //This one prints out Invalid Value
glDrawArrays(GL_POINTS,0,dataW.size());
glEndTransformFeedback();
glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
glDisable(GL_RASTERIZER_DISCARD);
//retrieve updated data
glGetQueryObjectuiv(query, GL_QUERY_RESULT, &count); //count is 0 -> nothing happend
glBindVertexArray(0);
glDisableVertexAttribArray(shader.getAttributeIndex("speed"));
glDeleteQueries(1, &query);
App.Display();
//some other benchmark stuff
}
着色器: vert
#version 150 core
in vec3 speed;
varying vec3 sp;
const float gravity_constant = 9.81f;
void main(){
sp = speed;
sp += vec3(0,-gravity_constant,0);
}
frag
#version 150 core
varying vec3 sp;
void main (void)
{
vec3 c = sp;
gl_FragColor = vec4(c,1.0);
}
片段着色器仅用于 GLSL 优化。如果不使用 sp GLSL 会清除它。 可能存在一些小错误,因为我从更大的代码中提取了它,并且多个变化也失败了。
GLSL 1.50, openGL 3.3.
I've been lately trying to get my tranform feedback working but without success. I still receive error after glBeginTranformFeedback() and as I haven't found any full working code I have stacked up my knowledge with some code that I found and documentation, it should be working well by now but I am missing something. So if anybody got full code (initializing of buffers, setting up, updating, rendering, reading back) it would definitelly help and if you don't but know what's going on you could take look at my code. I excluded some benchmarking, handling of windows and it's creation:
int main()
{
bool fullsize = false, paused = false;
std::string caption = "Tester";
GLuint dataVAO,speedUpdateVBO,dataVBO;
std::vector<vector3f> dataW;
// Create the main rendering window
init(); //just some camera commands
UniShader shader; //my shader class keeps everything together
shader.init();
shader.addShader("test.vert");
shader.addShader("test.frag");
shader.newAttributeVariable("speed");
shader.newFeedbackVarying("sp");
shader.linkShader();
shader.use();
//init some data
dataW.push_back(vector3f(0,1,0));
//creating VAO
glGenVertexArrays(1,&dataVAO);
glBindVertexArray(dataVAO);
//creating VBO
glGenBuffers(1,&dataVBO);
glBindBuffer(GL_ARRAY_BUFFER,dataVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vector3f), 0, GL_DYNAMIC_DRAW);
glVertexAttribPointer(shader.getAttributeIndex("speed"), 3, GL_FLOAT, GL_FALSE, 0, 0);
glGenBuffers(1, &speedUpdateVBO);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, speedUpdateVBO);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(vector3f), NULL, GL_DYNAMIC_COPY);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, speedUpdateVBO);
glBindVertexArray(0);
while (App.IsOpened())
{
App.SetActive();
benchP = Clock.GetElapsedTime();
//update calls
if(!paused)
//update
benchU = Clock.GetElapsedTime();
//render calls
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.6f,0.7f,0.7f);
GLuint query;
GLuint count = 0;
glGenQueries(1, &query);
glEnableVertexAttribArray(shader.getAttributeIndex("speed"));
glBindVertexArray(dataVAO);
glBindBuffer(GL_ARRAY_BUFFER,dataVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vector3f)*dataW.size(), &dataW[0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, speedUpdateVBO);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(vector3f)*dataW.size(), NULL, GL_DYNAMIC_COPY);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, speedUpdateVBO);
glEnable(GL_RASTERIZER_DISCARD);
glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, query);
printOglError(); //Until this everything OK, I think
glBeginTransformFeedback(GL_POINTS);
printOglError(); //This one prints out Invalid Value
glDrawArrays(GL_POINTS,0,dataW.size());
glEndTransformFeedback();
glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
glDisable(GL_RASTERIZER_DISCARD);
//retrieve updated data
glGetQueryObjectuiv(query, GL_QUERY_RESULT, &count); //count is 0 -> nothing happend
glBindVertexArray(0);
glDisableVertexAttribArray(shader.getAttributeIndex("speed"));
glDeleteQueries(1, &query);
App.Display();
//some other benchmark stuff
}
shaders:
vert
#version 150 core
in vec3 speed;
varying vec3 sp;
const float gravity_constant = 9.81f;
void main(){
sp = speed;
sp += vec3(0,-gravity_constant,0);
}
frag
#version 150 core
varying vec3 sp;
void main (void)
{
vec3 c = sp;
gl_FragColor = vec4(c,1.0);
}
Fragment shader is there just for GLSL optimalization. If sp wouldn't be used GLSL would clear it up.
There may be some minor bugs as I extracted this from much larger code with multiple varyings that fails aswell.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 OpenGL 示例包。您可能对 ogl-330-transform-feedback.cpp 和 ogl-400-transform-feedback-object.cpp 感兴趣。您甚至可以检查2011 年 1 月 OpenGL 驱动程序状态最新的驱动程序是否存在任何问题提供了示例。
祝你好运。
Take a look at OpenGL Samples Pack. You might be interested in ogl-330-transform-feedback.cpp and ogl-400-transform-feedback-object.cpp. You might even check January 2011 OpenGL drivers status whether latest drivers have any issues with the provided examples.
Good luck.
Daniel Rakos 有一个带有源代码的演示,使用 OpenGL 上的变换反馈实现实例剔除。查看它可能会有所帮助:
http://rastergrid。 com/blog/2010/02/instance-culling-using-geometry-shaders/
Daniel Rakos has a demo with source code, implementing instance culling using transform feedback on OpenGL. It might help to look at it :
http://rastergrid.com/blog/2010/02/instance-culling-using-geometry-shaders/
您可能想查看 Ogre 3D:
http://www.ogre3d.org/forums/viewtopic.php?p=299736
他们似乎有一个有效的转换反馈实现。
You might want to check out Ogre 3D:
http://www.ogre3d.org/forums/viewtopic.php?p=299736
They seem to have a working implementation of transform feedback.
我也遇到了类似的问题,
glBeginTransformFeedback
生成“无效操作”。当我阅读Orhun提供的包中的“gl-440-transform-feedback.cpp”文件中的源代码后,我发现在
glBeginTransformFeedback()
调用之前添加就可以解决问题。希望这能有所帮助。
I also experienced a similar problem that
glBeginTransformFeedback
generated "invalid operation". After I read the source code in file "gl-440-transform-feedback.cpp" from the package provided by Orhun, I found that addingbefore
glBeginTransformFeedback()
call just solves the problem.Hope this can help.