|
|
@@ -6,73 +6,33 @@ import com.jogamp.opengl.GLEventListener;
|
|
|
import com.jogamp.opengl.glu.GLU;
|
|
|
import com.jogamp.opengl.util.texture.Texture;
|
|
|
import com.jogamp.opengl.util.texture.TextureIO;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
|
|
|
public class BlochRenderer implements GLEventListener {
|
|
|
|
|
|
- private BlochSphere blochSphere;
|
|
|
+
|
|
|
private final GLU glu = new GLU();
|
|
|
Texture colormap;
|
|
|
+ final RenderState state;
|
|
|
|
|
|
- private float radius = 3;
|
|
|
- private float pitch = 0;
|
|
|
- private float yaw = 0;
|
|
|
-
|
|
|
- private float lineWidth = 1.0f;
|
|
|
-
|
|
|
-
|
|
|
- public float getNonlinearity() {
|
|
|
- return nonlinearity;
|
|
|
- }
|
|
|
-
|
|
|
- public void setNonlinearity(float nonlinearity) {
|
|
|
- this.nonlinearity = nonlinearity;
|
|
|
- }
|
|
|
|
|
|
private float nonlinearRemap(float x){
|
|
|
// from (Mathematica):
|
|
|
// f[x_] := a + b*x + c*x^2 + d*x^3
|
|
|
// Solve[{f[0] == 0, f[1] == 1, f[1/2] == 1/2, f'[1/2] == s}, {a, b, c,
|
|
|
// d}]
|
|
|
+ float nonlinearity = state.getNonlinearity();
|
|
|
float xx = x*x;
|
|
|
float xxx = xx*x;
|
|
|
- return (3-2*nonlinearity)*x+6*(nonlinearity-1)*xx-4*(nonlinearity-1)*xxx;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private float nonlinearity = 1.0f;
|
|
|
-
|
|
|
- public float getRadius() {
|
|
|
- return radius;
|
|
|
- }
|
|
|
-
|
|
|
- public void setRadius(float radius) {
|
|
|
- if (radius < 0) radius = 0;
|
|
|
- this.radius = radius;
|
|
|
- }
|
|
|
-
|
|
|
- public float getPitch() {
|
|
|
+ float val = (3-2*nonlinearity)*x+6*(nonlinearity-1)*xx-4*(nonlinearity-1)*xxx;
|
|
|
+ return Math.max(0.0f, Math.min(val, 1.0f));
|
|
|
|
|
|
- return pitch;
|
|
|
}
|
|
|
|
|
|
- public void setPitch(float pitch) {
|
|
|
- while(pitch > 2*Math.PI) pitch -= (float)2*Math.PI;
|
|
|
- while(pitch < 0) pitch += (float)2*Math.PI;
|
|
|
- this.pitch = pitch;
|
|
|
- }
|
|
|
-
|
|
|
- public float getYaw() {
|
|
|
- return yaw;
|
|
|
- }
|
|
|
|
|
|
- public void setYaw(float yaw) {
|
|
|
- if (yaw > 0.4*Math.PI) yaw = (float) (0.4*Math.PI);
|
|
|
- if (yaw < -0.4*Math.PI) yaw = (float) (-0.4*Math.PI);
|
|
|
- this.yaw = yaw;
|
|
|
- }
|
|
|
|
|
|
@Override
|
|
|
public void init(GLAutoDrawable drawable) {
|
|
|
@@ -90,14 +50,15 @@ public class BlochRenderer implements GLEventListener {
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public void display(GLAutoDrawable drawable) {
|
|
|
+ public void display(@NotNull GLAutoDrawable drawable) {
|
|
|
|
|
|
final GL2 gl = drawable.getGL().getGL2();
|
|
|
setupViewMatrix(gl);
|
|
|
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
|
|
|
gl.glEnable(gl.GL_DEPTH_TEST);
|
|
|
- gl.glLineWidth(lineWidth);
|
|
|
- if (blochSphere != null) {
|
|
|
+ gl.glLineWidth(state.getLineWidth());
|
|
|
+ if (state.getBlochSphere() != null) {
|
|
|
+ BlochSphere blochSphere = state.getBlochSphere();
|
|
|
if (colormap != null){
|
|
|
colormap.enable(gl);
|
|
|
colormap.bind(gl);
|
|
|
@@ -117,14 +78,22 @@ public class BlochRenderer implements GLEventListener {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private void setupViewMatrix(GL2 gl) {
|
|
|
+ private void setupViewMatrix(@NotNull GL2 gl) {
|
|
|
gl.glMatrixMode( GL2.GL_MODELVIEW );
|
|
|
gl.glLoadIdentity();
|
|
|
+ float radius = state.getRadius();
|
|
|
+ float yaw = state.getYaw();
|
|
|
+ float pitch = state.getPitch();
|
|
|
+
|
|
|
glu.gluLookAt(radius*Math.sin(pitch)*Math.cos(yaw), radius*Math.cos(pitch)*Math.cos(yaw), radius*Math.sin(yaw), 0,0 ,0, 0, 0, 1);
|
|
|
}
|
|
|
|
|
|
+ public BlochRenderer(RenderState state) {
|
|
|
+ this.state = state;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
- public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
|
|
|
+ public void reshape(@NotNull GLAutoDrawable drawable, int x, int y, int width, int height) {
|
|
|
GL2 gl = drawable.getGL().getGL2();
|
|
|
if( height <=0 )
|
|
|
height =1;
|
|
|
@@ -134,19 +103,5 @@ public class BlochRenderer implements GLEventListener {
|
|
|
gl.glLoadIdentity();
|
|
|
glu.gluPerspective( 45.0f, h, 1.0, 20.0 );
|
|
|
setupViewMatrix(gl);
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public void setBlochSphere(BlochSphere blochSphere) {
|
|
|
- this.blochSphere = blochSphere;
|
|
|
- }
|
|
|
-
|
|
|
- public float getLineWidth() {
|
|
|
- return lineWidth;
|
|
|
- }
|
|
|
-
|
|
|
- public void setLineWidth(float lineWidth) {
|
|
|
- this.lineWidth = lineWidth;
|
|
|
}
|
|
|
}
|