GLFun(28)~OpenGL ESテンプレートの利用(3)

2012. 02. 20
●GLFunView.mソースファイルからの移植

オリジナルのGLFunViewController.mから、ヘッダファイルで宣言したプロパティの実装と選択色の設定、図形描画のコード、そして3つのタッチイベントのメソッドをコピーします。
(太字が追加した部分)

(前略)

@synthesize firstTouch;
@synthesize lastTouch;
@synthesize currentColor;
@synthesize useRandomColor;
@synthesize shapeType;
@synthesize sprite;


- (void)awakeFromNib
{
    EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    if (!aContext)
    {
        aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
    }

    if (!aContext)
        NSLog(@"Failed to create ES context");
    else if (![EAGLContext setCurrentContext:aContext])
        NSLog(@"Failed to set ES context current");

    self.context = aContext;
    [aContext release];

    [(EAGLView *)self.view setContext:context];
    [(EAGLView *)self.view setFramebuffer];

    if ([context API] == kEAGLRenderingAPIOpenGLES2)
        [self loadShaders];

    self.currentColor = [UIColor redColor];
    self.useRandomColor = NO;

}

- (void)dealloc
{
    if (program)
    {
        glDeleteProgram(program);
        program = 0;
    }

    // Tear down context.
    if ([EAGLContext currentContext] == context)
        [EAGLContext setCurrentContext:nil];

    [context release];

    [colorControl release];

    [currentColor release];
    [sprite release];


    [super dealloc];
}

(中略)

