【问题标题】:Sample from GL_TEXTURE_1D in fragment shader片段着色器中来自 GL_TEXTURE_1D 的样本
【发布时间】:2017-03-15 02:59:52
【问题描述】:

一直在尝试从 1D 纹理 (.png) 中采样,得到了一个具有正确纹理坐标的模型,但我只是无法让纹理显示出来。几何图形只是呈现黑色,我对 OpenGL 中的纹理一定有一些误解,但看不到它。

任何指针?

C++

// Setup
GLint texCoordAttrib = glGetAttribLocation(batch_shader_program, "vTexCoord");
glVertexAttribPointer(texCoordAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex<float>), (const void *)offsetof(Vertex<float>, texCoord));
glEnableVertexAttribArray(texCoordAttrib);

// Loading
GLuint load_1d_texture(std::string filepath) {
  SDL_Surface *image = IMG_Load(filepath.c_str());
  int width  = image->w;
  GLuint texture;
  glGenTextures(1, &texture);
  glBindTexture(GL_TEXTURE_1D, texture);
  glTexImage2D(GL_TEXTURE_1D, 0, GL_RGBA, width, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
  SDL_FreeSurface(image);
  return texture;
}

// Rendering
glUseProgram(batch.gl_program);
glBindTexture(GL_TEXTURE_1D, batch.mesh.texture.gl_texture_reference);
glDraw***

顶点着色器

#version 330 core

in vec3 position;
in vec4 vColor;
in vec3 normal; // Polygon normal
in vec2 vTexCoord;

// Model
in mat4 model;

out vec4 fColor; 
out vec3 fTexcoord;

// View or a.k.a camera matrix
uniform mat4 camera_view;

// Projection or a.k.a perspective matrix
uniform mat4 projection;

void main() {
   gl_Position = projection * camera_view * model * vec4(position, 1.0);
   fTexcoord = vec3(vTexCoord, 1.0);
}

片段着色器

#version 330 core   

in  vec4 fColor; 
out vec4 outColor;

in vec3 fTexcoord;   // passthrough shading for interpolated textures
uniform sampler1D sampler;

void main() {
   outColor = texture(sampler, fTexcoord.x);
}

【问题讨论】:

  • 您是否生成了 mipmap 或将 minicifaction 过滤器更改为不需要 mipmap 的模式?
  • @BDL,没有。这是必需的吗?
  • 默认的缩小过滤器是GL_NEAREST_MIPMAP_LINEAR。当没有 mipmap 存在时,过滤后的值始终为黑色。

标签: c++ opengl textures


【解决方案1】:
glBindTexture(GL_TEXTURE_2D, texture);

glBindTexture(GL_TEXTURE_1D, batch.mesh.texture.gl_texture_reference);

假设这两行代码讨论的是同一个 OpenGL 对象,你就不能这样做。使用 2D 纹理目标的纹理是 2D 纹理。它不是一维纹理,也不是一层的二维数组纹理,也不是深度为 1 的 3D 纹理,而是二维纹理。

在生成纹理对象后绑定它,texture's target is fixed.您可以使用view textures 创建具有不同目标的相同存储的视图,但原始纹理对象本身不受此影响。而且您无法创建 2D 纹理的 1D 视图。

当您尝试将 2D 纹理绑定为 1D 时,您应该会收到 GL_INVALID_OPERATION 错误。当您遇到 OpenGL 问题时,您应该始终check for errors

【讨论】:

    【解决方案2】:

    最后没有问题,只是纹理坐标加载中的一个错误(它从顶点获取了错误的索引..)..

    【讨论】:

      猜你喜欢
      • 2019-10-24
      • 2017-12-14
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多