在OpenGL中创建和使用浮点纹理

3

我正在尝试在OpenGL中创建浮点纹理。 我有4个顶点(2个多边形)组成一个正方形:

                  -------
                  |\    |
                  |  \  |
                  |    \|
                  -------

现在我想用浮点值创建纹理,每个纹理值表示每个像素的基本颜色强度。
我想在片段着色器中计算像素颜色,如下所示:
color = texture2D(texture, coordinates).r * vec4(0.4, 1.0, 0.8, 1.0);

vec4(0.4, 1.0, 0.8, 1.0) 是我使用的基本颜色。

当我准备数据时:

int width, height;
width = 16;
height = 16;
float data[16][16];
for(int i = 0; i < width; i++){
    for(int j = 0; j < height; j++){
        data[i][j] = 0.5f;
    }
}

GLuint n_tex_surface;
glGenTextures(1, &n_tex_surface);
glBindTexture(GL_TEXTURE_2D, n_tex_surface);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, data);
glGenerateMipmap(GL_TEXTURE_2D);

编辑:

初始化

vertices[] = {
0, 1, -1, -1, 1, /**/1, 1, 1, -1, 1,/**/ 1, 0, 1, 1, 1,/**/ 0, 0, -1, 1, 1};
indices[] = {   
    0, 1, 2, 0, 2, 3};

glGenBuffers(1, &n_vertex_buffer_object);
glBindBuffer(GL_ARRAY_BUFFER, n_vertex_buffer_object);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glGenBuffers(1, &n_index_buffer_object);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, n_index_buffer_object);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

glGenVertexArrays(1, &n_vertex_array_object);
glBindVertexArray(n_vertex_array_object);
{
    glBindBuffer(GL_ARRAY_BUFFER, n_vertex_buffer_object);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), p_OffsetInVBO(0));
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), p_OffsetInVBO(2 * sizeof(float)));   

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, n_index_buffer_object);
}
glBindVertexArray(0);

绘图
glBindVertexArray(n_vertex_array_object);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, p_OffsetInVBO(0));
glBindVertexArray(0);

我的结果看起来像这样:16 与仅有一种颜色强度的正方形不同,我得到了这个混乱的图案。似乎纹理太小了,不适合正方形。纹理应该是什么尺寸?我该如何找出?
我创建纹理的方式是否正确?
128x128尺寸: 128 64x64尺寸时,我得到了这一列,这正是我想要的,但为什么它不能覆盖整个正方形? enter image description here 这是在我的计算机上使用OpenGL 3.3。
你能帮助我吗?
编辑:
当我用Addison Wesley OpenGL编程指南中的示例替换我的纹理创建代码时,它运行得很好,为什么我的代码不行呢?
GLuint n_tex_surface;
GLubyte checkImage[dataheight][datawidth][4];
 int i, j, c;
for (i = 0; i < dataheight; i++) {
  for (j = 0; j < datawidth; j++) {
     c = (((i&0x8)==0)^((j&0x8))==0)*255;
     checkImage[i][j][0] = (GLubyte) c;
     checkImage[i][j][1] = (GLubyte) c;
     checkImage[i][j][2] = (GLubyte) c;
     checkImage[i][j][3] = (GLubyte) 255;
  }
}

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &n_tex_surface);
glBindTexture(GL_TEXTURE_2D, n_tex_surface);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
               GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
               GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, datawidth, 
            dataheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 
            checkImage);

编辑:顶点着色器

#version 330
in vec2 v_tex;
in vec3 v_pos;
uniform mat4 t_modelview_projection_matrix;
out vec2 v_texcoord;
void main()
{
gl_Position = t_modelview_projection_matrix * vec4(v_pos, 1.0);
v_texcoord = v_tex;
} 

片元着色器
#version 330
in vec2 v_texcoord;
out vec4 color;

uniform sampler2D n_box_tex;

void main()
{
frag_color = texture2D(n_box_tex, v_texcoord).r * vec4(0.4, 1.0, 0.8, 1.0);
}

请展示绘制三角形的代码,包括顶点的纹理坐标。 - Kristian Duske
p_OffsetInVbo是什么作用? - Kristian Duske
#define p_OffsetInVBO(off) ((void*)(off)) - rluks
好的,最后,你如何在着色器中访问纹理坐标? - Kristian Duske
如果您将这两行代码放入您的程序中会发生什么?glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - Kristian Duske
https://dl.dropboxusercontent.com/u/46537231/stack_opengl_texture64-nowrap.PNG - rluks
2个回答

7
您只为您的纹理分配了1/4的存储空间。
您需要使用float data[16][16][4],然后将纹理的每个组件赋值为0.5f
int width, height;
width = 16;
height = 16;
float data[16][16][4];
for(int i = 0; i < width; i++){
    for(int j = 0; j < height; j++){
        data[i][j][0] = 0.5f;
        data[i][j][1] = 0.5f;
        data[i][j][2] = 0.5f;
        data[i][j][3] = 0.5f;
    }
}

你很幸运,因为你原先尝试的方法并没有导致软件崩溃。实际上,当GL读取纹理图像数据时,你正在强制GL超出已分配的存储空间。

但我想创建只有一个浮点值的纹理,每个像素都是如此。因此它不会是RGBA纹理。仅有一个...比方说...红色通道,带有浮点值。我该怎么做? - rluks
@Pan.student 好的,那么请使用单通道纹理,即使用 GL_R32F 而不是 GL_RGBA32F,并且使用 GL_RED 而不是 GL_RGBA。你认为一个 GL_RGBA32F 纹理包含多少个通道,或者 OpenGL 如何能够通过看到你提供的数据太短而确定你想要的内部格式呢? - Christian Rau

0

谢谢大家,问题出在GL_常量上! 这个代码现在可以正常工作:

GLuint n_tex_surface;
GLfloat checkImage[dataheight][datawidth];

for (int i = 0; i < dataheight; i++) {
    for (int j = 0; j < datawidth; j++) {
        float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX); = r + 0.01f;
        checkImage[i][j] = (GLfloat) r;
    }
}

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &n_tex_surface);
glBindTexture(GL_TEXTURE_2D, n_tex_surface);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

这里我们有GL_RED(单通道纹理),它是GL_FLOAT类型的:

 glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, datawidth, dataheight, 0, GL_RED, GL_FLOAT, checkImage);

其余的代码和着色器保持不变。


1
答案错误。GL_RED 不会创建浮点纹理,它将您的浮点数据转换为规范化的固定点格式(除此之外,它还是一种未指定大小的格式,因此已被弃用)。您需要的是 GL_R32F(当然,这是内部格式,CPU 格式仍为 GL_RED)。 - Christian Rau
那么应该是:glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, datawidth, dataheight, 0, GL_RED, GL_FLOAT, checkImage); 对吗? - rluks
没错。像GL_RGBAGL_RGBA8这样的纹理格式始终是规范化的定点格式,无论输入数据的类型如何。对于整数和浮点纹理,您需要在内部格式中明确使用FIUI - Christian Rau
接受已经给出原问题正确答案的回答,并删除不涉及原问题的回答,这样就足够了。;-) - Christian Rau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接