mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-22 15:09:19 +02:00
reformat renderer
This commit is contained in:
1467
renderer.cpp
1467
renderer.cpp
File diff suppressed because it is too large
Load Diff
295
renderer.h
295
renderer.h
@@ -31,79 +31,83 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
//#define EU07_USE_DEBUG_CAMERA
|
//#define EU07_USE_DEBUG_CAMERA
|
||||||
//#define EU07_USE_DEBUG_SOUNDEMITTERS
|
//#define EU07_USE_DEBUG_SOUNDEMITTERS
|
||||||
|
|
||||||
struct opengl_light : public basic_light {
|
struct opengl_light : public basic_light
|
||||||
|
{
|
||||||
|
|
||||||
GLuint id{(GLuint)-1};
|
GLuint id{(GLuint)-1};
|
||||||
|
|
||||||
float factor;
|
float factor;
|
||||||
|
|
||||||
void
|
void apply_intensity(float const Factor = 1.0f);
|
||||||
apply_intensity( float const Factor = 1.0f );
|
void apply_angle();
|
||||||
void
|
|
||||||
apply_angle();
|
|
||||||
|
|
||||||
opengl_light &
|
opengl_light &operator=(basic_light const &Right)
|
||||||
operator=( basic_light const &Right ) {
|
{
|
||||||
basic_light::operator=(Right);
|
basic_light::operator=(Right);
|
||||||
return *this; }
|
return *this;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// encapsulates basic rendering setup.
|
// encapsulates basic rendering setup.
|
||||||
// for modern opengl this translates to a specific collection of glsl shaders,
|
// for modern opengl this translates to a specific collection of glsl shaders,
|
||||||
// for legacy opengl this is combination of blending modes, active texture units etc
|
// for legacy opengl this is combination of blending modes, active texture units etc
|
||||||
struct opengl_technique {
|
struct opengl_technique
|
||||||
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
// simple camera object. paired with 'virtual camera' in the scene
|
// simple camera object. paired with 'virtual camera' in the scene
|
||||||
class opengl_camera {
|
class opengl_camera
|
||||||
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// methods:
|
// methods:
|
||||||
inline
|
inline void update_frustum()
|
||||||
void
|
{
|
||||||
update_frustum() { update_frustum( m_projection, m_modelview ); }
|
update_frustum(m_projection, m_modelview);
|
||||||
void
|
}
|
||||||
update_frustum( glm::mat4 const &Projection, glm::mat4 const &Modelview );
|
void update_frustum(glm::mat4 const &Projection, glm::mat4 const &Modelview);
|
||||||
bool
|
bool visible(scene::bounding_area const &Area) const;
|
||||||
visible( scene::bounding_area const &Area ) const;
|
bool visible(TDynamicObject const *Dynamic) const;
|
||||||
bool
|
inline glm::dvec3 const &position() const
|
||||||
visible( TDynamicObject const *Dynamic ) const;
|
{
|
||||||
inline
|
return m_position;
|
||||||
glm::dvec3 const &
|
}
|
||||||
position() const { return m_position; }
|
inline glm::dvec3 &position()
|
||||||
inline
|
{
|
||||||
glm::dvec3 &
|
return m_position;
|
||||||
position() { return m_position; }
|
}
|
||||||
inline
|
inline glm::mat4 const &projection() const
|
||||||
glm::mat4 const &
|
{
|
||||||
projection() const { return m_projection; }
|
return m_projection;
|
||||||
inline
|
}
|
||||||
glm::mat4 &
|
inline glm::mat4 &projection()
|
||||||
projection() { return m_projection; }
|
{
|
||||||
inline
|
return m_projection;
|
||||||
glm::mat4 const &
|
}
|
||||||
modelview() const { return m_modelview; }
|
inline glm::mat4 const &modelview() const
|
||||||
inline
|
{
|
||||||
glm::mat4 &
|
return m_modelview;
|
||||||
modelview() { return m_modelview; }
|
}
|
||||||
inline
|
inline glm::mat4 &modelview()
|
||||||
std::vector<glm::vec4> &
|
{
|
||||||
frustum_points() { return m_frustumpoints; }
|
return m_modelview;
|
||||||
|
}
|
||||||
|
inline std::vector<glm::vec4> &frustum_points()
|
||||||
|
{
|
||||||
|
return m_frustumpoints;
|
||||||
|
}
|
||||||
// transforms provided set of clip space points to world space
|
// transforms provided set of clip space points to world space
|
||||||
template <class Iterator_>
|
template <class Iterator_> void transform_to_world(Iterator_ First, Iterator_ Last) const
|
||||||
void
|
{
|
||||||
transform_to_world( Iterator_ First, Iterator_ Last ) const {
|
std::for_each(First, Last, [this](glm::vec4 &point) {
|
||||||
std::for_each(
|
|
||||||
First, Last,
|
|
||||||
[this]( glm::vec4 &point ) {
|
|
||||||
// transform each point using the cached matrix...
|
// transform each point using the cached matrix...
|
||||||
point = this->m_inversetransformation * point;
|
point = this->m_inversetransformation * point;
|
||||||
// ...and scale by transformed w
|
// ...and scale by transformed w
|
||||||
point = glm::vec4{ glm::vec3{ point } / point.w, 1.f }; } ); }
|
point = glm::vec4{glm::vec3{point} / point.w, 1.f};
|
||||||
|
});
|
||||||
|
}
|
||||||
// debug helper, draws shape of frustum in world space
|
// debug helper, draws shape of frustum in world space
|
||||||
void
|
void draw(glm::vec3 const &Offset) const;
|
||||||
draw( glm::vec3 const &Offset ) const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// members:
|
// members:
|
||||||
@@ -116,76 +120,61 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
// bare-bones render controller, in lack of anything better yet
|
// bare-bones render controller, in lack of anything better yet
|
||||||
class opengl_renderer {
|
class opengl_renderer
|
||||||
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// types
|
// types
|
||||||
|
|
||||||
// destructor
|
|
||||||
~opengl_renderer() { gluDeleteQuadric( m_quadric ); }
|
|
||||||
|
|
||||||
// methods
|
// methods
|
||||||
bool
|
bool Init(GLFWwindow *Window);
|
||||||
Init( GLFWwindow *Window );
|
|
||||||
// main draw call. returns false on error
|
// main draw call. returns false on error
|
||||||
bool
|
bool Render();
|
||||||
Render();
|
|
||||||
void SwapBuffers();
|
void SwapBuffers();
|
||||||
inline
|
inline float Framerate()
|
||||||
float
|
{
|
||||||
Framerate() { return m_framerate; }
|
return m_framerate;
|
||||||
|
}
|
||||||
// geometry methods
|
// geometry methods
|
||||||
// NOTE: hands-on geometry management is exposed as a temporary measure; ultimately all visualization data should be generated/handled automatically by the renderer itself
|
// NOTE: hands-on geometry management is exposed as a temporary measure; ultimately all visualization data should be generated/handled automatically by the renderer itself
|
||||||
// creates a new geometry bank. returns: handle to the bank or NULL
|
// creates a new geometry bank. returns: handle to the bank or NULL
|
||||||
gfx::geometrybank_handle
|
gfx::geometrybank_handle Create_Bank();
|
||||||
Create_Bank();
|
|
||||||
// creates a new geometry chunk of specified type from supplied vertex data, in specified bank. returns: handle to the chunk or NULL
|
// creates a new geometry chunk of specified type from supplied vertex data, in specified bank. returns: handle to the chunk or NULL
|
||||||
gfx::geometry_handle
|
gfx::geometry_handle Insert(gfx::vertex_array &Vertices, gfx::geometrybank_handle const &Geometry, int const Type);
|
||||||
Insert( gfx::vertex_array &Vertices, gfx::geometrybank_handle const &Geometry, int const Type );
|
|
||||||
// replaces data of specified chunk with the supplied vertex data, starting from specified offset
|
// replaces data of specified chunk with the supplied vertex data, starting from specified offset
|
||||||
bool
|
bool Replace(gfx::vertex_array &Vertices, gfx::geometry_handle const &Geometry, int const Type, std::size_t const Offset = 0);
|
||||||
Replace( gfx::vertex_array &Vertices, gfx::geometry_handle const &Geometry, int const Type, std::size_t const Offset = 0 );
|
|
||||||
// adds supplied vertex data at the end of specified chunk
|
// adds supplied vertex data at the end of specified chunk
|
||||||
bool
|
bool Append(gfx::vertex_array &Vertices, gfx::geometry_handle const &Geometry, int const Type);
|
||||||
Append( gfx::vertex_array &Vertices, gfx::geometry_handle const &Geometry, int const Type );
|
|
||||||
// provides direct access to vertex data of specfied chunk
|
// provides direct access to vertex data of specfied chunk
|
||||||
gfx::vertex_array const &
|
gfx::vertex_array const &Vertices(gfx::geometry_handle const &Geometry) const;
|
||||||
Vertices( gfx::geometry_handle const &Geometry ) const;
|
|
||||||
// material methods
|
// material methods
|
||||||
material_handle
|
material_handle Fetch_Material(std::string const &Filename, bool const Loadnow = true);
|
||||||
Fetch_Material( std::string const &Filename, bool const Loadnow = true );
|
void Bind_Material(material_handle const Material);
|
||||||
void
|
|
||||||
Bind_Material( material_handle const Material );
|
|
||||||
|
|
||||||
// shader methods
|
// shader methods
|
||||||
std::shared_ptr<gl::program> Fetch_Shader(std::string const &name);
|
std::shared_ptr<gl::program> Fetch_Shader(std::string const &name);
|
||||||
|
|
||||||
opengl_material const &
|
opengl_material const &Material(material_handle const Material) const;
|
||||||
Material( material_handle const Material ) const;
|
|
||||||
// texture methods
|
// texture methods
|
||||||
texture_handle
|
texture_handle Fetch_Texture(std::string const &Filename, bool const Loadnow = true);
|
||||||
Fetch_Texture( std::string const &Filename, bool const Loadnow = true );
|
void Bind_Texture(size_t Unit, texture_handle const Texture);
|
||||||
void
|
opengl_texture &Texture(texture_handle const Texture) const;
|
||||||
Bind_Texture( size_t Unit, texture_handle const Texture );
|
|
||||||
opengl_texture &
|
|
||||||
Texture( texture_handle const Texture ) const;
|
|
||||||
// utility methods
|
// utility methods
|
||||||
TSubModel const *
|
TSubModel const *Pick_Control() const
|
||||||
Pick_Control() const { return m_pickcontrolitem; }
|
{
|
||||||
scene::basic_node const *
|
return m_pickcontrolitem;
|
||||||
Pick_Node() const { return m_picksceneryitem; }
|
}
|
||||||
|
scene::basic_node const *Pick_Node() const
|
||||||
|
{
|
||||||
|
return m_picksceneryitem;
|
||||||
|
}
|
||||||
// maintenance methods
|
// maintenance methods
|
||||||
void
|
void Update(double const Deltatime);
|
||||||
Update( double const Deltatime );
|
TSubModel const *Update_Pick_Control();
|
||||||
TSubModel const *
|
scene::basic_node const *Update_Pick_Node();
|
||||||
Update_Pick_Control();
|
|
||||||
scene::basic_node const *
|
|
||||||
Update_Pick_Node();
|
|
||||||
// debug methods
|
// debug methods
|
||||||
std::string const &
|
std::string const &info_times() const;
|
||||||
info_times() const;
|
std::string const &info_stats() const;
|
||||||
std::string const &
|
|
||||||
info_stats() const;
|
|
||||||
|
|
||||||
// members
|
// members
|
||||||
GLenum static const sunlight{GL_LIGHT0};
|
GLenum static const sunlight{GL_LIGHT0};
|
||||||
@@ -193,7 +182,8 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
// types
|
// types
|
||||||
enum class rendermode {
|
enum class rendermode
|
||||||
|
{
|
||||||
none,
|
none,
|
||||||
color,
|
color,
|
||||||
shadows,
|
shadows,
|
||||||
@@ -203,7 +193,8 @@ private:
|
|||||||
pickscenery
|
pickscenery
|
||||||
};
|
};
|
||||||
|
|
||||||
struct debug_stats {
|
struct debug_stats
|
||||||
|
{
|
||||||
int dynamics{0};
|
int dynamics{0};
|
||||||
int models{0};
|
int models{0};
|
||||||
int submodels{0};
|
int submodels{0};
|
||||||
@@ -218,7 +209,8 @@ private:
|
|||||||
using distancecell_pair = std::pair<double, scene::basic_cell *>;
|
using distancecell_pair = std::pair<double, scene::basic_cell *>;
|
||||||
using cell_sequence = std::vector<distancecell_pair>;
|
using cell_sequence = std::vector<distancecell_pair>;
|
||||||
|
|
||||||
struct renderpass_config {
|
struct renderpass_config
|
||||||
|
{
|
||||||
|
|
||||||
opengl_camera camera;
|
opengl_camera camera;
|
||||||
rendermode draw_mode{rendermode::none};
|
rendermode draw_mode{rendermode::none};
|
||||||
@@ -228,76 +220,42 @@ private:
|
|||||||
typedef std::vector<opengl_light> opengllight_array;
|
typedef std::vector<opengl_light> opengllight_array;
|
||||||
|
|
||||||
// methods
|
// methods
|
||||||
bool
|
bool Init_caps();
|
||||||
Init_caps();
|
void setup_pass(renderpass_config &Config, rendermode const Mode, float const Znear = 0.f, float const Zfar = 1.f, bool const Ignoredebug = false);
|
||||||
void
|
void setup_matrices();
|
||||||
setup_pass( renderpass_config &Config, rendermode const Mode, float const Znear = 0.f, float const Zfar = 1.f, bool const Ignoredebug = false );
|
void setup_drawing(bool const Alpha = false);
|
||||||
void
|
void setup_shadow_map(opengl_texture *tex);
|
||||||
setup_matrices();
|
void setup_environment_light(TEnvironmentType const Environment = e_flat);
|
||||||
void
|
|
||||||
setup_drawing( bool const Alpha = false );
|
|
||||||
void
|
|
||||||
setup_shadow_map(opengl_texture *tex);
|
|
||||||
void
|
|
||||||
setup_environment_light( TEnvironmentType const Environment = e_flat );
|
|
||||||
// runs jobs needed to generate graphics for specified render pass
|
// runs jobs needed to generate graphics for specified render pass
|
||||||
void
|
void Render_pass(rendermode const Mode);
|
||||||
Render_pass( rendermode const Mode );
|
|
||||||
// creates dynamic environment cubemap
|
// creates dynamic environment cubemap
|
||||||
bool
|
bool Render_reflections();
|
||||||
Render_reflections();
|
bool Render(world_environment *Environment);
|
||||||
bool
|
void Render(scene::basic_region *Region);
|
||||||
Render( world_environment *Environment );
|
void Render(section_sequence::iterator First, section_sequence::iterator Last);
|
||||||
void
|
void Render(cell_sequence::iterator First, cell_sequence::iterator Last);
|
||||||
Render( scene::basic_region *Region );
|
void Render(scene::shape_node const &Shape, bool const Ignorerange);
|
||||||
void
|
void Render(TAnimModel *Instance);
|
||||||
Render( section_sequence::iterator First, section_sequence::iterator Last );
|
bool Render(TDynamicObject *Dynamic);
|
||||||
void
|
bool Render(TModel3d *Model, material_data const *Material, float const Squaredistance, Math3D::vector3 const &Position, Math3D::vector3 const &Angle);
|
||||||
Render( cell_sequence::iterator First, cell_sequence::iterator Last );
|
bool Render(TModel3d *Model, material_data const *Material, float const Squaredistance);
|
||||||
void
|
void Render(TSubModel *Submodel);
|
||||||
Render( scene::shape_node const &Shape, bool const Ignorerange );
|
void Render(TTrack *Track);
|
||||||
void
|
void Render(scene::basic_cell::path_sequence::const_iterator First, scene::basic_cell::path_sequence::const_iterator Last);
|
||||||
Render( TAnimModel *Instance );
|
bool Render_cab(TDynamicObject const *Dynamic, bool const Alpha = false);
|
||||||
bool
|
void Render(TMemCell *Memcell);
|
||||||
Render( TDynamicObject *Dynamic );
|
void Render_Alpha(scene::basic_region *Region);
|
||||||
bool
|
void Render_Alpha(cell_sequence::reverse_iterator First, cell_sequence::reverse_iterator Last);
|
||||||
Render( TModel3d *Model, material_data const *Material, float const Squaredistance, Math3D::vector3 const &Position, Math3D::vector3 const &Angle );
|
void Render_Alpha(TAnimModel *Instance);
|
||||||
bool
|
void Render_Alpha(TTraction *Traction);
|
||||||
Render( TModel3d *Model, material_data const *Material, float const Squaredistance );
|
void Render_Alpha(scene::lines_node const &Lines);
|
||||||
void
|
bool Render_Alpha(TDynamicObject *Dynamic);
|
||||||
Render( TSubModel *Submodel );
|
bool Render_Alpha(TModel3d *Model, material_data const *Material, float const Squaredistance, Math3D::vector3 const &Position, Math3D::vector3 const &Angle);
|
||||||
void
|
bool Render_Alpha(TModel3d *Model, material_data const *Material, float const Squaredistance);
|
||||||
Render( TTrack *Track );
|
void Render_Alpha(TSubModel *Submodel);
|
||||||
void
|
void Update_Lights(light_array &Lights);
|
||||||
Render( scene::basic_cell::path_sequence::const_iterator First, scene::basic_cell::path_sequence::const_iterator Last );
|
glm::vec3 pick_color(std::size_t const Index);
|
||||||
bool
|
std::size_t pick_index(glm::ivec3 const &Color);
|
||||||
Render_cab( TDynamicObject const *Dynamic, bool const Alpha = false );
|
|
||||||
void
|
|
||||||
Render( TMemCell *Memcell );
|
|
||||||
void
|
|
||||||
Render_Alpha( scene::basic_region *Region );
|
|
||||||
void
|
|
||||||
Render_Alpha( cell_sequence::reverse_iterator First, cell_sequence::reverse_iterator Last );
|
|
||||||
void
|
|
||||||
Render_Alpha( TAnimModel *Instance );
|
|
||||||
void
|
|
||||||
Render_Alpha( TTraction *Traction );
|
|
||||||
void
|
|
||||||
Render_Alpha( scene::lines_node const &Lines );
|
|
||||||
bool
|
|
||||||
Render_Alpha( TDynamicObject *Dynamic );
|
|
||||||
bool
|
|
||||||
Render_Alpha( TModel3d *Model, material_data const *Material, float const Squaredistance, Math3D::vector3 const &Position, Math3D::vector3 const &Angle );
|
|
||||||
bool
|
|
||||||
Render_Alpha( TModel3d *Model, material_data const *Material, float const Squaredistance );
|
|
||||||
void
|
|
||||||
Render_Alpha( TSubModel *Submodel );
|
|
||||||
void
|
|
||||||
Update_Lights( light_array &Lights );
|
|
||||||
glm::vec3
|
|
||||||
pick_color( std::size_t const Index );
|
|
||||||
std::size_t
|
|
||||||
pick_index( glm::ivec3 const &Color );
|
|
||||||
|
|
||||||
// members
|
// members
|
||||||
GLFWwindow *m_window{nullptr};
|
GLFWwindow *m_window{nullptr};
|
||||||
@@ -314,7 +272,6 @@ private:
|
|||||||
texture_handle m_suntexture{-1};
|
texture_handle m_suntexture{-1};
|
||||||
texture_handle m_moontexture{-1};
|
texture_handle m_moontexture{-1};
|
||||||
texture_handle m_reflectiontexture{-1};
|
texture_handle m_reflectiontexture{-1};
|
||||||
GLUquadricObj *m_quadric { nullptr }; // helper object for drawing debug mode scene elements
|
|
||||||
// TODO: refactor framebuffer stuff into an object
|
// TODO: refactor framebuffer stuff into an object
|
||||||
bool m_framebuffersupport{false};
|
bool m_framebuffersupport{false};
|
||||||
#ifdef EU07_USE_PICKING_FRAMEBUFFER
|
#ifdef EU07_USE_PICKING_FRAMEBUFFER
|
||||||
|
|||||||
Reference in New Issue
Block a user