Jelajahi Sumber

Less messy Renderer Configuration

Andreas Völker 5 tahun lalu
induk
melakukan
bb29f3728e

+ 9 - 0
BlochRender.iml

@@ -10,5 +10,14 @@
     <orderEntry type="library" name="com.io7m.bundles:org.jogamp.jogl:2.3.2001" level="project" />
     <orderEntry type="library" name="com.miglayout:miglayout-swing:4.2" level="project" />
     <orderEntry type="library" name="jogamp-fat" level="project" />
+    <orderEntry type="module-library">
+      <library>
+        <CLASSES>
+          <root url="jar://$MODULE_DIR$/lib/annotations-20.1.0.jar!/" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
   </component>
 </module>

TEMPAT SAMPAH
lib/annotations-20.1.0.jar


+ 14 - 22
src/de/andreasvoelker/BlochFrame.java

@@ -5,39 +5,33 @@ import com.jogamp.opengl.GLCapabilitiesImmutable;
 import com.jogamp.opengl.GLProfile;
 import com.jogamp.opengl.awt.GLCanvas;
 import com.jogamp.opengl.util.Animator;
+import org.jetbrains.annotations.NotNull;
 
 import java.awt.event.*;
 
 public class BlochFrame extends GLCanvas {
-    final BlochRenderer renderer;
-    final Animator animator;
-
-    public void setNonlinearity(float v) {
-        renderer.setNonlinearity(v);
-    }
-
-    public void setLineWidth(float value) {
-        renderer.setLineWidth(value);
-    }
+    final @NotNull BlochRenderer renderer;
+    final @NotNull Animator animator;
+    final RenderState state;
 
 
     class MyMouseMotionListener implements MouseMotionListener {
         int prevX, prevY;
 
         @Override
-        public void mouseDragged(MouseEvent e) {
+        public void mouseDragged(@NotNull MouseEvent e) {
             int dx = e.getX()-prevX;
             int dy = e.getY()-prevY;
 
-            renderer.setPitch(renderer.getPitch()+0.01f*dx);
-            renderer.setYaw(renderer.getYaw()+0.01f*dy);
+            state.setPitch(state.getPitch()+0.01f*dx);
+            state.setYaw(state.getYaw()+0.01f*dy);
 
             prevX = e.getX();
             prevY = e.getY();
         }
 
         @Override
-        public void mouseMoved(MouseEvent e) {
+        public void mouseMoved(@NotNull MouseEvent e) {
             prevX = e.getX();
             prevY = e.getY();
 
@@ -47,16 +41,17 @@ public class BlochFrame extends GLCanvas {
     class MyMouseWheelListener implements MouseWheelListener {
 
         @Override
-        public void mouseWheelMoved(MouseWheelEvent e) {
+        public void mouseWheelMoved(@NotNull MouseWheelEvent e) {
             System.out.println("Executed");
             System.out.println(e.getWheelRotation());
-            renderer.setRadius(renderer.getRadius()+0.1f*(float)e.getWheelRotation());
+            state.setRadius(state.getRadius()+0.1f*(float)e.getWheelRotation());
         }
     }
 
-    BlochFrame(){
+    BlochFrame(RenderState state){
         super(getCapabilities());
-        renderer = new BlochRenderer();
+        this.state = state;
+        renderer = new BlochRenderer(state);
         addGLEventListener(renderer);
         setSize(400, 400);
         animator = new Animator(this);
@@ -65,12 +60,9 @@ public class BlochFrame extends GLCanvas {
         addMouseWheelListener(this.new MyMouseWheelListener());
     }
 
-    private static GLCapabilitiesImmutable getCapabilities() {
+    private static @NotNull GLCapabilitiesImmutable getCapabilities() {
         final GLProfile profile = GLProfile.get(GLProfile.GL2);
         return new GLCapabilities(profile);
     }
 
-    public void setBlochSphere(BlochSphere blochSphere) {
-        renderer.setBlochSphere(blochSphere);
-    }
 }

+ 20 - 65
src/de/andreasvoelker/BlochRenderer.java

@@ -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;
     }
 }

+ 6 - 4
src/de/andreasvoelker/BlochSphere.java

@@ -1,12 +1,14 @@
 package de.andreasvoelker;
 
+import org.jetbrains.annotations.NotNull;
+
 public class BlochSphere {
     private static final int INITIAL_RESERVED = 20;
     private static final float GOLDEN_RATIO = 1.618f;
-    public float [] t = new float[INITIAL_RESERVED];
-    public float [] f = new float[INITIAL_RESERVED];
-    public float [] pre = new float[INITIAL_RESERVED];
-    public float [] pim = new float[INITIAL_RESERVED];
+    public float @NotNull [] t = new float[INITIAL_RESERVED];
+    public float @NotNull [] f = new float[INITIAL_RESERVED];
+    public float @NotNull [] pre = new float[INITIAL_RESERVED];
+    public float @NotNull [] pim = new float[INITIAL_RESERVED];
 
     private int size=0;
     private int reserved = INITIAL_RESERVED;

+ 13 - 10
src/de/andreasvoelker/MainWindows.java

@@ -1,21 +1,24 @@
 package de.andreasvoelker;
 
 import net.miginfocom.swing.MigLayout;
+import org.jetbrains.annotations.NotNull;
 
 import javax.swing.*;
 import java.io.*;
 
 public class MainWindows extends JFrame {
-    final JButton button;
-    final BlochFrame blochFrame;
-    final JSlider nonlinearRemapping;
-    final JSlider lineWidth;
+    final @NotNull JButton button;
+    final @NotNull BlochFrame blochFrame;
+    final @NotNull RenderState state;
+    final @NotNull JSlider nonlinearRemapping;
+    final @NotNull JSlider lineWidth;
 
     MainWindows(){
         super("BlochRender");
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 
-        blochFrame = new BlochFrame();
+        state = new RenderState();
+        blochFrame = new BlochFrame(state);
 
         button = new JButton("Select File");
         button.addActionListener(e -> chooseFile());
@@ -32,14 +35,14 @@ public class MainWindows extends JFrame {
         nonlinearRemapping.setMinimum(0);
         nonlinearRemapping.setMaximum(200);
         nonlinearRemapping.setValue(20);
-        nonlinearRemapping.addChangeListener(e -> blochFrame.setNonlinearity((float)(nonlinearRemapping.getValue())/20.0f));
+        nonlinearRemapping.addChangeListener(e -> state.setNonlinearity((float)(nonlinearRemapping.getValue())/20.0f));
 
         setLayout(new MigLayout("fill"));
         lineWidth = new JSlider();
         lineWidth.setMinimum(1);
         lineWidth.setMaximum(200);
         lineWidth.setValue(1);
-        lineWidth.addChangeListener(e -> blochFrame.setLineWidth((float)(lineWidth.getValue())/10.0f));
+        lineWidth.addChangeListener(e -> state.setLineWidth((float)(lineWidth.getValue())/10.0f));
 
         add(blochFrame, "grow, span 1 4");
 
@@ -47,7 +50,7 @@ public class MainWindows extends JFrame {
 
         add(new JLabel("Nonlinear remapping"));
         add(nonlinearRemapping, "wrap");
-        add(new JLabel("Linewidth"));
+        add(new JLabel("Line width"));
         add(lineWidth, "wrap");
 
         add(label, "span 2");
@@ -64,7 +67,7 @@ public class MainWindows extends JFrame {
         }
     }
 
-    private void loadFile(File selectedFile) {
+    private void loadFile(@NotNull File selectedFile) {
         FileInputStream fileInputStream;
         try {
             fileInputStream = new FileInputStream(selectedFile);
@@ -89,6 +92,6 @@ public class MainWindows extends JFrame {
             io.printStackTrace();
             return;
         }
-        blochFrame.setBlochSphere(blochSphere);
+        state.setBlochSphere(blochSphere);
     }
 }

+ 65 - 0
src/de/andreasvoelker/RenderState.java

@@ -0,0 +1,65 @@
+package de.andreasvoelker;
+
+import org.jetbrains.annotations.Nullable;
+
+public class RenderState {
+    private @Nullable BlochSphere blochSphere = null;
+    private float radius = 3;
+    private float pitch = 0;
+    private float yaw = 0;
+    private float lineWidth = 1.0f;
+    private float nonlinearity = 1.0f;
+
+    public @Nullable BlochSphere getBlochSphere() {
+        return blochSphere;
+    }
+
+    public void setBlochSphere(@Nullable BlochSphere blochSphere) {
+        this.blochSphere = blochSphere;
+    }
+
+    public float getRadius() {
+        return radius;
+    }
+
+    public void setRadius(float radius) {
+        if (radius < 0) radius = 0;
+        this.radius = radius;
+    }
+
+    public float getPitch() {
+        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;
+    }
+
+    public float getLineWidth() {
+        return lineWidth;
+    }
+
+    public void setLineWidth(float lineWidth) {
+        this.lineWidth = lineWidth;
+    }
+
+    public float getNonlinearity() {
+        return nonlinearity;
+    }
+
+    public void setNonlinearity(float nonlinearity) {
+        this.nonlinearity = nonlinearity;
+    }
+}