2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // DPatch.cpp: implementation of the DPatch class.
22 //////////////////////////////////////////////////////////////////////
33 #include "./dialogs/dialogs-gtk.h"
35 //////////////////////////////////////////////////////////////////////
36 // Construction/Destruction
37 //////////////////////////////////////////////////////////////////////
39 // Added patch merging, wahey!
42 // problem is, you cant put patches into entities as yet :(
47 width = MIN_PATCH_WIDTH;
48 height = MIN_PATCH_HEIGHT;
57 void DPatch::SetTexture(const char *textureName)
59 strcpy(texture, textureName);
62 void CopyDrawVert(const drawVert_t* in, drawVert_t* out)
64 out->lightmap[0] = in->lightmap[0];
65 out->lightmap[1] = in->lightmap[1];
66 out->st[0] = in->st[0];
67 out->st[1] = in->st[1];
68 VectorCopy(in->normal, out->normal);
69 VectorCopy(in->xyz, out->xyz);
72 void DPatch::BuildInRadiant(scene::Node* entity)
74 NodeSmartReference node(GlobalPatchCreator().createPatch());
77 Node_getTraversable(*entity)->insert(node);
79 Node_getTraversable(GlobalRadiant().getMapWorldEntity())->insert(node);
82 int nIndex = g_FuncTable.m_pfnCreatePatchHandle();
83 //$ FIXME: m_pfnGetPatchHandle
84 patchMesh_t* pm = g_FuncTable.m_pfnGetPatchData(nIndex);
87 b->pPatch = Patch_Alloc();
88 b->pPatch->setDims(width,height);
90 for(int x = 0; x < height; x++)
92 for(int y = 0; y < width; y++)
94 float *p = b->pPatch->ctrlAt(ROW,x,y);
95 p[0] = points[x][y].xyz[0];
96 p[1] = points[x][y].xyz[1];
97 p[2] = points[x][y].xyz[2];
98 p[3] = points[x][y].st[0];
99 p[4] = points[x][y].st[1];
104 g_FuncTable.m_pfnCommitBrushHandleToEntity(QER_brush, entity);
106 g_FuncTable.m_pfnCommitBrushHandle(QER_brush);
108 for(int x = 0; x < width; x++)
109 for(int y = 0; y < height; y++)
110 CopyDrawVert(&points[x][y], &pm->ctrl[x][y]);
116 // strcpy(pm->d_texture->name, texture);
118 brush_t* brush = (brush_t*)g_FuncTable.m_pfnCreateBrushHandle();
119 brush->patchBrush = true;
122 pm->pSymbiot = brush;
123 pm->bSelected = false;
124 pm->bOverlay = false; // bleh, f*cks up, just have to wait for a proper function
125 pm->bDirty = true; // or get my own patch out....
128 g_FuncTable.m_pfnCommitBrushHandleToEntity(brush, entity);
130 else*/ // patch to entity just plain dont work atm
133 g_FuncTable.m_pfnCommitPatchHandleToEntity(nIndex, pm, texture, entity);
135 g_FuncTable.m_pfnCommitPatchHandleToMap(nIndex, pm, texture);
137 QER_brush = pm->pSymbiot;
141 void DPatch::LoadFromBrush(scene::Node& brush)
146 SetTexture(brush->pPatch->GetShader());
148 width = brush->pPatch->getWidth();
149 height = brush->pPatch->getHeight();
151 for(int x = 0; x < height; x++)
153 for(int y = 0; y < width; y++)
155 float *p = brush->pPatch->ctrlAt(ROW,x,y);
156 p[0] = points[x][y].xyz[0];
157 p[1] = points[x][y].xyz[1];
158 p[2] = points[x][y].xyz[2];
159 p[3] = points[x][y].st[0];
160 p[4] = points[x][y].st[1];
166 bool DPatch::ResetTextures(const char *oldTextureName, const char *newTextureName)
168 if( !oldTextureName || !strcmp(texture, oldTextureName))
170 strcpy(texture, newTextureName);
177 void Build1dArray(vec3_t* array, drawVert_t points[MAX_PATCH_WIDTH][MAX_PATCH_HEIGHT],
178 int startX, int startY, int number, bool horizontal, bool inverse)
180 int x = startX, y = startY, i, step;
187 for(i = 0; i < number; i++)
189 VectorCopy(points[x][y].xyz, array[i]);
198 void Print1dArray(vec3_t* array, int size)
200 for(int i = 0; i < size; i++)
201 globalOutputStream() << "(" << array[i][0] << " " << array[i][1] << " " << array[i][2] << ")\t";
202 globalOutputStream() << "\n";
205 bool Compare1dArrays(vec3_t* a1, vec3_t* a2, int size)
210 for(i = 0; i < size; i++)
212 if(!VectorCompare(a1[i], a2[size-i-1]))
221 patch_merge_t DPatch::IsMergable(DPatch *other)
224 vec3_t p1Array[4][MAX_PATCH_HEIGHT];
225 vec3_t p2Array[4][MAX_PATCH_HEIGHT];
230 patch_merge_t merge_info;
232 Build1dArray(p1Array[0], this->points, 0, 0, this->width, true, false);
233 Build1dArray(p1Array[1], this->points, this->width-1, 0, this->height, false, false);
234 Build1dArray(p1Array[2], this->points, this->width-1, this->height-1, this->width, true, true);
235 Build1dArray(p1Array[3], this->points, 0, this->height-1, this->height, false, true);
237 Build1dArray(p2Array[0], other->points, 0, 0, other->width, true, false);
238 Build1dArray(p2Array[1], other->points, other->width-1, 0, other->height, false, false);
239 Build1dArray(p2Array[2], other->points, other->width-1, other->height-1, other->width, true, true);
240 Build1dArray(p2Array[3], other->points, 0, other->height-1, other->height, false, true);
242 p1ArraySizes[0] = this->width;
243 p1ArraySizes[1] = this->height;
244 p1ArraySizes[2] = this->width;
245 p1ArraySizes[3] = this->height;
247 p2ArraySizes[0] = other->width;
248 p2ArraySizes[1] = other->height;
249 p2ArraySizes[2] = other->width;
250 p2ArraySizes[3] = other->height;
252 for(i = 0; i < 4; i++)
254 for(j = 0; j < 4; j++)
256 if(p1ArraySizes[i] == p2ArraySizes[j])
258 if(Compare1dArrays(p1Array[i], p2Array[j], p1ArraySizes[i]))
262 merge_info.mergable = true;
269 merge_info.mergable = false;
273 DPatch* DPatch::MergePatches(patch_merge_t merge_info, DPatch *p1, DPatch *p2)
275 while(merge_info.pos1 != 2)
279 if(merge_info.pos1 < 0)
280 merge_info.pos1 += 4;
283 while(merge_info.pos2 != 0)
287 if(merge_info.pos2 < 0)
288 merge_info.pos2 += 3;
291 int newHeight = p1->height + p2->height - 1;
292 if(newHeight > MAX_PATCH_HEIGHT)
295 DPatch* newPatch = new DPatch();
297 newPatch->height = newHeight;
298 newPatch->width = p1->width;
299 newPatch->SetTexture(p1->texture);
303 for(i = 0; i < p1->height; i++, y++)
304 for(int x = 0; x < p1->width; x++)
305 memcpy(&newPatch->points[x][y], &p1->points[x][i], sizeof(drawVert_t));
307 for(i = 1; i < p2->height; i++, y++)
308 for(int x = 0; x < p2->width; x++)
309 memcpy(&newPatch->points[x][y], &p2->points[x][i], sizeof(drawVert_t));
311 // newPatch->Invert();
316 void DPatch::Invert()
321 for(i = 0 ; i < width ; i++ )
323 for(j = 0; j < height / 2; j++)
325 memcpy(&vertTemp, &points[i][height - 1- j], sizeof (drawVert_t));
326 memcpy(&points[i][height - 1 - j], &points[i][j], sizeof(drawVert_t));
327 memcpy(&points[i][j], &vertTemp, sizeof(drawVert_t));
332 void DPatch::Transpose()
337 if ( width > height )
339 for ( i = 0 ; i < height ; i++ )
341 for ( j = i + 1 ; j < width ; j++ )
346 memcpy(&dv, &points[j][i], sizeof(drawVert_t));
347 memcpy(&points[j][i], &points[i][j], sizeof(drawVert_t));
348 memcpy(&points[i][j], &dv, sizeof(drawVert_t));
353 memcpy(&points[i][j], &points[j][i], sizeof(drawVert_t));
360 for ( i = 0 ; i < width ; i++ )
362 for ( j = i + 1 ; j < height ; j++ )
367 memcpy(&dv, &points[i][j], sizeof(drawVert_t));
368 memcpy(&points[i][j], &points[j][i], sizeof(drawVert_t));
369 memcpy(&points[j][i], &dv, sizeof(drawVert_t));
374 memcpy(&points[j][i], &points[i][j], sizeof(drawVert_t));
387 std::list<DPatch> DPatch::Split(bool rows, bool cols)
389 std::list<DPatch> patchList;
393 if(rows && height >= 5)
395 for(i = 0; i < (height-1)/2; i++)
401 p.SetTexture(texture);
403 for(y = 0; y < 3; y++)
405 for(x = 0; x < p.width; x++)
407 memcpy(&p.points[x][y], &points[x][(i*2)+y], sizeof(drawVert_t));
410 patchList.push_back(p);
413 if(cols && width >= 5)
415 std::list<DPatch> patchList2;
417 for(std::list<DPatch>::iterator patches = patchList.begin(); patches != patchList.end(); patches++)
419 std::list<DPatch> patchList3 = (*patches).Split(false, true);
421 for(std::list<DPatch>::iterator patches2 = patchList3.begin(); patches2 != patchList3.end(); patches2++)
422 patchList2.push_front(*patches2);
428 else if(cols && width >= 5)
430 for(i = 0; i < (width-1)/2; i++)
436 p.SetTexture(texture);
438 for(x = 0; x < 3; x++)
440 for(y = 0; y < p.height; y++)
442 memcpy(&p.points[x][y], &points[(i*2)+x][y], sizeof(drawVert_t));
446 patchList.push_back(p);