Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drawing a pixel buffer to the screen #210

Open
tommitytom opened this issue Apr 30, 2019 · 4 comments
Open

Drawing a pixel buffer to the screen #210

tommitytom opened this issue Apr 30, 2019 · 4 comments
Labels
enhancement New feature or request igraphics Issue relates to igraphics

Comments

@tommitytom
Copy link

I have a simple RGBA pixel buffer that I want to render to my UI every frame. My first thought was to create a texture/bitmap, write the pixels to it every frame and call IGraphics.DrawBitmap. However I can't work out how to create a bitmap. After a bit of digging I found that IGraphicsNanoVG.CreateAPIBitmap exists but it is not exposed, and even then the IBitmap and APIBitmap don't seem to offer a way of setting pixel data. Any suggestions on how to do this?

@olilarkin
Copy link
Member

have to discuss with @AlexHarker how we do this in a cross -graphics api way. Here is how you can do it with nanovg:

#define IMG_SIZE 300
//https://github.com/memononen/nanovg/issues/269#issuecomment-189381540
void drawImageTest(NVGcontext* vg, float x, float y, float w, float h, float t)
{
  static int img = -1;
  NVGpaint imgPaint;
  int i, j;
  unsigned char data[IMG_SIZE*IMG_SIZE*4];
  
  if (img == -1) {
    unsigned char* px = data;
    for (i = 0; i < IMG_SIZE; i++) {
      for (j = 0; j < IMG_SIZE; j++) {
        unsigned char g = (((i>>1) ^ (j>>1)) & 1) * 255;
        px[0] = 255;
        px[1] = 255;
        px[2] = 255;
        px[3] = g;
        px += 4;
      }
    }
    img = nvgCreateImageRGBA(vg, IMG_SIZE, IMG_SIZE, 0, data);
  } else {
    unsigned char c = (unsigned char)((1+sinf(t))*0.5f*255.0f);
    unsigned char* px = data;
    for (i = 0; i < IMG_SIZE; i++) {
      for (j = 0; j < IMG_SIZE; j++) {
        unsigned char g = (((i>>1) ^ (j>>1)) & 1) * 255;
        px[0] = c;
        px[1] = c;
        px[2] = c;
        px[3] = g;
        px += 4;
      }
    }
    nvgUpdateImage(vg, img, data);
  }
  
  nvgBeginPath(vg);
  
  imgPaint = nvgImagePattern(vg, x, y, w,h, 0, img, 1.0f);
  
  nvgRect(vg, x, y, w, h);
  nvgFillPaint(vg, imgPaint);
  nvgFill(vg);
}

class DrawRGBAControl : public IControl
{
public:
  DrawRGBAControl(IRECT bounds)
  : IControl(bounds)
  {
  }
  
  void Draw(IGraphics& g) override
  {
    NVGcontext* vg = (NVGcontext*) g.GetDrawContext();
    drawImageTest(vg, mRECT.L, mRECT.T, IMG_SIZE, IMG_SIZE, 1);
  }
};

@tommitytom
Copy link
Author

Thanks for the code, works a treat! Certainly curious as to a cross platform method, however :)

@AlexHarker
Copy link
Collaborator

Your pixel buffer is in memory, correct? I think we could add a loader for pixel buffers, but be good to figure out how that behaviour should work exactly.

@tommitytom
Copy link
Author

Yes, I'm generating it each frame. Before I moved to iPlug2 I was just using raw GL - writing the pixels to a texture every frame and drawing it to a quad

@AlexHarker AlexHarker added igraphics Issue relates to igraphics enhancement New feature or request labels May 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request igraphics Issue relates to igraphics
Projects
None yet
Development

No branches or pull requests

3 participants