Reputation: 93
I am developing a game in which I have to shake a dice to get number. I am using glkit to make a cube and texturing that cube by GLKBaseEffect. Well ! I want a cube having diffrent textured images on each faces so that it can simulates a dice. I want each faces of cube show different dice image like one face show number 1 another face show number two and so on.
I am pasting my code here.
- (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);
}
and calling draw elements method from here in 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);
}
How can I achieve this?
Upvotes: 0
Views: 728
Reputation: 1
I do this by layering, or having more objects. So, a cube, and then two squares that are just barely on the skin of the cube. If the cube is 2 x 2 x 2, just make the picture vertex 2.01, and so it is like cake icing
So you are not texturing the cube, you are adding more objects. Here is a video example - https://www.youtube.com/watch?v=lh2Zc8kHFbU&list=UUu5WfUbKdhLCW9aPq3WZsNA
Upvotes: 0
Reputation: 24675
GLKit has a limited number of textures allowed (2, I think), so you'll need what's called a texture-map. Basically, you set up a texture like this:
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
And then you set your texture coordinates for each vertex to point to the sub-portion of the texture that you want to use.
NOTE: Even if GLKit supported more textures, as many OpenGL implementations do, this method is MUCH faster than loading a gazillion textures and switching them all the time. glBind()
is a relatively slow operation.
Also note: Things are more efficient if your texture image sides are powers of 2 (2, 4, 8, 16, 32, ...) It's ok to have blank (unused) spots in your texture image. I typically put a big red blotch in the unused part, so I can tell if it inadvertently shows up on anything.
So your dice-texture might be, say, 32px tall, and 512 (32*8) wide, but you only use the first 192 (32*6) of it and have a set of six 32x32 textures all in one image.
Upvotes: 1