與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()
那產生這個圖像:
我想顏色要周圍的四個頂點的線性內插 - 是可能的。 我已經檢查了幾個例子和教程,但它們都沒有訪問周圍頂點的位置或顏色 –
@MadsMPedersen:根據定義,線性插值只能在兩個頂點之間起作用。 OpenGL的重心插值實際上非常接近簡單的線性插值。我建議你使用像Photoshop這樣的工具,並手動繪製你想看到的圖片。把它貼在這裏,我會建議你一個如何在OpenGL中完成的方法。 – datenwolf
編輯的問題 –