- (void)drawFrame
{
    [(EAGLView *)self.view setFramebuffer];

    // Replace the implementation of this method to do your own custom drawing.
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    CGColorRef color = currentColor.CGColor;
    const CGFloat *components = CGColorGetComponents(color);


    if ([context API] == kEAGLRenderingAPIOpenGLES2)
    {
        // Use shader program.
        glUseProgram(program);

        // Update uniform value.

        // Update attribute values.
        glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
        glEnableVertexAttribArray(ATTRIB_VERTEX);
        glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors);
        glEnableVertexAttribArray(ATTRIB_COLOR);

        switch (shapeType) {
            case kLineShape: {
                if (sprite) {
                    [sprite release];
                    self.sprite = nil;
                }
                GLfloat vertices[4];
                // Convert coordinates
                vertices[0] = firstTouch.x;
                vertices[1] = self.frame.size.height - firstTouch.y;
                vertices[2] = lastTouch.x;
                vertices[3] = self.frame.size.height - lastTouch.y;
                glLineWidth(2.0);
                glVertexPointer(2, GL_FLOAT, 0, vertices);
                glDrawArrays(GL_LINES, 0, 2);
                break;
            }
            case kRectShape: {
                if (sprite) {
                    [sprite release];
                    self.sprite = nil;
                }
                // Calculate bounding rect and store in vertices
                GLfloat vertices[8];
                GLfloat minX = (firstTouch.x > lastTouch.x) ? lastTouch.x : firstTouch.x;
                GLfloat minY =
                    (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y)
                    ? self.frame.size.height - lastTouch.y : self.frame.size.height - firstTouch.y;
                GLfloat maxX = (firstTouch.x > lastTouch.x) ? firstTouch.x : lastTouch.x;
                GLfloat maxY =
                    (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y)
                    ? self.frame.size.height - firstTouch.y : self.frame.size.height - lastTouch.y;
                vertices[0] = maxX;
                vertices[1] = maxY;
                vertices[2] = minX;
                vertices[3] = maxY;
                vertices[4] = minX;
                vertices[5] = minY;
                vertices[6] = maxX;
                vertices[7] = minY;
                glVertexPointer(2, GL_FLOAT, 0, vertices);
                glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
                break;
            }
            case kEllipseShape: {
                if (sprite) {
                    [sprite release];
                    self.sprite = nil;
                }
                GLfloat vertices[720];
                GLfloat xradius = (firstTouch.x > lastTouch.x) ?
                    (firstTouch.x - lastTouch.x)/2 : (lastTouch.x - firstTouch.x)/2;
                GLfloat yradius =
                (self.frame.size.height-firstTouch.y > self.frame.size.height-lastTouch.y) ?
                ((self.frame.size.height-firstTouch.y)-(self.frame.size.height-lastTouch.y))/2 :
                ((self.frame.size.height-lastTouch.y)-(self.frame.size.height-firstTouch.y))/2;
                for (int i = 0; i <= 720; i+=2) {
                    GLfloat xOffset =
                    (firstTouch.x>lastTouch.x) ? lastTouch.x + xradius : firstTouch.x + xradius;
                    GLfloat yOffset =
                    (self.frame.size.height-firstTouch.y > self.frame.size.height-lastTouch.y) ?
                    self.frame.size.height - lastTouch.y + yradius :
                    self.frame.size.height - firstTouch.y + yradius;
                    vertices[i] = (cos(degreesToRadian(i / 2)) * xradius) + xOffset;
                    vertices[i+1] = (sin(degreesToRadian(i / 2)) * yradius) + yOffset;
                }
                glVertexPointer(2, GL_FLOAT, 0, vertices);
                glDrawArrays(GL_TRIANGLE_FAN, 0, 360);
                break;
            }
            case kImageShape:
                if (sprite == nil) {
                    self.sprite = [[Texture2D alloc]
                            initWithImage:[UIImage imageNamed:@"iphone.png"]];
                    glBindTexture(GL_TEXTURE_2D, sprite.name);
                }
                glEnable(GL_TEXTURE_2D);
                [sprite
                drawAtPoint:CGPointMake(lastTouch.x, self.frame.size.height - lastTouch.y)];
                break;
            default:
                break;
        }


        // Validate program before drawing. This is a good check, but only really necessary in a
        // debug build. DEBUG macro must be defined in your debug configurations if that's not
        // already the case.

#if defined(DEBUG)
        if (![self validateProgram:program])
        {
            NSLog(@"Failed to validate program: %d", program);
            return;
        }
#endif
    }
    else
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glVertexPointer(2, GL_FLOAT, 0, squareVertices);
        glEnableClientState(GL_VERTEX_ARRAY);
        glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
        glEnableClientState(GL_COLOR_ARRAY);

        switch (shapeType) {
            case kLineShape: {
                if (sprite) {
                    [sprite release];
                    self.sprite = nil;
                }
                GLfloat vertices[4];
                // Convert coordinates
                vertices[0] = firstTouch.x;
                vertices[1] = self.frame.size.height - firstTouch.y;
                vertices[2] = lastTouch.x;
                vertices[3] = self.frame.size.height - lastTouch.y;
                glLineWidth(2.0);
                glVertexPointer(2, GL_FLOAT, 0, vertices);
                glDrawArrays(GL_LINES, 0, 2);
                break;
            }
            case kRectShape: {
                if (sprite) {
                    [sprite release];
                    self.sprite = nil;
                }
                // Calculate bounding rect and store in vertices
                GLfloat vertices[8];
                GLfloat minX = (firstTouch.x > lastTouch.x) ? lastTouch.x : firstTouch.x;
                GLfloat minY =
                    (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y)
                    ? self.frame.size.height - lastTouch.y : self.frame.size.height - firstTouch.y;
                GLfloat maxX = (firstTouch.x > lastTouch.x) ? firstTouch.x : lastTouch.x;
                GLfloat maxY =
                    (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y)
                    ? self.frame.size.height - firstTouch.y : self.frame.size.height - lastTouch.y;
                vertices[0] = maxX;
                vertices[1] = maxY;
                vertices[2] = minX;
                vertices[3] = maxY;
                vertices[4] = minX;
                vertices[5] = minY;
                vertices[6] = maxX;
                vertices[7] = minY;
                glVertexPointer(2, GL_FLOAT, 0, vertices);
                glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
                break;
            }
            case kEllipseShape: {
                if (sprite) {
                    [sprite release];
                    self.sprite = nil;
                }
                GLfloat vertices[720];
                GLfloat xradius = (firstTouch.x > lastTouch.x) ?
                    (firstTouch.x - lastTouch.x)/2 : (lastTouch.x - firstTouch.x)/2;
                GLfloat yradius =
                (self.frame.size.height-firstTouch.y > self.frame.size.height-lastTouch.y) ?
                ((self.frame.size.height-firstTouch.y)-(self.frame.size.height-lastTouch.y))/2 :
                ((self.frame.size.height-lastTouch.y)-(self.frame.size.height-firstTouch.y))/2;
                for (int i = 0; i <= 720; i+=2) {
                    GLfloat xOffset =
                    (firstTouch.x>lastTouch.x) ? lastTouch.x + xradius : firstTouch.x + xradius;
                    GLfloat yOffset =
                    (self.frame.size.height-firstTouch.y > self.frame.size.height-lastTouch.y) ?
                    self.frame.size.height - lastTouch.y + yradius :
                    self.frame.size.height - firstTouch.y + yradius;
                    vertices[i] = (cos(degreesToRadian(i / 2)) * xradius) + xOffset;
                    vertices[i+1] = (sin(degreesToRadian(i / 2)) * yradius) + yOffset;
                }
                glVertexPointer(2, GL_FLOAT, 0, vertices);
                glDrawArrays(GL_TRIANGLE_FAN, 0, 360);
                break;
            }
            case kImageShape:
                if (sprite == nil) {
                    self.sprite = [[Texture2D alloc]
                            initWithImage:[UIImage imageNamed:@"iphone.png"]];
                    glBindTexture(GL_TEXTURE_2D, sprite.name);
                }
                glEnable(GL_TEXTURE_2D);
                [sprite
                drawAtPoint:CGPointMake(lastTouch.x, self.frame.size.height - lastTouch.y)];
                break;
            default:
                break;
        }

     }
     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    [(EAGLView *)self.view presentFramebuffer];
}

