以下程序使用底层 OpenGL 在处理过程中正确渲染纹理四边形。 纹理在这里
PShape s;
void setup() {
size(800, 800, P3D);
PImage ringtexture = loadImage("saturnringcolor.jpg");
textureMode(NORMAL);
fill(150, 0, 0);
s = createShape();
s.beginShape();
s.texture(ringtexture);
final float INSET = 10;
final float x1 = INSET, x2 = width - INSET;
final float y1 = INSET, y2 = height - INSET;
s.vertex(x1+50, y1, 0, 0);
s.vertex(x2-50, y1, 1, 0);
s.vertex(x2, y2, 1, 1);
s.vertex(x1, y2, 0, 1);
s.endShape();
background(0);
shape(s);
}
当我创建一个具有多个四边形的环并尝试应用纹理时,它只是一种米色。
void setup() {
size(800,800,P3D);
PImage ringtexture = loadImage("saturnringcolor.jpg");
PShape s = createShape();
s.beginShape(QUADS);
s.noStroke();
float r2 = 250, r = 200;
float nexta;
s.texture(ringtexture);
for (float a = 0; a < PI*2; a = nexta) {
nexta = a + PI / 30;
// Define the 4 corners of the quad
s.vertex(r2 * cos(a), r2 * sin(a), 0, 0);
s.vertex(r2 * cos(nexta), r2 * sin(nexta), 1, 0);
s.vertex(r * cos(nexta), r * sin(nexta), 1, 1);
s.vertex(r * cos(a), r * sin(a), 0, 1);
}
s.endShape();
background(0);
translate(width/2, height/2);
shape(s);
}
除此之外,还有一个单独的透明图像,因为土星环有间隙。我假设我必须组合两个图像并以支持它的格式获取 Alpha 通道。那么将 jpeg 更改为 png 吗?
尝试添加textureMode(NORMAL)和noLoop();
PImage ringTexture;
void setup() {
size(800,800,P3D);
ringTexture = loadImage("saturnRing.jpg");
textureMode(NORMAL);
PShape s = createShape();
s.beginShape(QUADS);
s.noStroke();
float r2 = 250, r = 200;
float nexta;
s.texture(ringTexture);
for (float a = 0; a < PI*2; a = nexta) {
nexta = a + PI / 30;
// Define the 4 corners of the quad
s.vertex(r2 * cos(a), r2 * sin(a), 0, 0);
s.vertex(r2 * cos(nexta), r2 * sin(nexta), 1, 0);
s.vertex(r * cos(nexta), r * sin(nexta), 1, 1);
s.vertex(r * cos(a), r * sin(a), 0, 1);
}
s.endShape();
background(0);
translate(width/2, height/2);
shape(s,0,0);
noLoop();
}