update: 2012/04/03
reference:
1. 原文:
iPhone Development: OpenGL ES From the Ground Up, Part 2: A Look at Simple Drawing
2. 翻譯:
從零開始學習OpenGL ES之二 – 簡單繪圖概述
簡單繪圖之四: 設定頂點顏色
A. 說明
1. OpenGL ES 允許你將每個頂點使用的顏色置於一個顏色陣列 (color array) 中.
2. 如果你選擇使用顏色陣列,那麼你需要為每個頂點設定顏色(四個 GLfloat 值).
3. 可以通過以下的方法來啟動顏色陣列: glEnableClientState(GL_COLOR_ARRAY);
-----------------------------------------------------------------------------------
B. 開啓 OpenGLESCommon.h 檔案, 修改如下:
....
#pragma mark -
#pragma mark Color3D
#pragma mark -
// 顏色定義
typedef struct {
GLfloat red;
GLfloat green;
GLfloat blue;
GLfloat alpha;
} Color3D;
// 單個顏色
static inline Color3D Color3DMake(CGFloat inRed, CGFloat inGreen, CGFloat inBlue, CGFloat inAlpha)
{
Color3D ret;
ret.red = inRed;
ret.green = inGreen;
ret.blue = inBlue;
ret.alpha = inAlpha;
return ret;
}
// 設定現存顏色的值
static inline void Color3DSet(Color3D *color, CGFloat inRed, CGFloat inGreen, CGFloat inBlue, CGFloat inAlpha)
{
color->red = inRed;
color->green = inGreen;
color->blue = inBlue;
color->alpha = inAlpha;
}
....
-----------------------------------------------------------------------------------
C. 頂點顏色前置作業
1. 開啓 GLView.h 檔案, 修改如下:
....
//@add for protocol
@protocol GLViewDelegate
@required
- (void)setupView:(GLView *)view;
@optional
//@update for drawing
- (void)drawView:(GLView *)view;
- (void)drawTriangle3D; // 畫三角形
- (void)drawSquare; // 畫正方形
- (void)drawVertexColor; // 畫頂點顏色
@end
....
2. 開啓 ViewController.m 檔案, 修改如下:
....
// 畫頂點顏色
- (void)drawVertexColor
{
NSLog(@"ViewController => drawVertexColor");
// Draw code here
}
....
3. 開啓 GLView.m 檔案, 修改如下:
....
- (void)drawView
{
....
//@update for drawing
//[self.delegate drawTriangle3D]; // 畫三角形
//[self.delegate drawSquare]; // 畫正方形
[self.delegate drawVertexColor]; // 畫頂點顏色
....
}
....
-----------------------------------------------------------------------------------
D. 開啓 ViewController.m 檔案, 修改如下:
....
// 畫頂點顏色
- (void)drawVertexColor
{
NSLog(@"ViewController => drawVertexColor");
// Draw code here
Vertex3D vertex1 = Vertex3DMake(0.0, 1.0, -3.0);
Vertex3D vertex2 = Vertex3DMake(1.0, 0.0, -3.0);
Vertex3D vertex3 = Vertex3DMake(-1.0, 0.0, -3.0);
Triangle3D triangle = Triangle3DMake(vertex1, vertex2, vertex3);
Color3D *colors = malloc(sizeof(Color3D) * 3);
Color3DSet(&colors[0], 1.0, 0.0, 0.0, 1.0);
Color3DSet(&colors[1], 0.0, 1.0, 0.0, 1.0);
Color3DSet(&colors[2], 0.0, 0.0, 1.0, 1.0);
glLoadIdentity();
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertexPointer(3, GL_FLOAT, 0, &triangle);
glColorPointer(4, GL_FLOAT, 0, colors);
glDrawArrays(GL_TRIANGLES, 0, 9);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
if (colors != NULL)
free(colors);
}
....
-----------------------------------------------------------------------------------
E. 編譯並執行
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。