Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 09 Nov 2011
Offline Last Active Nov 15 2011 02:53 PM

Posts I've Made

In Topic: Repeat texture with GLKit

11 November 2011 - 05:28 AM

Thanks for your proposal, but same result with i or f, a black screen.
Here is big part of my class Shape.m, maybe it helps ? This code works and show a texture, but I don't find the way to repeat.

//Set the texture image
-(void)setTextureImage:(UIImage *)image {    NSError *error;

    texture = [GLKTextureLoader textureWithCGImage:image.CGImage options:nil error:&error];

    if (error) {

        NSLog(@"Error loading texture from image: %@",error);



// Set the texture coordinates

- (GLKVector2 *)textureCoordinates {

    if (textureCoordinateData == nil)

        textureCoordinateData = [NSMutableData dataWithLength:sizeof(GLKVector2)*self.numVertices];

    return [textureCoordinateData mutableBytes];


// called on each shape

-(void)renderInScene:(P3Scene *)scene {

    effect = [[GLKBaseEffect alloc] init];

    if (useConstantColor) {

        effect.useConstantColor = YES;

        effect.constantColor = color;


    //set the scene with the projectionMatrix we need

    effect.transform.projectionMatrix = scene.projectionMatrix;

    //translate, rotate and then scale

    modelviewMatrix = GLKMatrix4Multiply(GLKMatrix4MakeTranslation(position.x, position.y, 0), GLKMatrix4MakeRotation(rotation, 0, 0, 1));

    modelviewMatrix = GLKMatrix4Multiply(modelviewMatrix, GLKMatrix4MakeScale(scale.x, scale.y, 1));

    effect.transform.modelviewMatrix = modelviewMatrix;    

    //set a texture if exists    

    if (texture != nil) {

        effect.texture2d0.envMode = GLKTextureEnvModeReplace;

        effect.texture2d0.target = GLKTextureTarget2D;

        effect.texture2d0.name = texture.name;


    [effect prepareToDraw];    

    [self render];


//called in the renderInScene method

-(void)render {

    //enable transparency for sprites




    //tells the shader that we are going to use vertex position data.


    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, self.vertices);

    //set the color of each vertices

    if (!useConstantColor) {


        glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, self.vertexColors);


    if (texture != nil) {



        glBindTexture(GL_TEXTURE_2D, texture.name);

        glTexCoordPointer(3, GL_FLOAT, 0, self.textureCoordinates);


        //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );

        //glBindTexture( GL_TEXTURE_2D, texture.name );



        glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, self.textureCoordinates);




   glDrawArrays(GL_TRIANGLE_FAN, 0, self.numVertices);

    //is cleanup code that tells the shader that we’re done using vertex position data.


    if (!useConstantColor)


    if (texture != nil)




Is that code helping ? Thank you very much !

Ah I just spotted that you're using glTexParameterf which indicates that you will pass a float (hence the f at the end) to the function as a parameter. But the parameter GL_REPEAT is an enum (or an int) and thus you should use glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

In Topic: Repeat texture with GLKit

10 November 2011 - 07:12 AM

Ok, but I think that the error is that I've tried to add properties in openGL to texture loaded in GLKit.

To my mind, the texture.name is OK, because I successfully display it to my view.
The problem is that when I add the line


before , between, or after the lines

(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, self.textureCoordinates);

the view is empty and no texture is loaded.
So everything is cool without the glTextParameterf but not repeated, and it is empty with the glTextParameterf.


The error your getting is from openGL itself
2011-11-09 20:10:28.614 ********[16309:207] GL ERROR: 0x0500

a quick look trough gl.h states that error 0x0500 is GL_INVALID_ENUM.
It could be that texture.name is not a valid enum or the fact that your binding it after glTexParameterf.
Alternatively it could be because of quirks in openGL ES, but i don't think so.

In Topic: Repeat texture with GLKit

10 November 2011 - 02:42 AM

My code works without those lines, and don't work with those lines :

[font=monospace][size=2][color="#e2e2e2"]glTexParameterf[/color][color=#B5973B]([/color][color=#E2E2E2] GL_TEXTURE_2D[/color][color=#B5973B],[/color][color=#E2E2E2] GL_TEXTURE_WRAP_S[/color][color=#B5973B],[/color][color=#E2E2E2] GL_REPEAT [/color][color=#B5973B]);[/color][/size][/font]
[font=monospace][size=2][color=#E2E2E2]glBindTexture[/color][color=#B5973B]([/color][color=#E2E2E2] GL_TEXTURE_2D[/color][color=#B5973B],[/color][color=#E2E2E2] texture[/color][color=#B5973B].[/color][color=#E2E2E2]name [/color][color=#B5973B]);[/color][/size][/font]

This is the reason why I think there is a sort of conflict, and maybe a glkit way to repeat a texture, without glTexParameterf ?

Your problem is not the repeating of the texture, you already do the correct steps for that.

Or does the code work if you leave out the glTexParameteri call? I bet it doesn't

In Topic: Repeat texture with GLKit

09 November 2011 - 05:16 PM

Hi Caste, and thank you for your reply.

Thank you for your link to the docs.

The fact is that I don't find in the class GLKTextureLoader how to change WRAP_S and WRAP_T to GL_REPEAT.

It seems like there is a conflict if I use :


Thanks again,


Well as the error message you posted is already thrown in the 3rd line of code, your texture is not even loaded and will thus not be repeated as well.

You should make sure that your texture is loaded correctly (or properly created by hand). So either the file is not found if you load the UIImage from disk, or while using CGContextCreateBitmap you missed something..

If you're sure that the image you pass to your setTextureImage function is valid, you probably should have a look at the docs.

Repeating the texture is easy as soon as it loads, setting WRAP_S and WRAP_T to GL_REPEAT as you already did for _S, and then assign texture coordinates that are bigger than 1, so e.g. a rectangle where the texture coordinates range from 0x0 to 2x2 will have the image 4 times on it.