我需要为由多个四边形组成的曲面的某些点着色。问题是颜色不是对称分布在点周围,请参见图像,其中左方的右下角只有红色,而红色似乎被拉向右方的右上角)。
代码:
glBegin (GL_QUAD_STRIP);
glVertex3f (1., 0., 0)
glVertex3f (1., 1., 0)
glColor3f(1, 0, 0)
glVertex3f (0., 0., 0)
glColor3f(0, 0, 1)
glVertex3f (0., 1., 0)
glVertex3f (-1., 0., 0)
glVertex3f (-1., 1., 0)
glEnd ();
如果你制作两个单独的四边形,顺时针定义一个,另一个逆时针定义,那么颜色将围绕点对称,但不同于相邻点周围的颜色分布(如果它们是彩色的)。
那么如何用对称的颜色分布对任意点进行着色?
修改
我想想象一下基于每个顶点温度的表面温度,如下图所示(黄色= 0,橙色= 0.5,红色= 1):
答案 0 :(得分:5)
默认情况下,OpenGL在每个三角形上使用重心线性插值。这正是你所看到的。最简单的解决方案是编写一组顶点和片段着色器,实现基于距离的渐变。
细节取决于您的具体需求。
答案 1 :(得分:0)
在datenwolf的帮助下,我最终得到了以下方法:
代码:
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()
产生此图像的: