gpt4 book ai didi

c++ - OpenGL渲染黑色方 block

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:03:43 25 4
gpt4 key购买 nike

我正在尝试渲染一个彩色矩形,但我最终得到了一个黑色矩形。

我的着色器和着色器程序在编译或链接时不会产生任何错误,因此我确信在尝试缓冲数据时出现了问题。但是,在我看来一切都是正确的。

enter image description here

着色器源

std::string const fragment_shader_source = "#version 330 core\n"
""
"in vec4 fColor;\n"
"\n"
"out vec4 finalColor;\n"
"\n"
"void main() {\n"
" finalColor = fColor;\n"
"}";

std::string const vertex_shader_source = "#version 330 core\n"
""
"layout (location = 0) in vec2 vPos;\n"
"layout (location = 1) in vec4 vColor;\n"
"\n"
"out vec4 fColor;\n"
"void main() {\n"
" gl_Position = vec4(vPos.xy, 1.0, 1.0);\n"
" fColor = vColor;\n"
"}";

顶点数据

    static GLfloat const vertex_data[] = {
// //Position //Color //UV Coords
0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,

-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f
};

这是我的主循环:

int SDLApp::AppMain(int argc, char** argv) {

if(sdl_window_ == nullptr) {
acorn::Log(acorn::LoggingLevel::Fatal, "Could not initialize the window.");
return -1;
}

if(sdl_gl_context_ == nullptr) {
acorn::Log(acorn::LoggingLevel::Fatal, "Could not create an OpenGL context.");
return -1;
}

acorn::shader::FragmentShader f(fragment_shader_source);
acorn::shader::VertexShader v(vertex_shader_source);
acorn::shader::Program shader_program(f, v);
shader_program.Link();
shader_program.Use();


GLuint vao_;
glGenVertexArrays(1, &vao_);

GLuint vbo_;
glGenBuffers(1, &vbo_);

glBindVertexArray(vao_);
glBindBuffer(GL_ARRAY_BUFFER, vbo_);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void*)2);

glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(GLfloat) * 6, vertex_data, GL_STATIC_DRAW);


glClearColor(1.0f, 0.0f, 0.0f,1.0f);
SDL_Event event;
while (true) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return 0;
}
}
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

glDrawArrays(GL_TRIANGLES, 0, 6);
SDL_GL_SwapWindow(sdl_window_);
}
}

最佳答案

glVertexAttribPointer的最后一个参数被视为缓冲区对象数据存储中的字节偏移量
所以偏移量必须是 8,2*sizeof(float):

glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat),
(void*)(2*sizeof(GLfloat)));

更改后,您的代码可以正常工作

关于c++ - OpenGL渲染黑色方 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54717150/

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