在学习GLSL烹饪书第1章-统一块时遇到困难
Meet trouble when learning GLSL Cookbook Chapter 1 - Uniform Blocks
我没有看到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值
相关文章:
- 编译包含字符串的代码时遇到问题
- 遇到新行时,有没有办法停止istream_iterator
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 在 for 循环中查找问题时遇到困难
- 创建结构的数组时遇到分段错误
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 为什么我遇到分段错误?
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 这些是什么样的错误?即使我不在 Linux 上工作,我也遇到了 Linux 错误
- 为什么我在尝试模板时遇到视觉工作室C++错误
- 如何从文本文件中读取数值,直到遇到字符类型?
- 从其他类访问类时遇到问题
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 打印问题.我在数组中打印值时遇到一些问题
- 使用静态成员声明类时遇到问题
- 创建 Spdlog 异步文件记录器时遇到困难
- C++ 填充数组时遇到问题
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 查找奇数平方和时遇到麻烦