(中略)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (useRandomColor)
        self.currentColor = [UIColor randomColor];
    UITouch *touch = [[event touchesForView:self] anyObject];
    firstTouch = [touch locationInView:self];
    lastTouch = [touch locationInView:self];
    [self draw];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    lastTouch = [touch locationInView:self];
    [self draw];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    lastTouch = [touch locationInView:self];
    [self draw];
}


@end

7799

コピーして追加する項目は下記の通りです。

・ヘッダファイルで宣言を追加した6つのプロパティ

・選択色の初期設定(オリジナル:initWithCoder:メソッド → awakeFromNibメソッド)

・deallocメソッドでのプロパティの解放(6つのプロパティの内、Objective-Cオブジェクトの2つ)

・選択色の設定と図形描画(オリジナル:drawメソッド → drawFrameメソッド)
 (図形描画のswitch文はOpenGL ES 2.0と1.1のif~else文内の両方に追加)

・タッチイベントを検出して対応する3つのメソッド



参考文献

はじめてのiPhone3プログラミングはじめてのiPhone3プログラミング
(2009/12/17)
Dave Mark、Jeff LaMarche 他

商品詳細を見る

Beginning Ios 6 Development: Exploring the Ios SdkBeginning Ios 6 Development: Exploring the Ios Sdk
(2012/12/26)
David Mark、Jack Nutting 他

商品詳細を見る






bose_soundsport_free
0 Comments
Leave a comment
管理者にだけ表示を許可する
Top
0 Trackbacks
Top
Calendar
09 | 2017/10 | 11
Sun Mon Tue Wed Thu Fri Sat
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 - - - -
Recent Articles
iTunes


Swift
Categories
Tips
Profile

水月杏香

Author:水月杏香
永遠の初心者プログラマ。

Wish List
WACOM


ARC
Technical Q&A
情報プロパティリストキー
Start Developing iOS Apps Today
BOSE

