!B (Sandbox) (CE-21795) Importing models with multisubmaterials via fbx switches...
[CRYENGINE.git] / Code / Sandbox / Plugins / LodGeneratorPlugin / panel / AutoUVBaseType.cpp
bloba00c8a67b366d3738cd823a4af3ed68939d16120
1 #include "StdAfx.h"
2 #include "AutoUVBaseType.h"
4 namespace LODGenerator
6 SAutoUVTri::SAutoUVTri(Vec2 &a, Vec2 &b, Vec2 &c, Vec3 &ap, Vec3 &bp, Vec3 &cp, SAutoUVVertex *va, SAutoUVVertex *vb, SAutoUVVertex *vc)
8 v[0]=a;
9 v[1]=b;
10 v[2]=c;
11 pos[0]=ap;
12 pos[1]=bp;
13 pos[2]=cp;
14 orig[0]=va;
15 orig[1]=vb;
16 orig[2]=vc;
19 SAutoUVSquare::SAutoUVSquare(int _w, int _h, float _mx, float _my, int polyStart, int polyEnd)
21 w=_w;
22 h=_h;
23 s=polyStart;
24 e=polyEnd;
25 mx=_mx;
26 my=_my;
29 bool SAutoUVSquare::operator <(const SAutoUVSquare &rhs) const
31 int area=w*h;
32 int areaRHS=rhs.w*rhs.h;
33 return area>areaRHS;
36 void SAutoUVSquare::Rotate()
38 int tmp=w;
39 w=h;
40 h=tmp;
43 void SAutoUVSquare::RotateTris(std::vector<SAutoUVTri> &tris)
45 float newMx=(tris.begin()+s)->v[0].x;
46 float newMy=(tris.begin()+s)->v[0].y;
47 for (std::vector<SAutoUVTri>::iterator tit=tris.begin()+s, tend=tris.begin()+e; tit!=tend; ++tit)
49 tit->v[0]=tit->v[0].rot90cw();
50 tit->v[1]=tit->v[1].rot90cw();
51 tit->v[2]=tit->v[2].rot90cw();
52 if (tit->v[0].y<newMy)
53 newMy=tit->v[0].y;
54 if (tit->v[1].y<newMy)
55 newMy=tit->v[1].y;
56 if (tit->v[2].y<newMy)
57 newMy=tit->v[2].y;
58 if (tit->v[0].x<newMx)
59 newMx=tit->v[0].x;
60 if (tit->v[1].x<newMx)
61 newMx=tit->v[1].x;
62 if (tit->v[2].x<newMx)
63 newMx=tit->v[2].x;
65 mx=newMx;
66 my=newMy;
69 SAutoUVExpandingRasteriser::SAutoUVExpandingRasteriser(float resolution)
71 m_min[0]=m_min[1]=m_max[0]=m_max[1]=0;
72 m_resolution=1.0f/resolution;
73 m_pixels=NULL;
74 m_bInitialised=false;
77 SAutoUVExpandingRasteriser::~SAutoUVExpandingRasteriser()
79 if (m_pixels)
80 free(m_pixels);
83 void SAutoUVExpandingRasteriser::ExpandToFitVertices(int bbMin[2], int bbMax[2])
85 if (bbMin[0]<m_min[0] || bbMin[1]<m_min[1] || bbMax[0]>m_max[0] || bbMax[1]>m_max[1])
87 int newMin[2], newMax[2];
88 if (!m_bInitialised)
90 m_min[0]=m_max[0]=bbMin[0];
91 m_min[1]=m_max[1]=bbMin[1];
92 m_bInitialised=true;
94 newMin[0]=min(bbMin[0],m_min[0]);
95 newMin[1]=min(bbMin[1],m_min[1]);
96 newMax[0]=max(bbMax[0],m_max[0]);
97 newMax[1]=max(bbMax[1],m_max[1]);
98 int newSize=(newMax[0]-newMin[0])*(newMax[1]-newMin[1]);
99 char *newPixels=(char*)malloc(newSize);
100 memset(newPixels, 0, newSize);
101 for (int y=0; y<newMax[1]-newMin[1]; y++)
103 if (y>=m_min[1]-newMin[1] && y<m_max[1]-newMin[1])
104 memcpy(&newPixels[y*(newMax[0]-newMin[0])+m_min[0]-newMin[0]], &m_pixels[(y-(m_min[1]-newMin[1]))*(m_max[0]-m_min[0])], m_max[0]-m_min[0]);
106 if (m_pixels)
107 free(m_pixels);
108 m_pixels=newPixels;
109 m_min[0]=newMin[0];
110 m_min[1]=newMin[1];
111 m_max[0]=newMax[0];
112 m_max[1]=newMax[1];
116 bool SAutoUVExpandingRasteriser::RayTest(Vec2 &a, Vec2 &b, Vec2 c, int x, int y)
118 Vec2 p(x/m_resolution, y/m_resolution);
119 if ((b.x-a.x)*(p.y-a.y)-(b.y-a.y)*(p.x-a.x)<0.0f)
120 return false;
121 if ((c.x-b.x)*(p.y-b.y)-(c.y-b.y)*(p.x-b.x)<0.0f)
122 return false;
123 if ((a.x-c.x)*(p.y-c.y)-(a.y-c.y)*(p.x-c.x)<0.0f)
124 return false;
125 return true;
128 bool SAutoUVExpandingRasteriser::RasteriseTriangle(Vec2 &a, Vec2 &b, Vec2 &c, bool bWrite)
130 int bbMin[2];
131 int bbMax[2];
133 bbMin[0]=(int)floorf(a.x*m_resolution);
134 bbMin[1]=(int)floorf(a.y*m_resolution);
135 bbMax[0]=(int)ceilf(a.x*m_resolution);
136 bbMax[1]=(int)ceilf(a.y*m_resolution);
137 for (int i=0; i<2; i++)
139 Vec2 *v=(i==0)?&b:&c;
140 bbMin[0]=min(bbMin[0], (int)floorf(v->x*m_resolution));
141 bbMin[1]=min(bbMin[1], (int)floorf(v->y*m_resolution));
142 bbMax[0]=max(bbMax[0], (int)ceilf(v->x*m_resolution));
143 bbMax[1]=max(bbMax[1], (int)ceilf(v->y*m_resolution));
145 if (bWrite)
147 ExpandToFitVertices(bbMin, bbMax);
149 bbMin[0]=max(m_min[0], bbMin[0]);
150 bbMin[1]=max(m_min[1], bbMin[1]);
151 bbMax[0]=min(m_max[0], bbMax[0]);
152 bbMax[1]=min(m_max[1], bbMax[1]);
153 char ret=0;
154 for (int y=bbMin[1]; y<bbMax[1]; y++)
156 for (int x=bbMin[0]; x<bbMax[0]; x++)
158 if (RayTest(a,b,c,x,y))
160 char *pix=&m_pixels[(y-m_min[1])*(m_max[0]-m_min[0])+x-m_min[0]];
161 ret|=*pix;
162 if (bWrite)
163 *pix=1;
164 else if (ret)
165 return true;
169 return ret?true:false;