Skip to content
This repository has been archived by the owner on Dec 2, 2019. It is now read-only.

nk_fill_polygon, nk_stroke_polygon and nk_stroke_polyline always contain one point at the origin #808

Open
the-coding-fox opened this issue Feb 20, 2019 · 4 comments

Comments

@the-coding-fox
Copy link

In case it matters, I've been using the LWJGL binding of Nuklear and used code from their demo to bind Nuklear to OpenGL. https://github.com/LWJGL/lwjgl3/blob/master/modules/samples/src/test/java/org/lwjgl/demo/nuklear/GLFWDemo.java

Whenever I make calls to nk_fill_polygon, nk_stroke_polygon or nk_stroke_polyline, I always end up with a polygon that has one of its points at the top left of the window. If this is not a bug, can you tell me if there is a function that sets the origin for drawing polygons to something other than (0, 0)?


On a side note, I've had trouble drawing shapes in general, the library seems a bit limited in what I can draw. I wanted to draw a rectangle with one rounded edge.

My first approach was to draw a polygon that resembles a rectangle with a small square cut out of the corner and then draw an arc at that corner. Polygon did not work because of the issue mentioned above.

For my next approach I drew two rectangles, one large one and a smaller one, such that they look like a larger rectangle with a corner removed. when I went to add the arc I found that nk_stroke_arc actually draws straight lines along with the curve. Is there any way to just draw the curve without the radial lines attached to it.

Finally, I wanted to draw an L-shaped polygon with a rounded corner on the 270 degree angle, but it doesn't appear like there are any tools for drawing those kinds of corners, as the arc commands are specifically for filling the inside of circle that the arc belongs to.

@irtusb
Copy link

irtusb commented Feb 20, 2019

  1. try to draw things this way
    struct nk_command_buffer* buf = nk_window_get_canvas(&ctx);
    nk_fill_triangle(buf, 100, 100, 100, 200, 200, 200, nk_rgb(0, 0, 200));
  1. the provided implementation of nk_convert does indeed draw radii for arcs.
    2.1 you can patch it in nuklear.h. Look for NK_COMMAND_ARC.
    2.2 implement your own switch for NK_COMMAND_* with your renderer preferred primitives (some demo files use this)
    2.3 create a custom nuklear primitivevia NK_COMMAND_CUSTOM (sorry, no idea where to begin, but it does exist)

  2. you can overcome everything and draw the following
    3.1 if it is a filled rect divide your rect in 2 and draw the 3 objects together (2 rects + arc)
    3.2 else draw arc and overwrite radii with lines of background colour, then draw rect remainder using lines

edit: the are called primitives because they do the very basic thing, compositing is up to you

@the-coding-fox
Copy link
Author

None of this addresses the issue with all polygons having a point in the origin.

I'm not looking for workarounds, I just would like Nuklear to work correctly. I could just use OpenGL if I were willing to go through all the trouble of fixing Nuklear.

I commend the developers for making such a useful library and would like to offer my thanks. I hope they may take a look at this issue and let me know how the polygon functions work and if it can be solved.

@dumblob
Copy link
Contributor

dumblob commented Mar 11, 2019

I've downloaded LWJGL and studied their code. Unfortunately I could not find any mistake regarding origin [0, 0]. Could you please post full code which reproduces this issue?

@the-coding-fox
Copy link
Author

the-coding-fox commented Mar 15, 2019

My software is quite complex and is spread out over many classes, but the following code is a condensed version of the code I'm using to draw the polygon. I make a call to the test_polygon() function on each program loop, preceded by calls to the nk_begin and nk_layout_* functions to set up a window and place the widget.

I have set up Nuklear to work with OpenGL and GLFW similar to how it is shown in this example:
https://github.com/LWJGL/lwjgl3/blob/master/modules/samples/src/test/java/org/lwjgl/demo/nuklear/GLFWDemo.java

import org.lwjgl.system.MemoryStack;
import org.lwjgl.nuklear.NkColor;
import org.lwjgl.nuklear.NkRect;
import org.lwjgl.nuklear.NkCommandBuffer;
import static org.lwjgl.nuklear.Nuklear.nk_fill_polygon;
import static org.lwjgl.nuklear.Nuklear.nk_widget;
import static org.lwjgl.nuklear.Nuklear.nk_window_get_canvas;

public class NuklearExtended {
  public final static NkColor SELECTION_COLOR = NkColor.create().set((byte)64, (byte)196, (byte)255, (byte)128);

  public final static void test_polygon(NkContext context) {
    
    float STROKE_WIDTH = context.style().button().border();
    try(MemoryStack stack = MemoryStack.stackPush()) {
      NkRect bounds = NkRect.mallocStack(stack); // Reserve memory for widget bounds
      NkCommandBuffer commands = nk_window_get_canvas(context); // Get command buffer

      nk_widget(bounds, context);
      float x = bounds.x();
      float y = bounds.y();

      float[] points = {x+50, y+50, x+100, y+50, x+100, y+100, x+50, y+100};
      nk_fill_polygon(commands, points, SELECTION_COLOR);
    }
    
  }

}

For the moment, I've just replaced calls to nk_fill_polygon with nk_fill_rect and made a simplified version of what I intended to draw. It is not imperative to have this issue resolved since this part of the software is just for internal use.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants