]> git.tdb.fi Git - libs/gl.git/blob - source/object.cpp
Allow copying of Uniforms and ProgramData
[libs/gl.git] / source / object.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/datafile/collection.h>
9 #include <msp/strings/formatter.h>
10 #include "except.h"
11 #include "material.h"
12 #include "mesh.h"
13 #include "object.h"
14 #include "objectinstance.h"
15 #include "objectpass.h"
16 #include "program.h"
17 #include "programdata.h"
18 #include "technique.h"
19 #include "texture.h"
20 #include "texunit.h"
21
22 using namespace std;
23
24 namespace Msp {
25 namespace GL {
26
27 Object::Object():
28         meshes(1),
29         own_technique(false),
30         technique(0)
31 { }
32
33 Object::~Object()
34 {
35         if(own_technique)
36                 delete technique;
37 }
38
39 void Object::render(const Tag &tag) const
40 {
41         const RenderPass *pass=get_pass(tag);
42         if(!pass)
43                 return;
44
45         Bind bind(*pass);
46         meshes[0]->draw();
47 }
48
49 void Object::render(const ObjectInstance &inst, const Tag &tag) const
50 {
51         const RenderPass *pass=get_pass(tag);
52         if(!pass)
53                 return;
54
55         Bind bind(*pass);
56         render_instance(inst, tag);
57         meshes[0]->draw();
58 }
59
60 const RenderPass *Object::get_pass(const Tag &tag) const
61 {
62         if(!technique->has_pass(tag))
63                 return 0;
64         return &technique->get_pass(tag);
65 }
66
67 void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const
68 {
69         inst.setup_render(tag);
70         unsigned lod=min<unsigned>(inst.get_level_of_detail(), meshes.size()-1);
71         meshes[lod]->draw();
72         inst.finish_render(tag);
73 }
74
75
76 Object::Loader::Loader(Object &o, Collection &c):
77         DataFile::CollectionObjectLoader<Object>(o, &c)
78 {
79         add("lod_mesh", &Loader::lod_mesh);
80         add("mesh",     &Loader::mesh);
81         add("technique", &Loader::technique);
82         add("technique", &Object::technique);
83 }
84
85 void Object::Loader::lod_mesh(unsigned l, const string &n)
86 {
87         obj.meshes.resize(l+1, 0);
88         obj.meshes[l]=get_collection().get<Mesh>(n);
89 }
90
91 void Object::Loader::mesh(const string &n)
92 {
93         obj.meshes[0]=get_collection().get<Mesh>(n);
94 }
95
96 void Object::Loader::technique()
97 {
98         RefPtr<Technique> tch=new Technique;
99         load_sub(*tch, get_collection());
100         obj.technique=tch.release();
101         obj.own_technique=true;
102 }
103
104 } // namespace GL
105 } // namespace Msp