package com.test; import com.jogamp.opengl.glu.GLU; /** * * @author yogesh.mevada */ public class Camera { protected double[] eyeLoc = new double[3]; protected double[] directionVec = new double[3]; protected double[] viewLoc; protected double radius; protected double theta; protected double phi; Camera(float radius, double theta, double phi) { this.radius = radius; this.theta = theta; this.phi = phi; viewLoc = new double[]{0, 0, 0}; } void resetCamera(GLU glu) { glu.gluLookAt(eyeLoc[0], eyeLoc[1], eyeLoc[2], viewLoc[0], viewLoc[1], viewLoc[2], directionVec[0], directionVec[1], directionVec[2]); } public void updateThetaPhi(double thetaInc, double phiInc) { theta += thetaInc; phi += phiInc; calcEyePosition(); } public void updateRadius(double radiusInc) { radius += radiusInc; calcEyePosition(); } public void updateViewLocation(double[] viewLoc) { this.viewLoc = viewLoc; calcEyePosition(); } private void calcEyePosition() { double cy, cz, sy, sz; cy = Math.cos(theta); sy = Math.sin(theta); cz = Math.cos(phi); sz = Math.sin(phi); eyeLoc[0] = radius * cy * cz + viewLoc[0]; eyeLoc[1] = radius * sz + viewLoc[1]; eyeLoc[2] = -radius * sy * cz + viewLoc[2]; directionVec[0] = -cy * sz; directionVec[1] = cz; directionVec[2] = sy * sz; // To keep the camera always pointing upwards, if the y-component // of the up vector is negative, invert the whole up vector if (directionVec[1] < 0) { directionVec[0] = -directionVec[0]; directionVec[1] = -directionVec[1]; directionVec[2] = -directionVec[2]; } } }