Simulant  21.12-1275
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
generic_renderer.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef GENERIC_RENDERER_H
20 #define GENERIC_RENDERER_H
21 
22 #include <vector>
23 #include <memory>
24 #include <cstdint>
25 #include "../renderer.h"
26 #include "../gl_renderer.h"
27 #include "../../assets/material.h"
28 #include "../batching/render_queue.h"
29 
30 namespace smlt {
31 
32 struct GL2RenderGroupImpl;
33 class GenericRenderer;
34 class VBOManager;
35 struct GPUBuffer;
36 
37 struct RenderState {
38  Renderable* renderable;
39  MaterialPass* pass;
40  const Light* light;
41  batcher::Iteration iteration;
42  GL2RenderGroupImpl* render_group_impl;
43 };
44 
46 public:
48 
49  void start_traversal(const batcher::RenderQueue& queue, uint64_t frame_id, StageNode *stage) override;
50  void visit(const Renderable* renderable, const MaterialPass* pass, batcher::Iteration) override;
51  void end_traversal(const batcher::RenderQueue &queue, StageNode* stage) override;
52 
53  void change_render_group(const batcher::RenderGroup *prev, const batcher::RenderGroup *next) override;
54  void change_material_pass(const MaterialPass* prev, const MaterialPass* next) override;
55  void apply_lights(const LightPtr* lights, const uint8_t count) override;
56 
57 private:
58  GenericRenderer* renderer_;
59  CameraPtr camera_;
60  Color global_ambient_;
61 
62  GPUProgram* program_ = nullptr;
63  const MaterialPass* pass_ = nullptr;
64  const Light* light_ = nullptr;
65 
66  GL2RenderGroupImpl* current_group_ = nullptr;
67 
68  void do_visit(const Renderable* renderable, const MaterialPass* material_pass, batcher::Iteration iteration);
69 
70  void rebind_attribute_locations_if_necessary(const MaterialPass* pass, GPUProgram* program);
71 };
72 
74 
76  public GLRenderer {
77 
78 public:
79  GenericRenderer(Window* window, bool use_es=false);
80 
81  batcher::RenderGroupKey prepare_render_group(
82  batcher::RenderGroup* group, const Renderable* renderable,
83  const MaterialPass* material_pass, const RenderPriority priority,
84  const uint8_t pass_number, const bool is_blended,
85  const float distance_to_camera, uint16_t texture_id) override;
86 
87  void init_context() override;
88 
89  std::shared_ptr<batcher::RenderQueueVisitor> get_render_queue_visitor(CameraPtr camera) override;
90 
91  GPUProgramPtr new_or_existing_gpu_program(const std::string& vertex_shader_source, const std::string& fragment_shader_source) override;
92 
93  GPUProgramPtr gpu_program(const GPUProgramID& program_id) const override;
94  GPUProgramPtr current_gpu_program() const override;
95  bool supports_gpu_programs() const override { return true; }
96  GPUProgramPtr default_gpu_program() const override;
97 
98  std::string name() const override {
99  return (use_es_) ? "gles2x" : "gl2x";
100  }
101 
102  void prepare_to_render(const Renderable* renderable) override;
103 
104  bool is_gles() const { return use_es_; }
105 private:
106  GPUProgramManager program_manager_;
107  GPUProgramPtr default_gpu_program_ = 0;
108  bool use_es_ = false;
109  std::shared_ptr<VBOManager> buffer_manager_;
110 
111  void set_light_uniforms(const MaterialPass* pass, GPUProgram* program, const LightPtr light);
112  void set_material_uniforms(const MaterialPass *pass, GPUProgram* program);
113  void set_renderable_uniforms(const MaterialPass* pass, GPUProgram* program, const Renderable* renderable, Camera* camera);
114  void set_stage_uniforms(const MaterialPass* pass, GPUProgram* program, const Color& global_ambient);
115 
116  void set_auto_attributes_on_shader(GPUProgram *program, const Renderable* buffer, GPUBuffer* buffers);
117  void set_blending_mode(BlendType type);
118  void send_geometry(const Renderable* renderable, GPUBuffer* buffers);
119 
120  /* Stashed here in prepare_to_render and used later for that renderable */
121  std::shared_ptr<GPUBuffer> buffer_stash_;
122 
123  friend class GL2RenderQueueVisitor;
124 };
125 
126 }
127 
128 #endif // GENERIC_RENDERER_H
smlt::GPUProgram
Definition: gpu_program.h:69
smlt::GLRenderer
Definition: gl_renderer.h:50
smlt::GL2RenderQueueVisitor
Definition: generic_renderer.h:45
smlt
Definition: animation.cpp:25
smlt::RenderState
Definition: generic_renderer.h:37
smlt::Light
Definition: light.h:34
smlt::Window
Definition: window.h:68
smlt::batcher::RenderGroupKey
Definition: render_queue.h:58
smlt::batcher::RenderQueue
Definition: render_queue.h:169
smlt::Color
Definition: color.h:32
smlt::batcher::RenderQueueVisitor
Definition: render_queue.h:127
smlt::Camera
Definition: camera.h:17
smlt::ObjectManager< GPUProgramID, GPUProgram, DO_REFCOUNT >
smlt::MaterialPass
Definition: material.h:46
smlt::GL2RenderGroupImpl
Definition: generic_renderer.cpp:66
smlt::StageNode
Definition: stage_node.h:442
smlt::batcher::RenderGroup
Definition: render_queue.h:87
smlt::GenericRenderer
Definition: generic_renderer.h:76
smlt::Renderable
Definition: renderable.h:67
smlt::GPUBuffer
Definition: vbo_manager.h:33