From 75102c67903dae9f4a2d26535ada9515e4575293 Mon Sep 17 00:00:00 2001
From: Richard Pajersky <xpajersk@fi.muni.cz>
Date: Wed, 12 May 2021 10:32:31 +0200
Subject: [PATCH] Fixed backface to render properly

---
 .../analyst/gui/scene/DrawableMesh.java       | 10 +++++
 .../analyst/gui/scene/SceneRenderer.java      | 16 ++++---
 .../analyst/gui/tab/PostRegistrationCP.form   | 11 +++--
 .../analyst/gui/tab/PostRegistrationCP.java   | 43 +++++++++++++------
 .../RegistrationCPEventListener.java          |  9 ++++
 .../analyst/gui/tab/Bundle.properties         |  2 +-
 6 files changed, 66 insertions(+), 25 deletions(-)

diff --git a/GUI/src/main/java/cz/fidentis/analyst/gui/scene/DrawableMesh.java b/GUI/src/main/java/cz/fidentis/analyst/gui/scene/DrawableMesh.java
index d24fffd1..05b63074 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/gui/scene/DrawableMesh.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/gui/scene/DrawableMesh.java
@@ -32,6 +32,7 @@ public class DrawableMesh {
     private Vector3d scale = new Vector3d(0, 0, 0);
     // render mode
     private int renderMode = GL2.GL_FILL;
+    private boolean showBackface = true;
     // feature points
     private List<FeaturePoint> featurePoints = new ArrayList<>();
     private List<Color> featurePointsColor = new ArrayList<>();
@@ -169,4 +170,13 @@ public class DrawableMesh {
     public void setRenderFeaturePoints(boolean renderFeaturePoints) {
         this.renderFeaturePoints = renderFeaturePoints;
     }
+
+    public boolean isShowBackface() {
+        return showBackface;
+    }
+
+    public void setShowBackface(boolean showBackface) {
+        this.showBackface = showBackface;
+    }
+    
 }
diff --git a/GUI/src/main/java/cz/fidentis/analyst/gui/scene/SceneRenderer.java b/GUI/src/main/java/cz/fidentis/analyst/gui/scene/SceneRenderer.java
index 143338f8..faf88496 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/gui/scene/SceneRenderer.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/gui/scene/SceneRenderer.java
@@ -45,6 +45,7 @@ public class SceneRenderer {
         gl.setSwapInterval(1);
         gl.glEnable(GL2.GL_LIGHTING);
         gl.glEnable(GL2.GL_LIGHT0);
+        gl.glEnable(GL2.GL_LIGHT1);
         gl.glEnable(GL2.GL_DEPTH_TEST);
         gl.glClearColor(0,0,0,0);     // background for GLCanvas
         gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
@@ -110,6 +111,15 @@ public class SceneRenderer {
     public void renderScene(Camera camera, Collection<DrawableMesh> drawables) {
         clearScene();
         
+        // add back light to show/hide backface
+        float[] pos = {0f, 0f, -1f, 0f};
+        gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, pos, 0);
+        if (((DrawableMesh)drawables.toArray()[0]).isShowBackface()) {
+            gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, Color.white.getComponents(null), 0);
+        } else {
+            gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, Color.black.getComponents(null), 0);
+        }
+        
         // sets model to proper position
         Vector3d currentPosition = camera.getPosition();
         Vector3d center = camera.getCenter();
@@ -120,11 +130,6 @@ public class SceneRenderer {
 
         gl.glShadeModel(GL2.GL_SMOOTH);
         
-        // add back light
-        float[] pos = {0f, 0f, 1f, 0f};
-        gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, pos, 0);
-        gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, Color.white.getComponents(null), 0);
-        
         // set up transparent object to render later
         if (((DrawableMesh)drawables.toArray()[0]).getTransparency() != 1) {
             Collections.reverse((ArrayList)drawables);
@@ -188,7 +193,6 @@ public class SceneRenderer {
                 gl.glTranslated(point.getX(), point.getY(), point.getZ());
                 float[] rgba = {color.getRed() / 255f, color.getGreen() / 255f, 
                     color.getBlue() / 255f , obj.getTransparency()};
-                gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT, rgba, 0);
                 gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE, rgba, 0);
                 GLUquadric center = glu.gluNewQuadric();
                 glu.gluQuadricDrawStyle(center, GLU.GLU_FILL);
