FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   Spherical texture map
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Spherical texture map  (Read 4287 times)
Mark Hill

WWW Email
Spherical texture map
« on: Apr 7th, 2005, 2:25pm »


This code takes a drawing buffer and uses it as a texture map for a sphere (utilizing Toxi's sphere routine).
 
Is it possible to get a perfect continous wrap? I'm just doing a normal  
x >width = 0 type wrapping for coordinates, but the mapping at the caps is wrong.
 
The cylindrical portion of the sphere is ok!
 
Anyway here it is: (apologies for dirty code)
 
 
 
Code:

 
 
BGraphics buf;
 
color pen;
 
void setup() {
 
  size(300, 300);
  buf = new BGraphics(150,150);
   
  buf.background(0);
  buf.format = ALPHA;  
   for (int i = 0; i < buf.width * buf.height; i++)  
   {  
     buf.pixels[i] = buf.pixels[i] & 0xff;  
   }  
   
  pen = color(0x77,0x77,0x77);
 
  bx = buf.width/2;
  by = buf.height/2;
   
 
  buf.ellipseMode(CENTER_DIAMETER);
  buf.strokeWeight(2);
  buf.fill(pen);
  buf.smooth();
  buf.stroke(pen);
 
  stroke(255,150,150);
  noSmooth();
   
 
 
}
 
void loop() {
  background(0);
   
  oldbx = (int)bx;
  oldby = (int)by;
  updatePos();
 
  // Blit buffer to screen, draw line and blit back modified section to buffer
  smoothLine(buf, (int)oldbx, (int)oldby, (int)bx, (int)by, pen);
 
    translate(width/2, height/2);
 // rotateY(yrot+=ymag*0.0075);
 
  rotateY(yrot+=xmag*0.0075);
  rotateZ(yrot+=ymag*0.0075);
  rotateX(xrot+=0.01);
  sphereDetail(5);
  texturedSphere(75, buf);
   
   
 // image(buf,0,0,width,height);
}
 
float yrot,xrot,zrot;
 
float bx,by,oldbx,oldby;
float xmag, ymag = 0;
 
void updatePos() {
  xmag = (pmouseX-width/2)*0.030;
  ymag = (pmouseY-height/2)*0.030;
  bx = (bx+xmag);
  by = (by+ymag);
   
   
  if(bx>buf.width) {bx=0; oldbx=0;}
  else if (bx<0) {bx=width; oldbx=width;}
   
   if(by>buf.height) {by=0; oldby=0;}
  else if (by<0) {by=height; oldby=height;}
   
}
 
public void smoothLine(BGraphics g, int x1, int y1, int x2, int y2, color penCol) {
  g.stroke(penCol); // Stroke on @ set weight
  g.fill(penCol);
  g.noSmooth();
  g.line(x1,y1,x2,y2);     // draw the line
 
  if(g.strokeWeight>1) {
    g.noStroke();  // strokeweight>1 causes strange behaviour, so noStroke to be safe, probably quicker, too.
    g.ellipse(x1, y1, g.strokeWeight*2,g.strokeWeight*2);  // cap previous line with rounded end
  }
}
 
 
 
 
 
 
 
// Toxi
// generic routine to draw textured sphere,
// based on current settings of sphereDetail()
void texturedSphere(float r, BImage t) {
  int v1,v11,v2;
  float[] sphereX=g.sphereX;
  float[] sphereY=g.sphereY;
  float[] sphereZ=g.sphereZ;
  int sphere_detail=g.sphere_detail;
   
  beginShape(TRIANGLE_STRIP);
  texture(t);
  float iu=(float)(t.width-1)/(sphere_detail);
  float iv=(float)(t.height-1)/(sphere_detail);
  float u=0,v=iv;
  for (int i = 0; i < sphere_detail; i++) {
    vertex(0, -r, 0,u,0);
    vertex(sphereX[i]*r, sphereY[i]*r, sphereZ[i]*r,u,v);
    u+=iu;
  }
  vertex(0, -r, 0,u,0);
  vertex(sphereX[0]*r, sphereY[0]*r, sphereZ[0]*r,u,v);
  endShape();
 
  // middle rings
  int voff = 0;
  for(int i = 2; i < sphere_detail; i++) {
    v1=v11=voff;
    voff += sphere_detail;
    v2=voff;
    u=0;
    beginShape(TRIANGLE_STRIP);
    texture(t);
    for (int j = 0; j < sphere_detail; j++) {
 vertex(sphereX[v1]*r, sphereY[v1]*r, sphereZ[v1++]*r,u,v);
 vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2++]*r,u,v+iv);
 u+=iu;
    }
    // close each ring
    v1=v11;
    v2=voff;
    vertex(sphereX[v1]*r, sphereY[v1]*r, sphereZ[v1]*r,u,v);
    vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2]*r,u,v+iv);
    endShape();
    v+=iv;
  }
  u=0;
  // add the northern cap
  beginShape(TRIANGLE_STRIP);
  texture(t);
  for (int i = 0; i < sphere_detail; i++) {
    v2 = voff + i;
    vertex(0, r, 0,u,v+iv);
    vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2]*r,u,v);
    u+=iu;
  }
  vertex(0, r, 0,u,v+iv);
  vertex(sphereX[voff]*r, sphereY[voff]*r, sphereZ[voff]*r,u,v);
  endShape();
}
 
 
Pages: 1 

« Previous topic | Next topic »