c++ - 在 reading/dev/fb0 上没有得到预期的输出

标签 c++ linux opengl glut opengl-compat

我想使用 /dev/fb0 在 OpenGL 窗口中显示当前屏幕,我需要获得更快的像素映射,我已经编写了一些代码,但不幸的是它没有提供我所期望的?这是因为使用/dev/fb0 还是代码本身的问题?任何有关如何比 C++ 中的/dev/fb0 更快地获取屏幕映射的建议都将受到赞赏。

#used below Perl code just to check whether image formation is correct manually 

#But it didn't work (png didn't form as a screen) 

#!/usr/bin/perl -w
$w = shift || 240;
$h = shift || 320;
$pixels = $w * $h;
open OUT, "|pnmtopng" or die "Can't pipe pnmtopng: $!\n";
printf OUT "P6%d %d\n255\n", $w, $h;
while ((read STDIN, $raw, 2) and $pixels--) {
   $short = unpack('S', $raw);
   print OUT pack("C4444",
     ($short & 0xf800) >> 8,
     ($short & 0x7e0) >> 3,
     ($short & 0x1f) << 3);
}
close OUT;

也使用了 glDrawpixels 但没有打印:

//内部渲染函数(使用这个我在黑屏上变白)

glClear(GL_COLOR_BUFFER_BIT);
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
int wrap = 1;
long int screensize = 0;

fbfd = open("/dev/fb0", O_RDWR);    
if (fbfd == -1) {
   .....
}
printf("The framebuffer device was opened successfully.\n");
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
   ....
}
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
   .....
}
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
GLubyte * fbp = nullptr;
fbp = (GLubyte  * ) mmap(NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);    
if(fbp != MAP_FAILED){
    printf("mmap seems worked\n");
};
if ((int)*fbp == -1) {
    ......
    exit(4);
}
printf("The framebuffer device was mapped to memory successfully.\n");
//display
glBitmap(1366,768,0,0,0,0,fbp);
glFlush();

.......

最佳答案

  unsigned char pixelBuffer[vinfo.xres][vinfo.yres][3];//[1366][768][3]
      for(int y = 0 ; y < vinfo.yres ; y++){
          for(int x = 0 ; x < vinfo.xres ; x++){
              int pb_offset = 3 * x;
              size_t fb_offset = x * (bytes_per_pixel)+ y * finfo.line_length;
              uint32_t pixel = 0;
              switch (vinfo.bits_per_pixel)
              {
                  case 16:
                        pixel = *((uint16_t *)(fbp + fb_offset));
                        break;   
                  case 24:
                        pixel += *(fbp + fb_offset);
                        pixel += *(fbp + fb_offset + 1) << 8;
                        pixel += *(fbp + fb_offset + 2) << 16;
                        break;
                  case 32:
                        pixel = *((uint32_t *)(fbp + fb_offset));
                        break;
                  default:
                        // nothing to do                
                        break;
                   }
                  unsigned char r = (pixel >> vinfo.red.offset) & r_mask;
                  unsigned char g = (pixel >> vinfo.green.offset) & g_mask;
                  unsigned char b = (pixel >> vinfo.blue.offset) & b_mask;
                  pixelBuffer[x][y][0] =  (r * 0xFF)/r_mask;
                  pixelBuffer[x][y][1] = (g * 0xFF)/g_mask;
                  pixelBuffer[x][y][2] = (b * 0xFF)/b_mask;                            
               }        
       }

通过使用上面的代码片段我已经解决了我的问题

关于c++ - 在 reading/dev/fb0 上没有得到预期的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55878170/

相关文章:

python - 我应该使用哪个 Enthought EPD 发行版?

opengl - 如何在 glBufferData 期间处理 GL_OUT_OF_MEMORY 错误?

c++ - 确定从一个平面到另一平面的旋转角度

java - SMTP c++ 客户端

c++ - QTextEdit 中不可编辑的文本

linux 期望语言-脚本不适用于 if 语句

c++ - 传递多维数组给出和错误

c - TCP 校验和计算更改 - tcp 卸载已禁用

winapi - Win32 opengl 窗口创建

c++ - 尝试旋转此立方体时,为什么屏幕闪烁?