rag
graphic 2d engine
Renderer.h
1 //
2 // Renderer.h
3 // screen_lock_hacker
4 //
5 // Created by Ivan Sanchez Luque on 6/12/13.
6 // Copyright (c) 2013 Ivan Sanchez Luque. All rights reserved.
7 //
8 
9 #ifndef __rag__Renderer__
10 #define __rag__Renderer__
11 
12 #include <rag/Platform.h>
13 #include "Material.h"
14 #include "Color.h"
15 #include "RenderTarget.h"
16 #include <vector>
17 #include <map>
18 
19 #define renderer rag::Renderer::getInstance()
20 
21 namespace rag
22 {
23 
25 struct Program
26 {
27  std::string vsh;
28  std::string fsh;
29  GLuint handle;
30 
31  // These bool values are updated when the program is linked
32  bool usesVertexArray;
33  bool usesColorArray;
34  bool usesTexCoordArray;
35 };
36 
38 struct Shader
39 {
40  GLuint handle;
41  std::string file;
42 };
43 
45 
49 class Renderer
50 {
51 public:
52 
53  enum EMaterialAttributes
54  {
55  ATTRIB_VERTEX = 0,
56  ATTRIB_TEXTURECOORD,
57  ATTRIB_COLOR,
58 
59  ATTRIB_NORMAL,
60  ATTRIB_TANGENT,
61  ATTRIB_BONES_INDICES,
62  ATTRIB_BONES_WEIGHTS,
63 
64  NUM_ATTRIBUTES
65  };
66 
67  static Renderer& getInstance();
68 
69  void init();
70 
72  int loadProgram(const char* vsh, const char* fsh);
73 
75  void bindVertexArray(void* array, int size = 2, int stride = 0);
76 
78  void bindTextureArray(void* array, int size = 2, int stride = 0);
79 
81  void bindColorArray(void* array, int channels = 4, int stride = 0);
82 
84 
87  int bindMaterial(rag::Material* material);
88 
90  void bindTexture(int textureName);
91 
93  void setBlendFunc(GLenum source, GLenum dest);
94 
96  void setClearColor(Color color);
97 
98  void bindBuffer(int target, int buffer);
99 
100  void createVertexBuffer(GLuint& vboID);
101  void deleteVertexBuffer(GLuint& vboID);
102 
105 
107  void checkError();
108 
110  void precompileShader(const std::string& path);
111 
113  glm::mat4 getOrthoProjection();
114 
116  void clearShaders();
117 
120  return renderTarget;
121  };
122 
123 private:
124 
125  Renderer();
126 
128  const Shader& getShader(const std::string& path, GLenum shaderType);
129  GLuint compileShader(const std::string& path, GLenum shaderType);
130 
131  void useProgram(const Program& program);
132  void setUniform(const char* name, float value);
133  void setUniform(const char* name, float x, float y);
134  void setUniform(const char* name, float x, float y, float z);
135  void setUniform(const char* name, float x, float y, float z, float w);
136  void setUniformMatrix3(const char* name, float* mtx);
137  void setUniformMatrix4(const char* name, float* mtx);
138 
139  std::vector<Program> programs;
140  std::map<std::string, Shader> shaders;
141  int currentProgram;
142 
143  GLenum blendSource, blendDest;
144 
145  bool vertexArrayEnabled;
146  bool texCoordArrayEnabled;
147  bool colorArrayEnabled;
148 
149  rag::RenderTarget renderTarget;
150 
151 };
152 
153 } // rag
154 
155 #endif /* defined(__rag__Renderer__) */
Contains methods to render objects.
Definition: Renderer.h:49
std::string fsh
Path to the fragment shader.
Definition: Renderer.h:28
glm::mat4 getOrthoProjection()
Returns an orthographic projection.
Definition: Renderer.cpp:155
void setClearColor(Color color)
Set clear color.
Definition: Renderer.cpp:65
void bindTextureArray(void *array, int size=2, int stride=0)
Binds a texture array that would be used for the next draw call.
Definition: Renderer.cpp:264
Definition: Bitmap.h:8
int bindMaterial(rag::Material *material)
Binds the Material to be used in the next draw call.
Definition: Renderer.cpp:289
GLuint handle
Internal GL handle.
Definition: Renderer.h:29
std::string vsh
Path to the vertex shader.
Definition: Renderer.h:27
void precompileShader(const std::string &path)
Precomiples a Shader, useful to call it in loading times.
Definition: Renderer.cpp:70
Object where DisplayObject instances with render capability are suposed to render.
Definition: RenderTarget.h:44
int loadProgram(const char *vsh, const char *fsh)
Loads and starts using a program with the given shaders.
Definition: Renderer.cpp:95
void bindColorArray(void *array, int channels=4, int stride=0)
Binds a color array that would be used for the next draw call.
Definition: Renderer.cpp:269
Represents a render material that would affect how objects will be rendered.
Definition: Material.h:39
void checkError()
Displays an error if something's wrong.
Definition: Renderer.cpp:375
int getCurrentProgramHandle()
Returns the handle of the program used currently.
Definition: Renderer.cpp:370
void setBlendFunc(GLenum source, GLenum dest)
Sets blending function.
Definition: Renderer.cpp:279
void clearShaders()
Forget shaders and programs loaded. Programs will be generated again as needed.
Definition: Renderer.cpp:425
Represents a GL Shader.
Definition: Renderer.h:38
rag::RenderTarget & getRenderTarget()
TODO Pass render target as parameter to render() method in DisplayObject - so that it becomes more ob...
Definition: Renderer.h:119
Represents RGBA color.
Definition: Color.h:11
Represents a Shader Program.
Definition: Renderer.h:25
void bindTexture(int textureName)
Binds a texture by id.
Definition: Renderer.cpp:274
void bindVertexArray(void *array, int size=2, int stride=0)
Binds a vertex array that would be used for the next draw call.
Definition: Renderer.cpp:259