標(biāo)準(zhǔn)化設(shè)備坐標(biāo)
坐標(biāo)
GLfloat vertex[4*3] =
{
-0.5f, 0.5f, 0.0f, //x,y,z
0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
};
坐標(biāo) + 顏色
GLfloat vertex[ 4* (3+4)] =
{
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 1.0f, //x,y,z, r,g,b,a
0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 1.0f,
};
坐標(biāo) + 顏色 + 紋理
GLfloat vertex[ 4* (3+4+2)] =
{ //x,y,z, r,g,b,a s,t
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f,
};
VBO - VAO - EBO(或者叫IBO)
關(guān)于OpenGL ES 的頂點(diǎn)屬性配置 :
// Named vertex attributes for mapping GLKEffects logic to client vertex attrib enables
typedef NS_ENUM(GLint, GLKVertexAttrib)
{
GLKVertexAttribPosition, //坐標(biāo)
GLKVertexAttribNormal, //這個(gè)暫時(shí)不知道是什么.
GLKVertexAttribColor, //顏色
GLKVertexAttribTexCoord0, //紋理0
GLKVertexAttribTexCoord1 //紋理1
} NS_ENUM_AVAILABLE(10_8, 5_0);
// 為每個(gè)頂點(diǎn)屬性創(chuàng)建和初始化VBO
// 下面的例子顯示了一個(gè)建立position vertex屬性的例子纺铭。
// 對(duì)每個(gè)附加的所需屬性重復(fù)以下步驟:normal粘拾,color,texCoord0弓乙,texCoord1渔欢。
glGenBuffers(1, &positionVBO);
glBindBuffer(GL_ARRAY_BUFFER, positionVBO);
glBufferData(GL_ARRAY_BUFFER, vboSize, dataBufPtr, GL_STATIC_DRAW);
glVertexAttribPointer(GLKVertexAttribPosition, size, type, normalize, stride, NULL);
glEnableVertexAttribArray(GLKVertexAttribPosition);
... 對(duì)其他所需的頂點(diǎn)屬性重復(fù)上述步驟
注意 :
頂點(diǎn)索引的描述順序 :
- 正面 : 逆時(shí)針
- 反面 : 順時(shí)針
在Xcode中開(kāi)發(fā)的話. 形式可以為 :
//頂點(diǎn)數(shù)據(jù)游桩,前三個(gè)是頂點(diǎn)坐標(biāo)桩皿, 中間三個(gè)是頂點(diǎn)顏色, 最后兩個(gè)是紋理坐標(biāo)
GLfloat attrArr[] = {
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
};
//頂點(diǎn)索引
GLuint indices[] = {
0, 1, 2,
1, 2, 3,
};
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(attrArr), attrArr, GL_STATIC_DRAW);
GLuint index;
glGenBuffers(1, &index);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
//坐標(biāo)
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 9, (GLfloat *)NULL);
//頂點(diǎn)顏色
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 9, (GLfloat *)NULL + 3);
//紋理屬性0
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 9, (GLfloat *)NULL + 3 + 4);
//紋理屬性1
glEnableVertexAttribArray(GLKVertexAttribTexCoord1);
glVertexAttribPointer(GLKVertexAttribTexCoord1, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 9, (GLfloat *)NULL + 3 + 4);
//加載紋理
NSString* filePath0 = [[NSBundle mainBundle] pathForResource:@"texture0.jpg" ofType:@""];
NSDictionary* options0 = [NSDictionary dictionaryWithObjectsAndKeys:@(1), GLKTextureLoaderGenerateMipmaps, nil];
GLKTextureInfo* textureInfo0 = [GLKTextureLoader textureWithContentsOfFile:filePath0 options:options0 error:nil];
self.mEffect.texture2d0.enabled = GL_TRUE;
self.mEffect.texture2d0.name = textureInfo0.name;
//加載紋理
NSString* filePath1 = [[NSBundle mainBundle] pathForResource:@"texture1.png" ofType:@""];
NSDictionary* options1 = [NSDictionary dictionaryWithObjectsAndKeys:@(1), GLKTextureLoaderOriginBottomLeft, nil];
GLKTextureInfo* textureInfo1 = [GLKTextureLoader textureWithContentsOfFile:filePath1 options:options1 error:nil];
self.mEffect.texture2d1.enabled = GL_TRUE;
self.mEffect.texture2d1.name = textureInfo1.name;
PS : self.mEffect
- (GLKBaseEffect *)mEffect {
if (_mEffect == nil) {
_mEffect = [[GLKBaseEffect alloc]init];
}
return _mEffect;
}