View Full Version : create TBGL_Line in 3d space
largo_winch
12-08-2011, 10:09
hello. is there any TBGL example how to create a simple TBGL_LINE in 3d world (x,y,z) with tbgl_Rendermatrix3D. How to paint it with Mouse Handling from point A to point B? I didn't find any example. I suggest I must build this chapter on my own ? ;) bye, largo
Petr Schreiber
12-08-2011, 11:37
Hi Largo,
I think looking at these commands will lead you to the 2D path, I would recommend to start with TBGL_RenderMatrix2D, it is easier to set up coordinate system matching the screen:
' -- To get position
TBGL_MouseGetPosX
TBGL_MouseGetPosY
' -- To get mouse down
TBGL_MouseGetLButton
' -- To get info the mouse was clicked
TBGL_GetWindowKeyOnce(hWnd, %VK_LBUTTON)
' -- store the clicked points in some array
TYPE PointF
x AS SINGLE
y AS SINGLE
END TYPE
DIM PointCount AS LONG
DIM myPoints() AS PointF ' -- Use REDIM PRESERVE myPoints(PointCount) to make it grow and add new points to series
' -- To render line strip
TBGL_BeginPoly(%GL_LINE_STRIP)
FOR i = 1 to UBound(myPoints)
TBGL_Vertex myPoints(i).x, myPoints(i).y
NEXT
TBGL_EndPoly
These should be the code blocks you need to achieve your goal.
Drawing with mouse in 3D space is difficult because mouse as input device gives you only 2D coordinates (x, y), so where to take the z? If you would like to draw lines on existing objects, you could use something like this to retrieve coordinates where you clicked:
TBGL_GetPixelInfo(TBGL_MouseGetPosX, TBGL_MouseGetPosY, %TBGL_PINFO_XYZ, x, y, z)
Then the same code blocks would apply, with two changes:
adding Z to the PointF TYPE
adding myPoints(i).z to the TBGL_Vertex (this command is hybrid, can take both 2 or 3 params)
Petr
largo_winch
15-08-2011, 17:43
thank you petr for these good hints!
I've built this first issue. I am quite satisfied ;) but if you can have a closer look at this example I am glad. I think I must have a better control for the start point (x,y) of new tbgl lines (vertex/vertices). this example starts with point counts = 11.
command:
"TBGL_GetPixelInfo(TBGL_MouseGetPosX, TBGL_MouseGetPosY, %TBGL_PINFO_XYZ, x, y, z)"
must add new
"TBGL_GetPixelInfo(TBGL_MouseGetPosX, TBGL_MouseGetPosY, TBGL_MouseGetPosZ, %TBGL_PINFO_XYZ, x, y, z)"
to work for z-depth.
if you have a split view gui with different frame you can look from front side, top view, side, perspective view and so on. So if there's a possibility to switch the camera for (e.g.) z-direction it must be able to make a vertex point in "z" direction, isn't it? but I have no idea how to set a point in "z" (depth) direction.
bye, largo
Petr Schreiber
15-08-2011, 20:34
Hi Largo,
good job, very nice code! I must admit I am really very happy to see you mastering TBGL so fast!:cool:
I did mini modification, so it can draw infinite number of points in line. The main change is in GetLineID -> I renamed it to GetPointID, as it in fact returns the index of point and not line.
Second modification is that it updates screen after click, so you see the lines added immediately (and you can see them moved).
Petr
P.S. Regarding TBGL_GetPixelInfo(TBGL_MouseGetPosX, TBGL_MouseGetPosY, TBGL_MouseGetPosZ, %TBGL_PINFO_XYZ, x, y, z). TBGL_MouseGetPosZ sadly cannot be developed, as computer mouse sends only X, Y.
We had an interesting device on University, which also had Z component, you can see it here:
http://www.3dconnexion.com/index.php?id=231
... but normal mouse has no way to return such a information. Maybe you could use PageUp/PageDown to move virtual cursor up and down?
largo_winch
16-08-2011, 09:04
thank you petr for modification! a) how I can print a "TBGL_Font" or "Bitmap" on scene? Neither TBGL_font works here nor Bitmap (use the font demo of TBGL basic folder)
b) what Library do you are using for TBGL? glut, gl, freeglut ?
c) only for understanding:
cc) in that case (theoretically) I want to use to create a cube via mouse click and scale it I cannot do this for z-direction? then it's a 2d projection? dd) this current example can use tbgl entities (with camera, lights, FuncSlot) if I like, isn't it? but the 2d grid plane will prevent to look into depth, or I am mixing something (2d + 3d?) ;)
bye, largo
Petr Schreiber
16-08-2011, 10:34
Hi Largo,
a)
It should work, but you need to create font first. To do so, just modify the WM_InitDialog to:
Case %WM_INITDIALOG
Control Handle CBHNDL, %lCanvas To hCtrl
' -- Init OpenGL
TBGL_BindCanvas(hCtrl)
TBGL_BackColor 22, 84, 128
' -- Builds Arial font, size 12pt
TBGL_BuildFont(TBGL_FontHandle("Arial Black", 12))
... and then for printing, you can modify part before TBGL_DrawFrame in RenderMyImage:
' -- Draw gridbackground for fun
RenderGrid(width, height, 40)
TBGL_Color 255, 255, 255
' -- Prints at 100, 100
TBGL_PrintFont "Ciao Largo!", 100, 100
TBGL_DrawFrame
b)
TBGL is pure Win32 API and low level OpenGL. I don't like the GLUT concept very much, that is one of the reasons I created TBGL
c,cc)
For defining cube I would do it as following:
click 1 = place upper left corner of cube (let's call it PointA)
moving mouse now means drawing rectange with upper left corner fixed at PointA, and lower left corner based on mouse position in space
click 2 = place lower right corner of cube (let's call it PointB), now base is defined
moving mouse on your table could be handled as moving in Z, instead of tracking X, Y as usually
click 3 = end of definition
Petr
largo_winch
16-08-2011, 15:05
thank you petr. I placed tbgl_font in rendering part, so I couldn't see anything ;) I will check the cube points method, good idea!
you've said tbgl is more windows api than openGL. last week I discussed with a friend about these things what library could be the best one. Is there any other way to create 3d graphic primitives? "Irrlicht" is a very nice game engine tool, but very big and not very easy to use in my eyes (my opinion). I noticed you have translated a lot of include files as official headers from Khronos to thinbasic. I don't know how cad designed software were built, but there I cannot find any Glut or openGL.DLL, but how do they are producing such great designed houses and buildings? :)
bye, largo
Petr Schreiber
16-08-2011, 16:41
I am happy it worked for you :)
you've said tbgl is more windows api than openGL
Hehe, that's not entirely true. By mentioning Win32 I meant the tbgl Window is created via Win32 API, but of course wast majority of code is OpenGL based (based on gl, glExt Khronos headers translated excellentely by José).
CAD application today use OpenGL or Direct2D/3D. They don't need to supply any OpenGL.DLL, as OpenGL32.dll is already installed in system, and the HW acceleration and extensions are provided via drivers.
I do not use any third party openGL library at the moment, but I remember GLFW was nice, very simple but usable. Then there are engines like OGRE, also nice piece of software, but somehow a "big thing" too. I think you can reach great results both going low level and using high level engines as well, it depends on what you need to do.
Regarding 3d primitives - I almost do not use them, only the subset present in TBGL sometimes for temporary visualization, but if I do use some geometry, it is usually prepaired model or something generated procedurally from triangles/quads.
Petr