AGL  0
3D graphics library
entity.h
Go to the documentation of this file.
1 #ifndef ENTITY_H
2 #define ENTITY_H
3 
4 #include<vector>
5 #include<GL/gl.h>
6 #include<GLES3/gl32.h>
7 #include "glm/glm.hpp"
8 #include "util.h"
9 
10 namespace agl {
11 class Entity;
12 class Light;
13 class Scene;
14 
51 class Material
52 {
53 public:
54  glm::vec4 emission,
55  ambient,
56  diffuse,
57  specular,
58  ratios; // (color, texture, reflection, refraction) [0, 1] -1: disabled
59  float shininess = 32;
60  bool lightsEnabled = false,
61  customShader = false;
63  tex_width = -1,
64  tex_height = -1,
65  tex_channel = -1;
66  GLubyte *texture = nullptr;
67  GLuint progID = 0,
68  tID = 0,
69  mvpID,
70  mID,
71  nID,
72  eID,
73  aID,
74  dID,
75  sID,
76  gID,
77  vID;
78 
82  Material();
83  ~Material();
84 
92  Material operator+(const Material &other) const;
93  Material &operator+=(const Material &other);
94 
101  Material operator*(const Material &other) const;
102  Material &operator*=(const Material &other);
103 
107  void setColor(glm::vec4 color=glm::vec4(1));
115  void setColor(float r=1, float g=1, float b=1, float a=1);
120  void setColor(const Material &other);
131  void createTexture(const char *path=nullptr);
132 // void setTexture(const Material &other); // TODO: Why destructor does not work? I know why, need to deal with that.\
139  virtual std::pair<std::string, std::string> createShader(Entity *e=nullptr, std::vector<Light*> lights=std::vector<Light*>());
148  virtual void setShader(std::string vertexShader, std::string fragmentShader);
149 
159 private:
160  Material(float ar, float ag, float ab, float dr, float dg, float db, float sr, float sg, float sb, float sn);
161 };
162 
167 {
168 public:
169  Entity *parent = nullptr;
170  virtual ~BaseEntity();
171 };
172 
188 class Entity: virtual public BaseEntity
189 {
190 public:
191  GLuint VAO = 0,
192  VBO = 0,
193  EBO = 0;
194 // polyMode = GL_FILL;
195  bool dynamic = false;
196  std::vector<GLfloat> vertices,
197  normals,
198  uvs,
199  merged;
200  std::vector<GLuint> indices;
201  glm::vec3 position;
202  glm::mat4 model;
204  std::vector<BaseEntity*> children;
205 
210  Entity(const glm::vec3 &pos=glm::vec3(0));
215  Entity(const Entity &other);
216  ~Entity();
217 
222  void translate(const glm::vec3 &d=glm::vec3(0));
229  void translate(float dx=0, float dy=0, float dz=0);
235  void rotate(float rad=0, glm::vec3 axis=glm::vec3(0));
240  void scale(const glm::vec3 &s=glm::vec3(1));
249  void scale(float sx=1, float sy=std::nanf(""), float sz=std::nanf(""));
254  void transform(const glm::mat4 &m);
261  void applyTransform();
266  void add(BaseEntity &e);
270  virtual void mergeData();
274  virtual void createBuffers();
279  glm::mat4 getMatM();
280 };
281 
297 class Light: virtual public BaseEntity
298 {
299 public:
300  glm::vec4 ambient,
301  diffuse,
302  specular,
303  position;
304  glm::vec3 spotDirection;
305  float spotExponent = 0,
306  spotCosCutoff = -1,
307  constantAttenuation = 1,
308  linearAttenuation = 0,
309  quadraticAttenuation = 0;
310 
320  Light(const glm::vec3 &pos=glm::vec3(-2, 3, 2), const glm::vec4 &color=glm::vec4(1));
329  Light(const glm::vec3 &pos=glm::vec3(-2, 3, 2), float r=1, float g=1, float b=1, float a=1);
334  void setColor(glm::vec4 color=glm::vec4(1));
338  void setColor(float r=1, float g=1, float b=1, float a=1);
343  inline glm::vec4 getPos()
344  {
345  return parent == nullptr ? position : parent->getMatM() * position;
346  }
347 };
356 inline Light DirectionalLight(const glm::vec3 &dir=glm::vec3(0), const glm::vec4 &color=glm::vec4(1))
357 {
358  Light l(dir, color);
359  l.position.w = 0;
360  return l;
361 }
371 inline Light DirectionalLight(const glm::vec3 &dir=glm::vec3(0), float r=1, float g=1, float b=1, float a=1)
372 {
373  Light l(dir, r, g, b, a);
374  l.position.w = 0;
375  return l;
376 }
378 
388 class SharedEntity: virtual public Entity
389 {
390 public:
391  Entity *source = nullptr;
392 
397  SharedEntity(Entity &src);
401  virtual void mergeData();
409  virtual void createBuffers();
410 };
411 }
412 
413 #endif // ENTITY_H
glm::vec3 position
Position of the entity, the entity is centered here.
Definition: entity.h:201
The base for all Entity and Light.
Definition: entity.h:166
glm::vec4 emission
Emission, self color of the material.
Definition: entity.h:54
glm::vec4 specular
Specular, reflects from surface.
Definition: entity.h:300
static const Material copper
Definition: entity.h:155
void createTexture(const char *path=nullptr)
Creates a texture.
Definition: entity.cpp:191
static const Material white_plastic
Definition: entity.h:155
float shininess
Shininess, amount of light reflected by the material.
Definition: entity.h:59
static const Material emerald
Definition: entity.h:155
GLuint dID
diffuse color ID
Definition: entity.h:68
Material & operator*=(const Material &other)
Same as operator* but modifies the current material.
Definition: entity.cpp:167
glm::mat4 model
The model matrix for the entity. This is the M part of the MVP matrix. This is responsible for all th...
Definition: entity.h:202
static const Material green_rubber
Definition: entity.h:155
static const Material cyan_rubber
Definition: entity.h:155
GLuint eID
emission color ID
Definition: entity.h:68
static const Material cyan_plastic
Definition: entity.h:155
GLuint nID
normal matrix ID
Definition: entity.h:68
Material operator+(const Material &other) const
Mix two material.
Definition: entity.cpp:138
The Entity; basically all visible objects in AGL.
Definition: entity.h:188
glm::vec4 ambient
Ambient, color due to ambient light.
Definition: entity.h:54
static const Material gold
Definition: entity.h:155
glm::vec4 ratios
Definition: entity.h:54
GLuint mvpID
MVP matrix ID.
Definition: entity.h:68
Definition: create_shader.cpp:4
static const Material white_rubber
Definition: entity.h:155
Material operator*(const Material &other) const
Mix two material.
Definition: entity.cpp:157
static const Material chrome
Definition: entity.h:155
GLuint gID
shininess color ID
Definition: entity.h:68
A SharedEntity shares its data off another Entity.
Definition: entity.h:388
void setColor(glm::vec4 color=glm::vec4(1))
Set color for all the components.
Definition: entity.cpp:176
GLuint vID
camera position ID
Definition: entity.h:68
std::vector< GLfloat > vertices
Vertices.
Definition: entity.h:196
int tex_channel
Number of channels in texture, if used.
Definition: entity.h:65
static const Material yellow_rubber
Definition: entity.h:155
static const Material red_plastic
Definition: entity.h:155
virtual void setShader(std::string vertexShader, std::string fragmentShader)
Compile and set the shader for the material.
Definition: entity.cpp:211
GLuint mID
model matrix ID
Definition: entity.h:68
std::vector< BaseEntity * > children
Children of this entity.
Definition: entity.h:204
static const Material yellow_plastic
Definition: entity.h:155
glm::vec3 spotDirection
Direction of spotlight.
Definition: entity.h:304
~Material()
Definition: entity.cpp:134
int tex_height
Height of texture, if used.
Definition: entity.h:64
GLubyte * texture
Pointer to texture data, if present.
Definition: entity.h:66
static const Material silver
Definition: entity.h:155
Light, adds reality to the scene.
Definition: entity.h:297
GLuint aID
ambient color ID
Definition: entity.h:68
static const Material black_plastic
Definition: entity.h:155
static const Material brass
Definition: entity.h:155
Material material
Material for shading this entity.
Definition: entity.h:203
static const Material obsidian
Definition: entity.h:155
bool lightsEnabled
If true, no lighting calculations are done.
Definition: entity.h:60
static const Material jade
Definition: entity.h:155
static const Material black_rubber
Definition: entity.h:155
static const Material green_plastic
Definition: entity.h:155
static const Material turquoise
Definition: entity.h:155
int tex_width
Width of texture,if used.
Definition: entity.h:63
#define AGL_SHADING_PHONG
Each point (fragment) will be shaded.
Definition: util.h:71
Material()
Creates a material.
Definition: entity.cpp:131
GLuint sID
specular color ID
Definition: entity.h:68
Material & operator+=(const Material &other)
Same as operator+ but modifies the current material.
Definition: entity.cpp:148
glm::vec4 specular
Specular, color due to the reflection of light.
Definition: entity.h:54
glm::vec4 position
Position (direction) of the light.
Definition: entity.h:300
GLuint tID
texture ID
Definition: entity.h:68
glm::vec4 getPos()
Get the position of the light.
Definition: entity.h:343
Material class for entities.
Definition: entity.h:51
static const Material pearl
Definition: entity.h:155
Light DirectionalLight(const glm::vec3 &dir=glm::vec3(0), const glm::vec4 &color=glm::vec4(1))
Create a directional Light.
Definition: entity.h:356
static const Material ruby
Definition: entity.h:155
glm::vec4 diffuse
Diffuse, color when light hits a surface.
Definition: entity.h:54
GLuint progID
program ID
Definition: entity.h:67
static const Material red_rubber
Definition: entity.h:155
bool customShader
If true, Scene::prepare do not create shaders.
Definition: entity.h:61
int shadingType
Type of shading, currently unused.
Definition: entity.h:62
static const Material bronze
Definition: entity.h:155
std::vector< GLuint > indices
Indices.
Definition: entity.h:200