Changeset 2931


Ignore:
Timestamp:
Sep 15, 2013, 8:32:23 AM (6 years ago)
Author:
benlitz
Message:

mesh: submesh class working, can be rendered with any compatible shader without caring of attributes

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gpu/shader.cpp

    r2922 r2931  
    402402}
    403403
     404int Shader::GetAttribCount() const
     405{
     406    return data->attrib_locations.Count();
     407}
     408
    404409ShaderAttrib Shader::GetAttribLocation(VertexUsage usage, int index) const
    405410{
  • trunk/src/lol/gpu/shader.h

    r2922 r2931  
    158158    static void Destroy(Shader *shader);
    159159
     160    int GetAttribCount() const;
    160161    ShaderAttrib GetAttribLocation(struct VertexUsage usage, int index) const;
    161162
  • trunk/src/mesh/mesh.cpp

    r2816 r2931  
    2121{
    2222
     23/*
     24 * Mesh class
     25 */
     26
    2327Mesh::Mesh()
    2428{
     
    3135void Mesh::Render(mat4 const &model)
    3236{
    33     for (int i = 0; i < m_submeshes.Count(); ++i)
    34         m_submeshes[i]->Render(model);
     37    //for (int i = 0; i < m_submeshes.Count(); ++i)
     38    //    m_submeshes[i]->Render(model);
    3539}
    3640
    37 SubMesh::SubMesh()
     41/*
     42 * SubMesh class
     43 */
     44
     45SubMesh::SubMesh(lol::VertexDeclaration* vdecl)
     46    : m_mesh_prim(MeshPrimitive::Triangles)
    3847{
     48    m_vdecl = vdecl;
     49    m_vbos = new VertexBuffer*[vdecl->GetStreamCount()];
     50    memset(m_vbos, 0, sizeof(VertexBuffer*) * vdecl->GetStreamCount());
     51    m_vertex_count = -1;
    3952}
    4053
    4154SubMesh::~SubMesh()
    4255{
     56    // TODO: cleanup
    4357}
    4458
    45 void SubMesh::Render(mat4 const &model)
     59void SubMesh::SetMeshPrimitive(MeshPrimitive mesh_primitive)
    4660{
    47     /* FIXME: TODO */
     61    m_mesh_prim = mesh_primitive;
     62}
     63
     64void SubMesh::SetVertexBuffer(int index, VertexBuffer* vbo)
     65{
     66    m_vbos[index] = vbo;
     67    if (m_vertex_count < 0)
     68        m_vertex_count = vbo->GetSize() / m_vdecl->GetStream(index).GetSize();
     69}
     70
     71void SubMesh::AddTexture(const char* name, Texture* texture)
     72{
     73    m_textures.Push(String(name), texture);
     74}
     75
     76void SubMesh::Render(Shader* shader)
     77{
     78    ShaderAttrib attribs[12];
     79
     80    int c = 0;
     81    for (int i = 0; i < m_vdecl->GetStreamCount(); ++i)
     82    {
     83        ShaderAttrib attribs[12];
     84
     85        if (m_vbos[i] == nullptr)
     86        {
     87            Log::Error("trying to render a mesh with a null vbo");
     88            return;
     89        }
     90
     91        int indices[VertexUsage::Max];
     92        memset(indices, 0, sizeof(int) * VertexUsage::Max);
     93
     94        VertexStreamBase stream = m_vdecl->GetStream(i);
     95        for (int j = 0; j < stream.GetStreamCount(); ++j)
     96        {
     97            VertexUsage usage = stream.GetUsage(j);
     98            attribs[j] = shader->GetAttribLocation(usage, indices[usage]++);
     99        }
     100
     101        m_vdecl->SetStream(m_vbos[i], attribs);
     102    }
     103
     104    for (int i = 0; i < m_textures.Count(); ++i)
     105    {
     106        // TODO: might be good to cache this
     107        ShaderUniform uniform = shader->GetUniformLocation(m_textures[i].m1.C());
     108        shader->SetUniform(uniform, m_textures[i].m2->GetTexture(), i);
     109    }
     110
     111    m_vdecl->Bind();
     112    m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, m_vertex_count);
     113    m_vdecl->Unbind();
    48114}
    49115
  • trunk/src/mesh/mesh.h

    r2816 r2931  
    2323{
    2424public:
    25     SubMesh();
     25    SubMesh(VertexDeclaration* vdecl);
    2626    ~SubMesh();
    2727
    28     void Render(mat4 const &model);
     28    void SetMeshPrimitive(MeshPrimitive mesh_primitive);
     29    void SetVertexBuffer(int index, VertexBuffer* vbo);
     30    void AddTexture(const char* name, Texture* texture);
    2931
    30 private:
    31     Array<vec3> m_vertices;
    32     Array<vec2> m_uvs;
    33     Array<vec3> m_normals;
     32    void Render(Shader* shader);
     33
     34protected:
     35    VertexDeclaration* m_vdecl;
     36    MeshPrimitive m_mesh_prim;
     37    VertexBuffer** m_vbos;
     38    int m_vertex_count;
     39
     40    Array<String, Texture*> m_textures;
    3441};
    3542
Note: See TracChangeset for help on using the changeset viewer.