gpt4 book ai didi

c++ - 变换反馈的完整设置(openGL)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:35:58 28 4
gpt4 key购买 nike

GLSL 1.50,openGL 3.3。

我最近一直在尝试让我的转换反馈发挥作用,但没有成功。在 glBeginTranformFeedback() 之后我仍然收到错误,并且由于我没有找到任何完整的工作代码,我已经用我发现的一些代码和文档积累了我的知识,它现在应该运行良好但我遗漏了一些东西。因此,如果有人有完整的代码(缓冲区的初始化、设置、更新、渲染、回读),那肯定会有所帮助,如果你不知道,但知道发生了什么,你可以看看我的代码。我排除了一些基准测试、Windows 处理及其创建:

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
}

着色器:垂直

#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);
}

片段

#version 150 core
varying vec3 sp;

void main (void)
{
vec3 c = sp;
gl_FragColor = vec4(c,1.0);
}

片段着色器仅用于 GLSL 优化。如果不使用 sp,GLSL 会清除它。可能存在一些小错误,因为我从具有多个失败的更大代码中提取了它。

最佳答案

看看OpenGL Samples Pack .您可能对 ogl-330-transform-feedback.cpp 和 ogl-400-transform-feedback-object.cpp 感兴趣。你甚至可以检查 January 2011 OpenGL drivers status最新的驱动程序是否与提供的示例有任何问题。

祝你好运。

关于c++ - 变换反馈的完整设置(openGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4970922/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com