Hi!
I am receiving the following errors when trying to implement a 3d height map of triangles.
“Type mismatch: cannot convert from float to int”
The function “triangle()” expects parameters like: “triangle(float, float, float, float, float, float)”
// Set the size of each triangle in the grid
int triangleSize = 10;
// Set the maximum height of the triangles in the grid
int maxHeight = 255;
// Set the color of the triangles in the grid
color triangleColor = color(255, 255, 255);
// Load the height map image into the sketch
PImage heightMap;
void setup() {
// Set the size of the canvas
size(800, 600, P3D);
// Load the height map image into the sketch
heightMap = loadImage("heightmap.jpg");
}
void draw() {
// Clear the canvas to black
background(0);
// Iterate over the pixels in the height map
for (int x = 0; x < heightMap.width; x++) {
for (int y = 0; y < heightMap.height; y++) {
// Get the color of the current pixel in the height map
color pixelColor = heightMap.get(x, y);
// Extract the red, green, and blue values from the pixel color
int r = red(pixelColor);
int g = green(pixelColor);
int b = blue(pixelColor);
// Calculate the height of the triangle based on the value of the pixel
float height = maxHeight * (r + g + b) / (3 * 255.0);
// Create a PVector for the position of the triangle
PVector pos = new PVector((x - heightMap.width / 2) * triangleSize, (y - heightMap.height / 2) * triangleSize, height);
// Set the fill color for the triangle
fill(triangleColor);
// Draw the triangle at the calculated position
triangle(pos.x, pos.y, pos.z, pos.x + (float)triangleSize, pos.y, pos.z, pos.x + (float)triangleSize / 2, pos.y + (float)triangleSize, pos.z); // <-- added explicit type casts to float
}
}
}