Merge branch 'gfx-work' into sim

This commit is contained in:
milek7
2019-08-08 15:56:06 +02:00
29 changed files with 1178 additions and 84 deletions

View File

@@ -66,6 +66,117 @@ void opengl_camera::draw(glm::vec3 const &Offset) const
// m7t port to core gl
}
std::vector<std::pair<glm::vec3, glm::vec2>> const billboard_vertices {
{ { -0.5f, -0.5f, 0.f }, { 0.f, 0.f } },
{ { 0.5f, -0.5f, 0.f }, { 1.f, 0.f } },
{ { 0.5f, 0.5f, 0.f }, { 1.f, 1.f } },
{ { -0.5f, -0.5f, 0.f }, { 0.f, 0.f } },
{ { -0.5f, 0.5f, 0.f }, { 0.f, 1.f } },
{ { 0.5f, 0.5f, 0.f }, { 1.f, 1.f } }
};
void
opengl_particles::update( opengl_camera const &Camera ) {
m_particlevertices.clear();
// build a list of visible smoke sources
// NOTE: arranged by distance to camera, if we ever need sorting and/or total amount cap-based culling
std::multimap<float, smoke_source const &> sources;
for( auto const &source : simulation::Particles.sequence() ) {
if( false == Camera.visible( source.area() ) ) { continue; }
// NOTE: the distance is negative when the camera is inside the source's bounding area
sources.emplace(
static_cast<float>( glm::length( Camera.position() - source.area().center ) - source.area().radius ),
source );
}
if( true == sources.empty() ) { return; }
// build billboard data for particles from visible sources
auto const camerarotation { glm::mat3( Camera.modelview() ) };
particle_vertex vertex;
for( auto const &source : sources ) {
auto const &particles { source.second.sequence() };
// TODO: put sanity cap on the overall amount of particles that can be drawn
auto const sizestep { 256.0 * billboard_vertices.size() };
m_particlevertices.reserve(
sizestep * std::ceil( m_particlevertices.size() + ( particles.size() * billboard_vertices.size() ) / sizestep ) );
for( auto const &particle : particles ) {
// TODO: particle color support
vertex.color.r =
vertex.color.g =
vertex.color.b = Global.fLuminance * 0.125f;
vertex.color.a = std::clamp(particle.opacity, 0.0f, 1.0f);
auto const offset { glm::vec3{ particle.position - Camera.position() } };
auto const rotation { glm::angleAxis( particle.rotation, glm::vec3{ 0.f, 0.f, 1.f } ) };
for( auto const &billboardvertex : billboard_vertices ) {
vertex.position = offset + ( rotation * billboardvertex.first * particle.size ) * camerarotation;
vertex.texture = billboardvertex.second;
m_particlevertices.emplace_back( vertex );
}
}
}
// ship the billboard data to the gpu:
// make sure we have enough room...
if( m_buffercapacity < m_particlevertices.size() ) {
m_buffercapacity = m_particlevertices.size();
if (!m_buffer)
m_buffer.emplace();
m_buffer->allocate(gl::buffer::ARRAY_BUFFER,
m_buffercapacity * sizeof(particle_vertex), GL_STREAM_DRAW);
}
if (m_buffer) {
// ...send the data...
m_buffer->upload(gl::buffer::ARRAY_BUFFER,
m_particlevertices.data(), 0, m_particlevertices.size() * sizeof(particle_vertex));
}
}
void
opengl_particles::render() {
if( m_buffercapacity == 0 ) { return; }
if( m_particlevertices.empty() ) { return; }
if (!m_vao) {
m_vao.emplace();
m_vao->setup_attrib(*m_buffer, 0, 3, GL_FLOAT, sizeof(particle_vertex), 0);
m_vao->setup_attrib(*m_buffer, 1, 4, GL_FLOAT, sizeof(particle_vertex), 12);
m_vao->setup_attrib(*m_buffer, 2, 2, GL_FLOAT, sizeof(particle_vertex), 28);
m_buffer->unbind(gl::buffer::ARRAY_BUFFER);
m_vao->unbind();
}
if (!m_shader) {
gl::shader vert("smoke.vert");
gl::shader frag("smoke.frag");
gl::program *prog = new gl::program({vert, frag});
m_shader = std::unique_ptr<gl::program>(prog);
}
m_buffer->bind(gl::buffer::ARRAY_BUFFER);
m_shader->bind();
m_vao->bind();
glDrawArrays(GL_TRIANGLES, 0, m_particlevertices.size());
m_shader->unbind();
m_vao->unbind();
m_buffer->unbind(gl::buffer::ARRAY_BUFFER);
}
bool opengl_renderer::Init(GLFWwindow *Window)
{
if (!Init_caps())
@@ -105,6 +216,7 @@ bool opengl_renderer::Init(GLFWwindow *Window)
m_glaretexture = Fetch_Texture("fx/lightglare");
m_suntexture = Fetch_Texture("fx/sun");
m_moontexture = Fetch_Texture("fx/moon");
m_smoketexture = Fetch_Texture("fx/smoke");
WriteLog("...gfx data pre-loading done");
// prepare basic geometry chunks
@@ -342,6 +454,7 @@ bool opengl_renderer::init_viewport(viewport_config &vp)
if (!Global.gfx_usegles)
glEnable(GL_PROGRAM_POINT_SIZE);
if (!gl::vao::use_vao)
{
GLuint v;
glGenVertexArrays(1, &v);
@@ -668,6 +781,9 @@ void opengl_renderer::Render_pass(viewport_config &vp, rendermode const Mode)
setup_drawing(true);
Render_Alpha(simulation::Region);
// particles
Render_particles();
// precipitation; done at end, only before cab render
Render_precipitation();
@@ -2929,9 +3045,23 @@ void opengl_renderer::Render(TMemCell *Memcell)
::glPopMatrix();
}
void opengl_renderer::Render_particles()
{
// momentarily disable depth write, to allow vehicle cab drawn afterwards to mask it instead of leaving it 'inside'
glDepthMask(GL_FALSE);
model_ubs.set_modelview(OpenGLMatrices.data(GL_MODELVIEW));
model_ubo->update(model_ubs);
Bind_Texture(0, m_smoketexture);
m_particlerenderer.update(m_renderpass.pass_camera);
m_particlerenderer.render();
glDepthMask(GL_TRUE);
}
void opengl_renderer::Render_precipitation()
{
if (Global.Overcast <= 1.f)
{
return;
@@ -2939,7 +3069,10 @@ void opengl_renderer::Render_precipitation()
::glPushMatrix();
// tilt the precipitation cone against the velocity vector for crude motion blur
auto const velocity{simulation::Environment.m_precipitation.m_cameramove * -1.0};
// include current wind vector while at it
auto const velocity {
simulation::Environment.m_precipitation.m_cameramove * -1.0
+ glm::dvec3{ simulation::Environment.wind() } * 0.5 };
if (glm::length2(velocity) > 0.0)
{
auto const forward{glm::normalize(velocity)};