Texture repeating results in black objects

Started by
9 comments, last by Vorel512 12 years ago
Hi,

I've set up an image loading function, and when I want to repeat some textures, only black objects are rendered.

Here is my code: (the texture variable is GLKTextureInfo)

- (void) setTexture:(NSString*)fileName ofType:(NSString*)type
{
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:type];

NSError *error;

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:GLKTextureLoaderOriginBottomLeft];


texture = [GLKTextureLoader textureWithContentsOfFile:path
options:options error:&error];

if (texture == nil)
NSLog(@"Error loading texture: %@", [error localizedDescription]);
glActiveTexture(GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, texture.name );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);

tex = [[GLKEffectPropertyTexture alloc] init];
tex.enabled = YES;
tex.envMode = GLKTextureEnvModeDecal;
tex.name = texture.name;

}


Any help or suggestion is welcome!
Advertisement
i would use glTexParameteri for integer not float
did you try to set
GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MIN_FILTER
to GL_LINEAR ?

glTexParameteri(GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_MIN_FILTER,GL_LINEAR);


glBindTexture( GL_TEXTURE_2D, texture.name );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);


This does not helped the problem, the black stuff stays.
I've tried to put the bind after the texparams, and removed the activetexture, but these steps also did not help.

Thanx for the reply JoJoSim1!
does your texture has dimensions of power 2 ?
and set glColor to 1,1,1,1
My texture is 400x360, but I've tried it with 32x32 and the problem still remains.
I do not get the point why should I change the color, the color is grey, and the object I want to texture is at z position -0.6 and it is a rectangle, which I would like to (repeatedly) texture. I can see the gray background on the sides of the picture rectangle.
glColor can be used to tint your texture with glColor this results to black texture.
Your setup looks correct to me. Maybe you could post your rendering code.
I'm wondering why the gray sides a noticeable.
Here is my rendering code:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

Sprites *fgh;
Attribute_holder *asd;

for (unsigned int i=0; [gManager getAttributeArrayCount]>i; i++)
{
asd = [gManager getAttributeAtIndex:i];
if ([asd getShow] == 1)
{
fgh = [gManager getSpriteAtIndex:[asd getSpriteID]];
glBindVertexArrayOES([fgh getVertexArray]);

glUseProgram(_program);
[asd.effect prepareToDraw];

glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, 0);

//for debugging
//glDrawArrays(GL_LINE_LOOP, 0, 6);
}
}
}

I am storing the positions, isdisplayable , etc variables in Attribute_holder, and the vertexes and other stuff like textures in Sprites. gManager is my NSMutableArray where I store them like:
attrib0 - sprite0
attrib1 - sprite0
attrib2 - sprite1

I load the vertexes/uvs/normals to a main buffer and the textures to a "child" buffer:

- (void) createBuffers:(unsigned short) num
{
IndicesArray[0] = 0;
IndicesArray[1] = 1;
IndicesArray[2] = 2;
IndicesArray[3] = 3;

glGenVertexArraysOES(num, &_vertexArray);
glBindVertexArrayOES(_vertexArray);

glGenBuffers(num, &_vertexBuffer);
glGenBuffers(num, &_indexBuffer);

glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(IndicesArray), IndicesArray, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(VectorArray), VectorArray, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(0));

glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(12));

glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(24));

glBindVertexArrayOES(0);

And after the buffering I bind the texture name to the attrib array.
tempAttrib.effect.texture2d0.name = [tempSprite [getChildTexture:0];
}


And here is the object for the textures:

- (void) setTexture:(NSString*)fileName ofType:(NSString*)type
{
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:type];

NSError *error;

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

texture = [GLKTextureLoader textureWithContentsOfFile:path
options:options error:&error];

if (texture == nil)
NSLog(@"Error loading texture: %@", [error localizedDescription]);

glBindTexture( GL_TEXTURE_2D, texture.name );

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);

tex = [[GLKEffectPropertyTexture alloc] init];
tex.enabled = YES;
tex.envMode = GLKTextureEnvModeDecal;
tex.name = texture.name;

//glActiveTexture(GL_TEXTURE0);
//glBindTexture(GL_TEXTURE_2D, 0);
}


The weird thing is that if I remove the texparams and stuff the textures rendered correctly.
Okay, so...

If I put the texture parameters before the texture creation the scene renders perfect.
I assume, that GLKTextureLoader "lock" the params.
Resolved!

Okay, so...

If I put the texture parameters before the texture creation the scene renders perfect.
I assume, that GLKTextureLoader "lock" the params.


You mean, you put your glTexParameteri calls to occur before the calls to glBindTexture and/or the line [GLKTextureLoader textureWithContentsOfFile... ? This will result in the glTexParameteri calls applied to whatever old texture that happened to be bound to GL_TEXTURE_2D, and not the new texture you loaded.

In OpenGL ES, to enable GL_REPEAT, the texture MUST have width and height power of 2. If not, GLES is specified to give silent failure at your face, and read black from the texture instead. See the Notes part at glTexParameter documentation pages to confirm this.

With your original code, try replacing GL_REPEAT with GL_CLAMP_TO_EDGE to test that the texture will then render properly.

This topic is closed to new replies.

Advertisement