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

RAS_StorageIM.cpp « RAS_OpenGLRasterizer « Rasterizer « gameengine « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 80a1c4ddb5b82c159e38e05673d03a9b3bc7e49a (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * ***** 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.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include "RAS_StorageIM.h"

#include "GPU_compatibility.h"
#include "GPU_draw.h"
#include "GPU_extensions.h"
#include "GPU_material.h"

#include "DNA_meshdata_types.h"

#include "BKE_DerivedMesh.h"

RAS_StorageIM::RAS_StorageIM(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib) :
	m_texco_num(texco_num),
	m_texco(texco),
	m_attrib_num(attrib_num),
	m_attrib(attrib)
{
}
RAS_StorageIM::~RAS_StorageIM()
{
}

bool RAS_StorageIM::Init()
{
	return true;
}
void RAS_StorageIM::Exit()
{
}

void RAS_StorageIM::IndexPrimitives(RAS_MeshSlot& ms)
{
	IndexPrimitivesInternal(ms, false);
}

void RAS_StorageIM::IndexPrimitivesMulti(class RAS_MeshSlot& ms)
{
	IndexPrimitivesInternal(ms, true);
}

void RAS_StorageIM::TexCoord(const RAS_TexVert &tv)
{
	int unit;

	for (unit = 0; unit < *m_texco_num; unit++) {
		switch(m_texco[unit]) {
			case RAS_IRasterizer::RAS_TEXCO_ORCO:
			case RAS_IRasterizer::RAS_TEXCO_GLOB:
				gpuMultiTexCoord3fv(unit, tv.getXYZ());
				break;
			case RAS_IRasterizer::RAS_TEXCO_UV:
				gpuMultiTexCoord2fv(unit, tv.getUV(unit));
				break;
			case RAS_IRasterizer::RAS_TEXCO_NORM:
				gpuMultiTexCoord3fv(unit, tv.getNormal());
				break;
			case RAS_IRasterizer::RAS_TEXTANGENT:
				gpuMultiTexCoord4fv(unit, tv.getTangent());
				break;
			default:
				break;
		}
	}

	if (*m_attrib_num > 0) {
		int uv = 0;

		int index_f  = 0;
		int index_ub = 0;

		for (unit = 0; unit < *m_attrib_num; unit++) {
			switch(m_attrib[unit]) {
				case RAS_IRasterizer::RAS_TEXCO_ORCO:
				case RAS_IRasterizer::RAS_TEXCO_GLOB:
					gpuVertexAttrib3fv(index_f++, tv.getXYZ());
					break;
				case RAS_IRasterizer::RAS_TEXCO_UV:
					gpuVertexAttrib2fv(index_f++, tv.getUV(uv++));
					break;
				case RAS_IRasterizer::RAS_TEXCO_NORM:
					gpuVertexAttrib3fv(index_f++, tv.getNormal());
					break;
				case RAS_IRasterizer::RAS_TEXTANGENT:
					gpuVertexAttrib4fv(index_f++, tv.getTangent());
					break;
				case RAS_IRasterizer::RAS_TEXCO_VCOL:
					gpuVertexAttrib4ubv(index_ub++, tv.getRGBA());
					break;
				default:
					break;
			}
		}
	}
}

void RAS_StorageIM::SetCullFace(bool enable)
{
	if (enable)
		glEnable(GL_CULL_FACE);
	else
		glDisable(GL_CULL_FACE);
}

static bool current_wireframe;
static RAS_MaterialBucket *current_bucket;
static RAS_IPolyMaterial *current_polymat;
static RAS_MeshSlot *current_ms;
static RAS_MeshObject *current_mesh;
static int current_blmat_nr;
static GPUVertexAttribs current_gpu_attribs;
static Image *current_image;
static int CheckMaterialDM(int matnr, void *attribs)
{
	// only draw the current material
	if (matnr != current_blmat_nr)
		return 0;
	GPUVertexAttribs *gattribs = (GPUVertexAttribs *)attribs;
	if (gattribs)
		memcpy(gattribs, &current_gpu_attribs, sizeof(GPUVertexAttribs));
	return 1;
}

/*
static int CheckTexfaceDM(void *mcol, int index)
{

	// index is the original face index, retrieve the polygon
	RAS_Polygon* polygon = (index >= 0 && index < current_mesh->NumPolygons()) ?
		current_mesh->GetPolygon(index) : NULL;
	if (polygon && polygon->GetMaterial() == current_bucket) {
		// must handle color.
		if (current_wireframe)
			return 2;
		if (current_ms->m_bObjectColor) {
			MT_Vector4& rgba = current_ms->m_RGBAcolor;
			gpuCurrentColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
			// don't use mcol
			return 2;
		}
		if (!mcol) {
			// we have to set the color from the material
			unsigned char rgba[4];
			current_polymat->GetMaterialRGBAColor(rgba);
			gpuCurrentColor4ubv((const GLubyte *)rgba);
			return 2;
		}
		return 1;
	}
	return 0;
}
*/

static DMDrawOption CheckTexDM(MTFace *tface, int has_mcol, int matnr)
{
	// index is the original face index, retrieve the polygon
	if (matnr == current_blmat_nr && (tface == NULL || tface->tpage == current_image)) {
		// must handle color.
		if (current_wireframe) {
			return DM_DRAW_OPTION_NO_MCOL;
		}
		else if (current_ms->m_bObjectColor) {
			MT_Vector4& rgba = current_ms->m_RGBAcolor;
			gpuColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
			// don't use mcol

			return DM_DRAW_OPTION_NO_MCOL;
		}
		else if (!has_mcol) {
			// we have to set the color from the material
			unsigned char rgba[4];
			current_polymat->GetMaterialRGBAColor(rgba);
			gpuColor4ubv((const GLubyte *)rgba);

			return DM_DRAW_OPTION_NO_MCOL;
		}
		else {
			return DM_DRAW_OPTION_NORMALLY;
		}
	}
	else {
		return DM_DRAW_OPTION_SKIP;
	}
}

void RAS_StorageIM::IndexPrimitivesInternal(RAS_MeshSlot& ms, bool multi)
{ 
	bool obcolor = ms.m_bObjectColor;
	bool wireframe = m_drawingmode <= RAS_IRasterizer::KX_WIREFRAME;
	MT_Vector4& rgba = ms.m_RGBAcolor;
	RAS_MeshSlot::iterator it;

	if (ms.m_pDerivedMesh) {
		// mesh data is in derived mesh, 
		current_bucket = ms.m_bucket;
		current_polymat = current_bucket->GetPolyMaterial();
		current_ms = &ms;
		current_mesh = ms.m_mesh;
		current_wireframe = wireframe;
		// MCol *mcol = (MCol*)ms.m_pDerivedMesh->getFaceDataArray(ms.m_pDerivedMesh, CD_MCOL); /* UNUSED */

		// handle two-side
		if (current_polymat->GetDrawingMode() & RAS_IRasterizer::KX_BACKCULL)
			this->SetCullFace(true);
		else
			this->SetCullFace(false);

		if (current_polymat->GetFlag() & RAS_BLENDERGLSL) {
			// GetMaterialIndex return the original mface material index, 
			// increment by 1 to match what derived mesh is doing
			current_blmat_nr = current_polymat->GetMaterialIndex()+1;
			// For GLSL we need to retrieve the GPU material attribute
			Material* blmat = current_polymat->GetBlenderMaterial();
			Scene* blscene = current_polymat->GetBlenderScene();
			if (!wireframe && blscene && blmat)
				GPU_material_vertex_attributes(GPU_material_from_blender(blscene, blmat), &current_gpu_attribs);
			else
				memset(&current_gpu_attribs, 0, sizeof(current_gpu_attribs));
			// DM draw can mess up blending mode, restore at the end
			int current_blend_mode = GPU_get_material_alpha_blend();
			ms.m_pDerivedMesh->drawFacesGLSL(ms.m_pDerivedMesh, CheckMaterialDM);
			GPU_set_material_alpha_blend(current_blend_mode);
		} else {
			//ms.m_pDerivedMesh->drawMappedFacesTex(ms.m_pDerivedMesh, CheckTexfaceDM, mcol);
			current_blmat_nr = current_polymat->GetMaterialIndex();
			current_image = current_polymat->GetBlenderImage();
			ms.m_pDerivedMesh->drawFacesTex(ms.m_pDerivedMesh, CheckTexDM, NULL, NULL);
		}
		return;
	}

	{
		static const GLenum tex[16] = {
			GL_TEXTURE0,  GL_TEXTURE1,  GL_TEXTURE2,  GL_TEXTURE3,
			GL_TEXTURE4,  GL_TEXTURE5,  GL_TEXTURE6,  GL_TEXTURE7,
			GL_TEXTURE8,  GL_TEXTURE9,  GL_TEXTURE10, GL_TEXTURE11,
			GL_TEXTURE12, GL_TEXTURE13, GL_TEXTURE14, GL_TEXTURE15,
		};

		GLint texSize[16];

		GLenum attrib_f[16];
		GLint attribSize_f[16];
		GLint count_f = 0;
		GLenum attrib_ub[16];
		GLint attribSize_ub[16];
		GLint count_ub = 0;

		int unit;

		GPU_ASSERT(*m_texco_num  <= 16);
		GPU_ASSERT(*m_attrib_num <= 16);

		gpuImmediateElementSizes(3, 3, 4);

		for (unit = 0; unit < *m_texco_num; unit++) {
			switch(m_texco[unit]) {
				case RAS_IRasterizer::RAS_TEXCO_ORCO:
				case RAS_IRasterizer::RAS_TEXCO_GLOB:
					texSize[unit] = 3;
					break;
				case RAS_IRasterizer::RAS_TEXCO_UV:
					texSize[unit] = 2;
					break;
				case RAS_IRasterizer::RAS_TEXCO_NORM:
					texSize[unit] = 3;
					break;
				case RAS_IRasterizer::RAS_TEXTANGENT:
					texSize[unit] = 4;
					break;
				default:
					break;
			}
		}

		gpuImmediateTextureUnitCount(*m_texco_num);
		gpuImmediateTexCoordSizes(texSize);
		gpuImmediateTextureUnitMap(tex);

		for (unit = 0; unit < *m_attrib_num; unit++) {
			switch(m_attrib[unit]) {
				case RAS_IRasterizer::RAS_TEXCO_ORCO:
				case RAS_IRasterizer::RAS_TEXCO_GLOB:
					attrib_f[count_f] = unit;
					attribSize_f[count_f] = 3;
					count_f++;
					break;
				case RAS_IRasterizer::RAS_TEXCO_UV:
					attrib_f[count_f] = unit;
					attribSize_f[count_f] = 2;
					count_f++;
					break;
				case RAS_IRasterizer::RAS_TEXCO_NORM:
					attrib_f[count_f] = unit;
					attribSize_f[count_f] = 3;
					count_f++;
					break;
				case RAS_IRasterizer::RAS_TEXTANGENT:
					attrib_f[count_f] = unit;
					attribSize_f[count_f] = 4;
					count_f++;
					break;
				case RAS_IRasterizer::RAS_TEXCO_VCOL:
					attrib_ub[count_ub] = unit;
					attribSize_ub[count_ub] = 4;
					count_ub++;
					break;
				default:
					break;
			}
		}

		gpuImmediateFloatAttribCount(count_f);
		gpuImmediateFloatAttribSizes(attribSize_f);
		gpuImmediateFloatAttribIndexMap(attrib_f);

		gpuImmediateUbyteAttribCount(count_ub);
		gpuImmediateUbyteAttribSizes(attribSize_ub);
		gpuImmediateUbyteAttribIndexMap(attrib_ub);
	}

	gpuImmediateLock();

	// iterate over display arrays, each containing an index + vertex array
	for (ms.begin(it); !ms.end(it); ms.next(it)) {
		RAS_TexVert *vertex;
		size_t i, j, numvert;
		
		numvert = it.array->m_type;

		if (it.array->m_type == RAS_DisplayArray::LINE) {
			// line drawing
			gpuBegin(GL_LINES);

			for (i = 0; i < it.totindex; i += 2)
			{
				vertex = &it.vertex[it.index[i]];
				gpuVertex3fv(vertex->getXYZ());

				vertex = &it.vertex[it.index[i+1]];
				gpuVertex3fv(vertex->getXYZ());
			}

			gpuEnd();
		}
		else {
			// triangle and quad drawing
			if (it.array->m_type == RAS_DisplayArray::TRIANGLE)
				gpuBegin(GL_TRIANGLES);
			else
				gpuBegin(GL_QUADS);

			for (i = 0; i < it.totindex; i += numvert)
			{
				if (obcolor)
					gpuColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);

				for (j = 0; j < numvert; j++) {
					vertex = &it.vertex[it.index[i+j]];

					if (!wireframe) {
						if (!obcolor)
							gpuColor4ubv((const GLubyte *)(vertex->getRGBA()));

						gpuNormal3fv(vertex->getNormal());

						if (multi)
							TexCoord(*vertex);
						else
							gpuTexCoord2fv(vertex->getUV(0));
					}

					gpuVertex3fv(vertex->getXYZ());
				}
			}

			gpuEnd();
		}
	}

	gpuImmediateUnlock();
}