Simulant  21.12-574
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 class 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, Stage* stage);
50  void visit(const Renderable* renderable, const MaterialPass* pass, batcher::Iteration);
51  void end_traversal(const batcher::RenderQueue &queue, Stage* stage);
52 
53  void change_render_group(const batcher::RenderGroup *prev, const batcher::RenderGroup *next);
54  void change_material_pass(const MaterialPass* prev, const MaterialPass* next);
55  void apply_lights(const LightPtr* lights, const uint8_t count);
56 
57 private:
58  GenericRenderer* renderer_;
59  CameraPtr camera_;
60  Colour 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);
80 
81  batcher::RenderGroupKey prepare_render_group(
82  batcher::RenderGroup* group,
83  const Renderable *renderable,
84  const MaterialPass *material_pass,
85  const uint8_t pass_number,
86  const bool is_blended,
87  const float distance_to_camera
88  ) override;
89 
90  void init_context() override;
91 
92  std::shared_ptr<batcher::RenderQueueVisitor> get_render_queue_visitor(CameraPtr camera) override;
93 
94  GPUProgramID new_or_existing_gpu_program(const std::string& vertex_shader_source, const std::string& fragment_shader_source) override;
95 
96  GPUProgramPtr gpu_program(const GPUProgramID& program_id) const override;
97  GPUProgramID current_gpu_program_id() const override;
98  bool supports_gpu_programs() const override { return true; }
99  GPUProgramID default_gpu_program_id() const override;
100 
101  std::string name() const override {
102  return "gl2x";
103  }
104 
105  void prepare_to_render(const Renderable* renderable) override;
106 private:
107  GPUProgramManager program_manager_;
108  GPUProgramID default_gpu_program_id_ = 0;
109 
110  std::shared_ptr<VBOManager> buffer_manager_;
111 
112  void set_light_uniforms(const MaterialPass* pass, GPUProgram* program, const LightPtr light);
113  void set_material_uniforms(const MaterialPass *pass, GPUProgram* program);
114  void set_renderable_uniforms(const MaterialPass* pass, GPUProgram* program, const Renderable* renderable, Camera* camera);
115  void set_stage_uniforms(const MaterialPass* pass, GPUProgram* program, const Colour& global_ambient);
116 
117  void set_auto_attributes_on_shader(GPUProgram *program, const Renderable* buffer, GPUBuffer* buffers);
118  void set_blending_mode(BlendType type);
119  void send_geometry(const Renderable* renderable, GPUBuffer* buffers);
120 
121  /* Stashed here in prepare_to_render and used later for that renderable */
122  std::shared_ptr<GPUBuffer> buffer_stash_;
123 
124  friend class GL2RenderQueueVisitor;
125 };
126 
127 }
128 
129 #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::Stage
Definition: stage.h:80
smlt
Definition: animation.cpp:25
smlt::RenderState
Definition: generic_renderer.h:37
smlt::Light
Definition: light.h:35
smlt::Window
Definition: window.h:65
smlt::batcher::RenderGroupKey
Definition: render_queue.h:39
smlt::batcher::RenderQueue
Definition: render_queue.h:156
smlt::batcher::RenderQueueVisitor
Definition: render_queue.h:114
smlt::UniqueID< GPUProgramPtr >
smlt::Camera
Definition: camera.h:21
smlt::ObjectManager< GPUProgramID, GPUProgram, DO_REFCOUNT >
smlt::MaterialPass
Definition: material.h:47
smlt::GL2RenderGroupImpl
Definition: generic_renderer.cpp:59
smlt::batcher::RenderGroup
Definition: render_queue.h:47
smlt::GenericRenderer
Definition: generic_renderer.h:76
smlt::default_init_ptr< Camera >
smlt::Renderable
Definition: renderable.h:67
smlt::GPUBuffer
Definition: vbo_manager.h:33
smlt::Colour
Definition: colour.h:29