rotating globe problem
in
Programming Questions
•
2 years ago
i'm trying to create a 3d stamping program where stamp objects rotate around a globe according to where ever you originally stamped them. the globe rotates if you hold tab and drag and moves if you hold shift or alt and drag. I got the stamps to move along with the globe, but i cant get them to rotate properly. they seem to all be rotating at different angles. This is my code:
import processing.opengl.*;
PImage tex;
float rotx = PI/4;
float roty = PI/4;
float rotz= PI/4;
float moveX;
float moveY;
float moveZ;
int counter=1;
boolean press;
PImage b;
backdrop Clouds;
world World;
Stamp[] stamps;
PImage[] images;
void setup()
{
size(1000, 740, OPENGL);
background(255);
stamps = new Stamp[1];
images = new PImage[33];
stamps[0] = new Stamp(-390,-390,0,0,0,0,0,0);
images[0] = loadImage( "bicycle.jpg" );
images[1] = loadImage( "book.jpg" );
images[2] = loadImage( "box.jpg" );
images[3] = loadImage( "burst.jpg" );
images[4] = loadImage( "droplets.jpg" );
images[5] = loadImage( "es.jpg" );
images[6] = loadImage( "fireworks.jpg" );
images[7] = loadImage( "fish.jpg" );
images[8] = loadImage( "flower.jpg" );
images[9] = loadImage( "goop.jpg" );
images[10] = loadImage( "hills.jpg" );
images[11] = loadImage( "lizard.jpg" );
images[12] = loadImage( "men.jpg" );
images[13] = loadImage( "monogram.jpg" );
images[14] = loadImage( "monster.jpg" );
images[15] = loadImage( "notes.jpg" );
images[16] = loadImage( "pelican.jpg" );
images[17] = loadImage( "plant.jpg" );
images[18] = loadImage( "ponytail.jpg" );
images[19] = loadImage( "question.jpg" );
images[20] = loadImage( "scarf.jpg" );
images[21] = loadImage( "spiral.jpg" );
images[22] = loadImage( "squiggle.jpg" );
images[23] = loadImage( "stairs.jpg" );
images[24] = loadImage( "star.jpg" );
images[25] = loadImage( "tree.jpg" );
images[26] = loadImage( "truffula.jpg" );
images[27] = loadImage( "tunnel.jpg" );
images[28] = loadImage( "two.jpg" );
images[29] = loadImage( "Untitled-1.jpg" );
images[30] = loadImage( "inside.jpg" );
images[31] = loadImage( "wheel.jpg" );
images[32] = loadImage( "Zig.jpg" );
tex = loadImage("paper.jpg");
b = loadImage("bg.jpg");
Clouds = new backdrop();
World= new world();
textureMode(NORMALIZED);
moveX=width/2.0;
moveY=height/2.0;
moveZ=-100;
press=false;
}
void draw()
{
if(keyPressed == true){
press=true;
}else{
press=false;
}
background(255);
noStroke();
lights();
Clouds.display();
World.display();
/*
imageMode(CENTER);
// Update and display all stamps
for (int i = 0; i < stamps.length; i ++ ) {
stamps[i].display(i);
image(images[counter],mouseX,mouseY);
}
*/
}
void mouseDragged() {
float rate = 0.01;
if(key==TAB){
rotx += (pmouseY-mouseY) * rate;
roty += (mouseX-pmouseX) * rate;
rotz += (mouseX-pmouseX) * rate;
}else if (key==CODED){
if(keyCode==SHIFT){
moveY += (pmouseY-mouseY) * -1.5;
moveX += (mouseX-pmouseX) * 1.5;
} else if(keyCode==ALT){
moveZ += (mouseY-pmouseY) * 1.5;
}
}
}
void mousePressed() {
// A new Stamp object
if(!press){
Stamp s = new Stamp(mouseX,mouseY, rotx, roty, rotz, moveX, moveY, moveZ);
// Append to array
stamps = (Stamp[]) append(stamps,s);
if (counter < images.length-1){
counter ++;
} else {
counter=0;
}
}
}
class Stamp {
float x;
float y;
float currentRotX;
float currentRotY;
float currentRotZ;
float currentMoveX;
float currentMoveY;
float currentMoveZ;
Stamp(float tempX, float tempY,float temp_rotX, float temp_rotY, float temp_rotZ, float temp_moveX, float temp_moveY, float temp_moveZ) {
x = tempX-500;
y = tempY-370;
currentRotX = temp_rotX;
currentRotY = temp_rotY;
currentRotZ = temp_rotZ;
currentMoveX = temp_moveX;
currentMoveY = temp_moveY;
currentMoveZ = temp_moveZ;
}
void display(int tempI) {
pushMatrix();
int i= tempI;
float thetaX= (rotx-stamps[i].currentRotX);
float thetaY= (roty-stamps[i].currentRotY);
float thetaZ= (rotz-stamps[i].currentRotZ);
float xPos = moveX - stamps[i].currentMoveX + 500;
float yPos = moveY - stamps[i].currentMoveY + 370;
float zPos = moveZ;
println("moveZ is " + moveZ);
//println("currentMoveY of " + i + " is " + stamps[i].currentMoveY);
println("zPos is " + zPos);
imageMode(CENTER);
i= i % images.length;
translate(xPos, yPos, zPos);
rotateX(thetaX);
rotateY(thetaY);
rotateZ(thetaZ);
translate(x,y,0);
image(images[i],0,0);
popMatrix();
}
}
class backdrop {
backdrop(){
}
void display(){
pushMatrix();
translate(190,580,-230);
image(b,0,0,2000,1441);
popMatrix();
}
}
class world {
world(){
}
void display(){
pushMatrix();
translate(moveX, moveY, moveZ);
rotateX(rotx);
rotateY(roty);
rotateZ(rotz);
scale(90);
World.TexturedCube(tex);
imageMode(CENTER);
popMatrix();
pushMatrix();
for (int i = 0; i < stamps.length; i ++ ) {
stamps[i].display(i);
}
pushMatrix();
translate(0,0,moveZ);
for (int i = 0; i < stamps.length; i ++ ) {
image(images[counter],mouseX,mouseY);
}
popMatrix();
popMatrix();
}
void TexturedCube(PImage tex) {
beginShape(TRIANGLE_STRIP);
texture(tex);
vertex(1.191847, 0, -0.736602, 0.7, 0.6); //0
vertex(1.191847, 0, 0.736602, 0.91, 0.6); //1
vertex(0.736602, 1.191847, 0, 0.8, 0.4); //10
vertex(0.736602, 1.191847, 0, 0.8, 0.4); //10
vertex(1.191847, 0, -0.736602, 0.71, 0.6); //0
vertex(0, 0.736602, -1.191847, .62, .4); //6
vertex(0, 0.736602, -1.191847, .6, .4); //6
vertex(1.191847, 0, -0.736602, 0.7, 0.6); //0
vertex(0, -0.736602, -1.191847, 0.55, 0.6); //7
vertex(0, -0.736602, -1.191847, 0.55, 0.6); //7
vertex(0, 0.736602, -1.191847, .6, .4); //6
vertex(-1.191847, 0, -0.736602, 0.45, 0.4); //3
vertex(-1.191847, 0, -0.736602, 0.45, 0.4); //3
vertex(0, -0.736602, -1.191847, 0.55, 0.6); //7
vertex(-0.736602, -1.191847, 0, 0.35, 0.6); //8
vertex(-0.736602, -1.191847, 0, 0.35, 0.6); //8
vertex(-1.191847, 0, -0.736602, 0.45, 0.4); //3
vertex(-1.191847, 0, 0.736602, 0.3, 0.4); //2
vertex(-1.191847, 0, 0.736602, 0.3, 0.4); //2
vertex(-0.736602, -1.191847, 0, 0.35, 0.6); //8
vertex(0, -0.736602, 1.191847, 0.2, 0.6); //4
vertex(0, -0.736602, 1.191847, 0.2, 0.6); //4
vertex(-0.736602, -1.191847, 0, 0.35, 0.6); //8
vertex(0.736602, -1.191847, 0, 0.27, 0.75); //9
vertex(0.736602, -1.191847, 0, 0.45, 0.75); //9
vertex(-0.736602, -1.191847, 0, 0.35, 0.6); //8
vertex(0, -0.736602, -1.191847, 0.55, 0.6); //7
vertex(0, -0.736602, -1.191847, 0.55, 0.6); //7
vertex(0.736602, -1.191847, 0, 0.65, 0.75); //9
vertex(1.191847, 0, -0.736602, 0.7, 0.6); //0
vertex(0.736602, -1.191847, 0, 0.81, 0.75); //9
vertex(1.191847, 0, -0.736602, 0.72, 0.6); //0
vertex(1.191847, 0, 0.736602, 0.91, 0.6); //1
vertex(0.736602, -1.191847, 0, .1, 0.75); //9
vertex(1.191847, 0, 0.736602, 0, 0.58); //1
vertex(0, -0.736602, 1.191847, 0.2, 0.58); //4
vertex(1.191847, 0, 0.736602, 0, 0.58); //1
vertex(0, -0.736602, 1.191847, 0.2, 0.58); //4
vertex(0, 0.736602, 1.191847, .1, .4); //5
vertex(0, 0.736602, 1.191847, .09, .4); //5
vertex(0, -0.736602, 1.191847, 0.2, 0.6); //4
vertex(-1.191847, 0, 0.736602, 0.3, 0.4); //2
vertex(-1.191847, 0, 0.736602, 0.27, 0.4); //2
vertex(0, 0.736602, 1.191847, 0.1, 0.4); //5
vertex(-0.736602, 1.191847, 0, 0.18, 0.26); //11
vertex(-0.736602, 1.191847, 0, 0.36, 0.26); //11
vertex(-1.191847, 0, 0.736602, 0.3, 0.4); //2
vertex(-1.191847, 0, -0.736602, 0.45, 0.4); //3
vertex(-1.191847, 0, -0.736602, 0.45, 0.4); //3
vertex(-0.736602, 1.191847, 0, 0.54, 0.26); //11
vertex(0, 0.736602, -1.191847, .6, .4); //6
vertex(0, 0.736602, -1.191847, .6, .4); //6
vertex(-0.736602, 1.191847, 0, 0.72, 0.26); //11
vertex(0.736602, 1.191847, 0, 0.8, 0.4); //10
vertex(0.736602, 1.191847, 0, 0.8, 0.4); //10
vertex(0, 0.736602, -1.191847, .6, .4); //6
vertex(1.191847, 0, -0.736602, 0.7, 0.6); //0
vertex(1.191847, 0, -0.736602, 0.7, 0.6); //0
vertex(0.736602, 1.191847, 0, 0.8, 0.4); //10
vertex(1.191847, 0, 0.736602, 0.91, 0.6); //1
vertex(1.191847, 0, 0.736602, 0.91, 0.6); //1
vertex(0.736602, 1.191847, 0, 0.8, 0.41); //10
vertex(0, 0.736602, 1.191847, 1, 0.41); //5
vertex(0, 0.736602, 1.191847, 1, 0.4); //5
vertex(-0.736602, 1.191847, 0, 0.91, 0.26); //11
vertex(0.736602, 1.191847, 0, 0.81, 0.4); //10
endShape();
}
}
THANK YOU!!
import processing.opengl.*;
PImage tex;
float rotx = PI/4;
float roty = PI/4;
float rotz= PI/4;
float moveX;
float moveY;
float moveZ;
int counter=1;
boolean press;
PImage b;
backdrop Clouds;
world World;
Stamp[] stamps;
PImage[] images;
void setup()
{
size(1000, 740, OPENGL);
background(255);
stamps = new Stamp[1];
images = new PImage[33];
stamps[0] = new Stamp(-390,-390,0,0,0,0,0,0);
images[0] = loadImage( "bicycle.jpg" );
images[1] = loadImage( "book.jpg" );
images[2] = loadImage( "box.jpg" );
images[3] = loadImage( "burst.jpg" );
images[4] = loadImage( "droplets.jpg" );
images[5] = loadImage( "es.jpg" );
images[6] = loadImage( "fireworks.jpg" );
images[7] = loadImage( "fish.jpg" );
images[8] = loadImage( "flower.jpg" );
images[9] = loadImage( "goop.jpg" );
images[10] = loadImage( "hills.jpg" );
images[11] = loadImage( "lizard.jpg" );
images[12] = loadImage( "men.jpg" );
images[13] = loadImage( "monogram.jpg" );
images[14] = loadImage( "monster.jpg" );
images[15] = loadImage( "notes.jpg" );
images[16] = loadImage( "pelican.jpg" );
images[17] = loadImage( "plant.jpg" );
images[18] = loadImage( "ponytail.jpg" );
images[19] = loadImage( "question.jpg" );
images[20] = loadImage( "scarf.jpg" );
images[21] = loadImage( "spiral.jpg" );
images[22] = loadImage( "squiggle.jpg" );
images[23] = loadImage( "stairs.jpg" );
images[24] = loadImage( "star.jpg" );
images[25] = loadImage( "tree.jpg" );
images[26] = loadImage( "truffula.jpg" );
images[27] = loadImage( "tunnel.jpg" );
images[28] = loadImage( "two.jpg" );
images[29] = loadImage( "Untitled-1.jpg" );
images[30] = loadImage( "inside.jpg" );
images[31] = loadImage( "wheel.jpg" );
images[32] = loadImage( "Zig.jpg" );
tex = loadImage("paper.jpg");
b = loadImage("bg.jpg");
Clouds = new backdrop();
World= new world();
textureMode(NORMALIZED);
moveX=width/2.0;
moveY=height/2.0;
moveZ=-100;
press=false;
}
void draw()
{
if(keyPressed == true){
press=true;
}else{
press=false;
}
background(255);
noStroke();
lights();
Clouds.display();
World.display();
/*
imageMode(CENTER);
// Update and display all stamps
for (int i = 0; i < stamps.length; i ++ ) {
stamps[i].display(i);
image(images[counter],mouseX,mouseY);
}
*/
}
void mouseDragged() {
float rate = 0.01;
if(key==TAB){
rotx += (pmouseY-mouseY) * rate;
roty += (mouseX-pmouseX) * rate;
rotz += (mouseX-pmouseX) * rate;
}else if (key==CODED){
if(keyCode==SHIFT){
moveY += (pmouseY-mouseY) * -1.5;
moveX += (mouseX-pmouseX) * 1.5;
} else if(keyCode==ALT){
moveZ += (mouseY-pmouseY) * 1.5;
}
}
}
void mousePressed() {
// A new Stamp object
if(!press){
Stamp s = new Stamp(mouseX,mouseY, rotx, roty, rotz, moveX, moveY, moveZ);
// Append to array
stamps = (Stamp[]) append(stamps,s);
if (counter < images.length-1){
counter ++;
} else {
counter=0;
}
}
}
class Stamp {
float x;
float y;
float currentRotX;
float currentRotY;
float currentRotZ;
float currentMoveX;
float currentMoveY;
float currentMoveZ;
Stamp(float tempX, float tempY,float temp_rotX, float temp_rotY, float temp_rotZ, float temp_moveX, float temp_moveY, float temp_moveZ) {
x = tempX-500;
y = tempY-370;
currentRotX = temp_rotX;
currentRotY = temp_rotY;
currentRotZ = temp_rotZ;
currentMoveX = temp_moveX;
currentMoveY = temp_moveY;
currentMoveZ = temp_moveZ;
}
void display(int tempI) {
pushMatrix();
int i= tempI;
float thetaX= (rotx-stamps[i].currentRotX);
float thetaY= (roty-stamps[i].currentRotY);
float thetaZ= (rotz-stamps[i].currentRotZ);
float xPos = moveX - stamps[i].currentMoveX + 500;
float yPos = moveY - stamps[i].currentMoveY + 370;
float zPos = moveZ;
println("moveZ is " + moveZ);
//println("currentMoveY of " + i + " is " + stamps[i].currentMoveY);
println("zPos is " + zPos);
imageMode(CENTER);
i= i % images.length;
translate(xPos, yPos, zPos);
rotateX(thetaX);
rotateY(thetaY);
rotateZ(thetaZ);
translate(x,y,0);
image(images[i],0,0);
popMatrix();
}
}
class backdrop {
backdrop(){
}
void display(){
pushMatrix();
translate(190,580,-230);
image(b,0,0,2000,1441);
popMatrix();
}
}
class world {
world(){
}
void display(){
pushMatrix();
translate(moveX, moveY, moveZ);
rotateX(rotx);
rotateY(roty);
rotateZ(rotz);
scale(90);
World.TexturedCube(tex);
imageMode(CENTER);
popMatrix();
pushMatrix();
for (int i = 0; i < stamps.length; i ++ ) {
stamps[i].display(i);
}
pushMatrix();
translate(0,0,moveZ);
for (int i = 0; i < stamps.length; i ++ ) {
image(images[counter],mouseX,mouseY);
}
popMatrix();
popMatrix();
}
void TexturedCube(PImage tex) {
beginShape(TRIANGLE_STRIP);
texture(tex);
vertex(1.191847, 0, -0.736602, 0.7, 0.6); //0
vertex(1.191847, 0, 0.736602, 0.91, 0.6); //1
vertex(0.736602, 1.191847, 0, 0.8, 0.4); //10
vertex(0.736602, 1.191847, 0, 0.8, 0.4); //10
vertex(1.191847, 0, -0.736602, 0.71, 0.6); //0
vertex(0, 0.736602, -1.191847, .62, .4); //6
vertex(0, 0.736602, -1.191847, .6, .4); //6
vertex(1.191847, 0, -0.736602, 0.7, 0.6); //0
vertex(0, -0.736602, -1.191847, 0.55, 0.6); //7
vertex(0, -0.736602, -1.191847, 0.55, 0.6); //7
vertex(0, 0.736602, -1.191847, .6, .4); //6
vertex(-1.191847, 0, -0.736602, 0.45, 0.4); //3
vertex(-1.191847, 0, -0.736602, 0.45, 0.4); //3
vertex(0, -0.736602, -1.191847, 0.55, 0.6); //7
vertex(-0.736602, -1.191847, 0, 0.35, 0.6); //8
vertex(-0.736602, -1.191847, 0, 0.35, 0.6); //8
vertex(-1.191847, 0, -0.736602, 0.45, 0.4); //3
vertex(-1.191847, 0, 0.736602, 0.3, 0.4); //2
vertex(-1.191847, 0, 0.736602, 0.3, 0.4); //2
vertex(-0.736602, -1.191847, 0, 0.35, 0.6); //8
vertex(0, -0.736602, 1.191847, 0.2, 0.6); //4
vertex(0, -0.736602, 1.191847, 0.2, 0.6); //4
vertex(-0.736602, -1.191847, 0, 0.35, 0.6); //8
vertex(0.736602, -1.191847, 0, 0.27, 0.75); //9
vertex(0.736602, -1.191847, 0, 0.45, 0.75); //9
vertex(-0.736602, -1.191847, 0, 0.35, 0.6); //8
vertex(0, -0.736602, -1.191847, 0.55, 0.6); //7
vertex(0, -0.736602, -1.191847, 0.55, 0.6); //7
vertex(0.736602, -1.191847, 0, 0.65, 0.75); //9
vertex(1.191847, 0, -0.736602, 0.7, 0.6); //0
vertex(0.736602, -1.191847, 0, 0.81, 0.75); //9
vertex(1.191847, 0, -0.736602, 0.72, 0.6); //0
vertex(1.191847, 0, 0.736602, 0.91, 0.6); //1
vertex(0.736602, -1.191847, 0, .1, 0.75); //9
vertex(1.191847, 0, 0.736602, 0, 0.58); //1
vertex(0, -0.736602, 1.191847, 0.2, 0.58); //4
vertex(1.191847, 0, 0.736602, 0, 0.58); //1
vertex(0, -0.736602, 1.191847, 0.2, 0.58); //4
vertex(0, 0.736602, 1.191847, .1, .4); //5
vertex(0, 0.736602, 1.191847, .09, .4); //5
vertex(0, -0.736602, 1.191847, 0.2, 0.6); //4
vertex(-1.191847, 0, 0.736602, 0.3, 0.4); //2
vertex(-1.191847, 0, 0.736602, 0.27, 0.4); //2
vertex(0, 0.736602, 1.191847, 0.1, 0.4); //5
vertex(-0.736602, 1.191847, 0, 0.18, 0.26); //11
vertex(-0.736602, 1.191847, 0, 0.36, 0.26); //11
vertex(-1.191847, 0, 0.736602, 0.3, 0.4); //2
vertex(-1.191847, 0, -0.736602, 0.45, 0.4); //3
vertex(-1.191847, 0, -0.736602, 0.45, 0.4); //3
vertex(-0.736602, 1.191847, 0, 0.54, 0.26); //11
vertex(0, 0.736602, -1.191847, .6, .4); //6
vertex(0, 0.736602, -1.191847, .6, .4); //6
vertex(-0.736602, 1.191847, 0, 0.72, 0.26); //11
vertex(0.736602, 1.191847, 0, 0.8, 0.4); //10
vertex(0.736602, 1.191847, 0, 0.8, 0.4); //10
vertex(0, 0.736602, -1.191847, .6, .4); //6
vertex(1.191847, 0, -0.736602, 0.7, 0.6); //0
vertex(1.191847, 0, -0.736602, 0.7, 0.6); //0
vertex(0.736602, 1.191847, 0, 0.8, 0.4); //10
vertex(1.191847, 0, 0.736602, 0.91, 0.6); //1
vertex(1.191847, 0, 0.736602, 0.91, 0.6); //1
vertex(0.736602, 1.191847, 0, 0.8, 0.41); //10
vertex(0, 0.736602, 1.191847, 1, 0.41); //5
vertex(0, 0.736602, 1.191847, 1, 0.4); //5
vertex(-0.736602, 1.191847, 0, 0.91, 0.26); //11
vertex(0.736602, 1.191847, 0, 0.81, 0.4); //10
endShape();
}
}
THANK YOU!!
1