summaryrefslogtreecommitdiff
path: root/graphics/asymptote/material.h
blob: f16f421e52baf8248eccc34bc6b8a30f3c3a7638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef MATERIAL_H
#define MATERIAL_H

#ifdef HAVE_LIBGLM

#include <iostream>
#include <fstream>

#include "common.h"
#include "triple.h"

#include <glm/glm.hpp>

namespace camp {

inline bool operator < (const glm::vec4& m1, const glm::vec4& m2) {
  return m1[0] < m2[0] ||
                 (m1[0] == m2[0] &&
                  (m1[1] < m2[1] ||
                   (m1[1] == m2[1] &&
                    (m1[2] < m2[2] ||
                     (m1[2] == m2[2] &&
                      (m1[3] < m2[3]))))));
}

inline glm::vec4 GLparameters(GLfloat shininess, GLfloat metallic,
                              GLfloat fresnel0) {
  return glm::vec4(shininess,metallic,fresnel0,0.0);
}

inline ostream& operator << (ostream& out, const glm::vec4& v)
{
  out << "[" << v[0] << "," << v[1] << "," << v[2] << "," << v[3]
      << "]";
  return out;
}

struct Material {
public:
  glm::vec4 diffuse,emissive,specular;
  glm::vec4 parameters;
  Material() {}

  Material(const glm::vec4& diffuse, const glm::vec4& emissive,
           const glm::vec4& specular, double shininess, double metallic, double fresnel0) :
    diffuse(diffuse), emissive(emissive), specular(specular),
    parameters(GLparameters(shininess,metallic,fresnel0)) {}

  Material(Material const& m):
    diffuse(m.diffuse), emissive(m.emissive),
    specular(m.specular), parameters(m.parameters) {}
  ~Material() {}

  Material& operator=(Material const& m)
  {
    diffuse=m.diffuse;
    emissive=m.emissive;
    specular=m.specular;
    parameters=m.parameters;
    return *this;
  }

  friend bool operator < (const Material& m1, const Material& m2) {
    return m1.diffuse < m2.diffuse ||
                        (m1.diffuse == m2.diffuse &&
                         (m1.emissive < m2.emissive ||
                          (m1.emissive == m2.emissive &&
                           (m1.specular < m2.specular ||
                            (m1.specular == m2.specular &&
                             (m1.parameters < m2.parameters))))));
  }

  friend ostream& operator << (ostream& out, const Material& m) {
    out << "diffuse=" << m.diffuse << "," << newl
        << "emissive=" << m.emissive << "," << newl
        << "specular=" << m.specular << "," << newl
        << "shininess=" << m.parameters[0] << "," << newl
        << "metallic=" << m.parameters[1] << "," << newl
        << "fresnel0=" << m.parameters[2] << newl;
    return out;
  }

};

extern size_t Nmaterials; // Number of materials compiled in shader
extern size_t nmaterials; // Current size of materials buffer
extern size_t Maxmaterials; // Maxinum size of materials buffer
}

#endif
#endif