source: trunk/src/gfx/skeletal_mesh.cc @ 227

Last change on this file since 227 was 227, checked in by epyon, 11 years ago
  • skeletal mesh support
File size: 3.1 KB
RevLine 
[227]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
13nv::skeletal_mesh::skeletal_mesh( context* a_context, program* a_program, md5_loader* a_loader )
14        : m_context( a_context )
15        , m_program( a_program )
16        , m_va( nullptr )
17        , m_data( a_loader )
18        , m_animation( nullptr )
19{
20        nv::uint32 vcount = a_loader->get_vertex_count(0);
21        m_va = a_context->get_device()->create_vertex_array();
22
23        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() );
24        m_va->add_vertex_buffer( nv::slot::POSITION, m_vb_position, nv::FLOAT, 3, 0, 0, false );
25        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()  );
26        m_va->add_vertex_buffer( nv::slot::NORMAL,   m_vb_normal, nv::FLOAT, 3, 0, 0, false );
27        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() );
28        m_va->add_vertex_buffer( nv::slot::TANGENT,  m_vb_tangent, nv::FLOAT, 3, 0, 0, false );
29
30        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() );
31        m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
32        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() );
33        m_va->set_index_buffer( ib, nv::UINT, true );
34}
35
36void nv::skeletal_mesh::setup_animation( md5_animation* a_anim )
37{
38        if ( m_animation ) m_animation->reset_animation();
39        m_animation = a_anim;
40        m_animation->reset_animation();
41}
42
43void nv::skeletal_mesh::update( uint32 ms )
44{
45        if (m_animation != nullptr)
46        {
47                m_animation->update( ms * 0.001f );
48                m_data->apply( *m_animation );
49        }
50
51        nv::uint32 usize = m_data->get_vertex_count(0) * sizeof( nv::vec3 );
52        m_vb_position->bind();
53        m_vb_position->update( (const void*)m_data->get_positions(0).data(), 0, usize );
54        m_vb_normal  ->bind();
55        m_vb_normal  ->update( (const void*)m_data->get_normals(0).data(),   0, usize );
56        m_vb_tangent ->bind();
57        m_vb_tangent ->update( (const void*)m_data->get_tangents(0).data(),  0, usize );
58
59        // Technically this is not needed, because the va is just a fake class,
60        // but if it's real it will be needed?
61        m_va->update_vertex_buffer( nv::slot::POSITION, m_vb_position, false );
62        m_va->update_vertex_buffer( nv::slot::NORMAL,   m_vb_normal,   false );
63        m_va->update_vertex_buffer( nv::slot::TANGENT,  m_vb_tangent,  false );
64}
65
66void nv::skeletal_mesh::draw( render_state& rstate )
67{
68        m_context->draw( nv::TRIANGLES, rstate, m_program, m_va, m_data->get_index_count(0) );
69}
70
71nv::skeletal_mesh::~skeletal_mesh()
72{
73        delete m_va;
74}
Note: See TracBrowser for help on using the repository browser.