Skip to content

Commit

Permalink
Fixes #3279 Issue when upVector was aligned with the lookAt vector
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Feb 18, 2022
1 parent 0e57d04 commit d661d69
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 158 deletions.
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* AbstractCamera.java, in ummisco.gama.opengl, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* AbstractCamera.java, in ummisco.gama.opengl, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package ummisco.gama.opengl.camera;

Expand All @@ -22,6 +22,7 @@
import msi.gama.outputs.LayeredDisplayData;
import msi.gama.runtime.GAMA;
import msi.gama.runtime.PlatformHelper;
import msi.gaml.operators.Maths;
import ummisco.gama.dev.utils.DEBUG;
import ummisco.gama.dev.utils.FLAGS;
import ummisco.gama.opengl.renderer.IOpenGLRenderer;
Expand Down Expand Up @@ -173,25 +174,41 @@ public void update() {
cameraInteraction = !data.cameraInteractionDisabled();
updateSphericalCoordinatesFromLocations();
if (initialized) {
// if (flipped) {
// setUpVector(-(-Math.cos(theta * Maths.toRad) * Math.cos(phi * Maths.toRad)),
// -(-Math.sin(theta * Maths.toRad) * Math.cos(phi * Maths.toRad)), -Math.sin(phi * Maths.toRad));
// } else {
// setUpVector(-Math.cos(theta * Maths.toRad) * Math.cos(phi * Maths.toRad),
// -Math.sin(theta * Maths.toRad) * Math.cos(phi * Maths.toRad), Math.sin(phi * Maths.toRad));
// }
// AD Added here to recompute the upVector in case the lookAt and UpVector are aligned... See #3279
if (flipped) {
setUpVector(-(-Math.cos(theta * Maths.toRad) * Math.cos(phi * Maths.toRad)),
-(-Math.sin(theta * Maths.toRad) * Math.cos(phi * Maths.toRad)), -Math.sin(phi * Maths.toRad));
} else {
setUpVector(-Math.cos(theta * Maths.toRad) * Math.cos(phi * Maths.toRad),
-Math.sin(theta * Maths.toRad) * Math.cos(phi * Maths.toRad), Math.sin(phi * Maths.toRad));
}
drawRotationHelper();
}

initialized = true;
}

/**
* Update spherical coordinates from locations.
*/
protected abstract void updateSphericalCoordinatesFromLocations();

/**
* Draw rotation helper.
*/
protected abstract void drawRotationHelper();