diff --git a/GUI/src/main/java/cz/fidentis/analyst/gui/tab/PostRegistrationCP.form b/GUI/src/main/java/cz/fidentis/analyst/gui/tab/PostRegistrationCP.form
index e339dc51..467f150b 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/gui/tab/PostRegistrationCP.form
+++ b/GUI/src/main/java/cz/fidentis/analyst/gui/tab/PostRegistrationCP.form
@@ -637,7 +637,7 @@
                       <EmptySpace type="separate" max="-2" attributes="0"/>
                       <Component id="profileButton" min="-2" pref="50" max="-2" attributes="0"/>
                       <EmptySpace type="separate" max="-2" attributes="0"/>
-                      <Component id="jToggleButton1" min="-2" max="-2" attributes="0"/>
+                      <Component id="backfaceButton" min="-2" pref="79" max="-2" attributes="0"/>
                       <EmptySpace max="32767" attributes="0"/>
                   </Group>
               </Group>
@@ -648,7 +648,7 @@
                       <Component id="viewLabel" alignment="3" min="-2" max="-2" attributes="0"/>
                       <Component id="frontButton" alignment="3" min="-2" max="-2" attributes="0"/>
                       <Component id="profileButton" alignment="3" min="-2" max="-2" attributes="0"/>
-                      <Component id="jToggleButton1" alignment="3" min="-2" max="-2" attributes="0"/>
+                      <Component id="backfaceButton" alignment="3" min="-2" max="-2" attributes="0"/>
                   </Group>
               </Group>
             </DimensionLayout>
@@ -714,10 +714,10 @@
                 </Property>
               </Properties>
             </Component>
-            <Component class="javax.swing.JToggleButton" name="jToggleButton1">
+            <Component class="javax.swing.JToggleButton" name="backfaceButton">
               <Properties>
                 <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
-                  <ResourceString bundle="cz/fidentis/analyst/gui/tab/Bundle.properties" key="PostRegistrationCP.jToggleButton1.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
+                  <ResourceString bundle="cz/fidentis/analyst/gui/tab/Bundle.properties" key="PostRegistrationCP.backfaceButton.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
                 </Property>
                 <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
                   <Border info="org.netbeans.modules.form.compat2.border.TitledBorderInfo">
@@ -725,6 +725,9 @@
                   </Border>
                 </Property>
               </Properties>
+              <Events>
+                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="backfaceButtonActionPerformed"/>
+              </Events>
             </Component>
           </SubComponents>
         </Container>
diff --git a/GUI/src/main/java/cz/fidentis/analyst/gui/tab/PostRegistrationCP.java b/GUI/src/main/java/cz/fidentis/analyst/gui/tab/PostRegistrationCP.java
index 1e18dded..6c52dc2b 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/gui/tab/PostRegistrationCP.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/gui/tab/PostRegistrationCP.java
@@ -32,8 +32,8 @@ public class PostRegistrationCP extends javax.swing.JPanel {
     private void setDefaults() {
         //transparencySlider.setMaximum(2 * listener.getTransparencyRange());
         //transparencySlider.setValue(listener.getTransparencyRange());
-        primaryColorPanel.setBackground(listener.DEFAULT_PRIMARY_COLOR);
-        secondaryColorPanel.setBackground(listener.DEFAULT_SECONDARY_COLOR);
+        primaryColorPanel.setBackground(RegistrationCPEventListener.DEFAULT_PRIMARY_COLOR);
+        secondaryColorPanel.setBackground(RegistrationCPEventListener.DEFAULT_SECONDARY_COLOR);
         primaryFillRB.setSelected(true);
         secondaryFillRB.setSelected(true);
         highShiftRB.setSelected(true);
@@ -150,7 +150,7 @@ public class PostRegistrationCP extends javax.swing.JPanel {
         profileButton = new javax.swing.JButton();
         frontButton = new javax.swing.JButton();
         viewLabel = new javax.swing.JLabel();
-        jToggleButton1 = new javax.swing.JToggleButton();
+        backfaceButton = new javax.swing.JToggleButton();
         featurePointsPanel = new javax.swing.JPanel();
         featurePointsButton = new javax.swing.JToggleButton();
         thresholdDownButton = new javax.swing.JButton();
@@ -535,8 +535,13 @@ public class PostRegistrationCP extends javax.swing.JPanel {
         viewLabel.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
         org.openide.awt.Mnemonics.setLocalizedText(viewLabel, org.openide.util.NbBundle.getMessage(PostRegistrationCP.class, "PostRegistrationCP.viewLabel.text")); // NOI18N
 
-        org.openide.awt.Mnemonics.setLocalizedText(jToggleButton1, org.openide.util.NbBundle.getMessage(PostRegistrationCP.class, "PostRegistrationCP.jToggleButton1.text")); // NOI18N
-        jToggleButton1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
+        org.openide.awt.Mnemonics.setLocalizedText(backfaceButton, org.openide.util.NbBundle.getMessage(PostRegistrationCP.class, "PostRegistrationCP.backfaceButton.text")); // NOI18N
+        backfaceButton.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
+        backfaceButton.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                backfaceButtonActionPerformed(evt);
+            }
+        });
 
         javax.swing.GroupLayout viewPanelLayout = new javax.swing.GroupLayout(viewPanel);
         viewPanel.setLayout(viewPanelLayout);
@@ -549,7 +554,7 @@ public class PostRegistrationCP extends javax.swing.JPanel {
                 .addGap(18, 18, 18)
                 .addComponent(profileButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addGap(18, 18, 18)
-                .addComponent(jToggleButton1)
+                .addComponent(backfaceButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
         );
         viewPanelLayout.setVerticalGroup(
@@ -558,7 +563,7 @@ public class PostRegistrationCP extends javax.swing.JPanel {
                 .addComponent(viewLabel)
                 .addComponent(frontButton)
                 .addComponent(profileButton)
-                .addComponent(jToggleButton1))
+                .addComponent(backfaceButton))
         );
 
         featurePointsPanel.setBackground(new java.awt.Color(176, 230, 226));
@@ -1424,7 +1429,7 @@ public class PostRegistrationCP extends javax.swing.JPanel {
     }//GEN-LAST:event_primaryColorPanelMouseClicked
 
     private void secondaryColorPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_secondaryColorPanelMouseClicked
-        if (transparencySlider.getValue() == 2*listener.TRANSPARENCY_RANGE) {
+        if (transparencySlider.getValue() == 2*RegistrationCPEventListener.TRANSPARENCY_RANGE) {
             return;
         }
         Color current = secondaryColorPanel.getBackground();
@@ -1450,7 +1455,7 @@ public class PostRegistrationCP extends javax.swing.JPanel {
             primaryFillRB.setEnabled(false);
             primaryLinesRB.setEnabled(false);
             primaryPointsRB.setEnabled(false);
-        } else if (transparency == 2*listener.TRANSPARENCY_RANGE) {
+        } else if (transparency == 2*RegistrationCPEventListener.TRANSPARENCY_RANGE) {
             secondaryColorPanel.setBackground(Color.lightGray);
             secondaryHighlightsCB.setEnabled(false);
             secondaryHighlightsCB.setEnabled(false);
@@ -1582,8 +1587,8 @@ public class PostRegistrationCP extends javax.swing.JPanel {
     }//GEN-LAST:event_secondaryPointsRBActionPerformed
 
     private void colorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colorButtonActionPerformed
-        primaryColorPanel.setBackground(listener.DEFAULT_PRIMARY_COLOR);
-        secondaryColorPanel.setBackground(listener.DEFAULT_SECONDARY_COLOR);
+        primaryColorPanel.setBackground(RegistrationCPEventListener.DEFAULT_PRIMARY_COLOR);
+        secondaryColorPanel.setBackground(RegistrationCPEventListener.DEFAULT_SECONDARY_COLOR);
         listener.setDeafultColor();
     }//GEN-LAST:event_colorButtonActionPerformed
 
@@ -1741,16 +1746,27 @@ public class PostRegistrationCP extends javax.swing.JPanel {
     }//GEN-LAST:event_thersholdButtonActionPerformed
 
     private void highShiftRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_highShiftRBActionPerformed
-        listener.setMoveModifier(listener.HIGH_SHIFT_QUOTIENT);
+        listener.setMoveModifier(RegistrationCPEventListener.HIGH_SHIFT_QUOTIENT);
     }//GEN-LAST:event_highShiftRBActionPerformed
 
     private void lowShiftRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lowShiftRBActionPerformed
-        listener.setMoveModifier(listener.LOW_SHIFT_QUOTIENT);
+        listener.setMoveModifier(RegistrationCPEventListener.LOW_SHIFT_QUOTIENT);
     }//GEN-LAST:event_lowShiftRBActionPerformed
 
+    private void backfaceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backfaceButtonActionPerformed
+        if (listener.isShowBackfaceActive()) {
+            backfaceButton.setText("show backface");
+            listener.setShowBackfaceActive(false);
+        } else {
+            backfaceButton.setText("hide backface");
+            listener.setShowBackfaceActive(true);   
+        }
+    }//GEN-LAST:event_backfaceButtonActionPerformed
+
 
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JButton applyButton;
+    private javax.swing.JToggleButton backfaceButton;
     private javax.swing.JButton colorButton;
     private javax.swing.JToggleButton featurePointsButton;
     private javax.swing.JLabel featurePointsLabel;
