在学习GLSL烹饪书第1章-统一块时遇到困难

Meet trouble when learning GLSL Cookbook Chapter 1 - Uniform Blocks

本文关键字:一块 遇到 GLSL 学习 1章      更新时间:2023-10-16

我没有看到blob

我成功渲染没有glEnable(GL_BLEND)的矩形。

但是我什么也得不到。

我发现这个:https://github.com/daw42/glslcookbook/blob/master/chapter01/scenebasic_uniformblock.cpp

我认为我的代码和他非常相似…

下面是我的代码片段:

auto block_index = glGetUniformBlockIndex(basic_program.program_, "BlobSettings");
GLint block_size;
glGetActiveUniformBlockiv(basic_program.program_, block_index, GL_UNIFORM_BLOCK_DATA_SIZE, &block_size);
auto block_buffer = new GLubyte[block_size / sizeof(GLubyte)];
const GLchar *names[] = {"InnerColor", "OuterColor", "RadiusInner", "RadiusOuter"};
GLuint indices[4];
glGetUniformIndices(basic_program.program_, 4, names, indices);
GLint offsets[4];
glGetActiveUniformsiv(basic_program.program_, 4, indices, GL_UNIFORM_OFFSET, offsets);
GLfloat outer_color[] = {0, 0, 0, 0};
GLfloat inner_color[] = {1, 1, .75, 0};
GLfloat inner_radius = .25;
GLfloat outer_radius = .45;
memcpy(block_buffer + offsets[0], inner_color, 4 * sizeof(GLfloat));
memcpy(block_buffer + offsets[1], outer_color, 4 * sizeof(GLfloat));
memcpy(block_buffer + offsets[2], &inner_radius, sizeof(GLfloat));
memcpy(block_buffer + offsets[3], &outer_radius, sizeof(GLfloat));
GLuint ubo;
glGenBuffers(1, &ubo);
glBindBuffer(GL_UNIFORM_BUFFER, ubo);
glBufferData(GL_UNIFORM_BUFFER, block_size, block_buffer, GL_DYNAMIC_DRAW);
//                delete []blockBuffer;
glBindBufferBase(GL_UNIFORM_BUFFER, block_index, ubo);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLfloat position[] = {
    -.8, -.8, 0,
    .8, -.8, 0,
    .8, .8, 0,
    -.8, -.8, 0,
    .8, .8, 0,
    -.8, .8, 0
};
GLfloat texture_position[] = {
    0, 0,
    1, 0,
    1, 1,
    0, 0,
    1, 1,
    0, 1
};
GLuint position_buffer;
glGenBuffers(1, &position_buffer);
glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(position), position, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, static_cast<GLubyte *>(nullptr));
GLuint texture_position_buffer;
glGenBuffers(1, &texture_position_buffer);
glBindBuffer(GL_ARRAY_BUFFER, texture_position_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(texture_position), texture_position, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, texture_position_buffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, static_cast<GLubyte *>(nullptr));
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(.5, .5, .5, 0);
while (!glfwWindowShouldClose(window)) {
    glClear(GL_COLOR_BUFFER_BIT);
    glBindVertexArray(vao);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
    glfwSwapBuffers(window);
    glfwPollEvents();
}

下面是我的片段着色器:

#version 400
in vec3 TexCoord;
out vec4 FragColor;
uniform BlobSettings {
    vec4 InnerColor;
    vec4 OuterColor;
    float RadiusInner;
    float RadiusOuter;
};
void main() {
    float dx = TexCoord.x - 0.5;
    float dy = TexCoord.y + 0.5;
    float dist = sqrt(dx * dx + dy * dy);
    FragColor = mix(InnerColor, OuterColor,
                    smoothstep(RadiusInner, RadiusOuter, dist));
}

你的InnerColor和OuterColor正在产生一个alpha值0.0。当你mix (...)两个向量与α = 0.0,结果将是α = 0.0,无论你使用什么值作为a系数。

当你不使用混合时,这很好,但一旦你启用混合,你就会遇到麻烦。

当前混合函数:

GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA

有效地做到了以下几点:

FragColor.a * FragColor.rgba  +  (1.0 - FragColor.a) * FrameBufferColor.rgba

换句话说,您正在使用混合丢弃片段着色的结果。你应该使用一个非零的alpha值