Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Texture repeating results in black objects


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Vorel512   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 April 2012 - 03:27 AM

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!

Sponsor:

#2 JoJoSim1   Members   -  Reputation: 173

Like
0Likes
Like

Posted 12 April 2012 - 03:59 AM

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);


#3 Vorel512   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 April 2012 - 04:18 AM


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!

#4 JoJoSim1   Members   -  Reputation: 173

Like
0Likes
Like

Posted 12 April 2012 - 04:29 AM

does your texture has dimensions of power 2 ?
and set glColor to 1,1,1,1

#5 Vorel512   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 April 2012 - 05:13 AM

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.

#6 JoJoSim1   Members   -  Reputation: 173

Like
0Likes
Like

Posted 12 April 2012 - 06:03 AM

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.

#7 Vorel512   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 April 2012 - 06:22 AM

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.

#8 Vorel512   Members   -  Reputation: 130

Like
0Likes
Like

Posted 16 April 2012 - 05:11 AM

Okay, so...

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

#9 Vorel512   Members   -  Reputation: 130

Like
0Likes
Like

Posted 16 April 2012 - 06:11 AM

Resolved!

#10 clb   Members   -  Reputation: 1787

Like
0Likes
Like

Posted 16 April 2012 - 11:59 AM

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.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#11 Vorel512   Members   -  Reputation: 130

Like
0Likes
Like

Posted 17 April 2012 - 12:07 AM


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.

Hi clb!

My problem were solved! By the way thanks for the reply!!!!!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS