Advertisement
Guest User

queue dosen't work

a guest
Sep 21st, 2010
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (id) initWithFrame:(CGRect)frame useDepthBuffer:(BOOL)useDepthBuffer
  2. {
  3.     if( self = [super initWithFrame:frame useDepthBuffer:useDepthBuffer])
  4.     {
  5.         _characterX = 0;
  6.         _characterTargetX = 0;
  7.         _characterRotationY = 90;
  8.         _cameraZ = 0;
  9.         _cameraTargetZ = 0;
  10.         _controlX = 0;
  11.  
  12.         self.timeKeeper = [[FrameTimeKeeper new] autorelease];
  13.         [self setupGLPerspectiveNear:0.1 far:1000];
  14.         //[self setupGLPerspectiveNear:0.1
  15. //                                 far:1000
  16. //                                left:-0.058669651529843
  17. //                               right:0.058669651529843
  18. //                              bottom:-0.0439760
  19. //                                 top:0.0439760];
  20.        
  21.         previousScale = 1.0f;
  22.         instantObjectScale = 1.0f;
  23.         instantXRotation = 1.0f;
  24.         instantYRotation = 0.0f;
  25.         instantXTranslation = 0.0f;
  26.         instantYTranslation = 0.0f;
  27.         instantZTranslation = 0.0f;
  28.         twoFingersAreMoving = NO;
  29.         pinchGestureUnderway = NO;
  30.        
  31.         accumulatedXRotation = 0.0f;
  32.         accumulatedYRotation = 0.0f;
  33.         accumulatedScale = 1.0f;
  34.         accumulatedXTranslation = 0.0f;
  35.         accumulatedYTranslation = 0.0f;
  36.         isFirstDrawing = YES;
  37.        
  38.         NSMutableArray * theModels = [[NSMutableArray alloc] init];
  39.         self.models = theModels;
  40.         [theModels release];
  41.         NSMutableArray * positionMatrix = [[NSMutableArray alloc] init];
  42.         self.positions = positionMatrix;
  43.         [positionMatrix release];
  44.         renderingQueue = [[NSOperationQueue alloc] init];
  45.         [renderingQueue setMaxConcurrentOperationCount:1];
  46.        
  47.         queue = [[NSOperationQueue alloc] init];
  48.         [queue setMaxConcurrentOperationCount:1];
  49.  
  50.         self.multipleTouchEnabled = YES;
  51.        
  52.         self.state = QUIET;
  53.         self.rotationArray = [[NSMutableArray alloc] init];
  54.        
  55.         //self.autoresizesSubviews = NO;
  56.  
  57.     }
  58.     return self;
  59. }
  60.  
  61. /*******************************************************************************
  62.  *   Loading Models+Textures+Positions
  63.  *******************************************************************************/
  64.  
  65. - (void)setMD2ObjectModels:(NSArray *)theModels withTextures:(NSArray *)theTextures andPositions:(NSArray*)positionMatrix andRotations:(double*)rotations{
  66.    
  67.     NSInteger nModels = [theModels count];
  68.     for (NSInteger i = 0; i < nModels; i++)
  69.     {
  70.         UIImage * imageTx = [[UIImage alloc] initWithContentsOfFile:[theTextures objectAtIndex:i]];
  71.         Texture2D * characterTexture = [[Texture2D alloc] initWithImage:imageTx];
  72.         [imageTx release];
  73.         MD2Object * charObj = [[MD2Object alloc] initWithContentsOfFile:[theModels objectAtIndex:i] texture:characterTexture];  
  74.         [characterTexture release];
  75.         [self.models addObject:charObj];
  76.         [charObj release];
  77.         NSNumber * number = [[NSNumber alloc] initWithDouble:rotations[i]];
  78.         [self.rotationArray addObject:number];
  79.         [number release];
  80.     }
  81.     self.positions = (NSMutableArray *)positionMatrix;
  82. }
  83.  
  84. - (void)addMD2ObjectModel:(NSString *)theModel withTexture:(NSString *)theTexture andPosition:(NSArray *)position andRotation:(double)singleRotation{
  85.     UIImage * imageTx = [[UIImage alloc] initWithContentsOfFile:theTexture];
  86.     Texture2D * characterTexture = [[Texture2D alloc] initWithImage:imageTx];
  87.     [imageTx release];
  88.     MD2Object * charObj = [[MD2Object alloc] initWithContentsOfFile:theModel texture:characterTexture];  
  89.     [characterTexture release];
  90.     [self.models addObject:charObj];
  91.     [charObj release];
  92.     [self.positions addObject:position];
  93.     NSNumber * number = [[NSNumber alloc] initWithDouble:singleRotation];
  94.     [self.rotationArray addObject:number];
  95.     [number release];
  96. }
  97.  
  98. /****************************************************************************************************
  99.  *  dealloc
  100.  ****************************************************************************************************/
  101.  
  102. - (void) dealloc
  103. {
  104.     NSLog(@"OBJVIEWDELOZ");
  105.     [renderingQueue release];
  106.     self.dropButton = nil;
  107.     self.timeKeeper = nil;
  108.     self.models = nil;
  109.     self.positions = nil;
  110.     //self.stage = nil;
  111.     [super dealloc];
  112. }
  113.  
  114.  
  115. /****************************************************************************************************
  116.  *  drawView
  117.  ****************************************************************************************************/
  118.  
  119. - (void) drawView
  120. {
  121.     static GLfloat lightPosition[4] = { 200.0, 0.0, 11};
  122.     static GLfloat lightDefuse[4] = { 1.0, 1, 1, 1.0};
  123.     static GLfloat lightAmbient[4] = { 1.0, 1.0, 1.0, 1.0};
  124.    
  125.                 //-x destra +x sinistra     // z  avanti -z dietro
  126.     //glTranslatef( 3, 0, -5);
  127.  
  128.     _cameraTargetZ = -3.0 - fabs(_characterX * 2.13);
  129.     _cameraZ += (_cameraTargetZ - _cameraZ) * 0.1;
  130.    
  131.     _characterTargetX += _controlX * self.timeKeeper.deltaTime * 20;
  132.     _characterX += (_characterTargetX - _characterX) * 0.6;
  133.    
  134.     //glClearColor( 0, 0.0, 0.12, 1);
  135.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  136.    
  137.     // Setup Camera
  138.     glMatrixMode( GL_MODELVIEW);
  139.     glLoadIdentity();
  140.    
  141.     glEnable( GL_LIGHTING);
  142.     glEnable( GL_LIGHT0);
  143.     glLightfv( GL_LIGHT0, GL_DIFFUSE, lightDefuse);
  144.     glLightfv( GL_LIGHT0, GL_AMBIENT, lightAmbient);
  145.     glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, 68);
  146.     glLightfv( GL_LIGHT0, GL_POSITION, lightPosition);
  147.    
  148.     glTranslatef( 0, -2.2, _cameraZ+2.5);
  149.     glRotatef( -90.0, 1.0, 0.0, 0.0 );
  150.     glRotatef( -90.0, 0.0, 0.0, 1.0 );
  151.    
  152.     glTranslatef( -8, _characterX, 2.5);
  153.     glScalef( 0.119, 0.119, 0.119);
  154.    
  155.     NSInteger nModels = [models count];
  156.     for (NSInteger i = 0; i < nModels; i++)
  157.     {
  158.        
  159.        
  160.         //printf("\n drawn \n");
  161.         MD2Object * mdobj = [models objectAtIndex:i];
  162.         glPushMatrix();
  163.         NSArray * translationCoords = [positions objectAtIndex:i];
  164.        
  165.         double * currentCoords = [(PlayViewController*)viewController getCurrentStoryCoordinates];
  166.         double * deltas = calloc(sizeof(double),2);
  167.         deltas[0] = currentCoords[0] - [[translationCoords objectAtIndex:0] doubleValue];
  168.         deltas[1] = currentCoords[1] - [[translationCoords objectAtIndex:1] doubleValue];
  169.        
  170.         [(PlayViewController*)viewController getTransformCoordinateDeltas:deltas];
  171.        
  172.         printf("\n DELTA LAT   %lf , DELTA LON %lf \n",deltas[0],deltas[1]);
  173.        
  174.         printf("\n ROTATION %f",[[self.rotationArray objectAtIndex:i] doubleValue]);
  175.  
  176.  
  177.         glTranslatef(deltas[0], -deltas[1], 0);
  178.        
  179.         glRotatef([[self.rotationArray objectAtIndex:i] doubleValue], 0, 0, 1);
  180.  
  181.         free(deltas);
  182.         [mdobj setupForRenderGL];
  183.         [mdobj renderGL];  
  184.         [mdobj cleanupAfterRenderGL];
  185.         glPopMatrix();
  186.        
  187.     }
  188.  
  189.     [super drawView];
  190. }
  191.  
  192. - (void)redrawView{
  193.    
  194.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  195.    
  196.     NSInteger nModels = [models count];
  197.     double * currentCoords = [(PlayViewController*)viewController getCurrentStoryCoordinates];
  198. @synchronized(self){
  199.     for (NSInteger i = 0; i < nModels; i++)
  200.     {
  201.         //printf("\n drawn \n");
  202.         MD2Object * mdobj = [models objectAtIndex:i];
  203.         glPushMatrix();
  204.         NSArray * translationCoords = [positions objectAtIndex:i];
  205.        
  206.         double * deltas = calloc(sizeof(double),2);
  207.        
  208.         /// !-- attenzione dentro [[translationCoords objectAtIndex:0] doubleValue]; dopo LA 8a CIFRA DECIMALE, VENGONO AGGIUNTE COSE SPORCHE
  209.        
  210.         deltas[0] = currentCoords[0] - [[translationCoords objectAtIndex:0] doubleValue];
  211.         deltas[1] = currentCoords[1] - [[translationCoords objectAtIndex:1] doubleValue];
  212.        
  213.     //  printf("\n STAMPA COORD DIFFERENZA  %.15lf  %.15lf \n",currentCoords[1],[[translationCoords objectAtIndex:1] doubleValue]);
  214.        
  215.     //    printf("\n DELTA LAT BEFORE  %.15lf , DELTA LON %.15lf \n",deltas[0],deltas[1]);
  216.         [(PlayViewController*)viewController getTransformCoordinateDeltas:deltas];
  217.     //    printf("\n DELTA LAT   %.15lf , DELTA LON %.15lf \n",deltas[0],deltas[1]);
  218.        
  219.         glTranslatef(deltas[0], -deltas[1], 0);
  220.        
  221.         glRotatef([[self.rotationArray objectAtIndex:i] doubleValue] + deltaRotation, 0, 0, 1);
  222.  
  223.        
  224.         free(deltas);
  225.         [mdobj setupForRenderGL];
  226.         [mdobj renderGL];  
  227.         [mdobj cleanupAfterRenderGL];
  228.         glPopMatrix();
  229.        
  230.     }
  231.     }
  232.     [super drawView];
  233.    
  234. }
  235. - (void)moveThePlayerWithDeltasAndQueue:(double*)coord{
  236.    
  237.     //NSMethodSignature * sig = nil;
  238. //    sig = [self methodSignatureForSelector:@selector(moveThePlayerWithDeltas:)];
  239. //    NSInvocation *theInvocation = [NSInvocation invocationWithMethodSignature:sig];
  240. //    [theInvocation setTarget:self];
  241. //    [theInvocation setSelector:@selector(moveThePlayerWithDeltas:)];
  242. //    
  243. //    [theInvocation setArgument:&coord atIndex:2];
  244. //    
  245. //    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithInvocation:theInvocation];
  246. //    [renderingQueue addOperation:invocationOperation];
  247. //    [invocationOperation release];
  248.     [self moveThePlayerWithDeltas:coord];
  249.    
  250. }
  251. - (void)movethePlayerAroundYAxisAndQueue:(double)degrees{
  252.    
  253.     //NSMethodSignature * sig = nil;
  254. //    sig = [self methodSignatureForSelector:@selector(movethePlayerAroundYAxis:)];
  255. //    NSInvocation *theInvocation = [NSInvocation invocationWithMethodSignature:sig];
  256. //    [theInvocation setTarget:self];
  257. //    [theInvocation setSelector:@selector(movethePlayerAroundYAxis:)];
  258. //    
  259. //    [theInvocation setArgument:&degrees atIndex:2];
  260. //    
  261. //    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithInvocation:theInvocation];
  262. //    [renderingQueue addOperation:invocationOperation];
  263. //    [invocationOperation release];
  264.     [self movethePlayerAroundYAxis:degrees];
  265. }
  266.  
  267. - (void)moveThePlayerWithDeltas:(double*)playerCoord{
  268.    
  269.     if (playerCoord[0] != 0)
  270.         printf("\n LOg player coord %lf , %lf \n",(playerCoord[0]),(playerCoord[1]));
  271.    
  272.     glTranslatef((playerCoord[0]), (-playerCoord[1]),0);
  273.    
  274.     [self redrawView];
  275. }
  276. - (void)movethePlayerAroundYAxis:(double)degrees{
  277.    
  278.     glRotatef(degrees, 0, 0, 1);
  279.    
  280.     if (0 != degrees){
  281.         deltaRotation = - degrees;  
  282.  
  283.         printf("degree: %lf\n",deltaRotation);
  284.     }
  285.     [self redrawView];
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement