2012-08-08 14 views
11

मैं कच्चे बनावट डेटा को डिस्क पर (बाद में पढ़ने के लिए) डंप करना चाहता हूं, और मुझे यकीन नहीं है कि glReadPixel वर्तमान में बाध्य बनावट से पढ़ेगा।glReadPixels के साथ बनावट बाइट पढ़ें?

मैं अपने बनावट से बफर कैसे पढ़ सकता हूं?

+0

अरे @Geri, मेरा उत्तर आप मदद की किसी और GL_UNSIGNED_SHORT_5_6_5 परिवर्तित करने के लिए उपयोगी हो सकता है? – gergonzalez

उत्तर

30

glReadPixels फ़ंक्शन फ्रेमबफर से पढ़ता है, न कि बनावट। एक बनावट वस्तु को पढ़ने के लिए आप glGetTexImage लेकिन का उपयोग करना चाहिए यह OpenGL ES :(

में उपलब्ध नहीं है तो आप अपने बनावट से बफर पढ़ने के लिए आप इसे एक FBO करने के लिए बाध्य कर सकते हैं (FrameBuffer वस्तु चाहते हैं) और उपयोग glReadPixels:

//Generate a new FBO. It will contain your texture. 
glGenFramebuffersOES(1, &offscreen_framebuffer); 
glBindFramebufferOES(GL_FRAMEBUFFER_OES, offscreen_framebuffer); 

//Create the texture 
glGenTextures(1, &my_texture); 
glBindTexture(GL_TEXTURE_2D, my_texture); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 

//Bind the texture to your FBO 
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, my_texture, 0); 

//Test if everything failed  
GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES); 
if(status != GL_FRAMEBUFFER_COMPLETE_OES) { 
    printf("failed to make complete framebuffer object %x", status); 
} 

उसके बाद, आप केवल glReadPixels को कॉल करना होगा जब आप अपने बनावट से पढ़ना चाहते हैं:

//Bind the FBO 
glBindFramebufferOES(GL_FRAMEBUFFER_OES, offscreen_framebuffer); 
// set the viewport as the FBO won't be the same dimension as the screen 
glViewport(0, 0, width, height); 

GLubyte* pixels = (GLubyte*) malloc(width * height * sizeof(GLubyte) * 4); 
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 

//Bind your main FBO again 
glBindFramebufferOES(GL_FRAMEBUFFER_OES, screen_framebuffer); 
// set the viewport as the FBO won't be the same dimension as the screen 
glViewport(0, 0, screen_width, screen_height); 
+1

मैं मैक पर इस काम की पुष्टि कर सकता हूं साथ ही साथ सभी ओईएस प्रत्यय हटा दिए जाते हैं – nuclearnova

2

उत्तर Gergonzale के लिए धन्यवाद। मैं कुछ समय के लिए आज सुबह यह पता लगाने की कैसे इस 16-बिट बनावट के साथ काम करने की कोशिश कर बिताया है, इस कोड स्निपेट GL_UNSIGNED_BYTE को

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tSizeW, tSizeH, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, NULL); 
GLubyte* pixels = (GLubyte*) malloc(tSizeW * tSizeH * sizeof(GLubyte) * 2); 
glReadPixels(0, 0, tSizeW, tSizeH, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels); 

int index = (x*tSizeH + y)*2; 
unsigned int rgb = pixels[index + 1]*256 + pixels[index + 0]; 
unsigned int r = rgb; 
r &= 0xF800; // 1111 1000 0000 0000 
r >>= 11;  // 0001 1111 
r *= (255/31.); // Convert from 31 max to 255 max 

unsigned int g = rgb; 
g &= 0x7E0;  // 0000 0111 1110 0000 
g >>= 5;  // 0011 1111 
g *= (255/63.); // Convert from 63 max to 255 max 

unsigned int b = rgb; 
b &= 0x1F;  // 0000 0000 0001 1111 
//g >>= 0;  // 0001 1111 
b *= (255/31.); // Convert from 31 max to 255 max 
संबंधित मुद्दे