CGAL+OpenGL, incorrectly render mesh triangles

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

CGAL+OpenGL, incorrectly render mesh triangles

mikef
I am using CGAL  "make_surface_mesh" to create a mesh out of an ellipsoid function, then using OpenGL to draw it by plotting triangle by triangle. I have no clue what is going on...

Problem: the plotted surface is weird, some triangle has color, some not, some are like holes.

My codes for mesh generation: 
        Surface_3 surface(earth_function,             // pointer to function
                Sphere_3(CGAL::ORIGIN, 7000*7000));
        // bounding sphere
        // Note that "2." above is the *squared* radius of the bounding sphere!
        // defining meshing criteria
        CGAL::Surface_mesh_default_criteria_3 criteria(30.,  // angular bound
                500,  // radius bound
                50); // distance bound
                         // meshing surface
        CGAL::make_surface_mesh(c2t3, surface, criteria , CGAL::Manifold_tag());

My codes for mesh plotting:
        glNewList(EARTH_S, GL_COMPILE);
       
        Point_3 pt0, pt1, pt2, pt3;
        for (auto it = c2t3.facets_begin(); it != c2t3.facets_end(); it++) {
               
                pt0 = it->first->vertex((it->second) & 3)->point();
                pt1 = it->first->vertex((it->second + 1) & 3)->point();
                pt2 = it->first->vertex((it->second + 2) & 3)->point();
                pt3 = it->first->vertex((it->second + 3) & 3)->point();

                auto vv = pt1 - pt0;
                auto n = CGAL::normal(pt1, pt2, pt3);
                if (CGAL::angle(n, vv) == CGAL::ACUTE) { n = -n; }
                //auto n = ((it->second % 2 ) == 1)?CGAL::normal(pt1, pt2, pt3): CGAL::normal(pt1, pt3, pt2);

                glBegin(GL_TRIANGLES);
                glColor3f(0.4, 0.5, 0.3);
                glNormal3f(n.x(), n.y(), n.z());
                glVertex3f(pt1.x(), pt1.y(), pt1.z());
                glVertex3f(pt2.x(), pt2.y(), pt2.z());
                glVertex3f(pt3.x(), pt3.y(), pt3.z());
                glEnd();
        }
               
        glEndList();

Resulting plot:
Reply | Threaded
Open this post in threaded view
|

Re: CGAL+OpenGL, incorrectly render mesh triangles

chgans
On 14 April 2017 at 15:05, mikef <[hidden email]> wrote:
> I am using CGAL  "make_surface_mesh" to create a mesh out of an ellipsoid
> function, then using OpenGL to draw it by plotting triangle by triangle. I
> have no clue what is going on...

Sounds like you have a problem with your normals and or your winding direction.
What is likely happening is that your holes (grey) have the wrong
facing so they are not rendered, and the white have the wrong normals,
so their lighting is wrong.

OpenGL has x going right, y going up and z going towards you, CGAL has
x going right, y going toward the screen and z going up. With OpenGL
defaults, a front facing triangle is CCW and is rendered, a back
facing triangle is CW and is not rendered.

My 2 cents.
Chris

>
> *Problem:* the plotted surface is weird, some triangle has color, some not,
> some are like holes.
>
> *My codes for mesh generation:*
>         Surface_3 surface(earth_function,             // pointer to function
>                 Sphere_3(CGAL::ORIGIN, 7000*7000));
>         // bounding sphere
>         // Note that "2." above is the *squared* radius of the bounding
> sphere!
>         // defining meshing criteria
>         CGAL::Surface_mesh_default_criteria_3 criteria(30.,  // angular
> bound
>                 500,  // radius bound
>                 50); // distance bound
>                          // meshing surface
>         CGAL::make_surface_mesh(c2t3, surface, criteria ,
> CGAL::Manifold_tag());
>
> *My codes for mesh plotting: *
>         glNewList(EARTH_S, GL_COMPILE);
>
>         Point_3 pt0, pt1, pt2, pt3;
>         for (auto it = c2t3.facets_begin(); it != c2t3.facets_end(); it++) {
>
>                 pt0 = it->first->vertex((it->second) & 3)->point();
>                 pt1 = it->first->vertex((it->second + 1) & 3)->point();
>                 pt2 = it->first->vertex((it->second + 2) & 3)->point();
>                 pt3 = it->first->vertex((it->second + 3) & 3)->point();
>
>                 auto vv = pt1 - pt0;
>                 auto n = CGAL::normal(pt1, pt2, pt3);
>                 if (CGAL::angle(n, vv) == CGAL::ACUTE) { n = -n; }
>                 //auto n = ((it->second % 2 ) == 1)?CGAL::normal(pt1, pt2,
> pt3): CGAL::normal(pt1, pt3, pt2);
>
>                 glBegin(GL_TRIANGLES);
>                 glColor3f(0.4, 0.5, 0.3);
>                 glNormal3f(n.x(), n.y(), n.z());
>                 glVertex3f(pt1.x(), pt1.y(), pt1.z());
>                 glVertex3f(pt2.x(), pt2.y(), pt2.z());
>                 glVertex3f(pt3.x(), pt3.y(), pt3.z());
>                 glEnd();
>         }
>
>         glEndList();
>
> *Resulting plot: *
> <http://cgal-discuss.949826.n4.nabble.com/file/n4662670/Capture.jpg>
>
>
>
> --
> View this message in context: http://cgal-discuss.949826.n4.nabble.com/CGAL-OpenGL-incorrectly-render-mesh-triangles-tp4662670.html
> Sent from the cgal-discuss mailing list archive at Nabble.com.
>
> --
> You are currently subscribed to cgal-discuss.
> To unsubscribe or access the archives, go to
> https://sympa.inria.fr/sympa/info/cgal-discuss
>
>

--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss


Reply | Threaded
Open this post in threaded view
|

Re: CGAL+OpenGL, incorrectly render mesh triangles

andreas.fabri
The triangles are not oriented. Have a look at the function
http://doc.cgal.org/latest/Surface_mesher/group__PkgSurfaceMesher3FunctionsIO.html#gab2fbf4e1a4cf30fa19304138461623e7
Reply | Threaded
Open this post in threaded view
|

Re: CGAL+OpenGL, incorrectly render mesh triangles

mikef
Are you suggesting transform c2t3 into polyhedron? why so?



--
View this message in context: http://cgal-discuss.949826.n4.nabble.com/CGAL-OpenGL-incorrectly-render-mesh-triangles-tp4662670p4662674.html
Sent from the cgal-discuss mailing list archive at Nabble.com.

--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss


Reply | Threaded
Open this post in threaded view
|

Re: CGAL+OpenGL, incorrectly render mesh triangles

Laurent Rineau (CGAL/GeometryFactory)
In reply to this post by mikef
Le Friday, April 14, 2017 5:05:36 AM CEST mikef a ?crit :

> I am using CGAL  "make_surface_mesh" to create a mesh out of an ellipsoid
> function, then using OpenGL to draw it by plotting triangle by triangle. I
> have no clue what is going on...
>
> *Problem:* the plotted surface is weird, some triangle has color, some not,
> some are like holes.
>
> *My codes for mesh generation:*
>         Surface_3 surface(earth_function,             // pointer to
> function Sphere_3(CGAL::ORIGIN, 7000*7000));
>         // bounding sphere
>         // Note that "2." above is the *squared* radius of the bounding
> sphere!
>         // defining meshing criteria
>         CGAL::Surface_mesh_default_criteria_3 criteria(30.,  // angular
> bound
>                 500,  // radius bound
>                 50); // distance bound
>                          // meshing surface
>         CGAL::make_surface_mesh(c2t3, surface, criteria ,
> CGAL::Manifold_tag());
>
> *My codes for mesh plotting: *
>         glNewList(EARTH_S, GL_COMPILE);
>
>         Point_3 pt0, pt1, pt2, pt3;
>         for (auto it = c2t3.facets_begin(); it != c2t3.facets_end(); it++)
> {
>
>                 pt0 = it->first->vertex((it->second) & 3)->point();
>                 pt1 = it->first->vertex((it->second + 1) & 3)->point();
>                 pt2 = it->first->vertex((it->second + 2) & 3)->point();
>                 pt3 = it->first->vertex((it->second + 3) & 3)->point();
>
>                 auto vv = pt1 - pt0;
>                 auto n = CGAL::normal(pt1, pt2, pt3);
>                 if (CGAL::angle(n, vv) == CGAL::ACUTE) { n = -n; }
>                 //auto n = ((it->second % 2 ) == 1)?CGAL::normal(pt1, pt2,
> pt3): CGAL::normal(pt1, pt3, pt2);

First: to get the oriented list of three points of a facets, you should use
the function tr.vertex_triple_index(i, j), a function in a base class of the
triangulation.

http://doc.cgal.org/latest/TDS_3/
classCGAL_1_1Triangulation__utils__3.html#a71288c8c2833f954a73090336ed60054

Second: the facets of a c2t3 are actually not oriented, and that means that
their orientation is random. That probably explains your rendering issues.
You should use the OpenGL lightening model "two sides", and disable the back-
culling.

--
Laurent Rineau, PhD
R&D Engineer at GeometryFactory           http://www.geometryfactory.com/
Release Manager of the CGAL Project       http://www.cgal.org/


--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss


Reply | Threaded
Open this post in threaded view
|

Re: CGAL+OpenGL, incorrectly render mesh triangles

mikef
Laurent Rineau (CGAL/GeometryFactory) wrote
Le Friday, April 14, 2017 5:05:36 AM CEST mikef a ?crit :
> I am using CGAL  "make_surface_mesh" to create a mesh out of an ellipsoid
> function, then using OpenGL to draw it by plotting triangle by triangle. I
> have no clue what is going on...
>
> *Problem:* the plotted surface is weird, some triangle has color, some not,
> some are like holes.
>
> *My codes for mesh generation:*
>         Surface_3 surface(earth_function,             // pointer to
> function Sphere_3(CGAL::ORIGIN, 7000*7000));
>         // bounding sphere
>         // Note that "2." above is the *squared* radius of the bounding
> sphere!
>         // defining meshing criteria
>         CGAL::Surface_mesh_default_criteria_3 criteria(30.,  // angular
> bound
>                 500,  // radius bound
>                 50); // distance bound
>                          // meshing surface
>         CGAL::make_surface_mesh(c2t3, surface, criteria ,
> CGAL::Manifold_tag());
>
> *My codes for mesh plotting: *
>         glNewList(EARTH_S, GL_COMPILE);
>
>         Point_3 pt0, pt1, pt2, pt3;
>         for (auto it = c2t3.facets_begin(); it != c2t3.facets_end(); it++)
> {
>
>                 pt0 = it->first->vertex((it->second) & 3)->point();
>                 pt1 = it->first->vertex((it->second + 1) & 3)->point();
>                 pt2 = it->first->vertex((it->second + 2) & 3)->point();
>                 pt3 = it->first->vertex((it->second + 3) & 3)->point();
>
>                 auto vv = pt1 - pt0;
>                 auto n = CGAL::normal(pt1, pt2, pt3);
>                 if (CGAL::angle(n, vv) == CGAL::ACUTE) { n = -n; }
>                 //auto n = ((it->second % 2 ) == 1)?CGAL::normal(pt1, pt2,
> pt3): CGAL::normal(pt1, pt3, pt2);

First: to get the oriented list of three points of a facets, you should use
the function tr.vertex_triple_index(i, j), a function in a base class of the
triangulation.

http://doc.cgal.org/latest/TDS_3/
classCGAL_1_1Triangulation__utils__3.html#a71288c8c2833f954a73090336ed60054

Second: the facets of a c2t3 are actually not oriented, and that means that
their orientation is random. That probably explains your rendering issues.
You should use the OpenGL lightening model "two sides", and disable the back-
culling.

--
Laurent Rineau, PhD
R&D Engineer at GeometryFactory           http://www.geometryfactory.com/
Release Manager of the CGAL Project       http://www.cgal.org/


--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss
Laurent Rineau (CGAL/GeometryFactory) wrote
Le Friday, April 14, 2017 5:05:36 AM CEST mikef a ?crit :
> I am using CGAL  "make_surface_mesh" to create a mesh out of an ellipsoid
> function, then using OpenGL to draw it by plotting triangle by triangle. I
> have no clue what is going on...
>
> *Problem:* the plotted surface is weird, some triangle has color, some not,
> some are like holes.
>
> *My codes for mesh generation:*
>         Surface_3 surface(earth_function,             // pointer to
> function Sphere_3(CGAL::ORIGIN, 7000*7000));
>         // bounding sphere
>         // Note that "2." above is the *squared* radius of the bounding
> sphere!
>         // defining meshing criteria
>         CGAL::Surface_mesh_default_criteria_3 criteria(30.,  // angular
> bound
>                 500,  // radius bound
>                 50); // distance bound
>                          // meshing surface
>         CGAL::make_surface_mesh(c2t3, surface, criteria ,
> CGAL::Manifold_tag());
>
> *My codes for mesh plotting: *
>         glNewList(EARTH_S, GL_COMPILE);
>
>         Point_3 pt0, pt1, pt2, pt3;
>         for (auto it = c2t3.facets_begin(); it != c2t3.facets_end(); it++)
> {
>
>                 pt0 = it->first->vertex((it->second) & 3)->point();
>                 pt1 = it->first->vertex((it->second + 1) & 3)->point();
>                 pt2 = it->first->vertex((it->second + 2) & 3)->point();
>                 pt3 = it->first->vertex((it->second + 3) & 3)->point();
>
>                 auto vv = pt1 - pt0;
>                 auto n = CGAL::normal(pt1, pt2, pt3);
>                 if (CGAL::angle(n, vv) == CGAL::ACUTE) { n = -n; }
>                 //auto n = ((it->second % 2 ) == 1)?CGAL::normal(pt1, pt2,
> pt3): CGAL::normal(pt1, pt3, pt2);

First: to get the oriented list of three points of a facets, you should use
the function tr.vertex_triple_index(i, j), a function in a base class of the
triangulation.

http://doc.cgal.org/latest/TDS_3/
classCGAL_1_1Triangulation__utils__3.html#a71288c8c2833f954a73090336ed60054

Second: the facets of a c2t3 are actually not oriented, and that means that
their orientation is random. That probably explains your rendering issues.
You should use the OpenGL lightening model "two sides", and disable the back-
culling.

--
Laurent Rineau, PhD
R&D Engineer at GeometryFactory           http://www.geometryfactory.com/
Release Manager of the CGAL Project       http://www.cgal.org/


--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss
Laurent Rineau (CGAL/GeometryFactory) wrote
Le Friday, April 14, 2017 5:05:36 AM CEST mikef a ?crit :
> I am using CGAL  "make_surface_mesh" to create a mesh out of an ellipsoid
> function, then using OpenGL to draw it by plotting triangle by triangle. I
> have no clue what is going on...
>
> *Problem:* the plotted surface is weird, some triangle has color, some not,
> some are like holes.
>
> *My codes for mesh generation:*
>         Surface_3 surface(earth_function,             // pointer to
> function Sphere_3(CGAL::ORIGIN, 7000*7000));
>         // bounding sphere
>         // Note that "2." above is the *squared* radius of the bounding
> sphere!
>         // defining meshing criteria
>         CGAL::Surface_mesh_default_criteria_3 criteria(30.,  // angular
> bound
>                 500,  // radius bound
>                 50); // distance bound
>                          // meshing surface
>         CGAL::make_surface_mesh(c2t3, surface, criteria ,
> CGAL::Manifold_tag());
>
> *My codes for mesh plotting: *
>         glNewList(EARTH_S, GL_COMPILE);
>
>         Point_3 pt0, pt1, pt2, pt3;
>         for (auto it = c2t3.facets_begin(); it != c2t3.facets_end(); it++)
> {
>
>                 pt0 = it->first->vertex((it->second) & 3)->point();
>                 pt1 = it->first->vertex((it->second + 1) & 3)->point();
>                 pt2 = it->first->vertex((it->second + 2) & 3)->point();
>                 pt3 = it->first->vertex((it->second + 3) & 3)->point();
>
>                 auto vv = pt1 - pt0;
>                 auto n = CGAL::normal(pt1, pt2, pt3);
>                 if (CGAL::angle(n, vv) == CGAL::ACUTE) { n = -n; }
>                 //auto n = ((it->second % 2 ) == 1)?CGAL::normal(pt1, pt2,
> pt3): CGAL::normal(pt1, pt3, pt2);

