GLESRenderer.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <GLES2/gl2.h>
  25. #include "Renderer.h"
  26. #include "Texture.h"
  27. #include "VertexBuffer.h"
  28. #include "IndexBuffer.h"
  29. #include "RenderBuffer.h"
  30. #include "VertexShader.h"
  31. #include "PixelShader.h"
  32. #include "IdTable.h"
  33. #include "HeapAllocator.h"
  34. #include "Resource.h"
  35. #include "GLContext.h"
  36. namespace crown
  37. {
  38. const uint32_t MAX_TEXTURE_UNITS = 8;
  39. //-----------------------------------------------------------------------------
  40. struct Texture
  41. {
  42. GLuint gl_object;
  43. PixelFormat format;
  44. };
  45. //-----------------------------------------------------------------------------
  46. struct VertexBuffer
  47. {
  48. GLuint gl_object;
  49. size_t count;
  50. VertexFormat format;
  51. };
  52. //-----------------------------------------------------------------------------
  53. struct IndexBuffer
  54. {
  55. GLuint gl_object;
  56. uint32_t index_count;
  57. };
  58. //-----------------------------------------------------------------------------
  59. struct RenderBuffer
  60. {
  61. GLuint gl_frame_buffer;
  62. GLuint gl_render_buffer;
  63. };
  64. //-----------------------------------------------------------------------------
  65. struct VertexShader
  66. {
  67. GLuint gl_object;
  68. };
  69. //-----------------------------------------------------------------------------
  70. struct PixelShader
  71. {
  72. GLuint gl_object;
  73. };
  74. //-----------------------------------------------------------------------------
  75. struct GPUProgram
  76. {
  77. GLuint gl_object;
  78. };
  79. /// OpenGLES2 renderer
  80. class GLESRenderer : public Renderer
  81. {
  82. public:
  83. GLESRenderer();
  84. ~GLESRenderer();
  85. void init();
  86. void shutdown();
  87. // Vertex buffers
  88. VertexBufferId create_vertex_buffer(size_t count, VertexFormat format, const void* vertices);
  89. VertexBufferId create_dynamic_vertex_buffer(size_t count, VertexFormat format, const void* vertices);
  90. void update_vertex_buffer(VertexBufferId id, size_t offset, size_t count, const void* vertices);
  91. void destroy_vertex_buffer(VertexBufferId id);
  92. // Index buffers
  93. IndexBufferId create_index_buffer(size_t count, const void* indices);
  94. void destroy_index_buffer(IndexBufferId id);
  95. // Textures
  96. TextureId create_texture(uint32_t width, uint32_t height, PixelFormat format, const void* data);
  97. void update_texture(TextureId id, uint32_t x, uint32_t y, uint32_t width, uint32_t height, const void* data);
  98. void destroy_texture(TextureId id);
  99. void bind_texture(uint32_t unit, TextureId texture);
  100. void set_texturing(uint32_t unit, bool texturing);
  101. void set_texture_wrap(uint32_t unit, TextureWrap wrap);
  102. void set_texture_filter(uint32_t unit, TextureFilter filter);
  103. // Vertex shaders
  104. VertexShaderId create_vertex_shader(const char* program);
  105. void destroy_vertex_shader(VertexShaderId id);
  106. // Pixel shaders
  107. PixelShaderId create_pixel_shader(const char* program);
  108. void destroy_pixel_shader(PixelShaderId id);
  109. // GPU programs
  110. GPUProgramId create_gpu_program(VertexShaderId vs, PixelShaderId ps);
  111. void destroy_gpu_program(GPUProgramId id);
  112. void set_gpu_program_bool_uniform(GPUProgramId id, const char* name, bool value);
  113. void set_gpu_program_int_uniform(GPUProgramId id, const char* name, int value);
  114. void set_gpu_program_vec2_uniform(GPUProgramId id, const char* name, const Vec2& value);
  115. void set_gpu_program_vec3_uniform(GPUProgramId id, const char* name, const Vec3& value);
  116. void set_gpu_program_vec4_uniform(GPUProgramId id, const char* name, const Vec4& value);
  117. void set_gpu_porgram_mat3_uniform(GPUProgramId id, const char* name, const Mat3& value);
  118. void set_gpu_program_mat4_uniform(GPUProgramId id, const char* name, const Mat4& value);
  119. void set_gpu_program_sampler_uniform(GPUProgramId id, const char* name, uint32_t value);
  120. void bind_gpu_program(GPUProgramId id) const;
  121. // Frame buffers
  122. // RenderBufferId create_render_buffer(uint32_t width, uint32_t height, PixelFormat format);
  123. // void destroy_render_buffer(RenderBufferId id);
  124. void frame();
  125. void set_clear_color(const Color4& color);
  126. // Lighting
  127. void set_ambient_light(const Color4& color);
  128. void set_backface_culling(bool culling);
  129. // Fragment operations
  130. void set_depth_test(bool test);
  131. void set_depth_write(bool write);
  132. void set_depth_func(CompareFunction func);
  133. void set_blending(bool blending);
  134. void set_blending_params(BlendEquation equation, BlendFunction src, BlendFunction dst, const Color4& color);
  135. void set_color_write(bool write);
  136. void set_front_face(FrontFace face);
  137. void set_viewport_params(int32_t x, int32_t y, int32_t width, int32_t height);
  138. void get_viewport_params(int32_t& x, int32_t& y, int32_t& width, int32_t& height);
  139. void set_scissor(bool scissor);
  140. void set_scissor_params(int32_t x, int32_t y, int32_t width, int32_t height);
  141. void get_scissor_params(int32_t& x, int32_t& y, int32_t& width, int32_t& height);
  142. Mat4 get_matrix(MatrixType type) const;
  143. void set_matrix(MatrixType type, const Mat4& matrix);
  144. void bind_vertex_buffer(VertexBufferId vb) const;
  145. //void bind_render_buffer(RenderBufferId id) const;
  146. void draw_triangles(IndexBufferId id) const;
  147. void draw_lines(const float* vertices, const float* colors, uint32_t count);
  148. private:
  149. // Loads the default shaders
  150. void load_default_shaders();
  151. void unload_default_shaders();
  152. void reload_default_shaders();
  153. // Activates a texture unit and returns true if succes
  154. bool activate_texture_unit(uint32_t unit);
  155. bool activate_light(uint32_t light);
  156. // Shaders
  157. GLint find_gpu_program_uniform(GLuint program, const char* name) const;
  158. // GL error checking
  159. void check_gl_errors() const;
  160. private:
  161. HeapAllocator m_allocator;
  162. GLContext m_context;
  163. // Matrices
  164. Mat4 m_matrix[MT_COUNT];
  165. Mat4 m_model_view_matrix;
  166. Mat4 m_model_view_projection_matrix;
  167. // Limits
  168. int32_t m_max_texture_size;
  169. int32_t m_max_texture_units;
  170. int32_t m_max_vertex_indices;
  171. int32_t m_max_vertex_vertices;
  172. float m_max_anisotropy;
  173. float m_min_max_point_size[2];
  174. float m_min_max_line_width[2];
  175. // Viewport and scissor
  176. int32_t m_viewport[4];
  177. int32_t m_scissor[4];
  178. // Lighting
  179. Color4 m_ambient_light_color;
  180. // Texture management
  181. IdTable m_textures_id_table;
  182. Texture m_textures[MAX_TEXTURES];
  183. uint32_t m_active_texture_unit;
  184. GLuint m_texture_unit[MAX_TEXTURE_UNITS];
  185. GLenum m_texture_unit_target[MAX_TEXTURE_UNITS];
  186. // Vertex/Index buffer management
  187. IdTable m_vertex_buffers_id_table;
  188. VertexBuffer m_vertex_buffers[MAX_VERTEX_BUFFERS];
  189. IdTable m_index_buffers_id_table;
  190. IndexBuffer m_index_buffers[MAX_INDEX_BUFFERS];
  191. // Vertex shader management
  192. IdTable m_vertex_shaders_id_table;
  193. VertexShader m_vertex_shaders[MAX_VERTEX_SHADERS];
  194. // Pixel shader management
  195. IdTable m_pixel_shaders_id_table;
  196. PixelShader m_pixel_shaders[MAX_PIXEL_SHADERS];
  197. // GPU program management
  198. IdTable m_gpu_programs_id_table;
  199. GPUProgram m_gpu_programs[128];
  200. // Render buffer management
  201. //IdTable m_render_buffers_id_table;
  202. //GLRenderBuffer m_render_buffers[MAX_RENDER_BUFFERS];
  203. // Default shaders
  204. VertexShaderId m_default_vertex_shader;
  205. PixelShaderId m_default_pixel_shader;
  206. GPUProgramId m_default_gpu_program;
  207. };
  208. } // namespace crown