Polyhedron rending with OpenGL

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

Polyhedron rending with OpenGL

Lettuce
Hello,
firstly i use CGAL::poisson_suface_reconstruction_delaunay to creat a mesh
here is the code

CGAL::poisson_surface_reconstruction_delaunay
                (points.begin(), points.end(),
                CGAL::First_of_pair_property_map<Pwn>(),
                CGAL::Second_of_pair_property_map<Pwn>(),
                P, average_spacing);

then i want to use opengl to rending it and the code is

void gl_draw_facet(Polyhedron::Facet_iterator f){
        Polyhedron::Halfedge_around_facet_circulator he = f->facet_begin();
        do{
        const Point& p = he->vertex()->point();
                glVertex3d(p.x(), p.y(), p.z());
        } while (++he != f->facet_begin());
}

void drawPoint(){
        Polyhedron::Vertex_iterator v;
        Polyhedron::Facet_iterator f;
        Polyhedron::Edge_iterator eit;
        Polyhedron::Halfedge_handle h_handle;
        double x, y, z;

        CGAL::Polygon_mesh_processing::compute_normals(P,
                boost::make_assoc_property_map(vnormals),
                boost::make_assoc_property_map(fnormals));
               
        glBegin(GL_TRIANGLES);
                for (f = P.facets_begin(); f != P.facets_end(); f++){
                        xx = fnormals[f].x() / sqrt((fnormals[f].x()*fnormals[f].x()) +
(fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
                        yy = fnormals[f].y() / sqrt((fnormals[f].x()*fnormals[f].x()) +
(fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
                        zz = fnormals[f].z() / sqrt((fnormals[f].x()*fnormals[f].x()) +
(fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
                        glNormal3f(xx, yy, zz);
                        gl_draw_facet(f);
                }
                glEnd();
}
but the result shows there is something wrong with the normals. here is the
result
<http://cgal-discuss.949826.n4.nabble.com/file/t375948/cat.png>

i dont know what's wrong with my code.

need help,thank you.





--
Sent from: http://cgal-discuss.949826.n4.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: Polyhedron rending with OpenGL

Sebastien Loriot (GeometryFactory)
I don't see any error here.
Try posting a minimal complete example showing the error
so that we can reproduce it. Maybe the error is not where
you think it is and selected parts are not relevant.

Sebastien

On 06/06/2018 10:02 AM, Lettuce wrote:

> Hello,
> firstly i use CGAL::poisson_suface_reconstruction_delaunay to creat a mesh
> here is the code
>
> CGAL::poisson_surface_reconstruction_delaunay
> (points.begin(), points.end(),
> CGAL::First_of_pair_property_map<Pwn>(),
> CGAL::Second_of_pair_property_map<Pwn>(),
> P, average_spacing);
>
> then i want to use opengl to rending it and the code is
>
> void gl_draw_facet(Polyhedron::Facet_iterator f){
> Polyhedron::Halfedge_around_facet_circulator he = f->facet_begin();
> do{
>          const Point& p = he->vertex()->point();
> glVertex3d(p.x(), p.y(), p.z());
> } while (++he != f->facet_begin());
> }
>
> void drawPoint(){
> Polyhedron::Vertex_iterator v;
> Polyhedron::Facet_iterator f;
> Polyhedron::Edge_iterator eit;
> Polyhedron::Halfedge_handle h_handle;
> double x, y, z;
>
> CGAL::Polygon_mesh_processing::compute_normals(P,
> boost::make_assoc_property_map(vnormals),
> boost::make_assoc_property_map(fnormals));
>
> glBegin(GL_TRIANGLES);
> for (f = P.facets_begin(); f != P.facets_end(); f++){
> xx = fnormals[f].x() / sqrt((fnormals[f].x()*fnormals[f].x()) +
> (fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
> yy = fnormals[f].y() / sqrt((fnormals[f].x()*fnormals[f].x()) +
> (fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
> zz = fnormals[f].z() / sqrt((fnormals[f].x()*fnormals[f].x()) +
> (fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
> glNormal3f(xx, yy, zz);
> gl_draw_facet(f);
> }
> glEnd();
> }
> but the result shows there is something wrong with the normals. here is the
> result
> <http://cgal-discuss.949826.n4.nabble.com/file/t375948/cat.png>
>
> i dont know what's wrong with my code.
>
> need help,thank you.
>
>
>
>
>
> --
> Sent from: http://cgal-discuss.949826.n4.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: Polyhedron rending with OpenGL

Lettuce
thanks for you advice and here is my complete code:

#define CGAL_EIGEN3_ENABLED
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Polygon_mesh_processing/compute_normal.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/poisson_surface_reconstruction.h>
#include <CGAL/IO/read_xyz_points.h>

#include<gl/freeglut.h>
#include<gl/glut.h>
#include<GL/GL.H>
#include<gl/GLU.H>


#include <vector>
#include <fstream>

float xx;
float yy;
float zz;

typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_3 Point;
typedef Kernel::Vector_3 Vector;
typedef std::pair<Point, Vector> Pwn;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;

typedef boost::graph_traits<Polyhedron>::vertex_descriptor
vertex_descriptor;
typedef boost::graph_traits<Polyhedron>::face_descriptor   face_descriptor;

const float g_ambDiff[4] = { 1.0f, 0.5f, 0.0f, 1.0f };//value of lighting
const float g_lightPos[] = { 10.0f, 10.0f, 10.0f, 0.0f };
const float g_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const float g_shininess[] = { 50.0f };

Kernel::Vector_3 n;
Polyhedron P;
std::map<face_descriptor, Vector> fnormals;
std::map<vertex_descriptor, Vector> vnormals;

Polyhedron::Vertex_iterator v;

void gl_draw_facet(Polyhedron::Facet_iterator f){
        Polyhedron::Halfedge_around_facet_circulator he = f->facet_begin();
        do{
        const Point& p = he->vertex()->point();
                glVertex3d(p.x(), p.y(), p.z());
        } while (++he != f->facet_begin());
}

void getPoint()
{
        std::vector<Pwn> points;
        std::ifstream stream("data/kitten.xyz");
        if (!stream ||
                !CGAL::read_xyz_points(
                stream,
                std::back_inserter(points),
                CGAL::parameters::point_map(CGAL::First_of_pair_property_map<Pwn>()).
                normal_map(CGAL::Second_of_pair_property_map<Pwn>())))
        {
                std::cerr << "Error: cannot read file data/kitten.xyz" << std::endl;
        }


        double average_spacing =
CGAL::compute_average_spacing<CGAL::Sequential_tag>
                (points, 6,
CGAL::parameters::point_map(CGAL::First_of_pair_property_map<Pwn>()));

        CGAL::poisson_surface_reconstruction_delaunay
                (points.begin(), points.end(),
                CGAL::First_of_pair_property_map<Pwn>(),
                CGAL::Second_of_pair_property_map<Pwn>(),
                P, average_spacing);
}

void drawPoint(){
        Polyhedron::Vertex_iterator v;
        Polyhedron::Facet_iterator f;
        Polyhedron::Edge_iterator eit;
        Polyhedron::Halfedge_handle h_handle;
        double x, y, z;


        CGAL::Polygon_mesh_processing::compute_normals(P,
                boost::make_assoc_property_map(vnormals),
                boost::make_assoc_property_map(fnormals));
       
        glBegin(GL_TRIANGLES);
                for (f = P.facets_begin(); f != P.facets_end(); f++){
                        xx = fnormals[f].x() / sqrt((fnormals[f].x()*fnormals[f].x()) +
(fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
                        yy = fnormals[f].y() / sqrt((fnormals[f].x()*fnormals[f].x()) +
(fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
                        zz = fnormals[f].z() / sqrt((fnormals[f].x()*fnormals[f].x()) +
(fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
                        glNormal3f(xx, yy, zz);
                        gl_draw_facet(f);
                }
                glEnd();
       
}
void init(){
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glShadeModel(GL_FLAT);
}
void display(){
        glClear(GL_COLOR_BUFFER_BIT);
       
        glLoadIdentity();
       
        drawPoint();  
        glFlush();
        glutSwapBuffers();
}

int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB|GLUT_DEPTH);
        glutInitWindowSize(800, 600);
        glutInitWindowPosition(100, 100);
        glutCreateWindow("points");
        init();
        getPoint();
        glutDisplayFunc(display);
        glutKeyboardFunc(key);

        glClearColor(0.2, 0.55, 1.0, 1);

        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHTING);
        glLightfv(GL_LIGHT0, GL_POSITION, g_lightPos);

        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, g_ambDiff);
        glMaterialfv(GL_FRONT, GL_SPECULAR, g_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, g_shininess);

        glutMainLoop();
        return 0;
}
the data "kitten.xyz" comes from the cgal examples
"Poisson_surface_reconstruction_3" or you can see it from here
https://doc.cgal.org/latest/Poisson_surface_reconstruction_3/Poisson_surface_reconstruction_3_2poisson_reconstruction_function_8cpp-example.html



--
Sent from: http://cgal-discuss.949826.n4.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: Polyhedron rending with OpenGL

Lettuce
In reply to this post by Lettuce
sorry it's my first time to reply and i'm not sure is this the right way to reply to you

thanks for you advice and here is my complete code:

#define CGAL_EIGEN3_ENABLED
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Polygon_mesh_processing/compute_normal.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/poisson_surface_reconstruction.h>
#include <CGAL/IO/read_xyz_points.h>

#include<gl/freeglut.h>
#include<gl/glut.h>
#include<GL/GL.H>
#include<gl/GLU.H>


#include <vector>
#include <fstream>

float xx;
float yy;
float zz;

typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_3 Point;
typedef Kernel::Vector_3 Vector;
typedef std::pair<Point, Vector> Pwn;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;

typedef boost::graph_traits<Polyhedron>::vertex_descriptor
vertex_descriptor;
typedef boost::graph_traits<Polyhedron>::face_descriptor   face_descriptor;

const float g_ambDiff[4] = { 1.0f, 0.5f, 0.0f, 1.0f };//value of lighting
const float g_lightPos[] = { 10.0f, 10.0f, 10.0f, 0.0f };
const float g_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const float g_shininess[] = { 50.0f };

Kernel::Vector_3 n;
Polyhedron P;
std::map<face_descriptor, Vector> fnormals;
std::map<vertex_descriptor, Vector> vnormals;

Polyhedron::Vertex_iterator v;

void gl_draw_facet(Polyhedron::Facet_iterator f){
        Polyhedron::Halfedge_around_facet_circulator he = f->facet_begin();
        do{
        const Point& p = he->vertex()->point();
                glVertex3d(p.x(), p.y(), p.z());
        } while (++he != f->facet_begin());
}

void getPoint()
{
        std::vector<Pwn> points;
        std::ifstream stream("data/kitten.xyz");
        if (!stream ||
                !CGAL::read_xyz_points(
                stream,
                std::back_inserter(points),
                CGAL::parameters::point_map(CGAL::First_of_pair_property_map<Pwn>()).
                normal_map(CGAL::Second_of_pair_property_map<Pwn>())))
        {
                std::cerr << "Error: cannot read file data/kitten.xyz" << std::endl;
        }


        double average_spacing =
CGAL::compute_average_spacing<CGAL::Sequential_tag>
                (points, 6,
CGAL::parameters::point_map(CGAL::First_of_pair_property_map<Pwn>()));

        CGAL::poisson_surface_reconstruction_delaunay
                (points.begin(), points.end(),
                CGAL::First_of_pair_property_map<Pwn>(),
                CGAL::Second_of_pair_property_map<Pwn>(),
                P, average_spacing);
}

void drawPoint(){
        Polyhedron::Vertex_iterator v;
        Polyhedron::Facet_iterator f;
        Polyhedron::Edge_iterator eit;
        Polyhedron::Halfedge_handle h_handle;
        double x, y, z;


        CGAL::Polygon_mesh_processing::compute_normals(P,
                boost::make_assoc_property_map(vnormals),
                boost::make_assoc_property_map(fnormals));
       
        glBegin(GL_TRIANGLES);
                for (f = P.facets_begin(); f != P.facets_end(); f++){
                        xx = fnormals[f].x() / sqrt((fnormals[f].x()*fnormals[f].x()) +
(fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
                        yy = fnormals[f].y() / sqrt((fnormals[f].x()*fnormals[f].x()) +
(fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
                        zz = fnormals[f].z() / sqrt((fnormals[f].x()*fnormals[f].x()) +
(fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
                        glNormal3f(xx, yy, zz);
                        gl_draw_facet(f);
                }
                glEnd();
       
}
void init(){
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glShadeModel(GL_FLAT);
}
void display(){
        glClear(GL_COLOR_BUFFER_BIT);
       
        glLoadIdentity();
       
        drawPoint();  
        glFlush();
        glutSwapBuffers();
}

int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB|GLUT_DEPTH);
        glutInitWindowSize(800, 600);
        glutInitWindowPosition(100, 100);
        glutCreateWindow("points");
        init();
        getPoint();
        glutDisplayFunc(display);
        glutKeyboardFunc(key);

        glClearColor(0.2, 0.55, 1.0, 1);

        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHTING);
        glLightfv(GL_LIGHT0, GL_POSITION, g_lightPos);

        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, g_ambDiff);
        glMaterialfv(GL_FRONT, GL_SPECULAR, g_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, g_shininess);

        glutMainLoop();
        return 0;
}
the data "kitten.xyz" comes from the cgal examples
"Poisson_surface_reconstruction_3" or you can see it from here
https://doc.cgal.org/latest/Poisson_surface_reconstruction_3/Poisson_surface_reconstruction_3_2poisson_reconstruction_function_8cpp-example.html


<quote author='Sebastien Loriot (GeometryFactory)'>
I don't see any error here.
Try posting a minimal complete example showing the error
so that we can reproduce it. Maybe the error is not where
you think it is and selected parts are not relevant.

Sebastien

On 06/06/2018 10:02 AM, Lettuce wrote:

> Hello,
> firstly i use CGAL::poisson_suface_reconstruction_delaunay to creat a mesh
> here is the code
>
> CGAL::poisson_surface_reconstruction_delaunay
> (points.begin(), points.end(),
> CGAL::First_of_pair_property_map<Pwn>(),
> CGAL::Second_of_pair_property_map<Pwn>(),
> P, average_spacing);
>
> then i want to use opengl to rending it and the code is
>
> void gl_draw_facet(Polyhedron::Facet_iterator f){
> Polyhedron::Halfedge_around_facet_circulator he = f->facet_begin();
> do{
>          const Point& p = he->vertex()->point();
> glVertex3d(p.x(), p.y(), p.z());
> } while (++he != f->facet_begin());
> }
>
> void drawPoint(){
> Polyhedron::Vertex_iterator v;
> Polyhedron::Facet_iterator f;
> Polyhedron::Edge_iterator eit;
> Polyhedron::Halfedge_handle h_handle;
> double x, y, z;
>
> CGAL::Polygon_mesh_processing::compute_normals(P,
> boost::make_assoc_property_map(vnormals),
> boost::make_assoc_property_map(fnormals));
>
> glBegin(GL_TRIANGLES);
> for (f = P.facets_begin(); f != P.facets_end(); f++){
> xx = fnormals[f].x() / sqrt((fnormals[f].x()*fnormals[f].x()) +
> (fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
> yy = fnormals[f].y() / sqrt((fnormals[f].x()*fnormals[f].x()) +
> (fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
> zz = fnormals[f].z() / sqrt((fnormals[f].x()*fnormals[f].x()) +
> (fnormals[f].y()*fnormals[f].y()) + (fnormals[f].z()*fnormals[f].z()));
> glNormal3f(xx, yy, zz);
> gl_draw_facet(f);
> }
> glEnd();
> }
> but the result shows there is something wrong with the normals. here is
> the
> result
> <http://cgal-discuss.949826.n4.nabble.com/file/t375948/cat.png>
>
> i dont know what's wrong with my code.
>
> need help,thank you.
>
>
>
>
>
> --
> Sent from: http://cgal-discuss.949826.n4.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



</quote>
Quoted from:
http://cgal-discuss.949826.n4.nabble.com/Polyhedron-rending-with-OpenGL-tp4663546p4663547.html


_____________________________________
Sent from http://cgal-discuss.949826.n4.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