在 datenwolf 的大力帮助下,我最终采用了以下方法:
- 制作节点标量的 8 位 2D 纹理(例如温度)
- 在片段着色器中查找 1D 颜色图纹理中的灰度级别
和代码:
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys
from PIL import Image
import numpy as np
from OpenGL.GL import shaders
def LoadTexture():
global ID
arr = np.array([[0, 255, 0], [0, 0, 0]], dtype=np.int8)
im = Image.fromarray(arr, "L")
ix, iy, image = im.size[0], im.size[1], im.tobytes()
ID = glGenTextures(1)
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ix, iy, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, image)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
glEnable(GL_TEXTURE_2D)
def loadColorMap(imageName="jet.bmp"):
im = Image.open(imageName)
ix, _, image = im.size[0], im.size[1], im.tostring("raw", "RGBX", 0, -1)
ID = glGenTextures(1)
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_1D, ID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexImage1D(GL_TEXTURE_1D, 0, 3, ix, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) # avoids 1 => 0
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
glEnable(GL_TEXTURE_1D)
return ID
glutInit(sys.argv)
glutCreateWindow("")
VERTEX_SHADER = shaders.compileShader("""
void main() {
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}""", GL_VERTEX_SHADER)
FRAGMENT_SHADER = shaders.compileShader("""
uniform sampler2D texture;
uniform sampler1D colormap;
void main() {
float level = texture2D(texture, vec2(gl_TexCoord[0].s, gl_TexCoord[0].t)).r;
gl_FragColor = texture1D(colormap, level);
}""", GL_FRAGMENT_SHADER)
LoadTexture()
loadColorMap()
shader = shaders.compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
shaders.glUseProgram(shader)
texloc = glGetUniformLocation(shader, 'texture');
glUniform1i(texloc, 0);
texloc = glGetUniformLocation(shader, "colormap");
glUniform1i(texloc, 1);
glMatrixMode(GL_PROJECTION)
gluPerspective(45.0, 1, 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity() # Reset The View
glTranslatef(0.0, 0.0, -3.0) # Move Into The Screen
glBegin(GL_QUAD_STRIP)
glTexCoord2f(1. / 6, .25); glVertex3f(-1, 0, 0)
glTexCoord2f(1. / 6, .75); glVertex3f(-1, 1, 0)
glTexCoord2f(.5, .25);glVertex3f(0, 0, 0)
glTexCoord2f(.5, .75);glVertex3f(0, 1, 0)
glTexCoord2f(5. / 6, .25);glVertex3f(1, 0, 0)
glTexCoord2f(5. / 6, .75); glVertex3f(1, 1, 0)
glEnd()
glutSwapBuffers()
glutMainLoop()
产生这个图像: