OpenGL ES實踐(二)—— 一個簡單的三角形

版本記錄

版本號 時間
V1.0 2018.01.06

前言

OpenGL ES圖形庫項目中一直也沒用過揭厚,最近也想學著使用這個圖形庫,感覺還是很有意思扶供,也就自然想著好好的總結(jié)一下筛圆,希望對大家能有所幫助。下面就開始進行實踐椿浓,寫一些小程序太援。感興趣的可以看上面幾篇文章闽晦。
1. OpenGL ES實踐(一)—— 一個簡單的小程序

功能需求

畫一個簡單的三角形。


功能實現(xiàn)

下面我們就直接看代碼提岔。

1. JJOpenglesVC.h
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>

@interface JJOpenglesVC : GLKViewController

@end
2. JJOpenglesVC.m
#import "JJOpenglesVC.h"

@interface JJOpenglesVC()
{
    GLuint vertexBufferID;
}

@property (strong, nonatomic) GLKBaseEffect *baseEffect;

@end

@implementation JJOpenglesVC

@synthesize baseEffect;

/////////////////////////////////////////////////////////////////
// This data type is used to store information for each vertex
//這個結(jié)構體用于存放頂點數(shù)據(jù)
typedef struct {
    GLKVector3  positionCoords;
}SceneVertex;

/////////////////////////////////////////////////////////////////
// Define vertex data for a triangle to use in example
//定義例子中三角形需要的頂點數(shù)據(jù)
static const SceneVertex vertices[] =
{
    {{-0.5f, -0.5f, 0.0}}, // lower left corner
    {{ 0.5f, -0.5f, 0.0}}, // lower right corner
    {{-0.5f,  0.5f, 0.0}}  // upper left corner
};

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    // Verify the type of view created automatically by the
    // Interface Builder storyboard
    GLKView *view = (GLKView *)self.view;
    NSAssert([view isKindOfClass:[GLKView class]],
             @"View controller's view is not a GLKView");
    
    // Create an OpenGL ES 2.0 context and provide it to the
    // view
    view.context = [[EAGLContext alloc]
                    initWithAPI:kEAGLRenderingAPIOpenGLES3];
    
    // Make the new context current
    [EAGLContext setCurrentContext:view.context];
    
    // Create a base effect that provides standard OpenGL ES 3.0
    // Shading Language programs and set constants to be used for
    // all subsequent rendering
    self.baseEffect = [[GLKBaseEffect alloc] init];
    self.baseEffect.useConstantColor = GL_TRUE;
    self.baseEffect.constantColor = GLKVector4Make(
                                                   0.7f, // Red
                                                   0.6f, // Green
                                                   0.4f, // Blue
                                                   1.0f);// Alpha
    
    // Set the background color stored in the current context
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // background color
    
    // Generate, bind, and initialize contents of a buffer to be
    // stored in GPU memory
    glGenBuffers(1,                // STEP 1
                 &vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER,  // STEP 2
                 vertexBufferID);
    glBufferData(                  // STEP 3
                 GL_ARRAY_BUFFER,  // Initialize buffer contents
                 sizeof(vertices), // Number of bytes to copy
                 vertices,         // Address of bytes to copy
                 GL_STATIC_DRAW);  // Hint: cache in GPU memory
}

/////////////////////////////////////////////////////////////////
// Called when the view controller's view has been unloaded
// Perform clean-up that is possible when you know the view
// controller's view won't be asked to draw again soon.
- (void)viewDidUnload
{
    [super viewDidUnload];
    
    // Make the view's context current
    GLKView *view = (GLKView *)self.view;
    [EAGLContext setCurrentContext:view.context];
    
    // Delete buffers that aren't needed when view is unloaded
    if (0 != vertexBufferID)
    {
        glDeleteBuffers (1,          // STEP 7
                         &vertexBufferID);
        vertexBufferID = 0;
    }
    
    // Stop using the context created in -viewDidLoad
    ((GLKView *)self.view).context = nil;
    [EAGLContext setCurrentContext:nil];
}

#pragma mark - GLKViewDelegate

/////////////////////////////////////////////////////////////////
// GLKView delegate method: Called by the view controller's view
// whenever Cocoa Touch asks the view controller's view to
// draw itself. (In this case, render into a frame buffer that
// shares memory with a Core Animation Layer)
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    [self.baseEffect prepareToDraw];
    
    // Clear Frame Buffer (erase previous drawing)
    glClear(GL_COLOR_BUFFER_BIT);
    
    // Enable use of positions from bound vertex buffer
    glEnableVertexAttribArray(      // STEP 4
                              GLKVertexAttribPosition);
    
    glVertexAttribPointer(          // STEP 5
                          GLKVertexAttribPosition,
                          3,                   // three components per vertex
                          GL_FLOAT,            // data is floating point
                          GL_FALSE,            // no fixed point scaling
                          sizeof(SceneVertex), // no gaps in data
                          NULL);               // NULL tells GPU to start at
    // beginning of bound buffer
    
    // Draw triangles using the first three vertices in the
    // currently bound vertex buffer
    glDrawArrays(GL_TRIANGLES,      // STEP 6
                 0,  // Start with first vertex in currently bound buffer
                 3); // Use three vertices from currently bound buffer
}

@end


功能效果

下面我們看一下功能效果仙蛉。

后記

未完,待續(xù)~~~

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末碱蒙,一起剝皮案震驚了整個濱河市荠瘪,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌赛惩,老刑警劉巖哀墓,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異喷兼,居然都是意外死亡篮绰,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門季惯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來阶牍,“玉大人,你說我怎么就攤上這事星瘾∽吣酰” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵琳状,是天一觀的道長磕瓷。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么接谨? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任肋层,我火速辦了婚禮,結(jié)果婚禮上硕盹,老公的妹妹穿的比我還像新娘。我一直安慰自己叨咖,他們只是感情好瘩例,可當我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著甸各,像睡著了一般垛贤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上趣倾,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天聘惦,我揣著相機與錄音,去河邊找鬼儒恋。 笑死善绎,一個胖子當著我的面吹牛黔漂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播禀酱,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼炬守,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了比勉?” 一聲冷哼從身側(cè)響起劳较,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎浩聋,沒想到半個月后观蜗,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡衣洁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年墓捻,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坊夫。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡砖第,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出环凿,到底是詐尸還是另有隱情梧兼,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布智听,位于F島的核電站羽杰,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏到推。R本人自食惡果不足惜考赛,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望莉测。 院中可真熱鬧颜骤,春花似錦、人聲如沸捣卤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽梯找。三九已至,卻和暖如春益涧,著一層夾襖步出監(jiān)牢的瞬間驯鳖,已是汗流浹背久免。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留扭弧,地道東北人阎姥。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像呼巴,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子御蒲,可洞房花燭夜當晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容