如何在现代OpenGL中对2D纹理进行调色化?

im9ewurl  于 2022-09-26  发布在  其他
关注(0)|答案(1)|浏览(163)

问题

我正在尝试用C语言编写一个简单的程序,使用OpenGL,它将允许根据调色板“绘制”2D C数组(int**,32位整数)。

目前(我还没有做到这一点,还远远没有))我正在学习如何将32位带符号的整数数组发送到GPU并以某种方式显示出来。

我正试图用现代的OpenGL来做这件事。

我的方法(请原谅我,因为我两天前才开始学习这些主题):

1.几何数据由四个顶点(vertices)组成,用于定义基于两个三角形的矩形(通过使用索引(indices)拾取顶点来定义)。vertices数据还与2D纹理坐标交错(用于着色器中的纹理采样)。
1.完成了VAO、VBO和EBO的生成和绑定,将顶点数据从RAM存储到VRAM中。
1.然后,我使用glTexImage2D()创建2D纹理,内部格式等于GL_R32I,因为我的C数组属于int**类型。我不太确定formattype参数,但我已经将它们分别设置为GL_RED_INTEGERGL_UNSIGNED_INT
1.在片段着色器中,我试图通过执行类似texture(texture1, TexCoord).r的操作来“读取”原始整数,但这可能是不正确的……我还尝试将红色组件强制转换为Float:(float) texture(texture1, TexCoord).r,但也不起作用。为了让您放心,代码可能会做一些正确的事情,只有片段着色器中的FragColor = vec4(1.0f, 0.8f, 0.2f, 1.0f);确实显示了这种颜色,这意味着我会得到一个用这种颜色填充窗口的矩形。因此,只有当我开始摆弄纹理时,我才会看到黑屏或青色RGB:(0, 1.0, 1.0, 1.0)

注意:我的C数组名为plane,现在它被左边的0值块和右边的1块填满。

现在,如果我能在片段着色器中硬编码一个if语句,将32位plane中的01着色为其他任何两种颜色,我会很高兴。然后我想我可以继续在调色板中加入一维纹理…如here所示。

代码

pixel.h


# ifndef PIXEL_H

# define PIXEL_H

/* 
  To make sure there will be no header conflicts, you can define
  GLFW_INCLUDE_NONE before the GLFW header to explicitly disable
  inclusion of the development environment header. This also allows
  the two headers to be included in any order. 

* /

# define GLFW_INCLUDE_NONE

# include <glad/glad.h>

# include <GLFW/glfw3.h>

# include <plane.h>

# include <utils.h>

# include <stdlib.h>

# include <stdio.h>

# endif

pixel.c


# include <pixel.h>

const char *vertexShaderSource = "#version 330 coren"
"layout (location = 0) in vec3 aPos;n"
"layout (location = 1) in vec2 aTexCoord;n"
"out vec2 TexCoord;n"
"void main()n"
"{n"
"   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);n"
"   TexCoord = vec2(aTexCoord.x, aTexCoord.y);n"
"}0";
const char *fragmentShaderSource = "#version 330 coren"
"out vec4 FragColor;n"
"in vec2 TexCoord;n"
"uniform isampler2D texture1;n"
"void main()n"
"{n"
"   FragColor = vec4(1.0f, 0.8f, 0.2f, 1.0f);n"
"   //FragColor = vec4(texture(texture1, TexCoord).r, 1.0f, 1.0f, 1.0f);n"
"}n0";

int main(void)
{

  // Window width and height.
  const unsigned int width = 20;
  const unsigned int height = 10;

  // Before you can use most GLFW functions, the library must be initialized.
  if (!glfwInit()) {
    printf("Could not initialise GLFW library!");
    exit(EXIT_FAILURE);
  }

  /*
   * By default, the OpenGL context GLFW creates may have any version.
   * You can require a minimum OpenGL version by setting the
   * GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR hints
   * before creation. If the required minimum version is not supported
   * on the machine, context (and window) creation fails.
   */
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  // Create a GLFW window.
  GLFWwindow* window = glfwCreateWindow(width, height, "pixel example", NULL, NULL);
  if (!window)
  {
    printf("Window or OpenGL context creation failed!n");
    glfwTerminate();
    exit(EXIT_FAILURE);
  }

  // Before you can use the OpenGL API, you must have a current OpenGL context.
  glfwMakeContextCurrent(window);

  /* 
   * If you are using an extension loader library to access modern OpenGL
   * then this is when to initialize it, as the loader needs a current
   * context to load from. This example uses glad, but the same rule applies
   * to all such libraries.
   */
  gladLoadGL();

  /*
   * Set a framebuffer size callback to update the viewport when
   * the window size changes.
   */
  glfwSetFramebufferSizeCallback(window, fb);

  /*
   * 
   * Data to be drawn.
   * 
   */
  int**plane = NewPlane(width, height);
  PLANE(width, height, if (i < width / 2) plane[i][j] = 0; else plane[i][j] = 1;)
  //plane[width/2][height/2] = 1;
  //PLANE(width, height, printf("%d %d %dn", i, j, plane[i][j]);)
  printf("size of int: %ld bytesn", sizeof(int));

  // build and compile our shader program
  // ------------------------------------
  // vertex shader
  unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
  glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
  glCompileShader(vertexShader);
  // check for shader compile errors
  int success;
  char infoLog[512];
  glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  if (!success)
  {
    glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
    printf("ERROR::SHADER::VERTEX::COMPILATION_FAILEDn%sn", infoLog);
  }
  // fragment shader
  unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
  glCompileShader(fragmentShader);
  // check for shader compile errors
  glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
  if (!success)
  {
    glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
  printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILEDn%sn", infoLog);
  }
  // link shaders
  unsigned int shaderProgram = glCreateProgram();
  glAttachShader(shaderProgram, vertexShader);
  glAttachShader(shaderProgram, fragmentShader);
  glLinkProgram(shaderProgram);
  // check for linking errors
  glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  if (!success) {
    glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
    printf("ERROR::SHADER::PROGRAM::LINKING_FAILED%sn", infoLog);
  }
  glDeleteShader(vertexShader);
  glDeleteShader(fragmentShader);

  // float vertices[] = {
  //    1.0f,  1.0f, 0.0f, // top right
  //    1.0f, -1.0f, 0.0f, // bottom right
  //   -1.0f, -1.0f, 0.0f, // bottom left
  //   -1.0f,  1.0f, 0.0f  // top left 
  // };
  float vertices[] = {
    // positions          // texture coords
     1.0f,  1.0f, 0.0f,   1.0f, 1.0f, // top right
     1.0f, -1.0f, 0.0f,   1.0f, 0.0f, // bottom right
    -1.0f, -1.0f, 0.0f,   0.0f, 0.0f, // bottom left
    -1.0f,  1.0f, 0.0f,   0.0f, 1.0f  // top left 
  };

  unsigned int indices[] = {
    // note that we start from 0!
    0, 1, 3, // first triangle
    1, 2, 3 // second triangle
  };

  unsigned int VBO, VAO, EBO;
  glGenVertexArrays(1, &VAO);
  printf("VAO: %dn", VAO);
  glGenBuffers(1, &VBO);
  printf("VBO: %dn", VBO);
  glGenBuffers(1, &EBO);
  printf("EBO: %dn", EBO);
  // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
  glBindVertexArray(VAO);

  glBindBuffer(GL_ARRAY_BUFFER, VBO);
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

  // glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  // glEnableVertexAttribArray(0);
  // position attribute
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
  glEnableVertexAttribArray(0);
  // texture coord attribute
  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
  glEnableVertexAttribArray(1);

  // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
  glBindBuffer(GL_ARRAY_BUFFER, 0); 

  // remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound.
  //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

  // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
  // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
  glBindVertexArray(0); 

  // uncomment this call to draw in wireframe polygons.
  //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

  unsigned int texture;
  glGenTextures(1, &texture);
  glBindTexture(GL_TEXTURE_2D, texture);
  if (plane) {
    glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, width, height, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, plane);
  }

  /*
   * 
   * Main loop
   * 
   */

  while (!glfwWindowShouldClose(window))
  {

    // Check if Escape is pressed and signal to close the window.
    input(window);

    // The glClearColor function is a state-setting function
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

    // The glClear is a state-using function in that it uses the
    // current state to retrieve the clearing color from.
    glClear(GL_COLOR_BUFFER_BIT);

    // Rendering goes here.
    glUseProgram(shaderProgram);
    glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

    glfwSwapBuffers(window);
    glfwPollEvents();
  }

  glfwDestroyWindow(window);

  glfwTerminate();
  exit(EXIT_SUCCESS);
}

plane.h


# ifndef PLANE_H

# define PLANE_H

# include <stdlib.h>

# include <stdio.h>

# define PLANE(width, height, A) {int i,j,_ii,_jj;for(i=0,_ii=width;i<_ii;i++)for(j=0,_jj=height;j<_jj;j++){A};}

int**NewPlane(int, int);

# endif

plane.c


# include <plane.h>

int**NewPlane(int width,int height)
{
  int**a;
  int i,j;

  a = (int**)calloc((size_t)(width),sizeof(int *));
  if (a == NULL) {
    fprintf(stderr,"NewPlane: error in memory allocationn");
    exit(EXIT_FAILURE);
  }
  a[0] = (int *)calloc((size_t)((width)*(height)),sizeof(int));
  if (a[0] == NULL) {
    fprintf(stderr,"NewPlane: error in memory allocationn");
    exit(EXIT_FAILURE);
  }
  for (i=1,j=width; i < j; i++)
    a[i] = a[i-1] + height;

  return a;
}
mqxuamgl

mqxuamgl1#

由于不能对整体纹理进行内插,因此需要启用缩小滤镜和放大滤镜,以选择最接近的滤镜:

glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

texture是一个重载函数。如果使用texture查找isampler2D,则返回值的类型为ivec4

uniform isampler2D texture1;

void main() 
{
    int value = texture(texture1, TexCoord).r;

    // [...]
}

由于内部数据类型为GL_R32I,因此返回值的范围在-2,147,483,648到2,147,483,647之间。默认帧缓冲区的格式是无符号规格化浮点格式。缓冲区中的数据表示范围[0.0,1.0]中的值。因此,需要缩放整数值(maxValue应为纹理中的最大值):

FragColor = vec4(float(value) / maxValue, 1.0f, 1.0f, 1.0f);

从整体纹理中获取颜色的一种方法是创建一个一维纹理,该纹理是一个颜色表,并使用该纹理中的整数值在表中查找颜色:

uniform isampler2D indexTexture;
uniform sampler1D colorTable;

void main() 
{
    int index = texture(indexTexture, TexCoord).r;
    vec4 color = texelFetch(colorTable, index, 0);

    // [...]
}

相关问题