Blame view

legacy/rts_glOBJ.cpp 10.7 KB
f1402849   dmayerich   renewed commit
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  #include "rts_glOBJ.h"

  #include "gl\glut.h"

  

  OBJint rts_glOBJ::f_RenderTexCoord(vertex_texture &texcoord)

  {

  	if(texcoord.mask & OBJ_VT_W)

  		glTexCoord3f(texcoord.u, texcoord.v, texcoord.w);

  	else if(texcoord.mask & OBJ_VT_V)

  		glTexCoord2f(texcoord.u, texcoord.v);

  	else

  		glTexCoord1f(texcoord.u);

  	return OBJ_OK;

  }

  

  OBJint rts_glOBJ::f_RenderNormal(vertex_normal &normal)

  {

  	if(normal.mask & OBJ_VN_K)

  		glNormal3f(normal.i, normal.j, normal.k);

  	else

  		return OBJ_ERROR;

  

  	return OBJ_OK;

  

  }

  

  OBJint rts_glOBJ::f_RenderVertex(vertex_position &v)

  {

  	if(v.mask & OBJ_V_W)

  		glVertex4f(v.x, v.y, v.z, v.w);

  	else if(v.mask & OBJ_V_Z)

  		glVertex3f(v.x, v.y, v.z);

  	else if(v.mask & OBJ_V_Y)

  		glVertex2f(v.x, v.y);

  	else

  		//glVertex1f(v.x);

  		return OBJ_ERROR;

  

  	return OBJ_OK;

  }

  

  inline void rts_glOBJ::f_RenderPointList(unsigned int id)

  {

  	glBegin(GL_POINTS);

  	unsigned int num_vertices = primitives[id].p.size();

  	for(unsigned int v = 0; v<num_vertices; v++)

  	{

  		f_RenderVertex(v_list[primitives[id].p[v].v]);

  	}

  	glEnd();

  }

  

  inline void rts_glOBJ::f_RenderLineStrip(unsigned int id)

  {

  	//render each line as a line strip

  	glBegin(GL_LINE_STRIP);

  	unsigned int num_vertices = primitives[id].p.size();

  	for(unsigned int v=0; v<num_vertices; v++)

  	{

  		f_RenderVertex(v_list[primitives[id].p[v].v]);

  	}

  

  	//end rendering

  	glEnd();

  

  }

  

  inline void rts_glOBJ::f_RenderFace(unsigned int id)

  {

  	//start the proper render mode

  	if(primitives[id].p.size() > 4)

  		glBegin(GL_POLYGON);

  	else if(primitives[id].p.size() == 4)

  		glBegin(GL_QUADS);

  	else if(primitives[id].p.size() == 3)

  		glBegin(GL_TRIANGLES);

  	else

  		return;

  

  	//now run through each vertex

  	unsigned int num_vertices = primitives[id].p.size();

  	for(unsigned int v=0; v<num_vertices; v++)

  	{

  		f_RenderVertex(v_list[primitives[id].p[v].v]);

  	}

  

  	//end rendering

  	glEnd();

  

  }

  

  OBJint rts_glOBJ::f_RenderPrimitive(unsigned int i)

  {

  	switch(primitives[i].type)

  	{

  	case OBJ_POINTS:

  		f_RenderPointList(i);

  		break;

  	case OBJ_LINES:

  		f_RenderLineStrip(i);

  		break;

  	case OBJ_FACE:

  		f_RenderFace(i);

  		break;

  	}

  

  	return OBJ_OK;

  

  }

  

  OBJint rts_glOBJ::f_CreateDisplayList()

  {

  	/*This function creates a display list representing the OBJ.

  	*/

  	

  	//create the display list

  	if(glIsList(dl))	//if the list already exists

  		glDeleteLists(dl, 1);

  	//create a new display list

  	dl = glGenLists(1);

  

  	//start the list

  	glNewList(dl, GL_COMPILE);

  

  	//draw the OBJ

  	unsigned int prim = primitives.size();

  	for(unsigned int i=0; i<prim; i++)

  		f_RenderPrimitive(i);

  

  	//finish the list

  	glEndList();

  

  	dl_valid = true;	//the display list is now valid

  

  	return OBJ_OK;

  }

  

  OBJint rts_glOBJ::glRender()

  {

  	//create the display list if necessary

  	if(!dl_valid)

  		f_CreateDisplayList();

  

  	//call the display list

  	glCallList(dl);

  

  	return OBJ_OK;

  }

  

  OBJint rts_glOBJ::glRenderSelected()

  {

  	if(is_selected)

  	{

  		list<unsigned int>::iterator p;

  		for(p = selected_primitives.begin(); p!=selected_primitives.end(); p++)

  			f_RenderPrimitive(*p);

  	}

  	return OBJ_OK;

  }

  

  

  void rts_glOBJ::SelectPrimitive(unsigned int primitive)

  {

  	//adds a primitive to the selection list

  	selected_primitives.push_back(primitive);

  	selected_primitives.unique();

  }

  

  void rts_glOBJ::UnselectPrimitive(unsigned int primitive)

  {

  	selected_primitives.remove(primitive);

  }

  

  void rts_glOBJ::SelectMultipleHits(GLuint* buffer, unsigned int num_hits)

  {

  	GLuint* hit_entry = buffer;

  	GLuint num_names;

  	GLuint nearest_name;

  	GLuint minZ = 0xffffffff;

  	

  	//push the hits into the selection buffer

  	for(int h=0; h<num_hits; h++)

  	{

  		num_names = hit_entry[0];

  		SelectPrimitive(hit_entry[3]);

  	

  		hit_entry = hit_entry + 3 + num_names;

  	}

  

  }

  

  void rts_glOBJ::UnselectMultipleHits(GLuint* buffer, unsigned int num_hits)

  {

  	GLuint* hit_entry = buffer;

  	GLuint num_names;

  	GLuint nearest_name;

  	GLuint minZ = 0xffffffff;

  	

  	//push the hits into the selection buffer

  	for(int h=0; h<num_hits; h++)

  	{

  		num_names = hit_entry[0];

  		UnselectPrimitive(hit_entry[3]);

  	

  		hit_entry = hit_entry + 3 + num_names;

  	}

  

  }

  

  void rts_glOBJ::SelectClosestHit(GLuint* buffer, unsigned int num_hits)

  {

  	GLuint* hit_entry = buffer;

  	GLuint num_names;

  	GLuint nearest_name;

  	GLuint minZ = 0xffffffff;

  	

  	//push the hits into the selection buffer

  	for(int h=0; h<num_hits; h++)

  	{

  		num_names = hit_entry[0];

  		if(hit_entry[1] < minZ)

  		{

  			nearest_name = hit_entry[3];

  			minZ = hit_entry[1];

  		}		

  		hit_entry = hit_entry + 3 + num_names;

  

  	}

  	SelectPrimitive(nearest_name);

  

  }

  

  void rts_glOBJ::UnselectClosestHit(GLuint* buffer, unsigned int num_hits)

  {

  	GLuint* hit_entry = buffer;

  	GLuint num_names;

  	GLuint nearest_name;

  	GLuint minZ = 0xffffffff;

  	

  	//push the hits into the selection buffer

  	for(int h=0; h<num_hits; h++)

  	{

  		num_names = hit_entry[0];

  		if(hit_entry[1] < minZ)

  		{

  			nearest_name = hit_entry[3];

  			minZ = hit_entry[1];

  		}		

  		hit_entry = hit_entry + 3 + num_names;

  

  	}

  	UnselectPrimitive(nearest_name);

  

  }

  

  

  void rts_glOBJ::Pick(unsigned int x, unsigned int y, unsigned int size_x, unsigned int size_y,

  					 unsigned int picktype)

  {

  	//initialize selection buffer properties

  	GLuint selectBuf[BUFSIZE];

  	GLint hits;

  	GLint viewport[4];		//storage space for viewport data

  

  	//get the viewport information

  	glGetIntegerv(GL_VIEWPORT, viewport);

  	

  	//set the selection buffer

  	glSelectBuffer(BUFSIZE, selectBuf);

  

  	//set the render mode to selection

  	glRenderMode(GL_SELECT);

  

  	//we pick the object with the assumption that the active projection

  	//matrix is the one used to display the object in the viewport

  

  	//get the current projection matrix

  	float projection[16];

  	glGetFloatv(GL_PROJECTION_MATRIX, projection);

  

  	//save and reset the current projection matrix

  	glMatrixMode(GL_PROJECTION);

  	glPushMatrix();

  	glLoadIdentity();

  

  	//set the picking matrix using GLU

  	gluPickMatrix(x, viewport[3] - y, size_x, size_y, viewport);

  	//multiply in the saved projection matrix

  	glMultMatrixf(projection);

  

  	//Draw the object with names for each primitive.

  

  	int num_prim = primitives.size();	//get the number of primitives

  	glInitNames();						//set the name stack to zero

  	for(int p = 0; p < num_prim; p++)

  	{

  		glPushName(p);				//push the name on to the stack

  		f_RenderPrimitive(p);			//render the primitive

  		glPopName();

  	}

  

  	glFinish();				//force the rendering to complete	

  

  	hits = glRenderMode(GL_RENDER);		//find the number of hits

  

  	if(hits)					//if there is a hit, select the OBJ and the primitive

  	{

  		is_selected = true;		//select the object

  

  		//if this is a new selection, clear the list

  		if(picktype == RTS_GLOBJ_SINGLE_NEW_SELECTION || 

  		   picktype == RTS_GLOBJ_MULTI_NEW_SELECTION)

  			selected_primitives.clear();

  

  		//if this is adding multiple hits to the selection list

  		if(picktype == RTS_GLOBJ_MULTI_NEW_SELECTION ||

  		   picktype == RTS_GLOBJ_MULTI_ADD_SELECTION)

  			SelectMultipleHits(selectBuf, hits);

  

  		if(picktype == RTS_GLOBJ_SINGLE_NEW_SELECTION ||

  		   picktype == RTS_GLOBJ_SINGLE_ADD_SELECTION)

  			SelectClosestHit(selectBuf, hits);

  

  		if(picktype == RTS_GLOBJ_SINGLE_SUB_SELECTION)

  			UnselectClosestHit(selectBuf, hits);

  

  		if(picktype == RTS_GLOBJ_MULTI_SUB_SELECTION)

  			UnselectMultipleHits(selectBuf, hits);

  		//SelectPrimitive(processHits(selectBuf, hits), picktype);

  		//selected_primitives.clear();

  		

  		//select the primitive

  		//selected_primitives.push_back(processHits(selectBuf, hits));

  	}

  	else

  		is_selected = false;

  

  	//restore the previous projection matrix

  	glMatrixMode(GL_PROJECTION);

  	glPopMatrix();

  

  }

  rts_glOBJ rts_glOBJ::GetSelected()

  {

  	//right now this creates duplicate vertices

  	

  	rts_glOBJ result;

  

  	//for each primitive

  	list<unsigned int>::iterator p;

  	for(p = selected_primitives.begin(); p != selected_primitives.end(); p++)

  	{

  		primitive selected_prim = primitives[(*p)];

  		//create a new primitive

  		primitive new_prim;

  		new_prim.mask = selected_prim.mask;

  		new_prim.type = selected_prim.type;

  

  		//for each vertex in the primitive

  		int num_vertices = selected_prim.p.size();

  		for(int v = 0; v<num_vertices; v++)

  		{

  			vertex new_vert;

  			if(new_prim.mask & OBJ_V)	//if the primitive has a vertex coord

  			{

  				new_vert.v = result.v_list.size();

  				new_prim.p.push_back(new_vert);

  				result.v_list.push_back(v_list[selected_prim.p[v].v]);				

  			}

  		}

  		result.primitives.push_back(new_prim);

  	}

  

  	return result;

  

  

  }

  

  void rts_glOBJ::ExpandSelection()

  {

  	/*

  	//create a list of all selected vertices

  	list<unsigned int> selected_v;

  

  	list<unsigned int>::iterator p;	//selected primitive

  	vector<vertex>::iterator v;		//vertex

  

  	//insert all selected vertex indices into selected_v

  	for(p = selected_primitives.begin(); p!=selected_primitives.end(); p++)

  	{

  		for(v=primitives[(*p)].p.begin(); v!=primitives[(*p)].p.end(); v++)

  		{

  			selected_v.push_back((*v).v);

  		}

  	}

  	selected_v.unique();	//remove redundant entries

  

  	//now find all primitives connected to these vertices

  	int num_primitives = primitives.size();

  	int prim;

  	list<unsigned int>::iterator s_v;

  	for(prim =0; prim<num_primitives; prim++)

  	{

  		for(v=primitives[prim].p.begin(); v!=primitives[prim].p.end(); v++)

  		{

  			for(s_v = selected_v.begin(); s_v!=selected_v.end(); s_v++)

  			{

  				if((*s_v) == (*v).v)

  				{

  					selected_primitives.push_back(prim);

  				}

  			}

  		}

  	}

  		selected_primitives.unique();

  

  	*/

  	

  

  	//create the vertex-primitive list

  	vector<unsigned int>* vertex_to_prim = new vector<unsigned int>[v_list.size()];

  	int num_primitives = primitives.size();

  	int p;

  	int num_vertices, v;

  	int vertex;

  	for(p=0; p<num_primitives; p++)

  	{

  		num_vertices = primitives[p].p.size();

  		for(v = 0; v<num_vertices; v++)

  		{

  			vertex = primitives[p].p[v].v;

  			vertex_to_prim[vertex].push_back(p);

  		}

  	}

  

  	//create the list of selected vertices

  	list<unsigned int> selected_verts;

  	list<unsigned int>::iterator sel;

  	for(sel = selected_primitives.begin(); sel != selected_primitives.end(); sel++)

  	{

  		num_vertices = primitives[(*sel)].p.size();

  		for(v=0; v<num_vertices; v++)

  		{

  			selected_verts.push_back(primitives[(*sel)].p[v].v);

  		}

  	}

  	selected_verts.unique();

  

  	//erase the selected array

  	selected_primitives.clear();

  	list<unsigned int>::iterator s_v;

  	for(s_v = selected_verts.begin(); s_v != selected_verts.end(); s_v++)

  	{

  		num_primitives = vertex_to_prim[(*s_v)].size();

  		for(p=0; p<num_primitives; p++)

  			selected_primitives.push_back(vertex_to_prim[(*s_v)][p]);

  	}

  	cout<<"Before: "<<selected_primitives.size()<<endl;

  

  	selected_primitives.sort();

  	selected_primitives.unique();

  	cout<<"After: "<<selected_primitives.size()<<endl;

  	

  

  

  }