OpenGL:如何创建色彩贴图纹理?

时间:2017-03-09 23:48:49

标签: opengl textures colormap

colorMap实例化:

GLfloat colormapColors[16];

这是我的色彩地图数据:

colormapColors[0] =  0.0f; // black
colormapColors[1] =  0.0f;
colormapColors[2] =  0.0f;
colormapColors[3] =  0.0f;

colormapColors[4] =  0.0f; // blue
colormapColors[5] =  0.0f;
colormapColors[6] =  1.0f;
colormapColors[7] =  0.0f;

colormapColors[8] =  1.0f; // red
colormapColors[9] =  0.0f;
colormapColors[10] = 0.0f;
colormapColors[11] = 0.0f;

colormapColors[12] = 1.0f; // white
colormapColors[13] = 1.0f;
colormapColors[14] = 1.0f;
colormapColors[15] = 1.0f;

在我的渲染循环中我有

glGenTextures(1, &texture); // get texture id
glBindTexture(GL_TEXTURE_1D, texture);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 4, 0, GL_RGBA, GL_FLOAT, colormapColors);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // linear interpolation when too small
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // linear interpolation when too bi
glErrorCheck;

片段着色器:

vec4 color = texture(ourTexture, clamp(alpha,0.0,1.0));
gl_FragColor = vec4(color.r, color.g, color.b, 1.0);

alpha是按照到中心的距离来计算的,我得到:

bad dot

所以,显然我们有黑色,蓝色,红色,白色。应该没有灰色。我认为这是一个数据对齐问题。我在做glTexImage1D错了吗?

1 个答案:

答案 0 :(得分:2)

您需要添加glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE),以便边缘纹理不会与另一侧的纹素混合。

您还可以将纹素区域中的alpha值([1/texSize;1-1/texSize])钳位,以演示纹素插值的工作原理。

相关问题