View Full Version : [TBGL] Logic trouble (help asked)
DirectuX
19-12-2018, 19:02
Hi ,
I've got trouble understanding these two lines of code:
line 33 & line 34
'TBGL_RenderMatrix2D(0,1,800,601) 'this works better but is not logic for me
TBGL_RenderMatrix2D(1,1,800,600) 'this don't work properly
in this little piece of code
'---Script created on 12-19-2018 17:30:47 by
uses "TBGL"
uses "console"
dim htbgl as DWord 'handler for draw window
const max_x Value = 800 'window size
const max_y value = 600
Function tbmain()
call window_init
call draw_loop
TBGL_DestroyWindow
end function
function draw_box()
TBGL_Line (1,1,1,max_y)
TBGL_Line (1,max_y,max_x,max_y)
TBGL_Line (max_x,max_y,max_x,1)
TBGL_Line (max_x,1,1,1)
end Function
function window_init()
htbgl = TBGL_CreateWindowEx ("800x600 - ESC to quit", 800,600,16,%TBGL_WS_WINDOWED + %TBGL_WS_DONTSIZE)
tbgl_ShowWindow
'TBGL_RenderMatrix2D(0,1,800,601) 'this works better but is not logic for me
TBGL_RenderMatrix2D(1,1,800,600) 'this don't work properly
TBGL_Color (255,255,255)
end Function
Function draw_loop()
' -- Main loop
While tbgl_IsWindow(htbgl)
If tbgl_GetWindowKeyState(htbgl, %VK_ESCAPE) Then Exit While
tbgl_ClearFrame
call draw_box
tbgl_DrawFrame
tbgl_ResetKeyState()
Wend
end Function
ReneMiner
21-12-2018, 20:33
Hmm its obvious, think "openGL" - not Windows...
The order of width and height must be in another order. I think it was just clockwise but am not sure.
I did a lot using 2d-matrix, if you don't find it for example in thinICE-project you will find for sure here in this game:
https://www.thinbasic.com/community/showthread.php?12553-Space-Legend-(game-RPG)
If you experience problems of the sound in the game then it's related to GetTickCount-Function, I replaced it using thinbasic Hires-timer but I don't remember if I uploaded it. Anyway its not about the game but the order of left - down - right - top or something ;)
DirectuX
22-12-2018, 17:31
Hmm its obvious, think "openGL" - not Windows...
The order of width and height must be in another order. I think it was just clockwise but am not sure.
Hi Rene,
this is not about that, but about the difference between the working line 33 and the not working line 34.
Look, help for TBGL_RenderMatrix2D states that
TBGL_RenderMatrix2D[( leftX, bottomY, rightX, topY )]
leftX Specifies X coordinate for left side of current viewport
bottomY Specifies Y coordinate for bottom side of current viewport
rightX Specifies X coordinate for right side of current viewport
topY Specifies Y coordinate for top side of current viewport
so,
const max_x Value = 800 'window size
const max_y value = 600
htbgl = TBGL_CreateWindowEx ("800x600 - ESC to quit",max_x,max_y,16,%TBGL_WS_WINDOWED + %TBGL_WS_DONTSIZE)
TBGL_RenderMatrix2D(1,1,max_x,max_y) 'this don't work properly
TBGL_Line (1,1,1,max_y) 'drawing a box
TBGL_Line (1,max_y,max_x,max_y)
TBGL_Line (max_x,max_y,max_x,1)
TBGL_Line (max_x,1,1,1)
... rest of the code ...
logically, for this code, one has to see a box at the utter limit of the drawing area. This isn't the case, there is a one pixel shift and in addition not for every parameter. To work around this issue, one has to cheat with TBGL_RenderMatrix2D(0,1,max_x,max_y+1). I found it weird and illogic.
Petr Schreiber
25-12-2018, 14:24
Hi,
thanks a lot for this discussion, you really discovered something worth closer look.
The way raw OpenGL rasterizes some primitives is a bit unintuitive, so I tried to address it with a bit of help.
Please find here attached new version of TBGL, which should draw points and lines correctly from "expectation" point of view ;)
You can test with the following variation of posted code below.
Before launching the code, please download and unzip the updated DLL to ThinBasic/Lib directory:
https://we.tl/t-IGCQmO63x9
uses "TBGL"
begin const
%max_x = 800
%max_y = 600
end const
function tbMain()
window_init()
draw_loop()
tbgl_destroyWindow
end function
function draw_box()
tbgl_line (1, 1, 1, %max_y)
tbgl_line (1, %max_y, %max_x, %max_y)
tbgl_line (%max_x, %max_y, %max_x, 1)
tbgl_line (%max_x, 1, 1, 1)
end function
function window_init()
global g_win as dword = tbgl_CreateWindowEx ("Pixel perfect 2D rendering - ESC to quit", 800, 600, 32, %TBGL_WS_WINDOWED or %TBGL_WS_DONTSIZE)
tbgl_showWindow
tbgl_renderMatrix2D (1, 1, 800, 600, %TBGL_PIXEL_PERFECT_2D) ' -- New, optional switch, for pixel perfect graphics
tbgl_color 255 , 0, 255
end function
function draw_loop()
' -- Main loop
tbgl_resetKeyState()
while tbgl_isWindow(g_win)
if tbgl_getWindowKeyState(g_win, %VK_ESCAPE) then exit while
tbgl_clearFrame
draw_box()
tbgl_drawFrame
wend
end function
Let me know how does it work for you.
Petr
Petr Schreiber
25-12-2018, 22:59
I performed further testing, and here is more robust version of the fix, please download here:
https://we.tl/t-3SEOPzgXCD
It will work with my example above.
Let me know whether it works for you.
Petr
DirectuX
03-01-2019, 13:28
I performed further testing, and here is more robust version of the fix, please download here:
https://we.tl/t-3SEOPzgXCD
It will work with my example above.
Let me know whether it works for you.
Petr
Hi Petr,
I read your answer but can't download the file : it expired meanwhile I was disconnected for Christmas break. Could you please share it anew?
Petr Schreiber
03-01-2019, 20:35
Sure thing,
please find it attached to this post :)
Petr
DirectuX
03-01-2019, 20:57
Thanks Petr,
I just tried it, the new parameter works ok.
By the way what do you think about the suggestion in the support thread ? https://www.thinbasic.com/community/project.php?issueid=555
Petr Schreiber
04-01-2019, 00:12
Very nice idea, Directux!
As TBGL development continues on gitHub, I created an issue for it:
https://github.com/ThinBASIC/thinBasic_TBGL/issues/3
Regarding pixel perfect mode, I will try to ensure it gets to the next release of thinBasic, so you can count on it.
Petr
DirectuX
08-01-2019, 18:49
Thanks Petr,
I just tried it, the new parameter works ok.
Hi Petr,
too quick. There are glitches.
Here is a sample: 9925 (use T to toggle views)
In the same sample you can also notice that there is something odd with TBGL_MouseGetPos... : is there a hidden limit to returned values ? , because mouse get unexpectedly off the window at right or bottom, this doesn't occur on a smaller window.
I've read github version of the issue, "...does not allow to draw visible lines using the following code" is not what I meant: lines are visible but off-setted.
While running the illustration code you provided on github, i got this bmp which curiously is ok for me: 9926
Meanwhile, I'll try to understand the commit (https://github.com/ThinBASIC/thinBasic_TBGL/pull/2/commits/638892a93a5c7b1ff1c4994127751128c977b929).
Edit: in src/tbgl_Frame.inc , line 153
why when mode2d ≠ %TBGL_PIXEL_PERFECT_2D don't you reset pixelOffset to zero ? As it is now, when switching pixel perfect mode on then off again, you don't get back the starting state (0) but (-0.5).
Petr Schreiber
12-01-2019, 15:29
Hi Directux,
thanks a lot for providing me with valuable feedback, this is exactly the kind of help which helps to push things forward :drink:
The non-reset of pixelOffset to zero is an ommision from my side. Do you have a gitHub account by the way? So I could assign pull requests to you?
Going to fix that offset non-reset issue now ;)
Petr
Petr Schreiber
12-01-2019, 15:45
DirectuX, I know the reason for strange artifacts in your example.
You create window of some size, but then you create matrix 1 bigger in each dimension.
Imagine you have window 1280x720. You should create TBGL_RenderMatrix2D(1, 1, 1280, 720), not TBGL_RenderMatrix2D(0, 0, 1280, 720) for it.
Why? Because the range is inclusive, 1 to 1280 on X, 1 to 720 on Y.
In 1280x720, there are really 1280 pixels * 720 pixels, not 1281 * 721.
Check this altered example:
#region "Uses"
uses "tbgl", "file", "ui"
#endRegion
type mousecur
position as POINTAPI
direction as Integer
end type
type mycolors
red as byte
green as Byte
blue as Byte
end type
dword htbgl 'handler for draw window
integer max_x = 1280' window dimensions
integer max_y = 720
dim mymouse as mousecur 'Cursor state
dim perfect as Boolean 'toggle for pixel perfect
dim black, white, gray, green, red, orange, blue as mycolors
white.red=255
white.green=255
white.blue=255
gray.red=50
gray.green=50
gray.blue=50
green.green=250
red.red=250
blue.green=125
blue.blue=250
orange.red=250
orange.green=125
function tbMain()
call myini
call draw_loop
tbgl_destroyWindow
end function
function myini()
'Display settings
htbgl = TBGL_CreateWindowEx ("[ ESC ] to quit - [ T ] toggle PERFECT PIXEL_2D ** OFF **", max_x, max_y, 32, %TBGL_WS_WINDOWED | %TBGL_WS_DONTSIZE)
tbgl_ShowWindow
TBGL_ShowCursor false
TBGL_UseVSync true
TBGL_RenderMatrix2D(1, 1, max_x, max_y)
TBGL_Color_set (white)
TBGL_BackColor_set (black)
tbgl_ResetKeyState()
'Font settings
TBGL_UseTexturing (False)
end Function
Function draw_loop()
' -- Main loop
While tbgl_IsWindow(htbgl)
If tbgl_GetWindowKeyState(htbgl, %VK_ESCAPE) Then Exit While
If TBGL_GetWindowKeyOnce(htbgl, %VK_T) then
perfect = not perfect
if perfect then
TBGL_RenderMatrix2D(1,1,max_x,max_y)
TBGL_SetWindowTitle( htbgl, "[ ESC ] to quit - [ T ] toggle PERFECT PIXEL_2D ** OFF **" )
Else
TBGL_RenderMatrix2D(1,1,max_x,max_y, %TBGL_PIXEL_PERFECT_2D)
TBGL_SetWindowTitle( htbgl, "[ ESC ] to quit - [ T ] toggle PERFECT PIXEL_2D ** ON **" )
endif
endif
tbgl_clearFrame
call draw_mouse
call draw_lines
call draw_mouse_cross
tbgl_drawFrame
Wend
end Function
function draw_mouse()
dim position as Integer
mymouse.position.x=TBGL_MouseGetPosX
mymouse.position.y=max_y-TBGL_MouseGetPosy
end function
function draw_lines()
integer ii, jj
for ii = 1 to max_x Step 10
for jj = 1 to max_y step 10
tbgl_line(ii,jj,ii+7,jj+7) ' regular dashes
next ii
next jj
end function
function draw_mouse_cross()
TBGL_PushColor_set(green)
Tbgl_line(mymouse.position.x,1,mymouse.position.x,max_y)
Tbgl_line(1,mymouse.position.y,max_x,mymouse.position.y)
TBGL_PopColor
end function
function TBGL_Color_set (mycolor as mycolors)
TBGL_Color(mycolor.red,mycolor.green,mycolor.blue)
end function
function TBGL_BackColor_set (mycolor as mycolors)
TBGL_BackColor(mycolor.red,mycolor.green,mycolor.blue)
end function
function TBGL_PushColor_set (mycolor as mycolors)
TBGL_PushColor(mycolor.red,mycolor.green,mycolor.blue)
end function
In the meantime, this is a fix for your observation:
https://github.com/ThinBASIC/thinBasic_TBGL/pull/4/files
The fix will be present in next thinBasic release, thank you!
Petr