bose_soundsport_free
Reference
NSApplicationDelegateプロトコル
NSArrayクラス
NSAutoreleasePoolクラス
NSBundleクラス
NSBundle UIKit追加分
NSCalendarクラス
NSCoderクラス
NSCodingプロトコル
NSCopyingプロトコル
NSDataクラス
NSDateクラス
NSDateFormatterクラス
NSDictionaryクラス
NSEntityDescriptionクラス
NSEnumeratorクラス
NSErrorクラス
NSExceptionクラス
NSFetchRequestクラス
NSFileHandleクラス
NSFileManagerクラス
NSIndexPathクラス
NSIndexPath UIKit追加分
NSKeyedArchiverクラス
NSKeyedUnarchiverクラス
NSKeyValueCodingプロトコル
NSLocaleクラス
NSManagedObjectクラス
NSManagedObjectContextクラス
NSManagedObjectModelクラス
NSMutableArrayクラス
NSMutableCopyingプロトコル
NSMutableDictionaryクラス
NSMutableSetクラス
NSNotificationクラス
NSNotificationCenterクラス
NSNullクラス
NSNumberクラス
NSObjectクラス
NSObject UIKit追加分
NSObjectプロトコル
NSPersistentStoreクラス
NSPersistentStoreCoordinatorクラス
NSPredicateクラス
NSPropertyListSerializationクラス
NSRunLoopクラス
NSSetクラス
NSStringクラス
NSString UIKit追加分
NSTimerクラス
NSTimeZoneクラス
NSURLクラス
NSURLProtectionSpaceクラス
NSURLRequestクラス
NSUserDefaultsクラス
NSValueクラス

UIActionSheetクラス
UIActionSheetDelegateプロトコル
UIActivityIndicatorViewクラス
UIAlertViewクラス
UIAlertViewDelegateプロトコル
UIApplicationクラス
UIApplicationDelegateプロトコル
UIBarButtonItemクラス
UIBarItemクラス
UIButtonクラス
UIColorクラス
UIControlクラス
UIDatePickerクラス
UIDeviceクラス
UIEventクラス
UIFontクラス
UIGestureRecognizerクラス
UIImageクラス
UIImageViewクラス
UIKit Function
UILabelクラス
UINavigationControllerクラス
UINavigationItemクラス
UIPickerViewクラス
UIPickerViewDataSourceプロトコル
UIPickerViewDelegateプロトコル
UIPinchGestureRecognizerクラス
UIResponderクラス
UIScreenクラス
UIScrollViewクラス
UISearchBarクラス
UISearchBarDelegateプロトコル
UISegmentedControlクラス
UISliderクラス
UISwipeGestureRecognizerクラス
UISwitchクラス
UITableViewクラス
UITableViewCellクラス
UITableViewControllerクラス
UITableViewDataSourceプロトコル
UITableViewDelegateプロトコル
UITapGestureRecognizerクラス
UITextFieldクラス
UITextFieldDelegateプロトコル
UITextInputTraitsプロトコル
UITextViewクラス
UITextViewDelegateプロトコル
UIToolbarクラス
UITouchクラス
UIViewクラス
UIViewControllerクラス
UIWebViewクラス
UIWebViewDelegateプロトコル
UIWindowクラス

AVAudioPlayerクラス
AVAudioPlayerDelegateプロトコル

CADisplayLinkクラス
CAEAGLLayerクラス
CALayerクラス

CGAffineTransform
CGBitmapContext
CGColor
CGColorSpace
CGContext
CGGeometry
CGImage
CGPath

EAGLContextクラス
EAGLDrawableプロトコル

Foundation Constants
Foundation Data Types
Foundation Functions

MPMediaItemクラス
MPMediaItemArtworkクラス
MPMediaPlaylistクラス
MPMediaPropertyPredicateクラス
MPMediaQueryクラス
MPMusicPlayerControllerクラス

Randomization Services

System Sound Services
Amazon


OpenGL ES
SQLite
Monthly Archives
Recent Comments
Recent TrackBacks
RSS Link
Visitors
QR Code
QR