11
A
回答
30
glReadPixels函數從幀緩衝區讀取,而不是紋理。要讀取紋理對象必須使用glGetTexImage但它不可在OpenGL ES :(
如果你想讀你的紋理緩衝,然後您可以將其綁定到一個FBO(幀緩衝區對象)和使用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
我可以確認這個工作在Mac上,一旦所有的OES後綴都被刪除 – nuclearnova 2014-08-18 19:10:46
2
謝謝你的答案Gergonzale。今天早上我花了一些時間,試圖找出如何讓這16位紋理工作,這個代碼片斷可能是其他人將GL_UNSIGNED_SHORT_5_6_5有用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
相關問題
- 1. OpenGL ES使用glReadPixels讀取浮點值()
- 2. 使用OpenGL ES的紋理緩存,而不是glReadPixels獲得的紋理數據
- 3. glReadPixels到EGLImage直接紋理比glReadPixels更慢比ByteBuffer和glTexSubImage2D?
- 4. 使用OpenCV處理來自glReadPixels的圖像並將其作爲紋理返回
- 5. 使用C#字節讀取字節數組我使用C#
- 6. 用python讀取MNIST字節
- 7. glReadPixels()讀取速度緩慢GL_DEPTH_COMPONENT
- 8. 使用Asio讀取單個字節::讀取
- 9. Opengl紋理和字節順序
- 10. 如何讀取direct3d紋理像素
- 11. Android OpenGL ES讀取紋理像素
- 12. 從samplerExternalOES讀取紋理像素時
- 13. THREE.js從GPUComputationRenderer紋理中讀取像素
- 14. 讀取和寫入一個紋理(OpenGL)
- 15. WebGL紋理同時讀取/寫入
- 16. 使用無符號字節的紋理與DirectX 10月11日
- 17. 讀取節點,節省使用批處理 - Drupal的7
- 18. OpenGL ES 2.0:glReadPixels()與浮動或half_float紋理
- 19. Android OpenGL ES 2.0 - glReadPixels()和glTexImage2D()繪製黑色紋理?
- 20. 當使用流的讀取字節我讀什麼樣的字節
- 21. 從字節讀取字節從Db
- 22. 每個字節讀取陣列字節
- 23. glReadPixels使用太慢
- 24. 使用YouTube視頻獲取webgl紋理?
- 25. TSocket讀取0字節
- 26. cpp字節文件讀取
- 27. VB.NET的XmlTextReader讀取字節
- 28. tcpclient getstream - 零字節讀取
- 29. 從串口讀取字節
- 30. 從類讀取字節
嘿@Geri,我的回答幫助你的? – gergonzalez 2013-11-06 14:52:23