标准化设备坐标
坐标
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,
};
坐标 + 颜色
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,
};
坐标 + 颜色 + 纹理
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)
关于OpenGL ES 的顶点属性配置 :
// Named vertex attributes for mapping GLKEffects logic to client vertex attrib enables
typedef NS_ENUM(GLint, GLKVertexAttrib)
{
GLKVertexAttribPosition, //坐标
GLKVertexAttribNormal, //这个暂时不知道是什么.
GLKVertexAttribColor, //颜色
GLKVertexAttribTexCoord0, //纹理0
GLKVertexAttribTexCoord1 //纹理1
} NS_ENUM_AVAILABLE(10_8, 5_0);
// 为每个顶点属性创建和初始化VBO
// 下面的例子显示了一个建立position vertex属性的例子。
// 对每个附加的所需属性重复以下步骤: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);
... 对其他所需的顶点属性重复上述步骤
注意 :
顶点索引的描述顺序 :
- 正面 : 逆时针
- 反面 : 顺时针
在Xcode中开发的话. 形式可以为 :
//顶点数据,前三个是顶点坐标, 中间三个是顶点颜色, 最后两个是纹理坐标
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,
};
//顶点索引
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);
//坐标
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 9, (GLfloat *)NULL);
//顶点颜色
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;
}