Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (id) initWithFrame:(CGRect)frame useDepthBuffer:(BOOL)useDepthBuffer
- {
- if( self = [super initWithFrame:frame useDepthBuffer:useDepthBuffer])
- {
- _characterX = 0;
- _characterTargetX = 0;
- _characterRotationY = 90;
- _cameraZ = 0;
- _cameraTargetZ = 0;
- _controlX = 0;
- self.timeKeeper = [[FrameTimeKeeper new] autorelease];
- [self setupGLPerspectiveNear:0.1 far:1000];
- //[self setupGLPerspectiveNear:0.1
- // far:1000
- // left:-0.058669651529843
- // right:0.058669651529843
- // bottom:-0.0439760
- // top:0.0439760];
- previousScale = 1.0f;
- instantObjectScale = 1.0f;
- instantXRotation = 1.0f;
- instantYRotation = 0.0f;
- instantXTranslation = 0.0f;
- instantYTranslation = 0.0f;
- instantZTranslation = 0.0f;
- twoFingersAreMoving = NO;
- pinchGestureUnderway = NO;
- accumulatedXRotation = 0.0f;
- accumulatedYRotation = 0.0f;
- accumulatedScale = 1.0f;
- accumulatedXTranslation = 0.0f;
- accumulatedYTranslation = 0.0f;
- isFirstDrawing = YES;
- NSMutableArray * theModels = [[NSMutableArray alloc] init];
- self.models = theModels;
- [theModels release];
- NSMutableArray * positionMatrix = [[NSMutableArray alloc] init];
- self.positions = positionMatrix;
- [positionMatrix release];
- renderingQueue = [[NSOperationQueue alloc] init];
- [renderingQueue setMaxConcurrentOperationCount:1];
- queue = [[NSOperationQueue alloc] init];
- [queue setMaxConcurrentOperationCount:1];
- self.multipleTouchEnabled = YES;
- self.state = QUIET;
- self.rotationArray = [[NSMutableArray alloc] init];
- //self.autoresizesSubviews = NO;
- }
- return self;
- }
- /*******************************************************************************
- * Loading Models+Textures+Positions
- *******************************************************************************/
- - (void)setMD2ObjectModels:(NSArray *)theModels withTextures:(NSArray *)theTextures andPositions:(NSArray*)positionMatrix andRotations:(double*)rotations{
- NSInteger nModels = [theModels count];
- for (NSInteger i = 0; i < nModels; i++)
- {
- UIImage * imageTx = [[UIImage alloc] initWithContentsOfFile:[theTextures objectAtIndex:i]];
- Texture2D * characterTexture = [[Texture2D alloc] initWithImage:imageTx];
- [imageTx release];
- MD2Object * charObj = [[MD2Object alloc] initWithContentsOfFile:[theModels objectAtIndex:i] texture:characterTexture];
- [characterTexture release];
- [self.models addObject:charObj];
- [charObj release];
- NSNumber * number = [[NSNumber alloc] initWithDouble:rotations[i]];
- [self.rotationArray addObject:number];
- [number release];
- }
- self.positions = (NSMutableArray *)positionMatrix;
- }
- - (void)addMD2ObjectModel:(NSString *)theModel withTexture:(NSString *)theTexture andPosition:(NSArray *)position andRotation:(double)singleRotation{
- UIImage * imageTx = [[UIImage alloc] initWithContentsOfFile:theTexture];
- Texture2D * characterTexture = [[Texture2D alloc] initWithImage:imageTx];
- [imageTx release];
- MD2Object * charObj = [[MD2Object alloc] initWithContentsOfFile:theModel texture:characterTexture];
- [characterTexture release];
- [self.models addObject:charObj];
- [charObj release];
- [self.positions addObject:position];
- NSNumber * number = [[NSNumber alloc] initWithDouble:singleRotation];
- [self.rotationArray addObject:number];
- [number release];
- }
- /****************************************************************************************************
- * dealloc
- ****************************************************************************************************/
- - (void) dealloc
- {
- NSLog(@"OBJVIEWDELOZ");
- [renderingQueue release];
- self.dropButton = nil;
- self.timeKeeper = nil;
- self.models = nil;
- self.positions = nil;
- //self.stage = nil;
- [super dealloc];
- }
- /****************************************************************************************************
- * drawView
- ****************************************************************************************************/
- - (void) drawView
- {
- static GLfloat lightPosition[4] = { 200.0, 0.0, 11};
- static GLfloat lightDefuse[4] = { 1.0, 1, 1, 1.0};
- static GLfloat lightAmbient[4] = { 1.0, 1.0, 1.0, 1.0};
- //-x destra +x sinistra // z avanti -z dietro
- //glTranslatef( 3, 0, -5);
- _cameraTargetZ = -3.0 - fabs(_characterX * 2.13);
- _cameraZ += (_cameraTargetZ - _cameraZ) * 0.1;
- _characterTargetX += _controlX * self.timeKeeper.deltaTime * 20;
- _characterX += (_characterTargetX - _characterX) * 0.6;
- //glClearColor( 0, 0.0, 0.12, 1);
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Setup Camera
- glMatrixMode( GL_MODELVIEW);
- glLoadIdentity();
- glEnable( GL_LIGHTING);
- glEnable( GL_LIGHT0);
- glLightfv( GL_LIGHT0, GL_DIFFUSE, lightDefuse);
- glLightfv( GL_LIGHT0, GL_AMBIENT, lightAmbient);
- glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, 68);
- glLightfv( GL_LIGHT0, GL_POSITION, lightPosition);
- glTranslatef( 0, -2.2, _cameraZ+2.5);
- glRotatef( -90.0, 1.0, 0.0, 0.0 );
- glRotatef( -90.0, 0.0, 0.0, 1.0 );
- glTranslatef( -8, _characterX, 2.5);
- glScalef( 0.119, 0.119, 0.119);
- NSInteger nModels = [models count];
- for (NSInteger i = 0; i < nModels; i++)
- {
- //printf("\n drawn \n");
- MD2Object * mdobj = [models objectAtIndex:i];
- glPushMatrix();
- NSArray * translationCoords = [positions objectAtIndex:i];
- double * currentCoords = [(PlayViewController*)viewController getCurrentStoryCoordinates];
- double * deltas = calloc(sizeof(double),2);
- deltas[0] = currentCoords[0] - [[translationCoords objectAtIndex:0] doubleValue];
- deltas[1] = currentCoords[1] - [[translationCoords objectAtIndex:1] doubleValue];
- [(PlayViewController*)viewController getTransformCoordinateDeltas:deltas];
- printf("\n DELTA LAT %lf , DELTA LON %lf \n",deltas[0],deltas[1]);
- printf("\n ROTATION %f",[[self.rotationArray objectAtIndex:i] doubleValue]);
- glTranslatef(deltas[0], -deltas[1], 0);
- glRotatef([[self.rotationArray objectAtIndex:i] doubleValue], 0, 0, 1);
- free(deltas);
- [mdobj setupForRenderGL];
- [mdobj renderGL];
- [mdobj cleanupAfterRenderGL];
- glPopMatrix();
- }
- [super drawView];
- }
- - (void)redrawView{
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- NSInteger nModels = [models count];
- double * currentCoords = [(PlayViewController*)viewController getCurrentStoryCoordinates];
- @synchronized(self){
- for (NSInteger i = 0; i < nModels; i++)
- {
- //printf("\n drawn \n");
- MD2Object * mdobj = [models objectAtIndex:i];
- glPushMatrix();
- NSArray * translationCoords = [positions objectAtIndex:i];
- double * deltas = calloc(sizeof(double),2);
- /// !-- attenzione dentro [[translationCoords objectAtIndex:0] doubleValue]; dopo LA 8a CIFRA DECIMALE, VENGONO AGGIUNTE COSE SPORCHE
- deltas[0] = currentCoords[0] - [[translationCoords objectAtIndex:0] doubleValue];
- deltas[1] = currentCoords[1] - [[translationCoords objectAtIndex:1] doubleValue];
- // printf("\n STAMPA COORD DIFFERENZA %.15lf %.15lf \n",currentCoords[1],[[translationCoords objectAtIndex:1] doubleValue]);
- // printf("\n DELTA LAT BEFORE %.15lf , DELTA LON %.15lf \n",deltas[0],deltas[1]);
- [(PlayViewController*)viewController getTransformCoordinateDeltas:deltas];
- // printf("\n DELTA LAT %.15lf , DELTA LON %.15lf \n",deltas[0],deltas[1]);
- glTranslatef(deltas[0], -deltas[1], 0);
- glRotatef([[self.rotationArray objectAtIndex:i] doubleValue] + deltaRotation, 0, 0, 1);
- free(deltas);
- [mdobj setupForRenderGL];
- [mdobj renderGL];
- [mdobj cleanupAfterRenderGL];
- glPopMatrix();
- }
- }
- [super drawView];
- }
- - (void)moveThePlayerWithDeltasAndQueue:(double*)coord{
- //NSMethodSignature * sig = nil;
- // sig = [self methodSignatureForSelector:@selector(moveThePlayerWithDeltas:)];
- // NSInvocation *theInvocation = [NSInvocation invocationWithMethodSignature:sig];
- // [theInvocation setTarget:self];
- // [theInvocation setSelector:@selector(moveThePlayerWithDeltas:)];
- //
- // [theInvocation setArgument:&coord atIndex:2];
- //
- // NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithInvocation:theInvocation];
- // [renderingQueue addOperation:invocationOperation];
- // [invocationOperation release];
- [self moveThePlayerWithDeltas:coord];
- }
- - (void)movethePlayerAroundYAxisAndQueue:(double)degrees{
- //NSMethodSignature * sig = nil;
- // sig = [self methodSignatureForSelector:@selector(movethePlayerAroundYAxis:)];
- // NSInvocation *theInvocation = [NSInvocation invocationWithMethodSignature:sig];
- // [theInvocation setTarget:self];
- // [theInvocation setSelector:@selector(movethePlayerAroundYAxis:)];
- //
- // [theInvocation setArgument:°rees atIndex:2];
- //
- // NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithInvocation:theInvocation];
- // [renderingQueue addOperation:invocationOperation];
- // [invocationOperation release];
- [self movethePlayerAroundYAxis:degrees];
- }
- - (void)moveThePlayerWithDeltas:(double*)playerCoord{
- if (playerCoord[0] != 0)
- printf("\n LOg player coord %lf , %lf \n",(playerCoord[0]),(playerCoord[1]));
- glTranslatef((playerCoord[0]), (-playerCoord[1]),0);
- [self redrawView];
- }
- - (void)movethePlayerAroundYAxis:(double)degrees{
- glRotatef(degrees, 0, 0, 1);
- if (0 != degrees){
- deltaRotation = - degrees;
- printf("degree: %lf\n",deltaRotation);
- }
- [self redrawView];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement