在iOS中设置View for NivevehGL的背景图像

时间:2013-04-27 10:43:18

标签: ios opengl-es

我正在尝试设置视图的背景图像(NGLView类型)并且它无法正常工作。

代码如下所示:

·H

@interface ViewController : UIViewController <NGLViewDelegate>
{
@private
    NGLMesh *_mesh;
    NGLCamera *_camera;

}
@property (strong,nonatomic) NSString *fileName;

-(void)screenBtnClicked:(id)sender;

@end

.m

#import "ViewController.h"

//Tags
#define kBackButtonTag 100

#pragma mark -
#pragma mark Constants
#pragma mark -
//**********************************************************************************************************
//
//  Constants
//
//**********************************************************************************************************

#pragma mark -
#pragma mark Private Interface
#pragma mark -
//**********************************************************************************************************
//
//  Private Interface
//
//**********************************************************************************************************

#pragma mark -
#pragma mark Public Interface
#pragma mark -
//**********************************************************************************************************
//
//  Public Interface
//
//**********************************************************************************************************

@implementation ViewController

@synthesize fileName;

#pragma mark -
#pragma mark Properties
//**************************************************
//  Properties
//**************************************************

#pragma mark -
#pragma mark Constructors
//**************************************************
//  Constructors
//**************************************************

#pragma mark -
#pragma mark Private Methods
//**************************************************
//  Private Methods
//**************************************************

#pragma mark -
#pragma mark Self Public Methods
//**************************************************
//  Self Public Methods
//**************************************************

- (void) drawView
{
//  _mesh.rotateY += 2.0f;
//  _mesh.rotateX -= 0.5f;

    [_camera drawCamera];
}

#pragma mark -
#pragma mark Override Public Methods
//**************************************************
//  Override Public Methods
//**************************************************

- (void) viewDidLoad
{
    // Must call super to agree with the UIKit rules.
    [super viewDidLoad];
    // Setting the loading process parameters. To take advantage of the NGL Binary feature,
    // remove the line "kNGLMeshOriginalYes, kNGLMeshKeyOriginal,". Your mesh will be loaded 950% faster.
    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                              kNGLMeshOriginalYes, kNGLMeshKeyOriginal,
                              kNGLMeshCentralizeYes, kNGLMeshKeyCentralize,
                              @"0.7", kNGLMeshKeyNormalize,
                              nil];



    _mesh = [[NGLMesh alloc] initWithFile:fileName settings:settings delegate:nil];

    _camera = [[NGLCamera alloc] initWithMeshes:_mesh, nil];
    [_camera autoAdjustAspectRatio:NO animated:YES];

    // Starts the debug monitor.
//  [[NGLDebug debugMonitor] startWithView:(NGLView *)self.view mesh:_mesh];


    UIButton *closeButton = [[UIButton alloc] init];
    closeButton.frame = CGRectMake(5, 5, 40, 40);
    closeButton.tag = 100;
    [closeButton setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(screenBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:closeButton];

//try this... not working
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];
    [self.view sendSubviewToBack:imageView];

//tried this not working either..
    UIColor *patternColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
    UIView  *patternView = [[UIView alloc] initWithFrame:self.view.frame];
    [patternView setBackgroundColor:patternColor];
    [self.view sendSubviewToBack:patternView];


}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{   
    // Super call, must be called if we have no plans to override all touch methods, it's an UIKit rule.
    [super touchesMoved:touches withEvent:event];

    UITouch *touch;
    CGPoint pointA, pointB;

    // Pan gesture.
    if ([touches count] == 1)
    {
        touch = [[touches allObjects] objectAtIndex:0];
        pointA = [touch locationInView:self.view];
        pointB = [touch previousLocationInView:self.view];

        _mesh.rotateY += (pointA.x - pointB.x) * 0.5f;
        _mesh.rotateX += (pointA.y - pointB.y) * 0.5f;

    }
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}

- (void) dealloc
{
    [_mesh release];
    [_camera release];

    [super dealloc];
}

-(void)screenBtnClicked:(UIButton*)sender
{
    switch (sender.tag) {
        case kBackButtonTag:
        {
            [self dismissModalViewControllerAnimated:YES];
            break;
        }
        default:
            break;
    }
}

尝试在xib文件中设置imageView并更改背景图片,但它无法正常工作...

1 个答案:

答案 0 :(得分:0)

self.view.backgroundColor = [UIColor clearColor];
nglGlobalColorFormat(NGLColorFormatRGBA);
nglGlobalFlush();

这三行允许您设置透明背景。