0

Here is my code:

-(void) mergeWithImage:(UIImage*) image{
    if(image==nil){
        return;
    }
glPushMatrix();
    glColor4f(256,
              256,
              256,
              1.0);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glGenTextures(1, &stampTexture);
    glBindTexture(GL_TEXTURE_2D, stampTexture);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);



    GLuint imgwidth = CGImageGetWidth(image.CGImage);
    GLuint imgheight = CGImageGetHeight(image.CGImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    void *imageData = malloc( imgheight * imgwidth * 4 );
    CGContextRef context2 = CGBitmapContextCreate( imageData, imgwidth, imgheight, 8, 4 * imgwidth, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
    CGContextTranslateCTM (context2, 0, imgheight);
    CGContextScaleCTM (context2, 1.0, -1.0);
    CGColorSpaceRelease( colorSpace );
    CGContextClearRect( context2, CGRectMake( 0, 0, imgwidth, imgheight ) );
    CGContextTranslateCTM( context2, 0, imgheight - imgheight );
    CGContextDrawImage( context2, CGRectMake( 0, 0, imgwidth, imgheight ), image.CGImage );

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imgwidth, imgheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

    CGContextRelease(context2);


    free(imageData);

    static const GLfloat texCoords[] = {
        0.0, 1.0,
        1.0, 1.0,
        0.0, 0.0,
        1.0, 0.0
    };

    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);   


    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    /*

     These array would need to be changed if the size of the paintview changes. You must make sure that all image imput is 64x64, 256x256, 512x512 or 1024x1024.  In this we are using 512, but you can use 1024 as follows:

     use the numbers:
     {
     0.0, height, 0.0,
     1024, height, 0.0,
     0.0, height-1024, 0.0,
     1024, height-1024, 0.0
     }
     */


    static const GLfloat vertices[] = {
        0.0, 1024, 0.0,
        1024, 1024, 0.0,
        0.0, 0, 0.0,
        1024, 0, 0.0
    };

    static const GLfloat normals[] = {
        0.0, 0.0, 1024,
        0.0, 0.0, 1024,
        0.0, 0.0, 1024,
        0.0, 0.0, 1024
    };

glBindTexture(GL_TEXTURE_2D, stampTexture);
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glNormalPointer(GL_FLOAT, 0, normals);
    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glPopMatrix();

glDeleteTextures( 1, &stampTexture );
//set back the brush
glBindTexture(GL_TEXTURE_2D, brushTexture);

glColor4f(lastSetRed,
          lastSetGreen,
          lastSetBlue,
          1.0);

// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

It works fine if my image is 1024x1024 but if I have an image with size 1024x768, what's the value to assign at vertices and normals?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241

2 Answers2

2

what's the value to assign at vertices and normals?

Those don't matter as they do not (directly) interfer with texture coordinates. BTW normals should always be unit length. Also if you do not want to apply lighting you don't need normals. And if you do want to apply lighting normals must be unit length.

Texture coordinates for regular textures are always in the range [0;1] no matter what the aspect ratio of your image is. The vertex positions should be chosen in accordance with the projection you use. You could for example use a

glOrtho(0, texture_width, 0, textture_height, …)

projection and then your vertices would be {0, texture_width}×{0, texture_height} either. There's no definitive answer to your problem.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
0

add these lines when you bind your texture

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //IMPORTANT FOR NON POWER OF 2 TEXTURES
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Fonix
  • 11,447
  • 3
  • 45
  • 74
  • ok but the values in the vertices and normals? I insert the suggest in the block comment? – cyclingIsBetter Jan 18 '13 at 10:44
  • you shouldnt need to change those, the textures image is mapped to the texture coordinates, which are always between 0 - 1 on the x and y. i suppose im not actually sure what you are doing so maybe you do, but i cant tell you that. normally you wouldnt though – Fonix Jan 18 '13 at 10:45
  • with your code now I can see my image in the texture and it's ok, this image size is 1024x768; now I'm using this values: static const GLfloat vertices[] = { 0.0, 1024, 0.0, 1024, 1024, 0.0, 0.0, 0, 0.0, 1024, 0, 0.0 }; static const GLfloat normals[] = { 0.0, 0.0, 1024, 0.0, 0.0, 1204, 0.0, 0.0, 1024, 0.0, 0.0, 1024 }; and I have not my image in the centre, but it seems to be of size 1024x1024, what can I change? – cyclingIsBetter Jan 18 '13 at 10:52
  • you will have to read in the dimensions of the texture before hand and change your vertices accordingly, opengl wont do that automatically for you – Fonix Jan 18 '13 at 10:56
  • it looks like you already know how in your code... `GLuint imgwidth = CGImageGetWidth(image.CGImage); GLuint imgheight = CGImageGetHeight(image.CGImage);` – Fonix Jan 18 '13 at 11:08
  • ok but my image is bigger of my screen...I should resize texture or my image? – cyclingIsBetter Jan 18 '13 at 11:25
  • if your image is going off your screen, i think that has more to do with how your vertices are positioned. do you mean the texture is getting cut off of your quad that you are texturing? note your vertices do not need to be (0, 0, 1024) etc. if your texture is 1024. those are unrelated. i think you need to play around with what you have to understand how the texture coordinates, the textures image size, and how the vertices all interact. you seem to not be understanding how the texture coordinates work – Fonix Jan 18 '13 at 11:30
  • then I should "play" with glVertexPointer and its values right? – cyclingIsBetter Jan 18 '13 at 11:42
  • yeah, maybe [this](http://stackoverflow.com/questions/5532595/how-do-opengl-texture-coordinates-work) will help explain – Fonix Jan 18 '13 at 11:49
  • Finally I set all in this way static const GLfloat vertices[] = { 0.0, 768, 0.0, 1024, 768, 0.0, 0.0, 0, 0.0, 1024, 0, 0.0 }; and my image is perfect in the texture, thanks a lot... – cyclingIsBetter Jan 18 '13 at 16:08