|
Post by vovchik on Jun 2, 2016 21:59:20 GMT 1
Dear all,
Here is another way to do triangles in the canvas (you need the latest beta BaCon for MIN/MAX and SWAP to work properly, I think):
INCLUDE canvas.bac DECLARE canvas_w, canvas_h TYPE int canvas_w = 500 canvas_h = 500
' ------------------ SUB VTRIANGLE(double x1, double y1, double x2, double y2, double x3, double y3, int fill) ' ------------------ LOCAL x, y, length, slope1, slope2, slope3 TYPE double ' order triangle vertices (x1 < x2 < x3) IF MIN(x1, x2) = x2 THEN SWAP x1, x2 SWAP y1, y2 END IF IF MIN(x2, x3) = x3 THEN SWAP x2, x3 SWAP y2, y3 END IF IF MIN(x1, x2) = x2 THEN SWAP x1, x2 SWAP y1, y2 END IF IF x1 <> x3 THEN slope1 = (y3 - y1) / (x3 - x1) ' draw the first half of the triangle length = x2 - x1 IF length <> 0 THEN slope2 = (y2 - y1) / (x2 - x1) IF fill THEN FOR x = 0 TO length - 1 ' above is vov fix for extra line ' FOR x = 0 TO length LINE(INT(x + x1), INT(x * slope1 + y1), INT(x + x1), INT(x * slope2 + y1)) NEXT x END IF END IF ' draw the second half of the triangle y = length * slope1 + y1 : length = x3 - x2 IF length <> 0 THEN slope3 = (y3 - y2) / (x3 - x2) IF fill THEN FOR x = 0 TO length LINE(INT(x + x2), INT(x * slope1 + y), INT(x + x2), INT(x * slope3 + y2)) NEXT x END IF END IF ' for filled outline, use a different INK before these LINEs LINE(x1, y1, x2, y2) LINE(x2, y2, x1, y1) LINE(x2, y2, x3, y3) LINE(x3, y3, x2, y2) LINE(x1, y1, x3, y3) LINE(x3, y3, x1, y1) END SUB
WINDOW("Vertex triangle", canvas_w, canvas_h) PEN(1.0, 1) INK(0, 0, 0, 240) CLS INK(190, 190, 190, 190) GRID(0, 0, canvas_w, canvas_h, canvas_w/10, canvas_h/10) INK(245, 100, 0, 240) VTRIANGLE(180, 10, 200, 400, 40, 350, 1) INK(0, 244, 0, 240) VTRIANGLE(80, 80, 300, 400, 400, 35, 1) INK(0, 244, 212, 240) PEN(3.0, 1) VTRIANGLE(300, 450, 90, 400, 450, 350, 0) WAITKEY
You supply three vertices (points) and fill or not.
With kind regards, vovchik
|
|
|
Post by Pjot on Jun 4, 2016 10:42:44 GMT 1
Thanks vovchik, Interesting use of SWAP BR Peter
|
|
|
Post by anurup on Jun 14, 2016 19:15:05 GMT 1
Hi,
I was trying out the high performance canvas for the first time and tried this:
INCLUDE "canvas.bac"
WINDOW("Canvas Test",640,480) POLYGON(100, 100, 50, 10, 0)
SUB Redraw SYNC END SUB
CALLBACK(50, Redraw) WAITKEY
I get a very perceptible flickering on the polygon. Is this to be expected? I read through this topic and it doesn't seem like anyone else has complained about this. Should I be doing anything differently? Thanks for your help.
Regards Anurup
|
|
|
Post by vovchik on Jun 14, 2016 19:21:29 GMT 1
Dear Anurup,
You are drawing the polygon and asking for a canvas redraw every 50 milliseconds when nothing has changed. For what you are doing this would suffice:
INCLUDE "canvas.bac"
WINDOW("Canvas Test",640,480) POLYGON(100, 100, 50, 10, 0) ' SYNC :' only if your OPEN_GL needs this WAITKEY
With kind regards, vovchik
|
|
|
Post by anurup on Jun 14, 2016 19:38:45 GMT 1
Dear vovchik, Many thanks - that makes sense. But if the aim is to draw the polygon everywhere I click the mouse - as in : INCLUDE "canvas.bac"
WINDOW("Canvas Test",640,480)
SUB Redraw ' SYNC x = MOUSE(0) y = MOUSE(1) IF MOUSE(2) THEN POLYGON(x, y, 50, 10, 0) END SUB
CALLBACK(50, Redraw) WAITKEY then is there is no way to get rid of the flickering? I tried with and without SYNC (admittedly without fully understanding the implications ). Some of the polygons I place on screen flicker - others don't - seems fairly random. The results (i.e. degree of randomness so as to which ones flicker and which ones don't) vary depending on the callback time (50ms in this example) and whether or not I use SYNC. Regards Anurup
|
|
|
Post by anurup on Jun 14, 2016 19:49:30 GMT 1
Just thought I'd mention that the HUG gives no such problems whatsoever for a similar program. Although it does not have all of the the delicious little things like POLYGON etc. I am essentially looking for high performance and versatility for a drawing platform on BaCon. As is obvious, I do not understand the internals and rather selfishly, just want my program to work.
|
|
|
Post by vovchik on Jun 15, 2016 12:55:40 GMT 1
Dear Anurup, Your program works fine on my Mint 17. You could try this to avoid unecessary queries of MOUSE position: INCLUDE "canvas.bac"
SUB REDRAW() IF MOUSE(2) THEN POLYGON(MOUSE(0), MOUSE(1), 50, 10, 0) END SUB
WINDOW("Canvas Test",640,480) CALLBACK(50, REDRAW) WAITKEY
And if you want convenient drawing and alpha, look at what Alex and I have done with GOO canvas: goo
|
|
|
Post by Pjot on Jun 15, 2016 19:03:34 GMT 1
Hi anurup,
In addition to the suggestions from vovchik, could you add the following lines to your code and show us the output?
PRINT "Current GL vendor: ", glGetString$(GL_VENDOR) PRINT "Current GL renderer: ", glGetString$(GL_RENDERER) PRINT "Current GL version: ", glGetString$(GL_VERSION)
If you experience this flickering, then maybe your system is using the OpenGL software renderer instead of the hardware of your graphics card.
With the output of the above lines we should be able to see if this is the case.
BR Peter
|
|
|
Post by anurup on Jun 16, 2016 17:55:09 GMT 1
Hi Peter, Thanks for getting back. I've already checked with glxinfo and I do have direct rendering but just for the sake of completeness here's the output that you asked for: Current GL vendor: ATI Technologies Inc. Current GL renderer: AMD Radeon HD 7700 Series Current GL version: 4.4.12967 Compatibility Profile Context 14.20 Thanks again! Anurup P.S. vovchik : Unnecessary use of variables - yes! Unnecessary MOUSE(x) queries - probably not! Do correct me if I missed something about C internals though...
|
|
|
Post by anurup on Jun 16, 2016 18:56:08 GMT 1
I also should mention that after pressing a key to exit the canvas I get :
ERROR: signal for SEGMENTATION FAULT received - memory invalid or array out of bounds? Try to compile the program with TRAP LOCAL to find the cause.
I somehow was assuming all this while that it was because of maybe the way WAITKEY was programmed, but now it strikes me that maybe it could point to something deeper. Does it?
|
|
|
Post by vovchik on Jun 16, 2016 19:01:31 GMT 1
Dear Anurup, Your OPEN_GL and hardware rendering seem fine. So, weird that you have flickering. What I meant with my SUB (and unlike yours) is that we do not need to read mouse coordinates every 50 milliseconds - only after we have a confirmed mouse click, so better check for a mouse click and then get the coordinates rather than the other way around, unless I am missing the purpose of your routine, which may end up doing more later and you need to do it this way. And the flickering is weird, but I get imperfect circles with my OPEN_GL (): Current GL vendor: X.Org Current GL renderer: Gallium 0.4 on AMD CAICOS Current GL version: 3.0 Mesa 10.5.9
With kind regards, vovchik
|
|
|
Post by anurup on Jun 16, 2016 19:13:15 GMT 1
Bloody Hell vovchik! You're bang on the money! Had completely missed that subtlety... Of course you are absolutely right about your assumptions on what I am trying to achieve. On the hardware rendering - I am flabbergasted really. I am using stock Ubuntu 14.04. Everything else I use with my graphics card runs superbly. In fact I am quite proud of the 7700's performance - both OpenGL and OpenCL - if I can take the opportunity to boast a bit.
|
|
|
Post by vovchik on Jun 16, 2016 19:25:01 GMT 1
Dear Anurup,
I think it might be OPEN_GL (which is, in the end, only a formal description) as implemented by various groups, vendors and individuals. Implementations tend to vary, and I get strange and unwanted artifacts (diagonal lines) with CIRCLE on this machine. I did a number of alternative circle tests - somewhere on this forum - and they are slower but do not give me artifacts. Each implementation seems to have its own characteristic bugs, be they vertex shaders or swap buffers or something else. I think we have to live with that, except that your keypress bug might be serious.
With kind regards, vovchik
|
|
|
Post by Pjot on Jun 17, 2016 18:39:27 GMT 1
Hi anurup,
Regarding the SEGFAULT, it could be that the canvas is using the 'classic' GLUT libraries (e.g. not FreeGLUT). I have seen some problems where the classic GLUT could not be initialized properly.
Could you try to run the program using the SDL backend for example? E.g.:
INCLUDE "canvas.bac"
BACKEND("SDL")
SUB REDRAW() IF MOUSE(2) THEN POLYGON(MOUSE(0), MOUSE(1), 50, 10, 0) END SUB
WINDOW("Canvas Test",640,480) CALLBACK(50, REDRAW) WAITKEY
BR Peter
|
|
|
Post by vovchik on Jun 17, 2016 20:54:20 GMT 1
Dear Peter, Works fine on my machine... Wonder what Anurup will report. With kind regards, vovchik
|
|