AGL  0
3D graphics library
scene.h
Go to the documentation of this file.
1 #ifndef SCENE_H
2 #define SCENE_H
3 
4 #include "entity.h"
5 #include<vector>
6 #include<GLFW/glfw3.h>
7 #include "glm/glm.hpp"
8 #include "glm/gtc/quaternion.hpp"
9 
10 namespace agl {
17 class Camera
18 {
19 public:
20  glm::mat4 view;
21  // Cached values
22  glm::vec3 _pos,
23  _lookAt,
24  _up;
25 
32  Camera(const glm::vec3 &pos=glm::vec3(3, 4, 5), const glm::vec3 &lookAt=glm::vec3(0), const glm::vec3 &up=glm::vec3(0, 1, 0));
33 
44  void setPos(const glm::vec3 &pos=glm::vec3(3, 4, 5));
51  void setPos(float x=3, float y=4, float z=5);
58  void lookAt(const glm::vec3 &target=glm::vec3(0));
65  void lookAt(float x=0, float y=0, float z=0);
70  void setUp(const glm::vec3 &up=glm::vec3(0, 1, 0));
77  void setUp(float x=0, float y=1, float z=0);
79 
87  void setView(const glm::mat4 &mat);
98  void rotate(float rad=0, const glm::vec3 &axis=glm::vec3(0), bool around=true);
104  void rotate(const glm::quat &rot=glm::quat(1, 0, 0, 0), bool around=true);
112  void rotateX(float rad=0, bool around=true, bool safe=true);
118  void rotateY(float rad=0, bool around=true);
124  void rotateZ(float rad=0, bool around=true);
129  void translate(const glm::vec3 &d=glm::vec3(0));
136  void translate(float dx=0, float dy=0, float dz=0);
137 
149  void viewTranslate(const glm::vec3 &d=glm::vec3(0));
156  void viewTranslate(float dx=0, float dy=0, float dz=0);
162  void viewRotate(float rad=0, const glm::vec3 &axis=glm::vec3(0));
167  void viewScale(const glm::vec3 &s=glm::vec3(1));
174  void viewScale(float sx=1, float sy=1, float sz=1);
179  void viewTransform(const glm::mat4 &m);
181 };
182 
183 
191 class Scene
192 {
193 public:
194  int width,
195  height;
196  glm::mat4 projection;
198  std::vector<BaseEntity*> children;
199  GLFWwindow* window;
200  std::vector<Entity*> entities;
201  std::vector<Light*> lights;
202 
213  Scene(int width=640, int height=480, const char *name="AGL");
214  ~Scene();
215 
223  void setPerspectiveProjection(float fovDeg=45, float aspectRatio=-1, float near=0.1, float far=100);
233  void setOrthographicProjection(float left=-1, float right=1, float bottom=-1, float top=1, float near=0.1, float far=100);
240  void setCamera(const glm::vec3 &pos, const glm::vec3 &lookAt, const glm::vec3 &up);
245  void setProjection(glm::mat4 &mat);
250  void setView(glm::mat4 &mat);
265  void setController(GLFWkeyfun keyCallback=nullptr, GLFWcursorposfun cursorPosCallback=nullptr,
266  GLFWmousebuttonfun mouseButtonCallback=nullptr, GLFWscrollfun scrollCallback=nullptr,
267  GLFWwindowsizefun windowSizeCallback=nullptr);
275  void setBGcolor(float r=0, float g=0, float b=0, float a=0);
280  void add(BaseEntity &e);
288  void enableLights(bool enable=true);
295  void prepare();
304  bool render();
309  glm::mat4 getMatVP();
310 
311 private:
312  glm::vec4 bgcolor;
313 
319  int setupGL(const char *name);
326  void getAllEntity(std::vector<BaseEntity*> &children);
327 };
328 
342 void defKeyCB(GLFWwindow* window, int key, int scancode, int action, int mods);
348 void defScrollCB(GLFWwindow* window, double xoffset, double yoffset);
355 void defWindowSizeCB(GLFWwindow* window, int w, int h);
356 }
357 
358 #endif // SCENE_H
The base for all Entity and Light.
Definition: entity.h:166
void rotate(float rad=0, const glm::vec3 &axis=glm::vec3(0), bool around=true)
Rotate the camera.
Definition: scene.cpp:256
int width
Width of the window.
Definition: scene.h:194
void viewScale(const glm::vec3 &s=glm::vec3(1))
Scale the view matrix.
Definition: scene.cpp:329
std::vector< Entity * > entities
Temporary store all the Entity before render.
Definition: scene.h:200
glm::vec3 _up
The up vector for the camera.
Definition: scene.h:22
std::vector< BaseEntity * > children
Stores all the Entity and Light.
Definition: scene.h:198
void lookAt(const glm::vec3 &target=glm::vec3(0))
Set the point the camera will look at.
Definition: scene.cpp:228
void defKeyCB(GLFWwindow *window, int key, int scancode, int action, int mods)
Default callback for key press.
Definition: scene.cpp:342
Definition: create_shader.cpp:4
GLFWwindow * window
The GLFW window for displaying everything.
Definition: scene.h:199
void viewTranslate(const glm::vec3 &d=glm::vec3(0))
Translate the view matrix.
Definition: scene.cpp:317
void rotateZ(float rad=0, bool around=true)
Rotate around the z-axis.
Definition: scene.cpp:297
Camera(const glm::vec3 &pos=glm::vec3(3, 4, 5), const glm::vec3 &lookAt=glm::vec3(0), const glm::vec3 &up=glm::vec3(0, 1, 0))
Creates a camera.
Definition: scene.cpp:211
glm::mat4 view
The view matrix.
Definition: scene.h:20
Camera camera
Camera for the scene.
Definition: scene.h:197
glm::vec3 _pos
The position of the camera.
Definition: scene.h:22
void setUp(const glm::vec3 &up=glm::vec3(0, 1, 0))
Set the up-vector.
Definition: scene.cpp:238
Scene class, which holds everything.
Definition: scene.h:191
void rotateY(float rad=0, bool around=true)
Rotate around the y-axis.
Definition: scene.cpp:289
std::vector< Light * > lights
Temporary store all the Light before render.
Definition: scene.h:201
void viewRotate(float rad=0, const glm::vec3 &axis=glm::vec3(0))
Rotate the view matrix.
Definition: scene.cpp:325
void defWindowSizeCB(GLFWwindow *window, int w, int h)
Default window resize callback.
Definition: scene.cpp:412
Camera class for the scene.
Definition: scene.h:17
glm::vec3 _lookAt
The point the camera looks at.
Definition: scene.h:22
void viewTransform(const glm::mat4 &m)
Transform the view matrix.
Definition: scene.cpp:337
void translate(const glm::vec3 &d=glm::vec3(0))
Translate (move, shift) the camera.
Definition: scene.cpp:305
void setPos(const glm::vec3 &pos=glm::vec3(3, 4, 5))
Sets the position of the camera.
Definition: scene.cpp:218
void setView(const glm::mat4 &mat)
Set the view matrix.
Definition: scene.cpp:248
glm::mat4 projection
Projection matrix for the render.
Definition: scene.h:196
void defScrollCB(GLFWwindow *window, double xoffset, double yoffset)
Default scroll callback.
Definition: scene.cpp:402
void rotateX(float rad=0, bool around=true, bool safe=true)
Rotate around the x-axis.
Definition: scene.cpp:279