0

这是我的代码:

-(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 的图像,那么在顶点和法线处分配的值是多少?

4

2 回答 2

2

在顶点和法线处分配的值是什么?

这些无关紧要,因为它们不会(直接)干扰纹理坐标。顺便说一句,法线应该始终是单位长度。此外,如果您不想应用照明,则不需要法线。如果您确实要应用照明法线,则必须是单位长度。

无论图像的纵横比如何,常规纹理的纹理坐标始终在 [0;1] 范围内。应根据您使用的投影选择顶点位置。例如,您可以使用

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

投影,然后您的顶点将是 {0, texture_width}×{0, texture_height} 。你的问题没有明确的答案。

于 2013-01-18T11:06:08.897 回答
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);
于 2013-01-18T10:40:41.507 回答