AnimEngine
Program.h
1 #pragma once
2 #ifndef __Program__
3 #define __Program__
4 
5 #include <unordered_map>
6 #include <istream>
7 #include <string>
8 #include <JSON/json.hpp>
9 
10 #include "GLSL.h"
11 
12 using namespace std;
13 using json = nlohmann::json;
14 
15 class Program{
16 public:
17  // Creates empty program object
18  Program();
19 
20  // Same as calling builFromJSONArray
21  Program(const json &program_obj);
22 
23  Program(istream &vertex, istream &fragment);
24 
25  Program(const std::string& vpath, const std::string& fpath);
26 
27  virtual ~Program();
28 
29  void setVerbose(bool v) { verbose = v; }
30  bool isVerbose() const { return verbose; }
31 
32  GLuint getPID() const { return(pid); }
33 
34  bool wasBuildSuccessful() {return(buildSuccess);}
35 
36 
37  // Note: istream is used as the interface here so that the functions can be more flexible.
38  // To load from a file an ifstream can be given. To load a string use istringstream, ect...
39 
40  // Build the classic vertex shader -> fragment shader program from two GLSL sources
41  // Returns true if program compiled and linked properly false otherwise
42  bool buildVsFsProgram(istream &vertex, istream &fragment);
43  bool buildVsFsProgram(const std::string& vertex, const std::string& fragment);
44 
45  // Build from a JSON array containing shader specifications. Program will be linked in order given.
46  // Structure should be as follows
47  /*
48  [
49  {
50  "name":"shaderName"
51  "type": "vertex | fragment | geometry | compute"
52  "attributes": [["float", "list"], ["vec2", "of"], ["mat4", "attributes"]]
53  "uniforms": [["sampler2D", "list"], ["int", "of"], ["vec3", "uniforms"]]
54  "src": "#version 330 core\n // Long line of the GLSL code"
55  },
56 
57  {
58  "name":"otherShaderName"
59  "type": "vertex | fragment | geometry | compute"
60  "attributes": [["float", "list"], ["vec2", "of"], ["mat4", "attributes"]]
61  "uniforms": [["sampler2D", "list"], ["int", "of"], ["vec3", "uniforms"]]
62  "src": "#version 330 core\n // Long line of the GLSL code"
63  }
64  ]
65  */
66  // Returns true if program compiled and linked properly false otherwise
67  bool buildFromJsonArray(const json &program_obj);
68 
69  virtual void bind();
70  virtual void unbind();
71 
72  GLint getAttribute(const string &name);
73  GLint getUniform(const string &name);
74  bool hasAttribute(const string &name);
75  bool hasUniform(const string &name);
76 
77 private:
78  GLuint pid;
79  unordered_map<string,GLint> attributes;
80  unordered_map<string,GLint> uniforms;
81  bool verbose;
82  bool buildSuccess = false;
83 };
84 
85 #endif
Definition: Program.h:15