@@ -1773,7 +1789,6 @@ public class PostRegistrationCP extends javax.swing.JPanel {
     private javax.swing.JSeparator jSeparator7;
     private javax.swing.JSeparator jSeparator8;
     private javax.swing.JSeparator jSeparator9;
-    private javax.swing.JToggleButton jToggleButton1;
     private javax.swing.JButton leftRotationXButton;
     private javax.swing.JButton leftRotationYButton;
     private javax.swing.JButton leftRotationZButton;
diff --git a/GUI/src/main/java/cz/fidentis/analyst/registration/RegistrationCPEventListener.java b/GUI/src/main/java/cz/fidentis/analyst/registration/RegistrationCPEventListener.java
index 97a47c05..5f6aef54 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/registration/RegistrationCPEventListener.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/registration/RegistrationCPEventListener.java
@@ -372,4 +372,13 @@ public class RegistrationCPEventListener {
         return canvas;
     }
     
+    public boolean isShowBackfaceActive() {
+        return primaryFace.isShowBackface();
+    }
+    
+    public void setShowBackfaceActive(boolean state) {
+        primaryFace.setShowBackface(state);
+        secondaryFace.setShowBackface(state);
+        canvas.renderScene();
+    }
 }
diff --git a/GUI/src/main/resources/cz/fidentis/analyst/gui/tab/Bundle.properties b/GUI/src/main/resources/cz/fidentis/analyst/gui/tab/Bundle.properties
index 2f814ad1..2dbd5d61 100644
--- a/GUI/src/main/resources/cz/fidentis/analyst/gui/tab/Bundle.properties
+++ b/GUI/src/main/resources/cz/fidentis/analyst/gui/tab/Bundle.properties
@@ -86,4 +86,4 @@ PostRegistrationCP.highShiftRB.toolTipText=set high precision
 PostRegistrationCP.highShiftRB.text=high
 PostRegistrationCP.lowShiftRB.toolTipText=set low precision
 PostRegistrationCP.lowShiftRB.text=low
-PostRegistrationCP.jToggleButton1.text=hide backface
+PostRegistrationCP.backfaceButton.text=hide backface
-- 
GitLab