!B (Sandbox) (CE-21795) Importing models with multisubmaterials via fbx switches...
[CRYENGINE.git] / Code / Sandbox / Plugins / LodGeneratorPlugin / control / MeshBakerPopupPreview.cpp
blob314608d449a065f02c2f52c4b3f1b34e21fcfb56
1 #include "StdAfx.h"
2 #include <Cry3DEngine/IStatObj.h>
3 #include "MeshBakerPopupPreview.h"
4 #include "DisplayViewportAdapter.h"
5 #include "Material/Material.h"
8 CCMeshBakerPopupPreview::CCMeshBakerPopupPreview(QViewport * viewPort):m_ViewPort(viewPort)
10 m_pViewportAdapter.reset(new CDisplayViewportAdapter(m_ViewPort));
11 m_pObject = NULL;
12 m_pMat = NULL;
14 m_bPrecacheMaterial = false;
15 m_bDrawWireFrame = false;
16 m_bRotate = false;
17 m_rotateAngle = 0;
19 CDLight l;
21 float L = 1.0f;
22 l.m_fRadius = 10000;
23 l.m_Flags |= DLF_SUN | DLF_DIRECTIONAL;
24 l.SetLightColor(ColorF(L,L,L,1));
25 l.SetPosition( Vec3(100,100,100) );
26 m_lights.push_back( l );
28 m_bShowObject = true;
30 m_clearColor.set(0.5f, 0.5f, 0.5f, 1.0f);
31 m_ambientColor.set(1.0f, 1.0f, 1.0f, 1.0f);
32 m_ambientMultiplier = 0.5f;
36 void CCMeshBakerPopupPreview::OnKeyEvent(const SKeyEvent& sKeyEvent)
41 void CCMeshBakerPopupPreview::OnMouseEvent(const SMouseEvent& sMouseEvent)
48 void CCMeshBakerPopupPreview::SetObject(IStatObj * pObj)
50 m_pObject = pObj;
53 void CCMeshBakerPopupPreview::ReleaseObject()
55 if (m_pObject != NULL)
57 m_pObject = NULL;
61 void CCMeshBakerPopupPreview::SetMaterial(IMaterial* pMat)
63 m_pMat = (CMaterial*)pMat;
66 void CCMeshBakerPopupPreview::SetRotate(bool rotate)
68 m_Rotate = rotate;
71 void CCMeshBakerPopupPreview::SetWireframe(bool wireframe)
73 m_bDrawWireFrame = wireframe;
76 void CCMeshBakerPopupPreview::SetGrid(bool grid)
78 m_Grid = grid;
82 void CCMeshBakerPopupPreview::OnRender(const SRenderContext& rc)
84 if (m_bDrawWireFrame)
86 NoMaterialRender(rc);
88 else
90 MaterialRender(rc);
94 bool CCMeshBakerPopupPreview::NoMaterialRender(const SRenderContext& rc)
96 IRenderAuxGeom* aux = gEnv->pRenderer->GetIRenderAuxGeom();
98 DisplayContext dc;
99 m_pViewportAdapter->EnableXYViewport(true);
100 dc.SetView(m_pViewportAdapter.get());
102 aux->SetRenderFlags(e_Mode3D|e_AlphaBlended|e_FillModeSolid|e_CullModeNone|e_DepthWriteOff|e_DepthTestOn);
104 dc.DepthTestOff();
106 RenderUnwrappedMesh(dc,rc);
108 dc.DepthTestOn();
110 return true;
113 void CCMeshBakerPopupPreview::RenderUnwrappedMesh(DisplayContext& dc,const SRenderContext& rc)
115 if( !m_pObject )
116 return;
118 // CEdGeometry* pCEdGeometry = m_pObject->GetGeometry();m_pObject->GetIndexedMesh(true);
119 // if (NULL == pCEdGeometry)
120 // return;
121 // IIndexedMesh* pIIndexedMesh = pCEdGeometry->GetIndexedMesh();
124 IIndexedMesh* pIIndexedMesh = m_pObject->GetIndexedMesh(true);
125 if (NULL == pIIndexedMesh)
126 return;
128 RenderUnwrappedLodPolygon(dc, pIIndexedMesh,rc);
132 void CCMeshBakerPopupPreview::RenderUnwrappedLodPolygon(DisplayContext& dc, IIndexedMesh* pIIndexedMesh,const SRenderContext& rc)
134 if (pIIndexedMesh->GetFaceCount() == 0)
136 pIIndexedMesh->RestoreFacesFromIndices();
139 IIndexedMesh::SMeshDescription sMeshDescription;
140 pIIndexedMesh->GetMeshDescription(sMeshDescription);
143 ColorB color = ColorB(255,0,0);
144 dc.SetColor(color);
146 for (int i=0; i<sMeshDescription.m_nFaceCount; i++)
148 const SMeshFace* pFace = &(sMeshDescription.m_pFaces[i]);
150 const Vec3* pSMeshVert0 = &sMeshDescription.m_pVerts[pFace->v[0]];
151 const Vec3* pSMeshVert1 = &sMeshDescription.m_pVerts[pFace->v[1]];
152 const Vec3* pSMeshVert2 = &sMeshDescription.m_pVerts[pFace->v[2]];
153 dc.DrawLine(*pSMeshVert0, *pSMeshVert1);
154 dc.DrawLine(*pSMeshVert1, *pSMeshVert2);
155 dc.DrawLine(*pSMeshVert0, *pSMeshVert2);
161 bool CCMeshBakerPopupPreview::MaterialRender(const SRenderContext& rc)
163 if (m_pMat == NULL)
164 return false;
166 IRenderer* renderer = gEnv->pRenderer;
167 int width = rc.viewport->width();
168 int height = rc.viewport->height();
169 if (height < 2 || width < 2)
170 return false;
172 SRenderingPassInfo passInfo = SRenderingPassInfo::CreateGeneralPassRenderingInfo(*rc.camera, SRenderingPassInfo::DEFAULT_FLAGS, true);
174 renderer->ChangeViewport(0,0,width,height);
175 renderer->SetCamera( *rc.camera );
177 CScopedWireFrameMode scopedWireFrame(renderer, m_bDrawWireFrame?R_WIREFRAME_MODE:R_SOLID_MODE);
179 // Add lights.
180 for (size_t i = 0; i < m_lights.size(); i++)
182 renderer->EF_ADDDlight(&m_lights[i], passInfo);
185 if(m_pMat)
186 m_pMat->DisableHighlight();
188 _smart_ptr<IMaterial> pMaterial;
189 if (m_pMat)
190 pMaterial = m_pMat->GetMatInfo();
192 if (m_bPrecacheMaterial)
194 IMaterial *pCurMat=pMaterial;
195 if (!pCurMat)
197 if (m_pObject)
198 pCurMat=m_pObject->GetMaterial();
199 // else if (m_pEntity)
200 // pCurMat=m_pEntity->GetMaterial();
201 // else if (m_pCharacter)
202 // pCurMat=m_pCharacter->GetIMaterial();
203 // else if (m_pEmitter)
204 // pCurMat=m_pEmitter->GetMaterial();
206 if (pCurMat)
208 pCurMat->PrecacheMaterial(0.0f, NULL, true, true);
212 { // activate shader item
213 IMaterial *pCurMat=pMaterial;
214 if (!pCurMat)
216 if (m_pObject)
217 pCurMat=m_pObject->GetMaterial();
218 // else if (m_pEntity)
219 // pCurMat=m_pEntity->GetMaterial();
220 // else if (m_pCharacter)
221 // pCurMat=m_pCharacter->GetIMaterial();
222 // else if (m_pEmitter)
223 // pCurMat=m_pEmitter->GetMaterial();
228 if (m_bShowObject)
229 RenderObject(pMaterial, passInfo);
234 return true;
238 void CCMeshBakerPopupPreview::RenderObject( IMaterial* pMaterial, SRenderingPassInfo& passInfo )
240 if (pMaterial == NULL)
241 return;
243 SRendParams rp;
244 rp.dwFObjFlags = 0;
245 //rp.nDLightMask = (1<<m_lights.size())-1;
246 rp.AmbientColor = m_ambientColor * m_ambientMultiplier;
247 rp.dwFObjFlags |= FOB_TRANS_MASK /*| FOB_GLOBAL_ILLUMINATION*/ | FOB_NO_FOG /*| FOB_ZPREPASS*/;
248 rp.pMaterial = pMaterial;
250 Matrix34 tm;
251 tm.SetIdentity();
252 rp.pMatrix = &tm;
254 if (m_bRotate)
256 tm.SetRotationXYZ(Ang3( 0,0,m_rotateAngle ));
257 m_rotateAngle += 0.1f;
260 // visn test
261 // if (m_pObject)
262 // m_pObject->Render( rp, passInfo );
264 // if (m_pEntity)
265 // m_pEntity->Render( rp, passInfo );
267 // if (m_pCharacter)
268 // m_pCharacter->Render( rp, QuatTS(IDENTITY), passInfo );
270 // if (m_pEmitter)
271 // {
272 // m_pEmitter->Update();
273 // m_pEmitter->Render( rp, passInfo );
274 // }
278 void CCMeshBakerPopupPreview::RenderUnwrappedLodPolygonUV(DisplayContext& dc, IIndexedMesh* pIIndexedMesh)
280 if (pIIndexedMesh->GetFaceCount() == 0)
282 pIIndexedMesh->RestoreFacesFromIndices();
285 IIndexedMesh::SMeshDescription sMeshDescription;
286 pIIndexedMesh->GetMeshDescription(sMeshDescription);
288 ColorB color = ColorB(255,255,255);
289 dc.SetColor(color);
291 for (int i=0; i<sMeshDescription.m_nFaceCount; i++)
293 const SMeshFace* pFace = &(sMeshDescription.m_pFaces[i]);
294 for (int j=0; j<3; j++)
296 const SMeshTexCoord* pSMeshTexCoord0 = &sMeshDescription.m_pTexCoord[pFace->v[0]];
297 const SMeshTexCoord* pSMeshTexCoord1 = &sMeshDescription.m_pTexCoord[pFace->v[1]];
298 const SMeshTexCoord* pSMeshTexCoord2 = &sMeshDescription.m_pTexCoord[pFace->v[2]];
299 Vec2 uv0 = pSMeshTexCoord0->GetUV();
300 Vec2 uv1 = pSMeshTexCoord1->GetUV();
301 Vec2 uv2 = pSMeshTexCoord2->GetUV();
302 dc.DrawLine(Vec3(uv0.x,uv0.y,0.01f), Vec3(uv1.x,uv1.y,0.01f));
303 dc.DrawLine(Vec3(uv0.x,uv0.y,0.01f), Vec3(uv2.x,uv2.y,0.01f));
304 dc.DrawLine(Vec3(uv1.x,uv1.y,0.01f), Vec3(uv2.x,uv2.y,0.01f));