我正在尝试读取表示要在呈现卷时映射到颜色的值的文件。即,每个体素在输入文件中都有其颜色。这遵循一个虚拟测试文件:使用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 };
最后,这是我的着色器:
- 顶点着色器:
。
#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();
然而,所有我得到的是一个空白卷:
我不” t知道如何正确设置纹理坐标以及如何正确实现这种情况下的片段着色器。
“*但是,你似乎期望OpenGL能够”神奇地“为你执行某种体积光栅化*”你为什么这么说? –
@NicolBolas:由于OP问题中的第一句话:'(...)渲染卷时。“ – datenwolf