First: to get the oriented list of three points of a facets, you should use
the function tr.vertex_triple_index(i, j), a function in a base class of the
triangulation.

http://doc.cgal.org/latest/TDS_3/
classCGAL_1_1Triangulation__utils__3.html#a71288c8c2833f954a73090336ed60054

Second: the facets of a c2t3 are actually not oriented, and that means that
their orientation is random. That probably explains your rendering issues.
You should use the OpenGL lightening model "two sides", and disable the back-
culling.

--
Laurent Rineau, PhD
R&D Engineer at GeometryFactory           http://www.geometryfactory.com/
Release Manager of the CGAL Project       http://www.cgal.org/


--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss
Thanks for reply.

I tried using vertex_triple_index in this way. It doesn't help.

                pt0 = ch->vertex(i&3)->point();
                pt1 = ch->vertex(tr.vertex_triple_index(i, 0))->point();
                pt2 = ch->vertex(tr.vertex_triple_index(i, 1))->point();
                pt3 = ch->vertex(tr.vertex_triple_index(i, 2))->point();

I added glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE), still no luck.

I saw your post somewhere else about the coherent normal of faces, so I added the following codes in hope of fixing the 2nd issue in your reply. But it doesn't do any good.
                auto vv = pt1 - pt0;
                auto n = CGAL::normal(pt1, pt2, pt3);
                if (CGAL::angle(n, vv) == CGAL::ACUTE) { n = -n; }
Reply | Threaded
Open this post in threaded view
|

Re: CGAL+OpenGL, incorrectly render mesh triangles

mikef
I found one bug: didn't normalize the normal vector before setting glNormal3f().

Now the white triangles are gone, but the holes are still there. It seems the normals are still the cause:
Any suggestions? Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: CGAL+OpenGL, incorrectly render mesh triangles

mikef
I solved the problem.

Solution 1: Instead of calucalting the face normal, I use one vertex's coordinates in glNormal3f, because the ellipsoid is centered in the origin. However, I still don't know why the original method for calculating the face normal is wrong.

Solution 2: Some holes were rendered previously because I set the radius bound and distance bound in the mesh criteria too large. Well, my solution is to reduce the scale of the ellipsoid by one hundredth.  

Now it is better:
Reply | Threaded
Open this post in threaded view
|

Re: CGAL+OpenGL, incorrectly render mesh triangles

Julia Fischer


On 14 April 2017 at 23:45, mikef <[hidden email]> wrote:
I solved the problem.

*Solution 1: *Instead of calucalting the face normal, I use one vertex's
coordinates in glNormal3f, because the ellipsoid is centered in the origin.
However, I still don't know why the original method for calculating the face
normal is wrong.

*Solution 2:* Some holes were rendered previously because I set the radius
bound and distance bound in the mesh criteria too large. Well, my solution
is to reduce the scale of the ellipsoid by one hundredth.

Now it is better:
<http://cgal-discuss.949826.n4.nabble.com/file/n4662682/Capture.jpg>



--
View this message in context: http://cgal-discuss.949826.n4.nabble.com/CGAL-OpenGL-incorrectly-render-mesh-triangles-tp4662670p4662682.html
Sent from the cgal-discuss mailing list archive at Nabble.com.

--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss




signature.txt (209K) Download Attachment