簡體   English   中英

在OpenGL中創建和使用浮點紋理

[英]Create and use floating point texture in OpenGL

我試圖在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我得到了這個專欄,這就是我想要的,但為什么不覆蓋整個廣場呢? 在此輸入圖像描述

這是在我的計算機上的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);
}

我們會根據您的紋理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_常量! 這按預期工作:

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);

其余的代碼和着色器保持不變。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM