-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathModelCache.cs
171 lines (135 loc) · 5.18 KB
/
ModelCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Copyright 2012 Kuribo64
This file is part of SM64DSe.
SM64DSe is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
SM64DSe is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with SM64DSe. If not, see http://www.gnu.org/licenses/.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace SM64DSe
{
static class ModelCache
{
public static BMD GetModel(string name)
{
if (Program.m_ROM == null)
{
return null;
}
if (m_Models.ContainsKey(name))
{
CachedModel found = m_Models[name];
found.m_References++;
return found.m_Model;
}
NitroFile mdfile = Program.m_ROM.GetFileFromName(name);
if (mdfile == null)
return null;
BMD model = new BMD(mdfile);
model.PrepareToRender();
CachedModel cmdl = new CachedModel();
cmdl.m_Model = model;
cmdl.m_DisplayLists = null;
cmdl.m_References = 1;
m_Models.Add(name, cmdl);
return model;
}
public static int[] GetDisplayLists(BMD model)
{
if (model == null || !m_Models.ContainsKey(model.m_FileName))
return null;
CachedModel cmdl = m_Models[model.m_FileName];
if (cmdl.m_DisplayLists != null)
return cmdl.m_DisplayLists;
int[] dl = new int[3];
bool keep = false;
dl[0] = GL.GenLists(1);
GL.NewList(dl[0], ListMode.Compile);
keep = cmdl.m_Model.Render(RenderMode.Opaque, 1f);
GL.EndList();
if (!keep) { GL.DeleteLists(dl[0], 1); dl[0] = 0; }
dl[1] = GL.GenLists(1);
GL.NewList(dl[1], ListMode.Compile);
keep = cmdl.m_Model.Render(RenderMode.Translucent, 1f);
GL.EndList();
if (!keep) { GL.DeleteLists(dl[1], 1); dl[1] = 0; }
dl[2] = GL.GenLists(1);
GL.NewList(dl[2], ListMode.Compile);
keep = cmdl.m_Model.Render(RenderMode.Picking, 1f);
GL.EndList();
if (!keep) { GL.DeleteLists(dl[2], 1); dl[2] = 0; }
cmdl.m_DisplayLists = dl;
return dl;
}
public static int[] GetDisplayLists(BMD model,BCA animation, int frame)
{
if (model == null || !m_Models.ContainsKey(model.m_FileName))
return null;
CachedModel cmdl = m_Models[model.m_FileName];
if (cmdl.m_DisplayLists != null)
return cmdl.m_DisplayLists;
int[] dl = new int[3];
bool keep = false;
dl[0] = GL.GenLists(1);
GL.NewList(dl[0], ListMode.Compile);
keep = cmdl.m_Model.Render(RenderMode.Opaque, 1f, animation, frame);
GL.EndList();
if (!keep) { GL.DeleteLists(dl[0], 1); dl[0] = 0; }
dl[1] = GL.GenLists(1);
GL.NewList(dl[1], ListMode.Compile);
keep = cmdl.m_Model.Render(RenderMode.Translucent, 1f, animation, frame);
GL.EndList();
if (!keep) { GL.DeleteLists(dl[1], 1); dl[1] = 0; }
dl[2] = GL.GenLists(1);
GL.NewList(dl[2], ListMode.Compile);
keep = cmdl.m_Model.Render(RenderMode.Picking, 1f, animation, frame);
GL.EndList();
if (!keep) { GL.DeleteLists(dl[2], 1); dl[2] = 0; }
cmdl.m_DisplayLists = dl;
return dl;
}
public static void RemoveModel(BMD model)
{
if (!m_Models.ContainsKey(model.m_FileName))
return;
RemoveModel(model.m_FileName);
}
public static void RemoveModel(string modelName)
{
if (!m_Models.ContainsKey(modelName))
return;
CachedModel cmdl = m_Models[modelName];
cmdl.m_References--;
if (cmdl.m_References > 0)
return;
if (cmdl.m_DisplayLists != null)
{
GL.DeleteLists(cmdl.m_DisplayLists[0], 1);
GL.DeleteLists(cmdl.m_DisplayLists[1], 1);
GL.DeleteLists(cmdl.m_DisplayLists[2], 1);
cmdl.m_DisplayLists = null;
}
cmdl.m_Model.Release();
m_Models.Remove(modelName);
}
private class CachedModel
{
public BMD m_Model;
public int[] m_DisplayLists;
public int m_References;
}
private static Dictionary<string, CachedModel> m_Models = new Dictionary<string, CachedModel>();
}
}