Hi @asymmetric,
As you need the colors strictly separated in this case the TRIANGLE_STRIP won’t work due to the kind of coloring behaviour for shared vertices. I’ve changed to draw the triangles separately so they have their own color each. See code below …
Cheers
— mnse
import peasy.*;
//GLOBAL VARIABLES
PImage img;
int borderGap = 10;
int resolution = 20;
//int maxHeight = 0;
Grid grid;
PeasyCam cam;
void vertexP(PVector P) {
vertex(P.x, P.y, P.z);
}
void setup() {
size(500, 500, P3D);
cam = new PeasyCam(this,600);
//pixelDensity(1);
grid = new Grid(resolution, borderGap);
smooth(8);
}
void draw() {
background(0);
translate(-width/2, -height/2 , 0);
//rotateY(frameCount/25.);
grid.display();
//noLoop();
}
/**
* Give vertices in this order:
*
* A -- B
* | |
* D -- C
*/
void triangulatedFace(PVector A, PVector B, PVector C, PVector D, boolean flipDiagonal) {
// B -> A -> C -> D
if (flipDiagonal) {
vertexP(B);
vertexP(A);
vertexP(C);
vertexP(D);
} else {
// A -> D -> B -> C
vertexP(A);
vertexP(D);
vertexP(B);
vertexP(C);
}
}
//CLASS
class Grid {
//INSTANCE VARIABLES
PVector[][] points;
boolean[][] diagonals;
boolean[][] colorswitch;
//CONSTRUCTOR
Grid(int resolution, int borderGap) {
points = new PVector[resolution][resolution];
diagonals = new boolean[resolution][resolution];
colorswitch = new boolean[resolution][resolution];
float spaceBetweenPoints = (float) (width - (2 * borderGap)) / (resolution - 1);
//img = loadImage("gradient888.jpg");
//img.loadPixels();
//img.resize(100,100);
// Construct points
for (int i = 0; i < resolution; i++) {
float x = borderGap + i * spaceBetweenPoints;
for (int j = 0; j < resolution; j++) {
float y = borderGap + j * spaceBetweenPoints;
float pointHeight = 200.*noise(i*0.1,j*0.1);// 0;//200 *(brightness(img.get(i,j))/ 255-0.8);
points[i][j] = new PVector(x, y, pointHeight);
// Store diagonal info, with 1/2 flip
diagonals[i][j] = random(1) < 0.5;
colorswitch[i][j] = random(1) < 0.5;
}
}
}
void displayPoints() {
strokeWeight(1);
stroke(#0BFF00);
for (int i = 0; i < resolution; i++) {
for (int j = 0; j < resolution; j++) {
PVector P = points[i][j];
point(P.x, P.y, P.z);
}
}
}
void displayTriangleStrips() {
stroke(255);
strokeWeight(1);
noFill();
for (int j = 0; j < resolution - 1; j++) {
for (int i = 0; i < resolution - 1; i++) {
PVector P1 = points[i][j];
PVector P2 = points[i+1][j];
PVector P3 = points[i+1][j+1];
PVector P4 = points[i][j+1];
noStroke();
beginShape(TRIANGLES);
if (diagonals[i][j]) {
fill(colorswitch[i][j] ? color(255, 0, 0) : color(0, 0, 255));
vertex(P4.x, P4.y, P4.z);
vertex(P1.x, P1.y, P1.z);
vertex(P2.x, P2.y, P2.z);
fill(!colorswitch[i][j] ? color(255, 0, 0) : color(0, 0, 255));
vertex(P4.x, P4.y, P4.z);
vertex(P2.x, P2.y, P2.z);
vertex(P3.x, P3.y, P3.z);
} else {
fill(colorswitch[i][j] ? color(255, 0, 0) : color(0, 0, 255));
vertex(P1.x, P1.y, P1.z);
vertex(P2.x, P2.y, P2.z);
vertex(P3.x, P3.y, P3.z);
fill(!colorswitch[i][j] ? color(255, 0, 0) : color(0, 0, 255));
vertex(P1.x, P1.y, P1.z);
vertex(P4.x, P4.y, P4.z);
vertex(P3.x, P3.y, P3.z);
}
//triangulatedFace(
// points[i][j],
// points[i + 1][j],
// points[i + 1][j + 1],
// points[i][j + 1],
// diagonals[i][j]
//);
endShape();
}
}
}
void display() {
displayTriangleStrips();
//displayPoints();
}
}
EDIT: Simply add PeasyCam so you can see it in 3D. Heightmap is random as I don’t have you image.