最终,我试图通过为每个对象提供不同的纹理坐标来使用单个图像来纹理多个对象(六边形)。
使用 OpenGL ES 2.0 和 GLKit,我想出了一个这样做的方法,只是发现由于某种原因,如果我手动 malloc 和初始化顶点数组,对象将不会呈现。我特别不明白为什么这是因为顶点数组的静态分配版本工作得很好。
前面是我正在使用的结构:
typedef struct {
GLKVector3 position;
GLKVector4 color;
GLKVector2 texCoords;
}Vertex;
因此,如果我在六边形的实现中执行此操作,我可以看到应用了纹理的各种六边形:
Vertex _Vertices[] = {
{{ 0, 0, 0}, {1, 1, 1, 0}, {0.50, 0.50}},//Z
{{ 1, 0, 0}, {1, 1, 1, 0}, {1.00, 0.50}},//A
{{ 0.5, -0.866, 0}, {1, 1, 1, 0}, {0.75, 0.00}},//B
{{-0.5, -0.866, 0}, {1, 1, 1, 0}, {0.25, 0.00}},//C
{{-1, 0, 0}, {1, 1, 1, 0}, {0, 0.50}},//D
{{-0.5, 0.866, 0}, {1, 1, 1, 0}, {0.25, 1.00}},//E
{{ 0.5, 0.866, 0}, {1, 1, 1, 0}, {0.75, 1.00}} //F
};
但是,如果我改为注释掉上面的声明并添加一个同名的实例变量(Vertex *Vertices 而不是 Vertex Vertices[],因为我不能直接 malloc 到数组)然后执行以下操作,没有任何渲染:
-(void)setVertices{
_Vertices = malloc(sizeof(Vertex) * 7);
//Z
_Vertices[0].position.x = 0;
_Vertices[0].position.y = 0;
_Vertices[0].position.z = 0;
_Vertices[0].color.r = 1;
_Vertices[0].color.g = 1;
_Vertices[0].color.b = 1;
_Vertices[0].color.a = 0;
_Vertices[0].texCoords.x = 0.5;
_Vertices[0].texCoords.y = 0.5;
//A
_Vertices[1].position.x = 1;
_Vertices[1].position.y = 0;
_Vertices[1].position.z = 0;
_Vertices[1].color.r = 1;
_Vertices[1].color.g = 1;
_Vertices[1].color.b = 1;
_Vertices[1].color.a = 0;
_Vertices[1].texCoords.x = 1;
_Vertices[1].texCoords.y = 0.5;
//B
_Vertices[2].position.x = 0.5;
_Vertices[2].position.y = -0.866;
_Vertices[2].position.z = 0;
_Vertices[2].color.r = 1;
_Vertices[2].color.g = 1;
_Vertices[2].color.b = 1;
_Vertices[2].color.a = 0;
_Vertices[2].texCoords.x = 0.75;
_Vertices[2].texCoords.y = 0.00;
//C
_Vertices[3].position.x = -0.5;
_Vertices[3].position.y = -0.866;
_Vertices[3].position.z = 0;
_Vertices[3].color.r = 1;
_Vertices[3].color.g = 1;
_Vertices[3].color.b = 1;
_Vertices[3].color.a = 0;
_Vertices[3].texCoords.x = 0.25;
_Vertices[3].texCoords.y = 0;
//D
_Vertices[4].position.x = -1;
_Vertices[4].position.y = 0;
_Vertices[4].position.z = 0;
_Vertices[4].color.r = 1;
_Vertices[4].color.g = 1;
_Vertices[4].color.b = 1;
_Vertices[4].color.a = 0;
_Vertices[4].texCoords.x = 0;
_Vertices[4].texCoords.y = 0.5;
//E
_Vertices[5].position.x = -0.5;
_Vertices[5].position.y = 0.866;
_Vertices[5].position.z = 0;
_Vertices[5].color.r = 1;
_Vertices[5].color.g = 1;
_Vertices[5].color.b = 1;
_Vertices[5].color.a = 0;
_Vertices[5].texCoords.x = 0.25;
_Vertices[5].texCoords.y = 1;
//F
_Vertices[6].position.x = 0.5;
_Vertices[6].position.y = 0.866;
_Vertices[6].position.z = 0;
_Vertices[6].color.r = 1;
_Vertices[6].color.g = 1;
_Vertices[6].color.b = 1;
_Vertices[6].color.a = 0;
_Vertices[6].texCoords.x = 0.75;
_Vertices[6].texCoords.y = 1;
}
这是我的设置功能:
-(void)setupGL{
self.effect = [[GLKBaseEffect alloc] init];
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(_Vertices), _Vertices, GL_STATIC_DRAW);
glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_Indices), _Indices, GL_STATIC_DRAW);
}
这是我的绘图功能:
-(void)draw{
glPushGroupMarkerEXT(0,"drawHex");
_effect.texture2d0.envMode = GLKTextureEnvModeReplace;
_effect.texture2d0.target = GLKTextureTarget2D;
_effect.texture2d0.name = _texture.name;
[self.effect prepareToDraw];
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, color));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, texCoords));
//draw
glDrawElements(GL_TRIANGLES, sizeof(_Indices)/sizeof(_Indices[0]),GL_UNSIGNED_BYTE, 0);
glPopGroupMarkerEXT();
}
如果重要,则在对象外部创建一次纹理,然后传入指向它的指针。像这样创建纹理:
-(void)setTextureImage:(UIImage *)image {
NSError *error;
_hexTexture = [GLKTextureLoader textureWithCGImage:image.CGImage options:nil error:&error];
if (error) {
NSLog(@"Error loading texture from image: %@",error);
}}
我对所有这些都是新手,试图弄清楚这里发生了什么变得非常令人沮丧。
在我的大量搜索中,我看到了一些关于“捕获 opengl es 框架”的信息,但它似乎没有为 Xcode 中的 iphone 模拟器启用,所以我无法深入研究它:(
我真的很想知道为什么会这样。
如果有帮助,我可以提供更多代码或解释。
有什么想法吗?