1 | // Copyright (C) 2011 Kornel Kisielewicz
|
---|
2 | // This file is part of NV Libraries.
|
---|
3 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
4 |
|
---|
5 | #include "nv/gfx/skeletal_mesh.hh"
|
---|
6 |
|
---|
7 | #include <glm/gtc/matrix_access.hpp>
|
---|
8 | #include <glm/gtx/matrix_interpolation.hpp>
|
---|
9 | #include "nv/interface/context.hh"
|
---|
10 | #include "nv/interface/device.hh"
|
---|
11 |
|
---|
12 |
|
---|
13 | nv::skeletal_mesh::skeletal_mesh( context* a_context, md5_loader* a_loader )
|
---|
14 | : animated_mesh()
|
---|
15 | , m_data( a_loader )
|
---|
16 | , m_animation( nullptr )
|
---|
17 | {
|
---|
18 | nv::uint32 vcount = a_loader->get_vertex_count(0);
|
---|
19 | m_va = a_context->get_device()->create_vertex_array();
|
---|
20 |
|
---|
21 | m_vb_position = a_context->get_device()->create_vertex_buffer( nv::STREAM_DRAW, vcount * sizeof( nv::vec3 ), (const void*)a_loader->get_positions(0).data() );
|
---|
22 | m_va->add_vertex_buffer( nv::slot::POSITION, m_vb_position, nv::FLOAT, 3, 0, 0, false );
|
---|
23 | m_vb_normal = a_context->get_device()->create_vertex_buffer( nv::STREAM_DRAW, vcount * sizeof( nv::vec3 ), (const void*)a_loader->get_normals(0).data() );
|
---|
24 | m_va->add_vertex_buffer( nv::slot::NORMAL, m_vb_normal, nv::FLOAT, 3, 0, 0, false );
|
---|
25 | m_vb_tangent = a_context->get_device()->create_vertex_buffer( nv::STREAM_DRAW, vcount * sizeof( nv::vec3 ), (const void*)a_loader->get_tangents(0).data() );
|
---|
26 | m_va->add_vertex_buffer( nv::slot::TANGENT, m_vb_tangent, nv::FLOAT, 3, 0, 0, false );
|
---|
27 |
|
---|
28 | nv::vertex_buffer* vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, vcount * sizeof( nv::vec2 ), (const void*)a_loader->get_texcoords(0).data() );
|
---|
29 | m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
|
---|
30 | nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, a_loader->get_index_count(0) * sizeof( nv::uint32 ), (const void*)a_loader->get_indices(0).data() );
|
---|
31 | m_va->set_index_buffer( ib, nv::UINT, true );
|
---|
32 | }
|
---|
33 |
|
---|
34 | void nv::skeletal_mesh::setup_animation( md5_animation* a_anim )
|
---|
35 | {
|
---|
36 | if ( m_animation ) m_animation->reset_animation();
|
---|
37 | m_animation = a_anim;
|
---|
38 | // TODO : INSTANCE!
|
---|
39 | m_animation->reset_animation();
|
---|
40 | }
|
---|
41 |
|
---|
42 | void nv::skeletal_mesh::update( uint32 ms )
|
---|
43 | {
|
---|
44 | if (m_animation != nullptr)
|
---|
45 | {
|
---|
46 | m_animation->update( ms * 0.001f );
|
---|
47 | m_data->apply( *m_animation );
|
---|
48 | }
|
---|
49 |
|
---|
50 | nv::uint32 usize = m_data->get_vertex_count(0) * sizeof( nv::vec3 );
|
---|
51 | m_vb_position->bind();
|
---|
52 | m_vb_position->update( (const void*)m_data->get_positions(0).data(), 0, usize );
|
---|
53 | m_vb_normal ->bind();
|
---|
54 | m_vb_normal ->update( (const void*)m_data->get_normals(0).data(), 0, usize );
|
---|
55 | m_vb_tangent ->bind();
|
---|
56 | m_vb_tangent ->update( (const void*)m_data->get_tangents(0).data(), 0, usize );
|
---|
57 |
|
---|
58 | // Technically this is not needed, because the va is just a fake class,
|
---|
59 | // but if it's real it will be needed?
|
---|
60 | // m_va->update_vertex_buffer( nv::slot::POSITION, m_vb_position, false );
|
---|
61 | // m_va->update_vertex_buffer( nv::slot::NORMAL, m_vb_normal, false );
|
---|
62 | // m_va->update_vertex_buffer( nv::slot::TANGENT, m_vb_tangent, false );
|
---|
63 | // TODO: answer is - probably not
|
---|
64 | }
|
---|
65 |
|
---|
66 | nv::skeletal_mesh::~skeletal_mesh()
|
---|
67 | {
|
---|
68 | delete m_va;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void nv::skeletal_mesh::run_animation( animation_entry* a_anim )
|
---|
72 | {
|
---|
73 | skeletal_animation_entry * anim = down_cast<skeletal_animation_entry>(a_anim);
|
---|
74 | // TODO : INSTANCE!
|
---|
75 | setup_animation( anim->m_animation );
|
---|
76 | }
|
---|