Type mismatch: cannot convert from float to int

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
    }
  }
}






According to triangle()'s online reference it expects exactly 6 arguments:

You’re passing 9 arguments; 3 more!

2 Likes

Other than point and line, triangle doesn’t know a 3D version as you attempted.

Here is a pyramid though (base with 3 corners):

see : Find centre of pyramid and rotate from there, not the apex - #3 by Chrisir

And with a Pyramid class

  • its base with 4 corners (iirc) EDITED
  • The position you translate the pyramid to is exactly in the 3D center of the pyramid (like with box and sphere in P3D)
// Pyramid class

Pyramid pyramid;  

void setup() {
  size(768, 768, P3D);
  pyramid = new Pyramid( 200, height-221, -100 );
}

void draw() {
  background(0);
  lights(); 
  pyramid.display();
}

// ==============================================================================================

class Pyramid {

  float x, y, z; 
  PVector[] basePts = new PVector[4];

  //constr
  Pyramid( float x_, float y_, float z_) {
    x=x_;
    y=y_;
    z=z_;

    for (int i = 0; i < 4; i++ ) {
      float ang = TWO_PI * i / 4;
      basePts[i] = new PVector(
        cos(ang) * 90, 
        90/2.0, 
        sin(ang) * 90  );
      println(basePts[i]);
    } // for
  } // constr

  void display() {
    translate(x, y, z);
    // rotateX(radians(frameCount/8 % 360));
    // rotateY(radians(frameCount/2 % 360));

    fill(255);
    strokeWeight(3);
    stroke(123);

    //sides
    beginShape(TRIANGLES);
    for (int i = 0; i < 4; i++ ) {
      int i2 = (i+1) % 4;
      vertex(basePts[i].x, basePts[i].y, basePts[i].z);
      vertex(basePts[i2].x, basePts[i2].y, basePts[i2].z);
      vertex(0, -90/2.0, 0 );
    }
    endShape();//sides

    // show base
    fill(255, 0, 9); 
    beginShape(); //base
    for (int i = 0; i < 4; i++ ) {
      vertex(basePts[i].x, basePts[i].y, basePts[i].z);
    }
    endShape(CLOSE);
  } //method
  //
} // class
//
2 Likes

You can also use beginShape… vertex … in 3D

1 Like