Add a color attribute to meshes and surfaces

This commit is contained in:
Théophile Bastian 2018-02-14 01:37:53 +01:00 committed by Rémi Oudin
parent 818aaf01a9
commit 3e3ec57bb5
5 changed files with 36 additions and 2 deletions

View file

@ -8,11 +8,16 @@ class ImplicitSurface {
double operator()(const Point& pt) const;
Point getCenter() const { return center;}
const Color& get_color() const { return color; }
void set_color(const Color& _color) { color = _color; }
virtual Point location_hint() const = 0;
/// Compute the normal vector to the isosurface at `pt`
Point normal_at(const Point& pt) const;
protected:
Point center;
Color color;
ImplicitSurface(Point _center) : center(_center) {}
};

View file

@ -24,6 +24,8 @@ MarchingCubes& MarchingCubes::add_hint(const Point& hint) {
Mesh MarchingCubes::operator()() {
Mesh output;
output.set_color(surface.get_color());
perf_counter = 0;
if(hints.empty())
@ -31,7 +33,9 @@ Mesh MarchingCubes::operator()() {
else
with_hints(output);
printf("Explored cubes: %ld\n", perf_counter);
#ifdef MC_SHOW_PERF
fprintf(stderr, "Explored cubes: %ld\n", perf_counter);
#endif // MC_SHOW_PERF
output.translate(surface.getCenter());

View file

@ -41,6 +41,9 @@ class Mesh {
/// Gets the various faces
const std::vector<Face>& get_faces() const;
const Color& get_color() const { return color; }
void set_color(const Color& _color) { color = _color; }
private: //struct
struct NormalVect {
NormalVect(const Point& vect, bool manual=false)
@ -63,4 +66,5 @@ class Mesh {
std::vector<std::vector<size_t> > faces_with_vert;
Point center;
Color color;
};

View file

@ -152,6 +152,20 @@ class Cuboid {
Point lowBound, highBound;
};
struct Color {
Color() : r(0), g(0), b(0) {}
Color(double r, double g, double b) : r(r), g(g), b(b) {}
double r, g, b;
double operator[](int i) const {
switch(i % 3) {
case 0: return r;
case 1: return g;
case 2: return b;
}
return 0; // won't happen
}
};
namespace std {
template<> struct hash<Face>
{

View file

@ -149,8 +149,15 @@ void GlutRender::display_mesh(Mesh& mesh) const {
#endif // DEBUG_DISPLAY_WIREFRAME
const Color& color = mesh.get_color();
GLfloat material_specular[] = {
(float)color[0],
(float)color[1],
(float)color[2],
1.};
glBegin(GL_TRIANGLES);
glColor3f(1., 1., 1.);
glColor3f(color[0], color[1], color[2]);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
for(const Face& face: mesh.get_faces()) {
Point p0 = face.pt(0, points) + mesh_center,
p1 = face.pt(1, points) + mesh_center,