OpenGL:在纹理中设置图像

时间:2013-01-18 10:27:32

标签: image opengl-es

这是我的代码:

-(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];

如果我的图像是1024x1024,但是如果我有一个大小为1024x768的图像,它在顶点和法线分配的值是什么?

1 个答案:

答案 0 :(得分:0)

绑定纹理时添加这些行

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);
相关问题