glsl 相关问题

OpenGL着色语言(GLSL)是OpenGL中着色器的标准编程语言。该语言有许多版本,每个版本对应一个OpenGL版本。 OpenGL ES 2.0及更高版本具有单独的GLSL版本。

如何获取当前在 OpenGL 中处理的顶点的索引

是否有其他方法可以获取当前在 opengl 中处理的顶点的索引? 下面的代码旨在为 3d 空间渲染一个平面。为了更容易获得顶点的索引......

回答 0 投票 0

为什么顶点着色器不编译?

我是 WebGl 的新手,但我想尝试一个例子。我这样创建了我的脚本: ` 从 './shaders/test/vertex.glsl' 导入 testVertexShader 从 './shaders/test/fr... 导入 testFragmentShader

回答 1 投票 0

绘制到 Framebuffer 时不能使用 sampler2D

在绘制到 Framebuffer 对象时,我在将 2D 纹理绑定到着色器时遇到问题。 当我直接在屏幕上绘图时没有问题,但是当我尝试使用 Framebuffer 时,整个

回答 0 投票 0

使用 `texelFetch` 从顶点着色器中的纹理进行采样:我应该期待什么样的色彩空间?

我在我的 WebGL 2 程序中使用变换反馈和顶点着色器来快速执行计算并获得结果。我将图像作为纹理提供给我的着色器(这几乎...

回答 0 投票 0

How to make a blob invert its direction in the fragment shader?

此代码使 blob 对象沿 y 轴上升,并且当它到达屏幕中心时应该反转其方向。 blob 对象上升,但到达

回答 0 投票 0

在 Vulkan 中协调描述符集和布局

我一直在通过 NVidia 的 vk_raytrace 示例研究 Vulkan 和光线追踪,但我对管道布局、GLSL 着色器“布局”之间的协调方式感到困惑...

回答 1 投票 0

优化 OpenGL 计算着色器

我一直在尝试制作自己的游戏,现在我正在研究PBR Bloom Effect。我做了一个简单的布隆测试应用程序并试图让它变得更好。因此,我有几个问题。 T...

回答 0 投票 0

计算着色器比预期慢

我有一个应该在 gpu 上运行的程序。现在我已经测量了 cpu 上一个函数 initEdgesX 的性能,它为我提供了大约 150 毫秒的 400³ 数据数组。现在我想要平行...

回答 0 投票 0

GPU 单通道光线投射空体积 [关闭]

你好,下面的代码是我的简单体积单通道光线投射算法的渲染管道。它使用 OpenGL (glew,freeglut) 和 GLSL。当我渲染体积时,我得到的只是...的外部

回答 0 投票 0

GPU单通道光线投射空体积(OpenGL,GLSL)

你好,下面的代码是我的简单体积单通道光线投射算法的渲染管道。它使用 OpenGL (glew,freeglut) 和 GLSL。当我渲染体积时,我得到的只是...的外部

回答 0 投票 0

WebGL 顶点着色器缩放几何,与正交投影中的方面无关

顶点着色器中的几何比例有问题。我有一个正交投影,并且希望无论缩放如何,文本都具有相同的大小(在屏幕上)。我设法做到了: 位置.x...

回答 0 投票 0

如何在 opengl 中使用白色作为纹理?

有这个简单的代码: #包括 #包括 #包括 #包括 #包括 #包括 有这个简单的代码: #include <iostream> #include <array> #include <glad/glad.h> #include <GLFW/glfw3.h> #include <test/main/shader.hpp> #include <test/main/texture.hpp> struct QuadVertex { glm::vec3 position; glm::vec4 color; glm::vec2 texture_coordinates; float texture_index; }; struct Data { static const int max_quads = 10000; static const int max_vertices = max_quads * 4; static const int max_indices = max_quads * 6; static const int max_texture_slots = 32; Shader *shader = nullptr; int index_count = 0; QuadVertex *quad_vb_ptr = nullptr; std::array<const Texture *, max_texture_slots> textures; int texture_slot_index = 1.0; }; static Data data; void DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color) { float texture_index = 0.0f; // white texture data.quad_vb_ptr->position = position; data.quad_vb_ptr->color = color; data.quad_vb_ptr->texture_coordinates = {0.0f, 0.0f}; data.quad_vb_ptr->texture_index = texture_index; data.quad_vb_ptr++; data.quad_vb_ptr->position = {position.x + size.x, position.y, 0.0f}; data.quad_vb_ptr->color = color; data.quad_vb_ptr->texture_coordinates = {1.0f, 0.0f}; data.quad_vb_ptr->texture_index = texture_index; data.quad_vb_ptr++; data.quad_vb_ptr->position = {position.x + size.x, position.y + size.y, 0.0f}; data.quad_vb_ptr->color = color; data.quad_vb_ptr->texture_coordinates = {1.0f, 1.0f}; data.quad_vb_ptr->texture_index = texture_index; data.quad_vb_ptr++; data.quad_vb_ptr->position = {position.x, position.y + size.y, 0.0f}; data.quad_vb_ptr->color = color; data.quad_vb_ptr->texture_coordinates = {0.0f, 1.0f}; data.quad_vb_ptr->texture_index = texture_index; data.quad_vb_ptr++; data.index_count += 6; } void DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color, const Texture *texture) { float texture_index = 0.0f; for (int i = 1; i < data.textures.size(); i++) { if (data.textures[i] && *data.textures[i] == *texture) { texture_index = (float) i; break; } } if (texture_index == 0.0f) { texture_index = (float) data.texture_slot_index; data.textures[data.texture_slot_index] = texture; data.texture_slot_index++; } data.quad_vb_ptr->position = position; data.quad_vb_ptr->color = color; data.quad_vb_ptr->texture_coordinates = {0.0f, 0.0f}; data.quad_vb_ptr->texture_index = texture_index; data.quad_vb_ptr++; data.quad_vb_ptr->position = {position.x + size.x, position.y, 0.0f}; data.quad_vb_ptr->color = color; data.quad_vb_ptr->texture_coordinates = {1.0f, 0.0f}; data.quad_vb_ptr->texture_index = texture_index; data.quad_vb_ptr++; data.quad_vb_ptr->position = {position.x + size.x, position.y + size.y, 0.0f}; data.quad_vb_ptr->color = color; data.quad_vb_ptr->texture_coordinates = {1.0f, 1.0f}; data.quad_vb_ptr->texture_index = texture_index; data.quad_vb_ptr++; data.quad_vb_ptr->position = {position.x, position.y + size.y, 0.0f}; data.quad_vb_ptr->color = color; data.quad_vb_ptr->texture_coordinates = {0.0f, 1.0f}; data.quad_vb_ptr->texture_index = texture_index; data.quad_vb_ptr++; data.index_count += 6; } int main() { glfwInit(); GLFWwindow *window = glfwCreateWindow(1280, 720, "test", nullptr, nullptr); if (window == nullptr) { std::cerr << "failed to create GLFW window\n"; } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, [](GLFWwindow *_, int width, int height) { glViewport(0, 0, width, height); }); if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { std::cerr << "failed to initialize GLAD\n"; } uint32_t quad_va, quad_vb, quad_ib; glGenVertexArrays(1, &quad_va); glBindVertexArray(quad_va); glGenBuffers(1, &quad_vb); glBindBuffer(GL_ARRAY_BUFFER, quad_vb); glBufferData(GL_ARRAY_BUFFER, data.max_vertices * sizeof(QuadVertex), nullptr, GL_DYNAMIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 10 * sizeof(float), nullptr); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 10 * sizeof(float), reinterpret_cast<const void *>(3 * sizeof(float))); glEnableVertexAttribArray(1); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(float), reinterpret_cast<const void *>(7 * sizeof(float))); glEnableVertexAttribArray(2); glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, 10 * sizeof(float), reinterpret_cast<const void *>(9 * sizeof(float))); glEnableVertexAttribArray(3); QuadVertex *quad_vb_base = new QuadVertex[data.max_vertices]; data.quad_vb_ptr = quad_vb_base; int *quad_indices = new int[data.max_indices]; int offset = 0; for (int i = 0; i < data.max_indices; i += 6) { quad_indices[i + 0] = offset + 0; quad_indices[i + 1] = offset + 1; quad_indices[i + 2] = offset + 2; quad_indices[i + 3] = offset + 2; quad_indices[i + 4] = offset + 3; quad_indices[i + 5] = offset + 0; offset += 4; } glGenBuffers(1, &quad_ib); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_ib); glBufferData(GL_ELEMENT_ARRAY_BUFFER, data.max_indices * sizeof(int), quad_indices, GL_STATIC_DRAW); delete[] quad_indices; Texture white_texture(1, 1); uint32_t white_texture_data = 0xffffffff; white_texture.SetData(&white_texture_data); data.textures[0] = &white_texture; Texture texture("assets/textures/checkerboard.png"); data.shader = new Shader("assets/GLSL/test3.glsl"); data.shader->Bind(); data.shader->SetInt("u_texture", 0); while (!glfwWindowShouldClose(window)) { glClearColor(0.1f, 0.1f, 0.1f, 1); glClear(GL_COLOR_BUFFER_BIT); data.shader->Bind(); DrawQuad({-0.5f, 0.0f, 0.0f}, {0.5f, 0.8f}, {0.8f, 0.2f, 0.3f, 1.0f}); DrawQuad({0.5f, -0.5f, 0.0f}, {0.5f, 0.75f}, {0.2f, 0.3f, 0.8f, 1.0f}); // DrawQuad({-0.5f, 0.0f, 0.0f}, {0.5f, 0.8f}, glm::vec4(1.0f), &texture); // THIS WORKS, BECAUSE THE TEXTURE IS SET EXPLICITLY (AND THUS NO "WHITE TEXTURE" IS USED) for (int i = 0; i < data.texture_slot_index; i++) { data.textures[i]->BindToArray(i); } int size = (uint8_t *) data.quad_vb_ptr - (uint8_t *) quad_vb_base; glBindBuffer(GL_ARRAY_BUFFER, quad_vb); glBufferSubData(GL_ARRAY_BUFFER, 0, size, quad_vb_base); glDrawElements(GL_TRIANGLES, data.index_count, GL_UNSIGNED_INT, nullptr); data.quad_vb_ptr = quad_vb_base; data.index_count = 0; glfwSwapBuffers(window); glfwPollEvents(); } } 着色器: #type vertex #version 330 core layout(location = 0) in vec3 a_pos; layout(location = 1) in vec4 a_color; layout(location = 2) in vec2 a_texcoord; layout(location = 3) in float a_texindex; out vec4 v_color; out vec2 v_texcoord; out float v_texindex; void main() { v_color = a_color; v_texcoord = a_texcoord; v_texindex = a_texindex; gl_Position = vec4(a_pos, 1.0); } #type fragment #version 330 core in vec4 v_color; in vec2 v_texcoord; in float v_texindex; out vec4 color; uniform sampler2DArray u_textures; void main() { color = texture(u_textures, vec3(v_texcoord.xy, v_texindex)) * v_color; } 未显示的 Shader 类简单地解析包含顶点和片段部分的着色器文件。 DrawQuad(position, size, color, texture) 函数运行正常(即绘制指定纹理)。但它的同胞没有纹理DrawQuad(position, size, color),其中未指定纹理,但选择为白色,因为texture_index设置为0.0f(其中white_texture也绑定为0),绘制黑色矩形(所以不工作)。我不确定原因是否是实际数据是0xffffffff,但这应该不是问题。但作为白色纹理(如 rgba 中的白色全为 1),它不应影响实际颜色(在 DrawQuad 中指定为 3 参数)。那么出了什么问题? 为了完整性,这里是纹理类: #include <test/main/texture.hpp> #include <iostream> #include <glad/glad.h> #define STB_IMAGE_IMPLEMENTATION #include <stb/stb_image.h> Texture::Texture(int width, int height) : width_(width), height_(height) { glGenTextures(1, &renderer_id_); glBindTexture(GL_TEXTURE_2D, renderer_id_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); InitArray(); } Texture::Texture(const std::string& filepath) { stbi_set_flip_vertically_on_load(1); data_ = stbi_load(filepath.c_str(), &width_, &height_, nullptr, 4); if (data_ == nullptr) { std::cerr << "Failed to load image"; } glGenTextures(1, &renderer_id_); glBindTexture(GL_TEXTURE_2D, renderer_id_); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_RGBA, GL_UNSIGNED_BYTE, data_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); InitArray(); } Texture::~Texture() { glDeleteTextures(1, &renderer_id_); } void Texture::Bind(int slot) const { glActiveTexture(GL_TEXTURE0 + slot); glBindTexture(GL_TEXTURE_2D, renderer_id_); } void Texture::BindToArray(int slot) const { if(data_ == nullptr){ std::cerr << "no texture data loaded"; } glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, slot, width_, height_, 1, GL_RGBA, GL_UNSIGNED_BYTE, data_); } void Texture::SetData(void *data) { data_ = data; glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); } void *Texture::GetData() const { return data_; } int Texture::GetWidth() const { return width_; } int Texture::GetHeight() const { return height_; } void Texture::InitArray() { if(array_renderer_id_ == 0){ glGenTextures(1, &array_renderer_id_); glBindTexture(GL_TEXTURE_2D_ARRAY, array_renderer_id_); int max_textures; glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_textures); glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, width_, height_, max_textures, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } } bool Texture::operator==(const Texture& rhs) const { return renderer_id_ == rhs.renderer_id_; } bool Texture::operator!=(const Texture& rhs) const { return !(rhs == *this); }

回答 0 投票 0

我正在尝试在 Webgl 中创建一个 3d 立方体,但我正在制作钻石?

我是 Webgl 的新手,并且一直在遵循许多指南来创建具有插值颜色的 3d 立方体,颜色被插值并且动画正在运行。但是,我需要的形状是...

回答 1 投票 0

无法渲染整个球体

我正在做一个需要我制作球体的项目。我决定使用的方法是一种称为八球体的方法。它是如何工作的,我从一个八面体开始,如下所示使用这个

回答 1 投票 0

bezier shape-fill:为相交测试寻找根的问题

我将这个 shadertoy 改编成一个 Unity 着色器并注意到,虽然它通常按预期工作,但会出现某些瑕疵,我将进一步解释。我相信他们是相关的...

回答 1 投票 0

奇怪的 rchit.glsl 着色器行为与 hitAttributeEXT 变量

当我遇到视觉错误时,我正在处理一些 Vulkan 光线跟踪渲染器。当我尝试调试它时,我注意到它归结为我的 rchit.glsl(射线最近命中)代码并且与

回答 0 投票 0

C++ 中 OpenGL 项目的着色器阅读器

我正在使用 Visual Studio 2022。 我正在尝试创建一个可以像着色器一样读取文件的程序。它会让我不把每一段代码都放在同一个文件中...... 所以,澄清一下我是什么

回答 0 投票 0

片段 GLSL 着色器中的奇怪行为

我试图在 OpenGL 中实现并行分割方差阴影贴图 (PSVSM)。我将视锥体分成 4 个部分。出于调试目的,我在每个平截头体分割中设置了不同的片段颜色。乙...

回答 0 投票 0

sfml 无法链接着色器

当我测试 gl 着色器时,它不起作用。 我尝试只加载片段着色器,在着色器中使用其他代码,但我的经验不允许我这样做 C++代码 #包括 #def...

回答 1 投票 0

OpenGL Matcap 着色器在透视中摇摆不定?

我正在尝试在 OpenGL #version 300 es 中编写一个 matcap 着色器。我正在努力将法线方向转换为 UV 空间。 到目前为止,它正在工作 90%,通过将网格乘以 n...

回答 0 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.