效果圖
本效果在上一章文章代碼(
11-3.GLKit-索引繪圖案例
)的基礎(chǔ)加以改造
主要需要修改ViewController
里的render
渲染圖形函數(shù)中新增紋理數(shù)據(jù)及操作
- 頂點(diǎn)數(shù)據(jù)中增加紋理坐標(biāo)
//1.頂點(diǎn)數(shù)據(jù)
// GLfloat attrArr[] =
// {
// -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, //左上
// 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, //右上
// -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, //左下
//
// 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, //右下
// 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, //頂點(diǎn)
// };
//前3個(gè)元素唧瘾,是頂點(diǎn)數(shù)據(jù);中間3個(gè)元素,是頂點(diǎn)顏色值,最后2個(gè)是紋理坐標(biāo)
GLfloat attrArr[] =
{
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f,//左上
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,//右上
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,//左下
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,//右下
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f,//頂點(diǎn)
};
- 修改連續(xù)頂點(diǎn)之間的偏移量
從6個(gè)偏移量變成8個(gè),因?yàn)轫旤c(diǎn)數(shù)組增加了數(shù)據(jù)
//使用頂點(diǎn)數(shù)據(jù)
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8, NULL);//從6個(gè)偏移量變成8個(gè),因?yàn)轫旤c(diǎn)數(shù)組增加了數(shù)據(jù)
//使用顏色數(shù)據(jù)
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8, (GLfloat *)NULL + 3);//從6個(gè)偏移量變成8個(gè),因?yàn)轫旤c(diǎn)數(shù)組增加了數(shù)據(jù)
- 使用紋理數(shù)據(jù),即將紋理坐標(biāo)傳入頂點(diǎn)著色器
//使用紋理數(shù)據(jù)
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8, (GLfloat *)NULL + 6);
- 獲取紋理圖片 & 加載紋理 & 導(dǎo)入圖片(本文使用
jielun.jpg
)
// ------獲取紋理路路徑
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"jielun" ofType:@"jpg"];
NSDictionary *option = [NSDictionary dictionaryWithObjectsAndKeys:@"1", GLKTextureLoaderOriginBottomLeft, nil];
GLKTextureInfo *info = [GLKTextureLoader textureWithContentsOfFile:filePath options:option error:nil];
- 在
effect
中設(shè)置紋理相關(guān)信息
//------著色器
self.mEffect = [[GLKBaseEffect alloc] init];
self.mEffect.texture2d0.enabled = GL_TRUE;
self.mEffect.texture2d0.name = info.name;
完整render函數(shù)代碼
-(void)render
{
//1.頂點(diǎn)數(shù)據(jù)
// GLfloat attrArr[] =
// {
// -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, //左上
// 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, //右上
// -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, //左下
//
// 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, //右下
// 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, //頂點(diǎn)
// };
//前3個(gè)元素,是頂點(diǎn)數(shù)據(jù);中間3個(gè)元素亲善,是頂點(diǎn)顏色值,最后2個(gè)是紋理坐標(biāo)
GLfloat attrArr[] =
{
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f,//左上
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,//右上
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,//左下
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,//右下
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f,//頂點(diǎn)
};
//2.繪圖索引
GLuint indices[] =
{
0, 3, 2,
0, 1, 3,
0, 2, 4,
0, 4, 1,
2, 3, 4,
1, 4, 3,
};
//頂點(diǎn)個(gè)數(shù)
self.count = sizeof(indices) /sizeof(GLuint);
//將頂點(diǎn)數(shù)組放入數(shù)組緩沖區(qū)中 GL_ARRAY_BUFFER
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(attrArr), attrArr, GL_STATIC_DRAW);
//將索引數(shù)組存儲(chǔ)到索引緩沖區(qū) GL_ELEMENT_ARRAY_BUFFER
GLuint index;
glGenBuffers(1, &index);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
//使用頂點(diǎn)數(shù)據(jù)
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8, NULL);//從6個(gè)偏移量變成8個(gè),因?yàn)轫旤c(diǎn)數(shù)組增加了數(shù)據(jù)
//使用顏色數(shù)據(jù)
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8, (GLfloat *)NULL + 3);//從6個(gè)偏移量變成8個(gè),因?yàn)轫旤c(diǎn)數(shù)組增加了數(shù)據(jù)
//使用紋理數(shù)據(jù)
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8, (GLfloat *)NULL + 6);
//獲取紋理路徑
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"jielun" ofType:@"png"];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:@"1",GLKTextureLoaderOriginBottomLeft, nil];
GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithContentsOfFile:filePath options:options error:nil];
//著色器
self.mEffect = [[GLKBaseEffect alloc]init];
self.mEffect.texture2d0.enabled = GL_TRUE;
self.mEffect.texture2d0.name = textureInfo.name;
//投影視圖
CGSize size = self.view.bounds.size;
//float aspect = fabs(size.width / size.height);
float aspect = size.width / size.height;
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(90.0), aspect, 0.1f, 100.0);
projectionMatrix = GLKMatrix4Scale(projectionMatrix, 1.0f, 1.0f, 1.0f);
self.mEffect.transform.projectionMatrix = projectionMatrix;
//模型視圖
GLKMatrix4 modelViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, -2.0f);
self.mEffect.transform.modelviewMatrix = modelViewMatrix;
//定時(shí)器
double seconds = 0.1;
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC, 0.0);
dispatch_source_set_event_handler(timer, ^{
self.XDegree += 0.1f * self.XB;
self.YDegree += 0.1f * self.YB;
self.ZDegree += 0.1f * self.ZB ;
});
dispatch_resume(timer);
}