VRGLViewController.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. //
  2. // VRGLViewController.m
  3. // VRDemo
  4. //
  5. // Created by J.J. on 16/8/26.
  6. // Copyright © 2016年 xm. All rights reserved.
  7. //
  8. #import "VRGLViewController.h"
  9. #import "VRSoft.h"
  10. @interface VRGLViewController ()
  11. {
  12. VRHANDLE mVRHandle;
  13. XMVRType mVRType;
  14. UInt64 mTimeTouchDown;
  15. double mTouchDownX;
  16. double mTouchDownY;
  17. QUEUE_YUV_DATA _yuvDatas;
  18. unsigned char * _pbyYUV;
  19. int _nYUVBufLen;
  20. int shapeNum;
  21. }
  22. @property(strong,nonatomic)EAGLContext* context;
  23. @property(strong,nonatomic)GLKBaseEffect* effect;
  24. @end
  25. @implementation VRGLViewController
  26. @synthesize context;
  27. @synthesize effect;
  28. -(id)init{
  29. self = [super init];
  30. if ( self ) {
  31. self.VRShowMode = -1;
  32. }
  33. return self;
  34. }
  35. -(void)setVRShapeType:(XMVRShape)shapetype
  36. {
  37. NSLog(@"\n\n-------> setVRType = %d\n\n", mVRType);
  38. if ( mVRHandle ) {
  39. VRSoft_SetShape(mVRHandle, shapetype);
  40. }
  41. }
  42. -(void)setVRType:(XMVRType)type {
  43. // set 180VR or 360VR
  44. mVRType = type;
  45. NSLog(@"\n\n-------> setVRType = %d\n\n", mVRType);
  46. if ( mVRHandle ) {
  47. VRSoft_SetType(mVRHandle, mVRType);
  48. VRSoft_SetCameraMount(mVRHandle,Ceiling);
  49. }
  50. if(mVRType == XMVR_TYPE_360D){
  51. VRSoft_SetCameraMount(mVRHandle,Ceiling);
  52. }
  53. }
  54. -(void)setVRVRCameraMount:(XMVRMount)Mount{
  55. VRSoft_SetCameraMount(mVRHandle,Mount);
  56. }
  57. -(void)setVRFecParams:(int)xCenter yCenter:(int)yCenter radius:(int)radius Width:(int)imgWidth Height:(int)imgHeight{
  58. if (mVRHandle) {
  59. VRSoft_SetFecParams(mVRHandle, xCenter, yCenter, radius, imgWidth, imgHeight);
  60. }
  61. }
  62. //智能分析报警之后界面旋转位置
  63. -(void)setAnalyzeWithXL:(int)x0 YL:(int)y0 XR:(int)x1 YR:(int)y1 Width:(int)imgWidth Height:(int)imgHeight
  64. {
  65. VRSoft_DisplayRect(mVRHandle, x0, y0, x1, y1, imgWidth, imgHeight);
  66. }
  67. - (void)viewDidLoad {
  68. [super viewDidLoad];
  69. self.view.backgroundColor =[UIColor orangeColor];
  70. [self configSoftEAGLContext];
  71. UIPinchGestureRecognizer * twoFingerPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(touchesPinch:)];
  72. [self.view addGestureRecognizer:twoFingerPinch];
  73. }
  74. //软解
  75. -(void)configSoftEAGLContext {
  76. [self creatGLKView];
  77. if ( !mVRHandle ) {
  78. VRSoft_Create(&mVRHandle);
  79. // 1.0.7版本之后必须设置版权声明信息
  80. VRSoft_SetAttribute(mVRHandle, [@"COPYRIGHT" UTF8String],
  81. (char *)[@"HangZhou XiongMai Technology CO.,LTD." UTF8String]);
  82. VRSoft_SetAttribute(mVRHandle, [@"PLATFORM" UTF8String],
  83. (char *)[@"iOS" UTF8String]);
  84. VRSoft_Prepare(mVRHandle);
  85. }
  86. NSLog(@"\n\n-------> mVRType = %d\n\n", mVRType);
  87. VRSoft_SetType(mVRHandle, mVRType);
  88. [self SetPPIandInt];
  89. const char *version = VRSoft_Version();
  90. NSLog(@"%s", version);
  91. }
  92. -(void)creatGLKView
  93. {
  94. GLKView* view = (GLKView*)self.view;
  95. // 使用“ES2”创建一个“EAGLEContext”实例
  96. // 将“view”的context设置为这个“EAGLContext”实例的引用。并且设置颜色格式和深度格式。
  97. //去掉这两句普通相机可以,加上这两句鱼眼相机可以,待调试。
  98. if (!context) {
  99. context = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2];
  100. view.context = context;
  101. view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
  102. view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
  103. // 将此“EAGLContext”实例设置为OpenGL的“当前激活”的“Context”。这样,以后所有“GL”的指令均作用在这个“Context”上。随后,发送第一个“GL”指令:激活“深度检测”。
  104. [EAGLContext setCurrentContext:context];
  105. self.preferredFramesPerSecond = 60;
  106. }
  107. [self.view setNeedsDisplay];
  108. [self.view.layer displayIfNeeded];
  109. }
  110. -(void)SetPPIandInt
  111. {
  112. CGFloat scale_screen = [UIScreen mainScreen].scale;
  113. VRSoft_SetPPIZoom(scale_screen);
  114. if (TARGET_IPHONE_SIMULATOR || scale_screen != 3) {
  115. VRSoft_Init(mVRHandle, self.view.frame.size.width , self.view.frame.size.height );
  116. }else{
  117. VRSoft_Init(mVRHandle, self.view.frame.size.width * 1080 / 1242, self.view.frame.size.height * 1920 / 2208);
  118. }
  119. }
  120. - (void)didReceiveMemoryWarning {
  121. [super didReceiveMemoryWarning];
  122. }
  123. - (void) viewWillAppear:(BOOL)animated {
  124. [super viewWillAppear:animated];
  125. NSLog(@"\n---------> vrview width : %f\n", self.view.frame.size.width);
  126. NSLog(@"\n---------> vrview height : %f\n", self.view.frame.size.height);
  127. VRSoft_Init(mVRHandle,
  128. self.view.frame.size.width,
  129. self.view.frame.size.height);
  130. NSLog(@"%f,%f",self.view.frame.size.width,self.view.frame.size.height);
  131. CGFloat scale_screen = [UIScreen mainScreen].scale;
  132. VRSoft_SetPPIZoom(scale_screen);
  133. }
  134. - (void)update {
  135. }
  136. -(void)reloadInitView:(CGRect)frame{
  137. VRSoft_Init(mVRHandle,
  138. frame.size.width,
  139. frame.size.height);
  140. }
  141. - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
  142. glClearColor(0, 0, 0, 1.0f);
  143. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  144. [self loadYUVImageFromFile];
  145. VRSoft_Drawself(mVRHandle);
  146. }
  147. -(void)SoftTouchMove:(NSSet *)SoftTouch Softevent:(UIEvent *)Softevent{
  148. // 最多只处理两个点
  149. GLfloat values[4];
  150. int pointerCount = 0;
  151. for(UITouch *touch in Softevent.allTouches) {
  152. if ( pointerCount < 2 ) {
  153. CGPoint locInSelf = [touch locationInView:self.view];
  154. values[pointerCount*2] = locInSelf.x;
  155. values[pointerCount*2 + 1] = locInSelf.y;
  156. pointerCount ++;
  157. continue;
  158. } else {
  159. break;
  160. }
  161. }
  162. if ( pointerCount == 1 ) {
  163. VRSoft_OnTouchMove(mVRHandle, values[0], values[1]);
  164. }
  165. }
  166. #pragma mark 捏合
  167. -(void)SoftTouchesPinch:(CGFloat)scale{
  168. VRSoft_OnTouchPinchScale(mVRHandle,scale);
  169. }
  170. //鱼眼YUV数据
  171. -(void)PushData:(int)width height:(int)height YUVData:(unsigned char *)pData{
  172. SYUVData *pNew = new SYUVData(width, height, pData);
  173. _yuvDatas.push(pNew);
  174. int nSize = (int)_yuvDatas.size();
  175. for (int i = 4; i < nSize; i++) {
  176. SYUVData *item = _yuvDatas.front();
  177. _yuvDatas.pop();
  178. delete item;
  179. }
  180. }
  181. -(SYUVData *)PopData{
  182. SYUVData *pItem = NULL;
  183. if (!_yuvDatas.empty()) {
  184. pItem = _yuvDatas.front();
  185. _yuvDatas.pop();
  186. }
  187. return pItem;
  188. }
  189. - (void)loadYUVImageFromFile{
  190. SYUVData *pData = [self PopData];
  191. if (pData) {
  192. int nDataLen = pData->width * pData->height * 3 / 2;
  193. if (_nYUVBufLen< nDataLen || _pbyYUV == NULL) {
  194. if (_pbyYUV) {
  195. free(_pbyYUV);
  196. _pbyYUV = NULL;
  197. }
  198. _pbyYUV = (unsigned char *)malloc(nDataLen);//new unsigned char[nDataLen];
  199. _nYUVBufLen = nDataLen;
  200. }
  201. memcpy(_pbyYUV, pData->pData, nDataLen);
  202. VRSoft_SetYUV420PTexture(mVRHandle,_pbyYUV, _nYUVBufLen , pData->width, pData->height );
  203. delete pData;
  204. }
  205. }
  206. -(UInt64)getTimeMS{
  207. return [[NSDate date] timeIntervalSince1970]*1000;
  208. }
  209. -(void)delayAutoAdjust {
  210. VRSoft_AutoAdjust(mVRHandle);
  211. }
  212. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  213. {
  214. NSLog(@"---> touchesBegan");
  215. for(UITouch *touch in event.allTouches) {
  216. CGPoint locInSelf = [touch locationInView:self.view];
  217. VRSoft_OnTouchDown(mVRHandle, locInSelf.x, locInSelf.y);
  218. mTouchDownX = locInSelf.x;
  219. mTouchDownY = locInSelf.y;
  220. mTimeTouchDown = [self getTimeMS];
  221. break;
  222. }
  223. }
  224. -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  225. // 最多只处理两个点
  226. GLfloat values[4];
  227. int pointerCount = 0;
  228. for(UITouch *touch in event.allTouches) {
  229. if ( pointerCount < 2 ) {
  230. CGPoint locInSelf = [touch locationInView:self.view];
  231. values[pointerCount*2] = locInSelf.x;
  232. values[pointerCount*2 + 1] = locInSelf.y;
  233. pointerCount ++;
  234. continue;
  235. } else {
  236. break;
  237. }
  238. }
  239. if ( pointerCount == 1 ) {
  240. VRSoft_OnTouchMove(mVRHandle, values[0], values[1]);
  241. }
  242. }
  243. -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  244. for(UITouch *touch in event.allTouches) {
  245. CGPoint locInSelf = [touch locationInView:self.view];
  246. VRSoft_OnTouchUp(mVRHandle, locInSelf.x, locInSelf.y);
  247. UInt64 timeOffset = [self getTimeMS] - mTimeTouchDown;
  248. if ( timeOffset > 0 && timeOffset < 200 ) {
  249. double dx = locInSelf.x - mTouchDownX;
  250. double dy = locInSelf.y - mTouchDownY;
  251. NSLog(@"---> timeOffset = %llu, dx = %f, dy = %f", timeOffset, dx, dy);
  252. if((dx > -10 && dx < 10) && (dy > -10 && dy < 10)){
  253. return;
  254. }
  255. double velocityX = dx * 1000 / timeOffset;
  256. double velocityY = dy * 1000 / timeOffset;
  257. NSLog(@"---> velocityX = %f, velocityY = %f", velocityX, velocityY);
  258. VRSoft_OnTouchFling(mVRHandle, velocityX*2, velocityY*2);
  259. }
  260. // 只处理一个点就可以了
  261. break;
  262. }
  263. [self performSelector:@selector(delayAutoAdjust) withObject:nil afterDelay:0.12f];
  264. }
  265. - (void) touchesPinch:(UIPinchGestureRecognizer *)recognizer{
  266. VRSoft_OnTouchPinchScale(mVRHandle,recognizer.scale);
  267. }
  268. -(void)DoubleTap:(UITapGestureRecognizer*)recognizer{
  269. if (mVRType == XMVR_TYPE_180D) {
  270. int times = 5;
  271. shapeNum++;
  272. if (shapeNum == times)
  273. {
  274. shapeNum = 0;
  275. }
  276. switch (shapeNum) {
  277. case 0:
  278. [self setVRShapeType:Shape_Ball];
  279. break;
  280. case 1:
  281. [self setVRShapeType:Shape_Rectangle];
  282. break;
  283. case 2:
  284. [self setVRShapeType:Shape_Cylinder];
  285. break;
  286. case 3:
  287. [self setVRShapeType:Shape_Grid_4R];
  288. break;
  289. case 4:
  290. [self setVRShapeType:Shape_Grid_3R];
  291. break;
  292. default:
  293. break;
  294. }
  295. }else if (mVRType == XMVR_TYPE_360D){
  296. //根据吸顶 还是 壁挂模式 设置双击时的响应的形状
  297. int times;
  298. if ( [[NSUserDefaults standardUserDefaults] integerForKey:@"VRShowMode"] == 1) {
  299. times = 7;
  300. }else{
  301. times = 2;
  302. }
  303. if (self.VRShowMode == 1) {
  304. times = 7;
  305. }else if (self.VRShowMode == 2){
  306. times = 2;
  307. }
  308. shapeNum++;
  309. if (shapeNum == times)
  310. {
  311. shapeNum = 0;
  312. }
  313. switch (shapeNum) {
  314. case 0:
  315. [self setVRShapeType:Shape_Ball];
  316. break;
  317. case 1:
  318. [self setVRShapeType:Shape_Rectangle];
  319. break;
  320. case 2:
  321. [self setVRShapeType:Shape_Ball_Bowl];
  322. break;
  323. case 3:
  324. [self setVRShapeType:Shape_Ball_Hat];
  325. break;
  326. case 4:
  327. [self setVRShapeType:Shape_Cylinder];
  328. break;
  329. case 5:
  330. [self setVRShapeType:Shape_Grid_4R];
  331. break;
  332. case 6:
  333. [self setVRShapeType:Shape_Rectangle_2R];
  334. break;
  335. default:
  336. break;
  337. }
  338. }
  339. //改变悬浮窗口的按钮状态
  340. NSString *shapeTag = [NSString stringWithFormat:@"%d",shapeNum];
  341. [[NSNotificationCenter defaultCenter] postNotificationName:@"AcceptDelegate" object:@{@"parameter1":shapeTag}];
  342. }
  343. -(void)setVRShowMode:(int)VRShowMode{
  344. _VRShowMode = VRShowMode;
  345. shapeNum = 0;
  346. }
  347. -(void)setChangeModel:(BOOL)changeModel{
  348. if(changeModel == YES){
  349. shapeNum = 0;
  350. }else{
  351. NSLog(@"the type no change");
  352. }
  353. }
  354. @end