@Override
/**
* Sets the position.
*
* @param xPos
* the x pos
* @param yPos
* the y pos
* @param zPos
* the z pos
*/
// @Override
public void setPosition(final double xPos, final double yPos, final double zPos) {
position.setLocation(xPos, yPos, zPos);
getRenderer().getData().setCameraPos(new GamaPoint(position));
Expand All @@ -212,10 +229,20 @@ public void setTarget(final double xLPos, final double yLPos, final double zLPos
getRenderer().getData().setCameraLookPos(new GamaPoint(target));
}

@Override
/**
* Sets the up vector.
*
* @param xPos
* the x pos
* @param yPos
* the y pos
* @param zPos
* the z pos
*/
// @Override
public void setUpVector(final double xPos, final double yPos, final double zPos) {
upVector.setLocation(xPos, yPos, zPos);
// DEBUG.OUT("Upvector modified as " + upVector);
// DEBUG.OUT("setUpVector modified as " + upVector);
getRenderer().getData().setCameraOrientation(new GamaPoint(upVector));
}

Expand All @@ -233,6 +260,7 @@ public void setUpVector(final double xPos, final double yPos, final double zPos)
@Override
public void animate() {
// TODO : Y and Z seem to be exchanged... Probably something to look at (see #3221)
// DEBUG.OUT("Animate: upVector = " + upVector);
glu.gluLookAt(position.x, position.y, position.z, target.x, target.y, target.z, upVector.x, upVector.y,
upVector.z);
}
Expand Down Expand Up @@ -307,6 +335,14 @@ protected final void internalMouseScrolled(final int count) {
zoom(count > 0);
}

/**
* Zoom.
*
* @param in
* the in
*/
protected abstract void zoom(boolean in);

/**
* Method mouseMove()
*
Expand Down Expand Up @@ -1027,7 +1063,13 @@ public void setPosition(final GamaPoint centre) {

}

@Override
/**
* Sets the initial Z factor corrector.
*
* @param corrector
* the new initial Z factor corrector
*/
// @Override
public void setInitialZFactorCorrector(final double corrector) { zCorrector = corrector; }

/**
Expand Down
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* CameraArcBall.java, in ummisco.gama.opengl, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* CameraArcBall.java, in ummisco.gama.opengl, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package ummisco.gama.opengl.camera;

Expand Down Expand Up @@ -35,7 +35,10 @@ public CameraArcBall(final IOpenGLRenderer renderer) {
super(renderer);
}

@Override
/**
* Update cartesian coordinates from angles.
*/
// @Override
public void updateCartesianCoordinatesFromAngles() {
theta = theta % 360;
phi = phi % 360;
Expand All @@ -48,7 +51,7 @@ public void updateCartesianCoordinatesFromAngles() {
final double sinT = Math.sin(factorT);
final double cosP = Math.cos(factorP);
final double sinP = Math.sin(factorP);
final double radius = getDistance();
final double radius = distance;
setPosition(radius * cosT * sinP + target.x, radius * sinT * sinP + target.y, radius * cosP + target.z);
// See #2854 -- see if putting this here does not restrict the moves using the mouse
if (flipped) {
Expand All @@ -58,6 +61,10 @@ public void updateCartesianCoordinatesFromAngles() {
}
}

/**
* Update spherical coordinates from locations.
*/
// @Override
@Override
public void updateSphericalCoordinatesFromLocations() {

Expand All @@ -67,7 +74,7 @@ public void updateSphericalCoordinatesFromLocations() {
theta = Maths.toDeg * Math.atan2(p.y, p.x);
// See issue on camera_pos
if (theta == 0) { theta = -90; }
phi = Maths.toDeg * Math.acos(p.z / getDistance());
phi = Maths.toDeg * Math.acos(p.z / distance);
}

/**
Expand Down Expand Up @@ -106,12 +113,12 @@ private void translateCameraFromScreenPlan(final double x_translation_in_screen,
final double y_translation_in_world = theta_vect_y_norm + phi_vect_y_norm;
final double z_translation_in_world = theta_vect_z_norm + phi_vect_z_norm;

setPosition(position.x - x_translation_in_world * getDistance() / 1000,
position.y - y_translation_in_world * getDistance() / 1000,
position.z - z_translation_in_world * getDistance() / 1000);
setTarget(target.x - x_translation_in_world * getDistance() / 1000,
target.y - y_translation_in_world * getDistance() / 1000,
target.z - z_translation_in_world * getDistance() / 1000);
setPosition(position.x - x_translation_in_world * distance / 1000,
position.y - y_translation_in_world * distance / 1000,
position.z - z_translation_in_world * distance / 1000);
setTarget(target.x - x_translation_in_world * distance / 1000,
target.y - y_translation_in_world * distance / 1000,
target.z - z_translation_in_world * distance / 1000);

updateSphericalCoordinatesFromLocations();
}
Expand Down Expand Up @@ -333,7 +340,7 @@ public void animate() {

@Override
public Double zoomLevel() {
return getRenderer().getMaxEnvDim() * getInitialZFactor() / getDistance();
return getRenderer().getMaxEnvDim() * getInitialZFactor() / distance;
}

@Override
Expand All @@ -342,12 +349,18 @@ public void zoom(final double level) {
updateCartesianCoordinatesFromAngles();
}

/**
* Zoom.
*
* @param in
* the in
*/
// @Override
@Override
public void zoom(final boolean in) {
if (keystoneMode) return;
final double step =
getDistance() != 0d ? getDistance() / 10d * GamaPreferences.Displays.OPENGL_ZOOM.getValue() : 0.1d;
setDistance(getDistance() + (in ? -step : step));
final double step = distance != 0d ? distance / 10d * GamaPreferences.Displays.OPENGL_ZOOM.getValue() : 0.1d;
setDistance(distance + (in ? -step : step));
// zoom(zoomLevel());
getRenderer().getData().setZoomLevel(zoomLevel(), true, false);
}
Expand Down Expand Up @@ -480,10 +493,13 @@ protected void drawRotationHelper() {
renderer.getOpenGLHelper().setRotationMode(ctrlPressed && cameraInteraction);
}

@Override
public double getDistance() { return distance; }

@Override
/**
* Sets the distance.
*
* @param distance
* the new distance
*/
// @Override
public void setDistance(final double distance) { this.distance = distance; }

}// End of Class CameraArcBall
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* FreeFlyCamera.java, in ummisco.gama.opengl, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* FreeFlyCamera.java, in ummisco.gama.opengl, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package ummisco.gama.opengl.camera;

Expand Down Expand Up @@ -44,7 +44,10 @@ public FreeFlyCamera(final IOpenGLRenderer renderer) {
initialize();
}

@Override
/**
* Update cartesian coordinates from angles.
*/
// @Override
public void updateCartesianCoordinatesFromAngles() {
if (phi > 89) {
this.phi = 89;
Expand All @@ -58,9 +61,6 @@ public void updateCartesianCoordinatesFromAngles() {
setTarget(forward.plus(position));
}

@Override
public double getDistance() { return position.minus(target).norm(); }

@Override
public void animate() {
super.animate();
Expand Down Expand Up @@ -137,6 +137,13 @@ public void zoom(final double level) {
updateCartesianCoordinatesFromAngles();
}

/**
* Zoom.
*
* @param in
* the in
*/
// @Override
@Override
public void zoom(final boolean in) {
final float step = Math.abs(getPosition().getZ() != 0 ? (float) position.getZ() / 10 : 0.1f);
Expand All @@ -146,11 +153,6 @@ public void zoom(final boolean in) {
getRenderer().getData().setZoomLevel(zoomLevel(), true, false);
}

@Override
public void setDistance(final double distance) {
// ??
}

@Override
public void zoomFocus(final Envelope3D env) {
final double extent = env.maxExtent();
Expand Down Expand Up @@ -207,4 +209,10 @@ public boolean isViewInXYPlan() {
@Override
protected void drawRotationHelper() {}

@Override
protected void updateSphericalCoordinatesFromLocations() {
// TODO Auto-generated method stub

}

}

0 comments on commit d661d69

Please sign in to comment.