From e9ed108a367159be3c62314a7099198270f0205d Mon Sep 17 00:00:00 2001
From: Amaury Johnen <amaury.johnen@uclouvain.be>
Date: Sat, 11 Nov 2017 08:58:04 +0100
Subject: [PATCH] change normal type into float (instead of char) for nice
 rendering

---
 Common/VertexArray.cpp | 23 ++++++++++-------------
 Common/VertexArray.h   |  8 ++++----
 Fltk/contextWindow.cpp |  2 +-
 Graphics/drawGeom.cpp  |  2 +-
 Graphics/drawMesh.cpp  |  2 +-
 Graphics/drawPost.cpp  | 19 +++++++++----------
 6 files changed, 26 insertions(+), 30 deletions(-)

diff --git a/Common/VertexArray.cpp b/Common/VertexArray.cpp
index ee329bf7e3..59d62ffe3d 100644
--- a/Common/VertexArray.cpp
+++ b/Common/VertexArray.cpp
@@ -33,12 +33,9 @@ void VertexArray::_addNormal(float nx, float ny, float nz)
 {
   // storing the normals as bytes hurts rendering performance, but it
   // significantly reduces the memory footprint
-  char cx = float2char(nx);
-  char cy = float2char(ny);
-  char cz = float2char(nz);
-  _normals.push_back(cx);
-  _normals.push_back(cy);
-  _normals.push_back(cz);
+  _normals.push_back(nx);
+  _normals.push_back(ny);
+  _normals.push_back(nz);
 }
 
 void VertexArray::_addColor(unsigned char r, unsigned char g, unsigned char b,
@@ -131,9 +128,9 @@ void VertexArray::finalize()
 
 class AlphaElement {
  public:
-  AlphaElement(float *vp, char *np, unsigned char *cp) : v(vp), n(np), c(cp) {}
+  AlphaElement(float *vp, float *np, unsigned char *cp) : v(vp), n(np), c(cp) {}
   float *v;
-  char *n;
+  float *n;
   unsigned char *c;
 };
 
@@ -182,14 +179,14 @@ void VertexArray::sort(double x, double y, double z)
   elements.reserve(n);
   for(int i = 0; i < n; i++){
     float *vp = &_vertices[3 * npe * i];
-    char *np = _normals.empty() ? 0 : &_normals[3 * npe * i];
+    float *np = _normals.empty() ? 0 : &_normals[3 * npe * i];
     unsigned char *cp = _colors.empty() ? 0 : &_colors[4 * npe * i];
     elements.push_back(AlphaElement(vp, np, cp));
   }
   std::sort(elements.begin(), elements.end(), AlphaElementLessThan());
 
   std::vector<float> sortedVertices;
-  std::vector<char> sortedNormals;
+  std::vector<float> sortedNormals;
   std::vector<unsigned char> sortedColors;
   sortedVertices.reserve(_vertices.size());
   sortedNormals.reserve(_normals.size());
@@ -215,7 +212,7 @@ void VertexArray::sort(double x, double y, double z)
 
 double VertexArray::getMemoryInMb()
 {
-  int bytes = _vertices.size() * sizeof(float) + _normals.size() * sizeof(char) +
+  int bytes = _vertices.size() * sizeof(float) + _normals.size() * sizeof(float) +
     _colors.size() * sizeof(unsigned char);
   return (double)bytes / 1024. / 1024.;
 }
@@ -224,7 +221,7 @@ char *VertexArray::toChar(int num, std::string name, int type, double min, doubl
                           int numsteps, double time, SBoundingBox3d bbox, int &len)
 {
   int vn = _vertices.size(), nn = _normals.size(), cn = _colors.size();
-  int vs = vn * sizeof(float), ns = nn * sizeof(char), cs = cn * sizeof(unsigned char);
+  int vs = vn * sizeof(float), ns = nn * sizeof(float), cs = cn * sizeof(unsigned char);
   int is = sizeof(int), ds = sizeof(double);
   int ss = name.size();
   double xmin = bbox.min().x(), ymin = bbox.min().y(), zmin = bbox.min().z();
@@ -314,7 +311,7 @@ void VertexArray::fromChar(int length, const char *bytes, int swap)
 
   int nn; memcpy(&nn, &bytes[index], is); index += is;
   if(nn){
-    _normals.resize(nn); int ns = nn * sizeof(char);
+    _normals.resize(nn); int ns = nn * sizeof(float);
     memcpy(&_normals[0], &bytes[index], ns); index += ns;
   }
 
diff --git a/Common/VertexArray.h b/Common/VertexArray.h
index 0bb0f8e565..940e680339 100644
--- a/Common/VertexArray.h
+++ b/Common/VertexArray.h
@@ -138,7 +138,7 @@ class VertexArray{
  private:
   int _numVerticesPerElement;
   std::vector<float> _vertices;
-  std::vector<char> _normals;
+  std::vector<float> _normals;
   std::vector<unsigned char> _colors;
   std::vector<MElement*> _elements;
   std::set<ElementData<3>, ElementDataLessThan<3> > _data3;
@@ -168,9 +168,9 @@ class VertexArray{
   std::vector<float>::iterator lastVertex(){return _vertices.end();}
 
   // return a pointer to the raw normal array
-  char *getNormalArray(int i=0){ return &_normals[i]; }
-  std::vector<char>::iterator firstNormal(){return _normals.begin();}
-  std::vector<char>::iterator lastNormal(){return _normals.end();}
+  float *getNormalArray(int i=0){ return &_normals[i]; }
+  std::vector<float>::iterator firstNormal(){return _normals.begin();}
+  std::vector<float>::iterator lastNormal(){return _normals.end();}
 
   // return a pointer to the raw color array
   unsigned char *getColorArray(int i=0){ return &_colors[i]; }
diff --git a/Fltk/contextWindow.cpp b/Fltk/contextWindow.cpp
index 0530366e2f..5cce70f79d 100644
--- a/Fltk/contextWindow.cpp
+++ b/Fltk/contextWindow.cpp
@@ -82,7 +82,7 @@ static void draw_stl(std::vector<SPoint3> &vertices, std::vector<SVector3> &norm
 
   glVertexPointer(3, GL_FLOAT, 0, va.getVertexArray());
   glEnableClientState(GL_VERTEX_ARRAY);
-  glNormalPointer(GL_BYTE, 0, va.getNormalArray());
+  glNormalPointer(GL_FLOAT, 0, va.getNormalArray());
   glEnableClientState(GL_NORMAL_ARRAY);
   glDisableClientState(GL_COLOR_ARRAY);
   glDrawArrays(GL_TRIANGLES, 0, va.getNumVertices());
diff --git a/Graphics/drawGeom.cpp b/Graphics/drawGeom.cpp
index 67cf4f25f1..275c9305dc 100644
--- a/Graphics/drawGeom.cpp
+++ b/Graphics/drawGeom.cpp
@@ -233,7 +233,7 @@ class drawGFace {
     glEnableClientState(GL_VERTEX_ARRAY);
     if(useNormalArray){
       glEnable(GL_LIGHTING);
-      glNormalPointer(GL_BYTE, 0, va->getNormalArray());
+      glNormalPointer(GL_FLOAT, 0, va->getNormalArray());
       glEnableClientState(GL_NORMAL_ARRAY);
     }
     else{
diff --git a/Graphics/drawMesh.cpp b/Graphics/drawMesh.cpp
index 1240f31170..9c6a147c52 100644
--- a/Graphics/drawMesh.cpp
+++ b/Graphics/drawMesh.cpp
@@ -367,7 +367,7 @@ static void drawArrays(drawContext *ctx, GEntity *e, VertexArray *va, GLint type
 
   if(useNormalArray){
     glEnable(GL_LIGHTING);
-    glNormalPointer(GL_BYTE, 0, va->getNormalArray());
+    glNormalPointer(GL_FLOAT, 0, va->getNormalArray());
     glEnableClientState(GL_NORMAL_ARRAY);
   }
   else
diff --git a/Graphics/drawPost.cpp b/Graphics/drawPost.cpp
index 9c960352cc..325439a963 100644
--- a/Graphics/drawPost.cpp
+++ b/Graphics/drawPost.cpp
@@ -39,8 +39,8 @@ static void drawArrays(drawContext *ctx, PView *p, VertexArray *va, GLint type,
       glColor4ubv((GLubyte *)va->getColorArray(4 * i));
       double f = 1.;
       if(opt->pointType > 1){
-        char *n = va->getNormalArray(3 * i);
-        f = char2float(*n);
+        float *n = va->getNormalArray(3 * i);
+        f = *n;
       }
       if(opt->pointType == 2){
         int s = (int)(opt->pointSize * f);
@@ -63,10 +63,9 @@ static void drawArrays(drawContext *ctx, PView *p, VertexArray *va, GLint type,
       double x[2] = {p0[0], p1[0]}, y[2] = {p0[1], p1[1]}, z[2] = {p0[2], p1[2]};
       glColor4ubv((GLubyte *)va->getColorArray(4 * i));
       if(opt->lineType == 2){
-        char *n0 = va->getNormalArray(3 * i);
-        char *n1 = va->getNormalArray(3 * (i + 1));
-        double v0 = char2float(*n0), v1 = char2float(*n1);
-        ctx->drawTaperedCylinder(opt->lineWidth, v0, v1, 0., 1., x, y, z, opt->light);
+        float *v0 = va->getNormalArray(3 * i);
+        float *v1 = va->getNormalArray(3 * (i + 1));
+        ctx->drawTaperedCylinder(opt->lineWidth, *v0, *v1, 0., 1., x, y, z, opt->light);
       }
       else if (opt->lineType == 1)
         ctx->drawCylinder(opt->lineWidth, x, y, z, opt->light);
@@ -74,9 +73,9 @@ static void drawArrays(drawContext *ctx, PView *p, VertexArray *va, GLint type,
 	float l = sqrt ((p0[0] - p1[0]) * (p0[0] - p1[0]) +
                         (p0[1] - p1[1]) * (p0[1] - p1[1]) +
                         (p0[2] - p1[2]) * (p0[2] - p1[2]) );
-        char *n0 = va->getNormalArray(3 * i);
-        char *n1 = va->getNormalArray(3 * (i + 1));
-        double v0 = char2float(*n0), v1 = char2float(*n1);
+        float *n0 = va->getNormalArray(3 * i);
+        float *n1 = va->getNormalArray(3 * (i + 1));
+        double v0 = *n0, v1 = *n1;
 	float dir [3] = {(p1[0] - p0[0]) / l , (p1[1] - p0[1]) / l , (p1[2] - p0[2]) / l};
 	printf("%g %g %g %g %g %g\n", v0, v1, p0[0], p0[1], p1[0], p1[1]);
 	ctx->drawVector(1, 0,
@@ -90,7 +89,7 @@ static void drawArrays(drawContext *ctx, PView *p, VertexArray *va, GLint type,
     glEnableClientState(GL_VERTEX_ARRAY);
     if(useNormalArray){
       glEnable(GL_LIGHTING);
-      glNormalPointer(GL_BYTE, 0, va->getNormalArray());
+      glNormalPointer(GL_FLOAT, 0, va->getNormalArray());
       glEnableClientState(GL_NORMAL_ARRAY);
     }
     else
-- 
GitLab