forked from chenzhi/CameraGame
/
CaptureFaceGS.cpp
277 lines (169 loc) · 5.47 KB
/
CaptureFaceGS.cpp
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
//
// CaptureVideoGS.cpp
// ogreApp
//
// Created by thcz on 11-6-22.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#include "pch.h"
#include "CaptureFaceGS.h"
#include "application.h"
#include "ogreapp/ofxiPhoneVideoGrabber.h"
#include "UICaptureFace.h"
#include "UISelectUser.h"
#include "Config.h"
#include "Tool.h"
#include "UISelectHead.h"
#include "UISelectFaceMode.h"
CaptureFaceGS::CaptureFaceGS( )
:GameState(ST_CAPTUREFACE),m_BackGround(NULL),m_pCameraNode(NULL),m_pCaptureUI(NULL),
m_pSelectUserUI(NULL),m_pSelectHeadUI(NULL),m_pSelectFaceUI(NULL)
{
}
CaptureFaceGS::~CaptureFaceGS()
{
}
void CaptureFaceGS::begin( )
{
GameState::begin();
m_pCameraNode=Application::getSingleton().getMainCameraNode();
#if defined __arm__
initVideo();
initBackGround();
#endif
///创建ui
initUI();
}
void CaptureFaceGS::end( )
{
GameState::end();
#if defined __arm__
// m_BackGround->setVisible(false);
// destroyBackGround();
ofxiPhoneVideoGrabber::getSingleton().stopCapture();
#endif
destroyUI();
}
/**更新函数*/
StateType CaptureFaceGS::update(float time)
{
return GameState::update(time);
}
void CaptureFaceGS::beginTouch(int x,int y)
{
// setNextStateType(ST_WAR);
return ;
}
void CaptureFaceGS::initBackGround()
{
///如果已经创建了直接返回
if(m_BackGround!=NULL)
return ;
float distance=20.0f;
float width=0,height=0;
Ogre::Vector3 camPos=m_pCameraNode->getPosition();
float fovy= Application::getSingleton().getMainCamera()->getFOVy().valueRadians()*0.5f;
height=Ogre::Math::Tan(fovy)*distance*2.0f;
width=Application::getSingleton().getMainCamera()->getAspectRatio()*height;
#if defined __arm__
float videowidth=ofxiPhoneVideoGrabber::getSingleton().getWidth();
float videoheight=ofxiPhoneVideoGrabber::getSingleton().getHeight();
float textWidth=m_pVideoTexture->getWidth();
float texheight=m_pVideoTexture->getHeight();
#else
float videowidth=480;
float videoheight=360;
float textWidth=512;
float texheight=512;
#endif
Ogre::Plane plane(Ogre::Vector3(0.0f,0.0f,1.0f),Ogre::Vector3(0.0f,0.0f,0.0f));
Ogre::MeshPtr pMesh= Ogre::MeshManager::getSingleton().
createPlane("backVideo", "General", plane,1,1,1,1,false,1,videowidth/textWidth,videoheight/texheight);
m_BackGround=m_pSceneManager->createEntity("videoBackGround", pMesh->getName());
Ogre::SceneNode* pBackNode=m_pCameraNode->createChildSceneNode();
pBackNode->attachObject(m_BackGround);
pBackNode->setPosition(0,0,-distance);
pBackNode->setScale(Ogre::Vector3(width,height,1.0f));
Ogre::MaterialPtr pBackGroundMaterial=Ogre::MaterialManager::getSingleton().create("BackGroundVideomat", "General");
Ogre::Pass*pPass=pBackGroundMaterial->getTechnique(0)->getPass(0);
#if defined __arm__
Ogre::TexturePtr pVideoTexture=ofxiPhoneVideoGrabber::getSingleton().getOgreTexture();
pPass->createTextureUnitState()->setTextureName(pVideoTexture->getName());
#else
pPass->createTextureUnitState()->setTextureName("videoTexture_ofxiPhoneVideoGrabber");
#endif
pPass->setAmbient(Ogre::ColourValue(1.0f,1.0f,1.0f,1.0f));
m_BackGround->getSubEntity(0)->setMaterialName(pBackGroundMaterial->getName());
#ifndef __arm__
m_BackGround->setVisible(false);
#endif
}
void CaptureFaceGS::destroyBackGround()
{
//m_pSceneManager
Ogre::SceneNode* pNode=m_BackGround->getParentSceneNode();
pNode->detachAllObjects();
pNode->getParentSceneNode()->removeAndDestroyChild(pNode->getName());
m_pSceneManager->destroyEntity(m_BackGround);
m_BackGround=NULL;
}
//------------------------------------------------------------
void CaptureFaceGS::initVideo()
{
#ifdef __arm__
m_pVideoTexture=ofxiPhoneVideoGrabber::getSingleton().getOgreTexture();
#endif
return ;
}
/**更新摄像头*/
void CaptureFaceGS::updateVideo()
{
return ;
}
void CaptureFaceGS::initUI()
{
///获取有多少个用户数据。
Ogre::StringVectorPtr pFileList=Tools::getUserFaceFileList();
///测试代码
m_pCaptureUI=new UICaptureFace();
m_pCaptureUI->init();
m_pCaptureUI->setVisible(false);
m_pCaptureUI->setUserCount(pFileList->size());
Application::getSingleton().registerUI(m_pCaptureUI);
m_pSelectUserUI=new UISelectUser();
m_pSelectUserUI->init();
m_pSelectUserUI->setVisible(false);
Application::getSingleton().registerUI(m_pSelectUserUI);
m_pSelectUserUI->setUserList(pFileList);///设置用户头像
if(pFileList->empty())
{
m_pCaptureUI->setVisible(true);
}
else
{
m_pSelectUserUI->setVisible(true);
}
///---------------------------选择头套
m_pSelectHeadUI=new UISelectHead();
m_pSelectHeadUI->init();
m_pSelectHeadUI->setVisible(true);
Application::getSingleton().registerUI(m_pSelectHeadUI);
m_pSelectHeadUI->setVisible(false);
///-------------------选择模型
m_pSelectFaceUI=new UISelectFaceMode();
m_pSelectFaceUI->init();
m_pSelectFaceUI->setVisible(false);
Application::getSingletonPtr()->registerUI(m_pSelectFaceUI);
}
void CaptureFaceGS::destroyUI()
{
Application* pApp=Application::getSingletonPtr();
pApp->destroyUI(m_pCaptureUI);
m_pCaptureUI=NULL;
pApp->destroyUI(m_pSelectUserUI);
m_pSelectUserUI=NULL;
pApp->destroyUI(m_pSelectHeadUI);
m_pSelectHeadUI=NULL;
pApp->destroyUI(m_pSelectFaceUI);
m_pSelectFaceUI=NULL;
}