Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RAS_ListRasterizer.cpp « RAS_OpenGLRasterizer « Rasterizer « gameengine « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1dbd6f0a7fc5c29737b0b65ce4c75c63f71dfe5 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_ListRasterizer.cpp
 *  \ingroup bgerastogl
 */

#include <iostream>

#include "RAS_ListRasterizer.h"

#ifdef WIN32
#include <windows.h>
#endif // WIN32

#include "GL/glew.h"

#include "RAS_MaterialBucket.h"
#include "RAS_TexVert.h"
#include "MT_assert.h"

//#if defined(DEBUG)
//#ifdef WIN32
//#define spit(x) std::cout << x << std::endl;
//#endif //WIN32
//#else
#define spit(x)
//#endif

RAS_ListSlot::RAS_ListSlot(RAS_ListRasterizer* rasty)
:	KX_ListSlot(),
	m_list(0),
	m_flag(LIST_MODIFY|LIST_CREATE),
	m_matnr(0),
	m_rasty(rasty)
{
}

int RAS_ListSlot::Release()
{
	if (--m_refcount > 0)
		return m_refcount;
	m_rasty->RemoveListSlot(this);
	delete this;
	return 0;
}

RAS_ListSlot::~RAS_ListSlot()
{
	RemoveList();
}


void RAS_ListSlot::RemoveList()
{
	if (m_list != 0) {
		spit("Releasing display list (" << m_list << ")");
		glDeleteLists((GLuint)m_list, 1);
		m_list =0;
	}
}

void RAS_ListSlot::DrawList()
{
	if (m_flag &LIST_STREAM || m_flag& LIST_NOCREATE) {
		RemoveList();
		return;
	}
	if (m_flag &LIST_MODIFY) {
		if (m_flag &LIST_CREATE) {
			if (m_list == 0) {
				m_list = (unsigned int)glGenLists(1);
				m_flag =  m_flag &~ LIST_CREATE;
				spit("Created display list (" << m_list << ")");
			}
		}
		if (m_list != 0)
			glNewList((GLuint)m_list, GL_COMPILE);
	
		m_flag |= LIST_BEGIN;
		return;
	}
	glCallList(m_list);
}

void RAS_ListSlot::EndList()
{
	if (m_flag & LIST_BEGIN) {
		glEndList();
		m_flag = m_flag &~(LIST_BEGIN|LIST_MODIFY);
		m_flag |= LIST_END;
		glCallList(m_list);
	}
}

void RAS_ListSlot::SetModified(bool mod)
{
	if (mod && !(m_flag & LIST_MODIFY)) {
		spit("Modifying list (" << m_list << ")");
		m_flag = m_flag &~ LIST_END;
		m_flag |= LIST_STREAM;
	}
}

bool RAS_ListSlot::End()
{
	return (m_flag &LIST_END)!=0;
}



RAS_ListRasterizer::RAS_ListRasterizer(RAS_ICanvas* canvas, bool lock, int storage)
:	RAS_OpenGLRasterizer(canvas, storage)
{
}

RAS_ListRasterizer::~RAS_ListRasterizer() 
{
	ReleaseAlloc();
}

void RAS_ListRasterizer::RemoveListSlot(RAS_ListSlot* list)
{
	if (list->m_flag & LIST_DERIVEDMESH) {
		RAS_DerivedMeshLists::iterator it = mDerivedMeshLists.begin();
		while (it != mDerivedMeshLists.end()) {
			RAS_ListSlots *slots = it->second;
			if (slots->size() > list->m_matnr && slots->at(list->m_matnr) == list) {
				(*slots)[list->m_matnr] = NULL;
				// check if all slots are NULL and if yes, delete the entry
				int i;
				for (i=slots->size(); i-- > 0; ) {
					if (slots->at(i) != NULL)
						break;
				}
				if (i < 0) {
					slots->clear();
					delete slots;
					mDerivedMeshLists.erase(it);
				}
				break;
			}
			++it;
		}
	} else {
		RAS_ArrayLists::iterator it = mArrayLists.begin();
		while (it != mArrayLists.end()) {
			if (it->second == list) {
				mArrayLists.erase(it);
				break;
			}
			it++;
		}
	}
}

RAS_ListSlot* RAS_ListRasterizer::FindOrAdd(RAS_MeshSlot& ms)
{
	/*
	 * Keep a copy of constant lists submitted for rendering,
	 * this guards against (replicated)new...delete every frame,
	 * and we can reuse lists!
	 * :: sorted by mesh slot
	 */
	RAS_ListSlot* localSlot = (RAS_ListSlot*)ms.m_DisplayList;
	if (!localSlot) {
		if (ms.m_pDerivedMesh) {
			// that means that we draw based on derived mesh, a display list is possible
			// Note that we come here only for static derived mesh
			int matnr = ms.m_bucket->GetPolyMaterial()->GetMaterialIndex();
			RAS_ListSlot* nullSlot = NULL;
			RAS_ListSlots *listVector;
			RAS_DerivedMeshLists::iterator it = mDerivedMeshLists.find(ms.m_pDerivedMesh);
			if (it == mDerivedMeshLists.end()) {
				listVector = new RAS_ListSlots(matnr+4, nullSlot);
				localSlot = new RAS_ListSlot(this);
				localSlot->m_flag |= LIST_DERIVEDMESH;
				localSlot->m_matnr = matnr;
				listVector->at(matnr) = localSlot;
				mDerivedMeshLists.insert(std::pair<DerivedMesh*, RAS_ListSlots*>(ms.m_pDerivedMesh, listVector));
			} else {
				listVector = it->second;
				if (listVector->size() <= matnr)
					listVector->resize(matnr+4, nullSlot);
				if ((localSlot = listVector->at(matnr)) == NULL) {
					localSlot = new RAS_ListSlot(this);
					localSlot->m_flag |= LIST_DERIVEDMESH;
					localSlot->m_matnr = matnr;
					listVector->at(matnr) = localSlot;
				} else {
					localSlot->AddRef();
				}
			}
		} else {
			RAS_ArrayLists::iterator it = mArrayLists.find(ms.m_displayArrays);
			if (it == mArrayLists.end()) {
				localSlot = new RAS_ListSlot(this);
				mArrayLists.insert(std::pair<RAS_DisplayArrayList, RAS_ListSlot*>(ms.m_displayArrays, localSlot));
			} else {
				localSlot = static_cast<RAS_ListSlot*>(it->second->AddRef());
			}
		}
	}
	MT_assert(localSlot);
	return localSlot;
}

void RAS_ListRasterizer::ReleaseAlloc()
{
	for (RAS_ArrayLists::iterator it = mArrayLists.begin();it != mArrayLists.end();++it)
		delete it->second;
	mArrayLists.clear();
	for (RAS_DerivedMeshLists::iterator it = mDerivedMeshLists.begin();it != mDerivedMeshLists.end();++it) {
		RAS_ListSlots* slots = it->second;
		for (int i=slots->size(); i-- > 0; ) {
			RAS_ListSlot* slot = slots->at(i);
			if (slot)
				delete slot;
		}
		slots->clear();
		delete slots;
	}
	mDerivedMeshLists.clear();
}

void RAS_ListRasterizer::IndexPrimitives(RAS_MeshSlot& ms)
{
	RAS_ListSlot* localSlot =0;

	if (ms.m_bDisplayList) {
		localSlot = FindOrAdd(ms);
		localSlot->DrawList();
		if (localSlot->End()) {
			// save slot here too, needed for replicas and object using same mesh
			// => they have the same vertexarray but different mesh slot
			ms.m_DisplayList = localSlot;
			return;
		}
	}
	
	RAS_OpenGLRasterizer::IndexPrimitives(ms);

	if (ms.m_bDisplayList) {
		localSlot->EndList();
		ms.m_DisplayList = localSlot;
	}
}


void RAS_ListRasterizer::IndexPrimitivesMulti(RAS_MeshSlot& ms)
{
	RAS_ListSlot* localSlot =0;

	if (ms.m_bDisplayList) {
		localSlot = FindOrAdd(ms);
		localSlot->DrawList();

		if (localSlot->End()) {
			// save slot here too, needed for replicas and object using same mesh
			// => they have the same vertexarray but different mesh slot
			ms.m_DisplayList = localSlot;
			return;
		}
	}

	RAS_OpenGLRasterizer::IndexPrimitivesMulti(ms);

	if (ms.m_bDisplayList) {
		localSlot->EndList();
		ms.m_DisplayList = localSlot;
	}
}

bool RAS_ListRasterizer::Init(void)
{
	return RAS_OpenGLRasterizer::Init();
}

void RAS_ListRasterizer::SetDrawingMode(int drawingmode)
{
	RAS_OpenGLRasterizer::SetDrawingMode(drawingmode);
}

void RAS_ListRasterizer::Exit()
{
	RAS_OpenGLRasterizer::Exit();
}

// eof