Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Drawing texture inside a nuklear window

  1. #1
    Newbie Newbie valera_rozuvan's Avatar
    Join Date
    Jul 2017
    Location
    Kyiv, Ukraine
    Posts
    1

    Drawing texture inside a nuklear window

    Hi guys!

    So I am using this library for my GUI: nuklear. I want to draw an image onto a nuklear window. I am creating my image as a 1D array, filled with RGBA pixels [R, G, B, A, R, G, B, A, ...]. Next I "render" this array onto an OpenGL texture, and pass it on to a nuklear function to draw this texture onto a window.

    The full code with details is following:

    Code :
    struct nk_command_buffer *canvas;
    const struct nk_color grid_color = nk_rgba(255, 255, 255, 255);
    struct nk_vec2 upperCoord;
     
    const unsigned int WIDTH_IMG = 80;
    const unsigned int HEIGHT_IMG = 80;
     
    unsigned char imageData[WIDTH_IMG * HEIGHT_IMG * 4];
     
    struct nk_image myImage;
    struct nk_rect total_space;
     
    void draw_line(struct nk_context* ctx)
    {
      canvas = nk_window_get_canvas(ctx);
      total_space = nk_window_get_content_region(ctx);
     
      // Draw simple line.
      upperCoord = nk_window_get_content_region_min(ctx);
      nk_stroke_line(canvas, upperCoord.x + 0, upperCoord.y + 0, upperCoord.x + 10, upperCoord.y + 10, 1.0f, grid_color);
     
      // Create an image. For now all pixels are the same.
      int i = 0;
      for (unsigned int y = 0; y < HEIGHT_IMG; ++y){
        for (unsigned int x = 0; x < WIDTH_IMG; ++x){
          imageData[i] = 174;
          imageData[i + 1] = 79;
          imageData[i + 2] = 206;
          imageData[i + 3] = 255;
     
          i += 4;
        }
      }
     
      // creating a texture
      unsigned int texture = 42;
      glGenTextures(1, &texture);
      glBindTexture(GL_TEXTURE_2D, texture);
     
      // set necessary texture parameters
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,     GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,     GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     
      // allocate memory and set texture data
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, WIDTH_IMG, HEIGHT_IMG, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
     
      // render texture inside current nuklear window
      myImage = nk_image_id((int)texture);
      nk_draw_image(canvas, total_space, &myImage, grid_color);
     
      // delete texture
      glDeleteTextures(1, &texture);
    }

    The most important part is:

    Code :
      // allocate memory and set texture data
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, WIDTH_IMG, HEIGHT_IMG, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
     
      // render texture inside current nuklear window
      myImage = nk_image_id((int)texture);
      nk_draw_image(canvas, total_space, &myImage, grid_color);
     
      // delete texture
      glDeleteTextures(1, &texture);

    The full program listing (along with instructions for running it) is at https://github.com/mandelbulb-4d/man...rawing.cpp#L51.

    The result is somewhat unexpected random gibberish:

    Click image for larger version. 

Name:	27911284-34ed78d6-6261-11e7-938a-f48bc9987a0a.jpg 
Views:	39 
Size:	10.4 KB 
ID:	2407

    What might be the problem? Please help me! Will be thankful for any input.

  2. #2
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    418
    You have two possibilities:

    First, try to contact the nuklear members and see if they can help you.
    Go into their code, understand their rendering logic in order to understand what went wrong with what you wanted to do.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •