arduino - using processing for 3D image mapping

actually I'm Arduino Neopixel fan and tried to run the spherical POV. For run POV, each image should be converted to the number of lines which is used in each revolution , I think convert the planner POV must not be so hard for java base processing to pixelizid into lines but I don't now how to map an image on spherical and then converted to image lines I really appreciated for any comment

1 Answer

  1. Andrew- Reply


    The Processing editor comes with examples, one of which maps a texture image to a sphere. It does this by splitting the sphere into triangles, and then using the texture() and vertex() functions to draw the textured sphere.

    You can get to the code from the Processing editor by going to File > Examples > Topics > Textures > TextureSphere, but here's the code:

     * Texture Sphere 
     * by Gillian Ramsay
     * Rewritten by Gillian Ramsay to better display the poles.
     * Previous version by Mike 'Flux' Chang (and cleaned up by Aaron Koblin). 
     * Original based on code by Toxi.
     * A 3D textured sphere with simple rotation control.
    int ptsW, ptsH;
    PImage img;
    int numPointsW;
    int numPointsH_2pi; 
    int numPointsH;
    float[] coorX;
    float[] coorY;
    float[] coorZ;
    float[] multXZ;
    void setup() {
      size(640, 360, P3D);
      // Parameters below are the number of vertices around the width and height
      initializeSphere(ptsW, ptsH);
    // Use arrow keys to change detail settings
    void keyPressed() {
      if (keyCode == ENTER) saveFrame();
      if (keyCode == UP) ptsH++;
      if (keyCode == DOWN) ptsH--;
      if (keyCode == LEFT) ptsW--;
      if (keyCode == RIGHT) ptsW++;
      if (ptsW == 0) ptsW = 1;
      if (ptsH == 0) ptsH = 2;
      // Parameters below are the number of vertices around the width and height
      initializeSphere(ptsW, ptsH);
    void draw() {
      camera(width/2+map(mouseX, 0, width, -2*width, 2*width), 
             height/2+map(mouseY, 0, height, -height, height),
             height/2/tan(PI*30.0 / 180.0), 
             width, height/2.0, 0, 
             0, 1, 0);
      translate(width/2, height/2, 0);  
      textureSphere(200, 200, 200, img);
    void initializeSphere(int numPtsW, int numPtsH_2pi) {
      // The number of points around the width and height
      numPointsH_2pi=numPtsH_2pi;  // How many actual pts around the sphere (not just from top to bottom)
      numPointsH=ceil((float)numPointsH_2pi/2)+1;  // How many pts from top to bottom (abs(....) b/c of the possibility of an odd numPointsH_2pi)
      coorX=new float[numPointsW];   // All the x-coor in a horizontal circle radius 1
      coorY=new float[numPointsH];   // All the y-coor in a vertical circle radius 1
      coorZ=new float[numPointsW];   // All the z-coor in a horizontal circle radius 1
      multXZ=new float[numPointsH];  // The radius of each horizontal circle (that you will multiply with coorX and coorZ)
      for (int i=0; i<numPointsW ;i++) {  // For all the points around the width
        float thetaW=i*2*PI/(numPointsW-1);
      for (int i=0; i<numPointsH; i++) {  // For all points from top to bottom
        if (int(numPointsH_2pi/2) != (float)numPointsH_2pi/2 && i==numPointsH-1) {  // If the numPointsH_2pi is odd and it is at the last pt
          float thetaH=(i-1)*2*PI/(numPointsH_2pi);
        else {
          //The numPointsH_2pi and 2 below allows there to be a flat bottom if the numPointsH is odd
          float thetaH=i*2*PI/(numPointsH_2pi);
          //PI+ below makes the top always the point instead of the bottom.
    void textureSphere(float rx, float ry, float rz, PImage t) { 
      // These are so we can map certain parts of the image on to the shape 
      float changeU=t.width/(float)(numPointsW-1); 
      float changeV=t.height/(float)(numPointsH-1); 
      float u=0;  // Width variable for the texture
      float v=0;  // Height variable for the texture
      for (int i=0; i<(numPointsH-1); i++) {  // For all the rings but top and bottom
        // Goes into the array here instead of loop to save time
        float coory=coorY[i];
        float cooryPlus=coorY[i+1];
        float multxz=multXZ[i];
        float multxzPlus=multXZ[i+1];
        for (int j=0; j<numPointsW; j++) { // For all the pts in the ring
          normal(-coorX[j]*multxz, -coory, -coorZ[j]*multxz);
          vertex(coorX[j]*multxz*rx, coory*ry, coorZ[j]*multxz*rz, u, v);
          normal(-coorX[j]*multxzPlus, -cooryPlus, -coorZ[j]*multxzPlus);
          vertex(coorX[j]*multxzPlus*rx, cooryPlus*ry, coorZ[j]*multxzPlus*rz, u, v+changeV);

    textured sphere

    Hopefully this is a good start, but more generally: Stack Overflow isn't really designed for general "how do I do this" type questions. It's more designed for specific "I tried X, expected Y, but got Z instead" type questions. Can you post anything you've tried? Where is your Minimal, Complete, and Verifiable example?

    If you have no idea how to start, then start smaller: can you get an image mapped to a rectangle? Work your way up from there and post if you get stuck. Good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>