Problems rotating camera angle in OpenGL
in
Programming Questions
•
5 months ago
I've created a sketch that plots different sized balls around the surface of a larger 3D sphere but I'm having trouble rotating the camera angle relative to the larger sphere.
I'm trying to compile this as javascript rather than java.
The Spherical part of the sketch is adapted from:
http://blog.blprnt.com/blog/blprnt/processing-tutorial-spherical-coordinates
The camera angle code has been cut from the example here:
http://processingjs.org/articles/RenderingModes.html
No luck with PeasyCam. I'm stuck as to how to proceed. I'm probably just doing something very stupid.
Any help would be most appreciated!
Here is my code:
import processing.opengl.*;
//import java.util.*;
//import java.math.BigDecimal;
Sphere mySphere;
String[] lines;
//float rotation = PI;
float ang = 0, ang2 = 0, ang3 = 0, ang4 = 0;
float px = 0, py = 0, pz = 0;
void setup()
{
size(500,500,OPENGL);
mySphere = new Sphere();
mySphere.init();
frameRate(50);
noStroke();
}
void draw()
{
mySphere.update();
mySphere.render();
println("in Camera");
beginCamera();
camera();
px = sin(radians(ang3)) * 170;
py = cos(radians(ang3)) * 300;
pz = sin(radians(ang4)) * 500;
translate(width/2 + px, height/2 + py, -700+pz);
rotateX(sin(radians(ang2)) * 120);
rotateY(sin(radians(ang2)) * 50);
rotateZ(sin(radians(ang2)) * 65);
ang2 += 0.01;
ang3 += 2.0;
ang4 += 0.75;
endCamera();
}
void mousePressed() {
mySphere.addSphereItem();
};
class Sphere {
float xPos = 250;
float yPos = 250;
float zPos = 250;
float radius = 50;
ArrayList items = new ArrayList();
public void Sphere() {
};
public void init() {
};
public void addSphereItem()
{
SphereItem si = new SphereItem();
si.parentSphere = this;
si.theta= random(PI*2);
si.phi= random(PI*2);
items.add(items.size(),si);
si.init();
println("addsphere");
}
public void update()
{
for (int i = 0; i < items.size(); i ++)
{
SphereItem si = (SphereItem) items.get(i);
si.update();
};
};
public void render() {
//Move to the center point of the sphere
translate(xPos, yPos, zPos);
//Mark our position in 3d space
pushMatrix();
//Render each SphereItem
for (int i = 0; i < items.size(); i ++)
{
SphereItem si = (SphereItem) items.get(i);
si.render();
};
popMatrix();
};
};
class SphereItem {
Sphere parentSphere;
float radius;
float theta;
float phi;
//Speed properties
float thetaSpeed = 0;
float phiSpeed = 0;
//Size
float itemSize = 5;
//String piDigits = "314159265358";
int piCounter = 0;
public void SphereItem()
{
};
public void init() {
itemSize = random(5);
thetaSpeed = 0.05;
phiSpeed = 0.06;
};
public void update()
{
theta += thetaSpeed;
phi += phiSpeed;
//println(piDigits.charAt(piCounter) - '0');
//itemSize = piDigits.charAt(piCounter) - '0';
//println(Float.parseFloat(piDigits.charAt(piCounter)));
//char piSize = piDigits.charAt(piCounter);
// println(x);
//String y = Character.toString(x);
//println(y);
String lines[] = loadStrings("pinumbers.txt");
//println(float(piSize));
itemSize = float(lines[piCounter])/3; // Set Item size as a x
piCounter= (piCounter + 1) % (lines.length - 1);
};
public void render() {
//Get the radius from the parent Sphere
float r = parentSphere.radius;
//Convert spherical coordinates into Cartesian coordinates
float x = cos(theta) * sin(phi) * r;
float y = sin(theta) * sin(phi) * r;
float z = cos(phi) * r;
//Mark our 3d space
pushMatrix();
//Move to the position of this item
translate(x,y,z);
//Set the fill colour
fill(0,0,0,150);
noStroke();
//Draw a circle
ellipse(0,0,itemSize,itemSize);
//Go back to our position in 3d space
popMatrix();
};
};
1