0

我正在开发一个游戏,我必须摇骰子才能获得数字。我正在使用 glkit 制作一个立方体并通过 GLKBaseEffect 对该立方体进行纹理化。出色地 !我想要一个在每个面上都有不同纹理图像的立方体,以便它可以模拟骰子。我希望立方体的每个面都显示不同的骰子图像,例如一个面显示数字 1,另一个面显示数字 2,依此类推。我在这里粘贴我的代码。

- (void)setupGL {

[EAGLContext setCurrentContext:self.context];
glEnable(GL_CULL_FACE);


self.effect = [[GLKBaseEffect alloc] init];

NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithBool:YES],
                          GLKTextureLoaderOriginBottomLeft, 
                          nil];

NSError * error;    
NSString *path = [[NSBundle mainBundle] pathForResource:@"tile_floor" ofType:@"png"];
GLKTextureInfo * info = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if (info == nil) {
    NSLog(@"Error loading file: %@", [error localizedDescription]);
}
self.effect.texture2d0.name = info.name;
self.effect.texture2d0.enabled = true;

// draw one texture per side



// New lines
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);

// Old stuff
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);

// New lines (were previously in draw)
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, TexCoord));

// New line
glBindVertexArrayOES(0); 

}

并在 drawInRects 中从这里调用绘制元素方法

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {

glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
[self.effect prepareToDraw];
glBindVertexArrayOES(_vertexArray);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);

}

任何人都可以帮助我实现这一目标。提前致谢。. . .

4

2 回答 2

1

GLKit 允许的纹理数量有限(我认为是 2 个),因此您需要所谓的纹理贴图。基本上,您设置了这样的纹理:

a a a a a a a a b b b b b b b b c c c c c c c c d d d d d d d d
a a a a a a a a b b b b b b b b c c c c c c c c d d d d d d d d
a a a a a a a a b b b b b b b b c c c c c c c c d d d d d d d d
a a a a a a a a b b b b b b b b c c c c c c c c d d d d d d d d
a a a a a a a a b b b b b b b b c c c c c c c c d d d d d d d d ...
a a a a a a a a b b b b b b b b c c c c c c c c d d d d d d d d
a a a a a a a a b b b b b b b b c c c c c c c c d d d d d d d d
a a a a a a a a b b b b b b b b c c c c c c c c d d d d d d d d

然后为每个顶点设置纹理坐标以指向要使用的纹理的子部分。

注意:即使 GLKit 支持更多纹理,正如许多 OpenGL 实现所做的那样,这种方法比加载大量纹理并一直切换它们要快得多。 glBind()是一个相对缓慢的操作。

另请注意:如果您的纹理图像边是 2 的幂(2、4、8、16、32,...),则事情会更有效。在纹理图像中有空白(未使用)点是可以的。我通常会在未使用的部分放置一个大红色斑点,这样我就可以判断它是否无意中出现在任何东西上。

所以你的骰子纹理可能是 32px 高和 512 (32*8) 宽,但你只使用它的前 192 (32*6) 个并且在一个图像中拥有一组 6 个 32x32 纹理。

于 2013-06-04T20:20:36.813 回答
0

我通过分层或拥有更多对象来做到这一点。所以,一个立方体,然后是两个刚刚在立方体表皮上的正方形。如果立方体是 2 x 2 x 2,只要把图片的顶点设为 2.01,就像蛋糕糖霜一样

因此,您不是在为立方体添加纹理,而是在添加更多对象。这是一个视频示例 - https://www.youtube.com/watch?v=lh2Zc8kHFbU&list=UUu5WfUbKdhLCW9aPq3WZsNA

如果您需要代码,请发送电子邮件至 StephenMWyatt2@gmail.com。

于 2014-11-16T17:49:20.333 回答