2013-01-16 27 views

Trả lời

10

tôi đã không kiểm tra sau nhưng tôi sẽ phân hủy các chuyển đổi trong 3 bước: info

  1. Extract cho hình ảnh của bạn:

    UIImage* image = [UIImage imageNamed:@"imageToApplyAsATexture.png"]; 
    CGImageRef imageRef = [image CGImage]; 
    int width = CGImageGetWidth(imageRef); 
    int height = CGImageGetHeight(imageRef); 
    
  2. Phân bổ một textureData với các tính chất trên:

    GLubyte* textureData = (GLubyte *)malloc(width * height * 4); // if 4 components per pixel (RGBA) 
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    NSUInteger bytesPerPixel = 4; 
    NSUInteger bytesPerRow = bytesPerPixel * width; 
    NSUInteger bitsPerComponent = 8; 
    CGContextRef context = CGBitmapContextCreate(textureData, width, height, 
                  bitsPerComponent, bytesPerRow, colorSpace, 
                  kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
    
    CGColorSpaceRelease(colorSpace); 
    
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
    CGContextRelease(context); 
    
  3. Thiết lập kết cấu của bạn:

    GLuint textureID;  
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
    glGenTextures(1, &textureID); 
    
    glBindTexture(GL_TEXTURE_2D, textureID); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData); 
    

EDIT:

đọc this tut; mọi thứ được giải thích từ việc chuyển đổi một hình ảnh thành một kết cấu và áp dụng một kết cấu trong môi trường iOS.

+0

Tôi có hai câu hỏi: hình ảnh của tôi là 1024x768 nơi tôi nên đặt những valiues này? và cuối cùng là kết cấu của tôi là gì? – CrazyDev

+0

nếu bạn biết kích thước hình ảnh bạn không cần phải trích xuất chiều rộng và chiều cao chỉ cần đặt nó trực tiếp – tiguero

+0

kết cấu 2d của bạn tương ứng với đối tượng gl bạn đang tạo khi gọi glTexImage2D, bạn có thể truy cập nó qua tên: textureID – tiguero

0

Một cách khác để làm điều này bằng cách sử dụng khuôn khổ GLKit:

//Path to image 
NSString *path = [[NSBundle mainBundle] pathForResource:@"textureImage" ofType:@"png"]; 

//Set eaglContext 
[EAGLContext setCurrentContext:[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]]; 

//Create texture 
NSError *theError;  
GLKTextureInfo *texture = [GLKTextureLoader textureWithContentsOfFile:filePath options:nil error:&theError]; 
glBindTexture(texture.target, texture.name); 

texture.name là tên Bối cảnh OpenGL cho kết cấu.

0

Đây là phiên bản nhanh chóng nhận được kết cấu ra khỏi một UIImage

func setupTexture(sourceImage: UIImage) -> GLuint { 

guard let textureImage = sourceImage.cgImage else { 
    print("Failed to load image") 
    return 0 
} 

let width = textureImage.width 
let height = textureImage.height 

/* 
it will write one byte each for red, green, blue, and alpha – so 4 bytes in total. 
*/ 

let textureData = calloc(width * height * 4, MemoryLayout<GLubyte>.size) //4 components per pixel (RGBA) 
let spriteContext = CGContext(data: textureData, 
           width: width, 
           height: height, 
           bitsPerComponent: 8, 
           bytesPerRow: width * 4, 
           space: textureImage.colorSpace!, 
           bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) 

spriteContext?.draw(textureImage, in: CGRect(x: 0, y: 0, width: width, height: height)) 

var textName = GLuint() 
glGenTextures(1, &textName) 
glBindTexture(GLenum(GL_TEXTURE_2D), textName) 
glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST) 
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width), 
      GLsizei(height), 0, GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), textureData) 

return textName 

}

Lưu ý:. Cần ghi nhớ rằng Lõi đồ họa làm bật bức tranh khi chúng ta nạp chúng trong

Các vấn đề liên quan