textures 相关问题

纹理是计算机图形中使用的一系列图像,用于将可见表面上的位置与不同的值相关联。这种关联通常通过将纹理上的位置(通常是二维的,但存在其他尺寸和类型的纹理)映射到渲染表面上的位置来完成。过滤通常用于在从纹理中采样值时消除锯齿,通常涉及称为mipmaps的纹理内的多个图像。

设置自定义形状的纹理

我是 Three.js 的新手,我遇到了这样的问题 我有一个像这样的自定义形状 // 创建自定义襟翼路径 constflapPath = new THREE.Path(); 瓣路径.moveTo(0, 0); // 0 襟翼路径。

回答 1 投票 0

vkCmdPipelineBarrier 抱怨 tessellationShader 功能未启用,但它甚至没有被使用

我正在努力在我的 Vulkan 渲染器中为每个对象添加 1 个或多个纹理。我遵循了关于无绑定资源的指南,该指南对其他人来说效果很好,并尝试在我的帮助下上传纹理

回答 1 投票 0

在 webGL2 #330 中,我可以将着色器的结果存储在纹理中吗?

在 webGL2 #330 中,我可以将着色器的结果存储在纹理中,然后通过统一的sampler2D 在另一个着色器中使用它吗? 如 openGL 版本 #460 所示 如果可能的话,我想避免在

回答 1 投票 0

窗户和门上的材料和纹理呈现全白色

基本上,我正在帮助开发一个应用程序,该应用程序允许用户创建建筑物和房屋的模型,然后将这些模型导出到移动应用程序中,以在现实世界中显示这些模型......

回答 1 投票 0

向水着色器图形添加渲染纹理

几个星期以来,我一直在尝试为 URP VR 游戏创建一个带有反射的水着色器图。这个游戏是作为小型大学课程的一部分创建的,所以我不想投资

回答 0 投票 0

使用纹理的 Alpha 通道编写具有适当 Alpha 的材质文件

我正在用 C# 编写一个程序来为我要导出的模型编写材料,目前它是这样工作的: mtlfs.WriteLine($"newmtl mtl_{id}"); mtlfs.WriteLine("...

回答 0 投票 0

为 3D 游戏的 SceneKit 创建纹理

我想看看使用 Scenekit 创建 3D 游戏。但我需要图像文件才能在 xcode 中与纹理一起使用。推荐的创建图像的软件是什么?例如,如果我想添加

回答 0 投票 0

MTKTextureLoader 因 mipmapped 纹理而失败

当我尝试从资产目录加载纹理时: 让 texLoader = MTKTextureLoader.init(设备:设备) texLoader.newTexture(withName: "Temple", scaleFactor: 1.0, bundle: nil, options: ...

回答 0 投票 0

.ktx 纹理、vulkan 和不需要的平铺问题

我正在将一些纹理从普通的 .png 文件转换为与 .ktx 文件一起使用。我已经能够加载它们并且它们实际上在模型上正确显示(意味着它们正确关联

回答 0 投票 0

如何在 Three.js 中使用噪声对立方体球体进行纹理处理,以便所有六个边都是连续的?

我正在做一个项目,我需要在 Three.js 中使用噪声对立方体球体进行纹理处理,以便纹理在所有六个面上都是连续的。我已经使用六个平面创建了立方体...

回答 0 投票 0

UE4:MSAA Texture2DArray 按切片绑定到渲染目标

好吧,看起来我是第一个在 UE4 源代码中使用 Create2DArray() 函数的人,因为 Visual Code 没有找到任何引用。 我想用

回答 0 投票 0

如何在 GLUT 上加载 bmp 以将其用作纹理?

我一直在四处寻找一个简单的解决方案,将精灵添加到我的 C++ OpenGl GLUT 简单月球着陆器游戏中,看来我必须使用 bmp,因为它们最容易加载并用作 te ...

回答 5 投票 0

如何在从 TextureRegistry.SurfaceTextureEntry 创建的表面上绘制

我想在flutter中使用Texture,然后使用java绘制TextureRegistry.SurfaceTextureEntry创建的表面;但我无法使用从表面获取的画布绘制矩形; 纹理规则...

回答 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

iOS 上的纹理库。无法更改蒙版角

请帮助我) 我无法将 maskedcorner 更改为 ASDisplayNode。 cornerRoundingType 有三种模式:ASCornerRoundingTypeClipping, ASCornerRoundingTypePrecomposited,

回答 0 投票 0

从 WebGL 纹理读取像素

我有一个 WebGLTexture 对象。我如何获取该纹理的像素(类似于 WebGL 的 readPixels,但用于纹理)? 我的一个想法是使用 preserveDrawing 创建画布和 WebGL 上下文...

回答 1 投票 0

如何使用 GL_TEXTURE_2D_ARRAY 绑定多个纹理(作为数组)?

根据这个答案:OpenGL sampler2D array 您不能使用片段着色器输入变量来索引纹理采样器数组。你必须使用 sampler2DArray (GL_TEXTURE_2D_ARRAY) inste ...

回答 1 投票 0

绑定 2 个不同的纹理单元,但只有一个会在 opengl 中渲染 [重复]

有这个简单的代码: #包括 #包括 #包括 #define STB_IMAGE_IMPLEMENTATION #包括 #包括 有这个简单的代码: #include <iostream> #include <glad/glad.h> #include <GLFW/glfw3.h> #define STB_IMAGE_IMPLEMENTATION #include <stb/stb_image.h> #include <test/main/shader.hpp> uint32_t LoadTexture(const std::string& filepath) { int w, h, bits; stbi_set_flip_vertically_on_load(1); uint8_t *pixels = stbi_load(filepath.c_str(), &w, &h, &bits, 4); uint32_t texture_id; glGenTextures(1, &texture_id); glBindTexture(GL_TEXTURE_2D, texture_id); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); stbi_image_free(pixels); return texture_id; } // https://stackoverflow.com/questions/60513272/alternative-for-glbindtextureunit-for-opengl-versions-less-than-4-5 void Bind2DTexture(int unit, uint32_t texture) { glActiveTexture(GL_TEXTURE0 + unit); glBindTexture(GL_TEXTURE_2D, texture); } int main() { glfwInit(); GLFWwindow *window = glfwCreateWindow(960, 540, "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"; } glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Shader shader("assets/GLSL/test.glsl"); shader.Bind(); int samplers[2] = {0, 1}; shader.SetIntArray("u_textures", samplers, 2); uint32_t quad_va, quad_vb, quad_ib; glGenVertexArrays(1, &quad_va); glBindVertexArray(quad_va); float vertices[] = { // first texture - logo.png -1.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, -1.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // second texture - chessboard.png 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f }; glGenBuffers(1, &quad_vb); glBindBuffer(GL_ARRAY_BUFFER, quad_vb); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, nullptr); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 6, reinterpret_cast<const void *>(sizeof(float) * 3)); glEnableVertexAttribArray(1); glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(float) * 6, reinterpret_cast<const void *>(sizeof(float) * 5)); glEnableVertexAttribArray(2); uint32_t indices[] = { 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4 }; glGenBuffers(1, &quad_ib); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_ib); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); uint32_t logo_id = LoadTexture("assets/textures/logo.png"); uint32_t chessboard_id = LoadTexture("assets/textures/chessboard.png"); while (!glfwWindowShouldClose(window)) { glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader.Bind(); Bind2DTexture(0, logo_id); Bind2DTexture(1, chessboard_id); glBindVertexArray(quad_va); glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, nullptr); glfwSwapBuffers(window); glfwPollEvents(); } } 和着色器: #type vertex #version 450 core layout(location = 0) in vec3 a_pos; layout(location = 1) in vec2 a_texcoord; layout(location = 0) in float a_texindex; out vec2 v_texcoord; out float v_texindex; void main() { v_texcoord = a_texcoord; v_texindex = a_texindex; gl_Position = vec4(a_pos, 1.0); } #type fragment #version 450 core in vec2 v_texcoord; in float v_texindex; uniform sampler2D u_textures[2]; out vec4 color; void main() { int index = int(v_texcoord); color = texture(u_textures[index], v_texcoord); } 未显示的 Shader 类简单地解析包含顶点和片段部分的着色器文件。现在两个纹理通过最后一个属性指针来区分,一个浮点数 0.0f 用于第一个纹理,1.0f 用于第二个纹理(并转换为 int 以用作着色器内的索引)。但是在输出中,只有第一个,即 logo.png(因为它是通过 Bind2DTexture(0, logo_id); 设置的)将被绘制,而 chessboard.png 不会被绘制。但是两个纹理都是通过 Bind2DTexture 设置为单位的。那么为什么chessborad.png 不是画出来的吗? PS:为了完整起见,这里是着色器类: 标题: #ifndef TEST_SHADER_HPP #define TEST_SHADER_HPP #include <string> #include <glm/glm.hpp> class Shader { public: explicit Shader(const std::string& filepath); void Bind() const; void SetInt(const std::string& name, int value) const; void SetIntArray(const std::string& name, int *values, int count); void SetFloat(const std::string& name, float value) const; void SetVec3(const std::string& name, const glm::vec3& vec) const; void SetVec4(const std::string& name, const glm::vec4& vec) const; void SetMat4(const std::string& name, const glm::mat4& mat) const; private: static void CheckCompileErrors(uint32_t shader_id, const std::string& type, const std::string& filepath); uint32_t id_; }; #endif //TEST_SHADER_HP 来源: #include <test/main/shader.hpp> #include <iostream> #include <fstream> #include <sstream> #include <glad/glad.h> void Shader::CheckCompileErrors(uint32_t shader_id, const std::string& type, const std::string& filepath) { int success; char infoLog[1024]; if (type != "PROGRAM") { glGetShaderiv(shader_id, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(shader_id, 1024, nullptr, infoLog); std::cout << "ERROR::SHADER::COMPILATION_ERROR of type: " << type << "\n" << infoLog << "filepath: " << filepath; } } else { glGetProgramiv(shader_id, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(shader_id, 1024, nullptr, infoLog); std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "filepath: " << filepath; } } } Shader::Shader(const std::string& filepath) { // 1. divide vertex and fragment part enum class ShaderType { NONE = -1, VERTEX = 0, FRAGMENT = 1 }; std::ifstream stream(filepath); std::string line; std::stringstream ss[2]; ShaderType type = ShaderType::NONE; while (getline(stream, line)) { if (line.find("#type") != std::string::npos) { if (line.find("vertex") != std::string::npos) { type = ShaderType::VERTEX; } else if (line.find("fragment") != std::string::npos) { type = ShaderType::FRAGMENT; } else { std::cout << "Unknown shader: " << line << '\n'; } } else { if (type == ShaderType::NONE) { std::cout << "No shader defined" << '\n'; } else { ss[(int) type] << line << '\n'; } } } std::string vertex_code = ss[(int) ShaderType::VERTEX].str(); std::string fragment_code = ss[(int) ShaderType::FRAGMENT].str(); // 2. convert, compile and link sources to shader program // 2.1 convert string to c str const char *vertex_str = vertex_code.c_str(); const char *fragment_str = fragment_code.c_str(); // 2.2.1 vertex shader uint32_t vertex_id = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_id, 1, &vertex_str, nullptr); glCompileShader(vertex_id); CheckCompileErrors(vertex_id, "VERTEX", filepath); // 2.2.2 fragment shader uint32_t fragment_id = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_id, 1, &fragment_str, nullptr); glCompileShader(fragment_id); CheckCompileErrors(fragment_id, "FRAGMENT", filepath); // 2.3 shader program id_ = glCreateProgram(); glAttachShader(id_, vertex_id); glAttachShader(id_, fragment_id); glLinkProgram(id_); CheckCompileErrors(id_, "PROGRAM", filepath); // 2.4 delete the shaders as they're linked into program and no longer necessary glDeleteShader(vertex_id); glDeleteShader(fragment_id); } void Shader::Bind() const { glUseProgram(id_); } void Shader::SetInt(const std::string& name, int value) const { glUniform1i(glGetUniformLocation(id_, name.c_str()), value); } void Shader::SetIntArray(const std::string& name, int *values, int count) { glUniform1iv(glGetUniformLocation(id_, name.c_str()), count, values); } void Shader::SetFloat(const std::string& name, float value) const { glUniform1f(glGetUniformLocation(id_, name.c_str()), value); } void Shader::SetVec4(const std::string& name, const glm::vec4& vec) const { glUniform4f(glGetUniformLocation(id_, name.c_str()), vec.x, vec.y, vec.z, vec.w); } void Shader::SetVec3(const std::string& name, const glm::vec3& vec) const { glUniform3f(glGetUniformLocation(id_, name.c_str()), vec.x, vec.y, vec.z); } void Shader::SetMat4(const std::string& name, const glm::mat4& mat) const { glUniformMatrix4fv(glGetUniformLocation(id_, name.c_str()), 1, GL_FALSE, &mat[0][0]); }

回答 0 投票 0

导出到 Unreal

从搅拌机导出到虚幻引擎时我有一个问题,我的颜色与搅拌机中的颜色不一样,添加的 UV 效果也没有保留。有没有什么办法解决这一问题?左边是对象impo...

回答 0 投票 0

质地不具约束力

我在使用 lwjgl 和 slick 2d 时一直遇到纹理无法绑定的问题。没有任何编译错误,它只是在纹理应该去的地方输出一个白框。 这是鳕鱼...

回答 0 投票 0

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