2017-01-23 26 views
-1

我正在尝试读取表示要在呈现卷时映射到颜色的值的文件。即,每个体素在输入文件中都有其颜色。这遵循一个虚拟测试文件:使用GL_TEXTURE_3D

4 4 4 
1 1 1 1 
1 1 1 1 
1 1 1 1 
1 1 1 1 
0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0 
1 1 1 1 
1 1 1 1 
1 1 1 1 
1 1 1 1 
0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0 

应该定义与它的顶部黑色的值,然后是白层,黑色层和白色一次。

这就是我读它:

int width, height, depth; 
file >> width; 
file >> height; 
file >> depth; 

printf("Texture data: %d, %d, %d\n", width, height, depth); 

// Creating buffer 
unsigned char buffer[height][width][depth]; 

// Reading content 
int v; 
for (int i = 0; i < height; i ++) { 
    for (int j = 0; j < width; j ++) { 
     for (int k = 0; k < depth; k ++) { 
      file >> v; 
      buffer[i][j][k] = v * 255; 
     } 
    } 
} 

这就是我如何创建我的质地:

// Create one OpenGL texture 
GLuint textureID; 
glGenTextures(1, &textureID); 

// "Bind" the newly created texture : all future texture functions will modify this texture 
glBindTexture(GL_TEXTURE_3D, textureID); 

// Give the image to OpenGL 
glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, height, width, depth, 0, GL_RED, GL_UNSIGNED_BYTE, buffer); 

glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 

这些是将要用于构建我VAO值:

_modelCoordinates = { -1.0, -1.0, 1.0, 
         1.0, -1.0, 1.0, 
         1.0, 1.0, 1.0, 
         -1.0, 1.0, 1.0, 
         -1.0, -1.0, -1.0, 
         1.0, -1.0, -1.0, 
         1.0, 1.0, -1.0, 
         -1.0, 1.0, -1.0 }; 

_modelIndices = { 0, 1, 2, 
        2, 3, 0, 
        1, 5, 6, 
        6, 2, 1, 
        7, 6, 5, 
        5, 4, 7, 
        4, 0, 3, 
        3, 7, 4, 
        4, 5, 1, 
        1, 0, 4, 
        3, 2, 6, 
        6, 7, 3 }; 

_textureCoordinates = { 0.0, 0.0, 1.0, 
         1.0, 0.0, 1.0, 
         1.0, 1.0, 1.0, 
         0.0, 1.0, 1.0, 
         0.0, 0.0, 0.0, 
         1.0, 0.0, 0.0, 
         1.0, 1.0, 0.0, 
         0.0, 1.0, 0.0 }; 

最后,这是我的着色器:

  1. 顶点着色器:

#version 410 core 

uniform mat4 mvMatrix; 
uniform mat4 pMatrix; 

in vec4 vPosition; 
in vec3 texCoord; 

// Input for the fragment shader 
smooth out vec3 uv; 

void main() { 
    gl_Position = pMatrix * mvMatrix * vPosition; 
    uv = texCoord; 
} 
  • 破片着色器:
  • #version 410 core 
    
    uniform sampler3D tex; 
    
    in vec3 uv; 
    out vec4 fragColor; 
    
    void main() 
    { 
        vec3 color = texture(tex, uv).rrr; 
        fragColor.rgb = color; 
    } 
    

    加载制服和属性值之后,我得出了音量:

    _modelView = modelView; 
    _projection = projection; 
    
    _succeed = Bind(); 
    if (_succeed) 
    { 
        glBindVertexArray(_vao); 
        LoadUniformVariables(); 
        glDrawElements(GL_TRIANGLES, _modelIndices.size(), GL_UNSIGNED_INT, 0); 
        glBindVertexArray(0); 
    } 
    UnBind(); 
    

    然而,所有我得到的是一个空白卷:

    enter image description here

    我不” t知道如何正确设置纹理坐标以及如何正确实现这种情况下的片段着色器。

    回答

    0

    添加这些行后:

    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); 
    

    ,一切运行良好。

    1

    看起来你的纹理坐标没有正确地分配给面顶点,所以应该修复。 但是你似乎预计OpenGL会“神奇地”为你执行某种体积光栅化。而且没有一种纹理坐标定位会做

    随着片段着色器的写入,只有通过卷数据的片段将被采样。你必须做的是写一个片段着色器,它实际上通过纹理发送光线,并在光线穿过的每个样本处进行采样。有一个在GPU上的宝石体绘制一整章:

    http://http.developer.nvidia.com/GPUGems/gpugems_ch39.html

    +0

    “*但是,你似乎期望OpenGL能够”神奇地“为你执行某种体积光栅化*”你为什么这么说? –

    +1

    @NicolBolas:由于OP问题中的第一句话:'(...)渲染卷时。“ – datenwolf