Adding texture to a sphere

edited March 2016 in Questions about Code

I'm working hard on my code and trying to add texture to my sphere. I'm not sure if I've written 'elegant' code. Probably not!

I have found the example of how to add texture which is:

PImage img;
PShape globe;

void setup() {
  img = loadImage("earth.jpg");
  globe = createShape(SPHERE, 50);
  globe.setTexture(img);
}

Mine is certainly longer. Although I know some of it isn't relevant to the question, I'll post my whole code. The texture I'd like to use (for the sake of this) is scales. It's in the data folder and the sketch runs without errors - just a wireframe sphere though - no texture.

From what I've read and understand - the texture() must be between beginShape and endShape() and "before any calls to vertices".

Where you're using a sphere(); without explicit vertices, how can texture() be implemented?

Rather than posting separately, I have a couple more questions regarding the same code.

  1. how can I slow down the movement of the camera vs mouse movement?
  2. I am using the .jpg "scales" at the moment but would like to use a patchy texture with the light shining through - imagine a mosaic with every other tile removed. Is this going to be difficult to implement for an obvious beginner like me?

Many thanks in advance

   // interactive sphere
PImage img;

int rotateX = 0, rotateY = 0;
int previousX, previousY;
float distanceX = 0.0, distanceY = 0.0;


void setup(){
size(1000, 1000, P3D);
img = loadImage("scales.jpg");
}

void draw(){
// background in draw not setup so drawn on loop and new sphere replaces old  
background(0);
//lights();
// converts mouse x movement into rotation around the object - over-ridden by mouse pressed/dragged functions
camera(mouseX, height/2, (height/2) / tan(PI/3), mouseX, height/2, 0, 0, 1, 0);
// set centre of screen fro drawing sphere
translate(width/2, height/2, 0);
rotateX(rotateX + distanceY);
rotateY(rotateY + distanceX);

noFill();
stroke(255);
texture(img);
sphere (100);

}

void mousePressed()
{
previousX = mouseX;
previousY = mouseY;
}
void mouseDragged()
{
distanceX = radians(mouseX - previousX);
distanceY = radians(previousY - mouseY);
}
void mouseReleased()
{
rotateX += distanceY;
rotateY += distanceX;
distanceX = distanceY = 0.0;
}

Answers

Sign In or Register to comment.