PDA

View Full Version : A challenge porting from FreeBasic (and originally from C)



sblank
11-11-2010, 06:09
Hi Petr, Eros, and Michael,

I'm attaching a neat little program from FreeBasic which I translated from C. The original program was from a math professor friend of mine, George Francis (at U of I... George uses my Python OpenGL text with his REU freshmen).

I would like to translate this program to thinBasic, but the glGetFloatv command, which fetches the aff() matrix (affine matrix) in the chaptrack() function seems problematic. Heck, there are a few other commands that are difficult for this thinBasic newbie, also!

Anyway, if time permits, please take a look and see if anything can be recommended?

What I like about this program is that it allows you to visualize 3D parametric surfaces and rotate them "trackball" style with the mouse (there is a python version in my text, also).

The tro.zip file contains both the .bas file and the .exe file.

Thanks,

Stan

Michael Hartlef
11-11-2010, 08:27
Hi Stan,

i have to go to work now and look at the code tonight if noone beats me
to it. What do you mean by difficult for tbgl?

Michael

Petr Schreiber
11-11-2010, 11:31
Hi Stan,

if you need OpenGL functions, they are available via headers.

You include them to script this way:


#INCLUDE Once "%APP_INCLUDEPATH%\thinbasic_gl.inc"
#INCLUDE Once "%APP_INCLUDEPATH%\thinbasic_glu.inc"
as demonstrates TBGL_UsingAPI.tBasic sample script. So that's the way if you need to port the code line by line. You can freely mix TBGL and OpenGL code as you need.

GLUT is not present in thinBasic, as its (in my opinion) unlucky architecture forcing the organization of program sparked the idea to start creating TBGL giving programmer more freedom back in 2005.

The 3D function plotting is quite popular topic in ThinBASIC, recently the user zak presented nice implementation here:
http://www.thinbasic.com/community/showthread.php?10835-3D-curves-texturing-between-mathematica-and-thinbasic&p=81268#post81268


Petr

Petr Schreiber
11-11-2010, 14:01
Why not take advantage of ability to draw in 3D to dialog?
Why not evaluate expressions on the fly? :p

Here goes example which allows both mouse manipulation of the 3D plot, but also definition of the function on the fly. All that in resizeable ThinBASIC dialog.


Petr

ErosOlmi
11-11-2010, 14:29
Petr,

you have the ability to ... amaze your readers.

Great example
Eros

sblank
11-11-2010, 16:42
Hi Michael,

As a thinBasic and TBGL beginner, I did not see the possibility of using raw OpenGL code... but Petr has set me on the correct path!

I'll echo Eros... Petr has the ability to amaze!

I now have a nice programming project for the weekend!

Thanks for the reply,

Stan


Hi Stan,

i have to go to work now and look at the code tonight if noone beats me
to it. What do you mean by difficult for tbgl?

Michael

sblank
11-11-2010, 16:44
Wow... you have blown me out of the water completely!

Very, very nice! And I mean VERY nice!

Thanks,

Stan


Why not take advantage of ability to draw in 3D to dialog?
Why not evaluate expressions on the fly? :p

Here goes example which allows both mouse manipulation of the 3D plot, but also definition of the function on the fly. All that in resizeable ThinBASIC dialog.


Petr

sblank
11-11-2010, 16:47
Thanks Petr,

Great information... I wasn't aware that I could include opengl headers. Now I have a project for the weekend!

GLUT is an odd (and old) window management system, but unfortunately for me, that is how I learned to use OpenGL. However, it is possible to teach an "old dog" new tricks and I can learn another (better) method.

It's going to be fun working with and learning thinBasic! I can already see that there are no real limitations to anything we need to accomplish.

Thanks again,

Stan


Hi Stan,

if you need OpenGL functions, they are available via headers.

You include them to script this way:


#INCLUDE Once "%APP_INCLUDEPATH%\thinbasic_gl.inc"
#INCLUDE Once "%APP_INCLUDEPATH%\thinbasic_glu.inc"
as demonstrates TBGL_UsingAPI.tBasic sample script. So that's the way if you need to port the code line by line. You can freely mix TBGL and OpenGL code as you need.

GLUT is not present in thinBasic, as its (in my opinion) unlucky architecture forcing the organization of program sparked the idea to start creating TBGL giving programmer more freedom back in 2005.

The 3D function plotting is quite popular topic in ThinBASIC, recently the user zak presented nice implementation here:
http://www.thinbasic.com/community/showthread.php?10835-3D-curves-texturing-between-mathematica-and-thinbasic&p=81268#post81268


Petr

Petr Schreiber
12-11-2010, 13:05
Thanks Stan,

I am happy it worked for you.


GLUT is an odd (and old) window management system, but unfortunately for me, that is how I learned to use OpenGL
I grew up on GLUT as well, I slept with its printed specification under pillow, but there was a moment I realised I need more control - especially the glutMainLoop, which could be quit only by exiting application made me decide to seek other approaches.


Petr

sblank
12-11-2010, 22:37
I can understand what you mean, particularly from the programming examples you have in thinBasic and in the bonus packs. I can discard GLUT... I simply need to think differently :D

My true interests in graphical programming are the visualization of mathematical objects (fractals, curves, surfaces... functional, implicit, and parametric, Riemann, etc.) and physics simulations. Your physics examples are fantastic and that is another area that I'm noting for further exploration.

Very nice work... and I have SO much to learn!

Stan


Thanks Stan,

I am happy it worked for you.


I grew up on GLUT as well, I slept with its printed specification under pillow, but there was a moment I realised I need more control - especially the glutMainLoop, which could be quit only by exiting application made me decide to seek other approaches.


Petr

sblank
15-11-2010, 16:28
I worked on porting my FreeBasic OpenGL program over the weekend and became stuck on the following two lines in the chaptrack function:

glMultMatrixf @aff(0)
glGetFloatv GL_TEXTURE_MATRIX, @aff(0)

The aff() matrix is the affine matrix used in the rotations of the 3D figure. I use the GL_TEXTURE_MATRIX because it is handy and not being used for any other purpose ;)

Anyway, I'm using a pointer to multiply the aff() matrix by the current transformation/rotation matrix and then storing the newly transformed matrix in GL_TEXTURE_MATRIX. Again, using pointers...

I am not certain how to do this using TBGL or raw OpenGL in thinBasic... I tried using varptr but obviously did not know what I was doing :D

Thanks for any "pointers" you might have,

Stan

Petr Schreiber
15-11-2010, 17:12
Hi Stan,

no need to struggle with pointers, as the declaration is made to get the array by reference. So for the two lines, the TB code would look like:


glMultMatrixf(matrix)
glGetFloatv(%GL_TEXTURE_MATRIX, matrix)



Petr

sblank
15-11-2010, 22:35
Fantastic... thanks for the "pointer" :D

Seriously, I'll give this a try. ByRef makes sense.

Cheers,

Stan


Hi Stan,

no need to struggle with pointers, as the declaration is made to get the array by reference. So for the two lines, the TB code would look like:


glMultMatrixf(matrix)
glGetFloatv(%GL_TEXTURE_MATRIX, matrix)

Petr

Petr Schreiber
15-11-2010, 22:55
I prefer the byref approach over pointers (less typing, same effect), that's why headers are translated this way.

If you are unsure about parameters, you can always check the headers in the ThinBASIC/Inc directory or ask here of course!

We had a few iterations of the headers until we found current state, originally there were the pointers everywhere, but I think the current way is more user friendly (once you know BYREF is there :) ).


Petr

sblank
16-11-2010, 18:20
I agree... ByRef is much simpler than trying to use pointers (for me, anyway). Sending the matrix name (ByRef) worked!

Another question:

What is the TBGL equivalent of the glutIdle() function? Is that your "while TBGL_IsWindow" function?

I use glutIdle() in the program I'm trying to port... just curious.

Thank,

Stan


I prefer the byref approach over pointers (less typing, same effect), that's why headers are translated this way.

If you are unsure about parameters, you can always check the headers in the ThinBASIC/Inc directory or ask here of course!

We had a few iterations of the headers until we found current state, originally there were the pointers everywhere, but I think the current way is more user friendly (once you know BYREF is there :) ).


Petr

Petr Schreiber
16-11-2010, 19:35
Hi Stan,

it's up to you how you structure code in TBGL, the endless loop is just one approach possible.

The another could be to use the periodic function and base the organization on that.

Here minimal example, hWnd and FrameRate are passed to idle and input functions just in case you would like to use it, but it is not necessary of course.


Uses "TBGL"

Global angle As Double

Function TBMain()
Local hWnd As DWord
Local FrameRate As Double

' -- Create and show window
hWnd = TBGL_CreateWindowEx("TBGL script - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

' -- Initialize lighting
TBGL_UseLighting(%TRUE)
TBGL_UseLightSource(%GL_LIGHT0, %TRUE)

TBGL_SetLightParameter(%GL_LIGHT0, %TBGL_LIGHT_POSITION, 15, 10, 15, 1)

' -- Resets status of all keys
TBGL_ResetKeyState()

TBGL_BindPeriodicFunction( hWnd, "funRender", 10 )
TBGL_ProcessPeriodicFunction(hWnd)

TBGL_DestroyWindow
End Function

' -- Do any calculations here
Function funIdle( hWnd As DWord, frameRate As Double )

angle += 90/FrameRate

End Function

' -- Handle input here
Function funInput( hWnd As DWord, frameRate As Double )

If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then
TBGL_UnBindPeriodicFunction(hWnd)
Exit Function
End If

End Function

Function funRender()

Dim hWnd As DWord = TBGL_CallingWindow
Dim FrameRate As Double

frameRate = TBGL_GetFrameRate

' -- Process input
funIdle( hWnd, FrameRate)

' -- Process calculations
funInput( hWnd, FrameRate )

FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame
TBGL_Camera(15, 15, 15, 0, 0, 0)

TBGL_Rotate(angle,0,1,0)
TBGL_Color(255, 128, 0)
TBGL_Box(1, 1, 1)

TBGL_DrawFrame

End Function



Petr

sblank
17-11-2010, 04:18
Hi Petr,

Very nice! This should help immensely...

When I finish the translation, I'll have to list you as a co-author :D

Thank you very much for the tip... and for the elegant program example!

Stan


Hi Stan,

it's up to you how you structure code in TBGL, the endless loop is just one approach possible.

The another could be to use the periodic function and base the organization on that.

Here minimal example, hWnd and FrameRate are passed to idle and input functions just in case you would like to use it, but it is not necessary of course.


Uses "TBGL"

Global angle As Double

Function TBMain()
Local hWnd As DWord
Local FrameRate As Double

' -- Create and show window
hWnd = TBGL_CreateWindowEx("TBGL script - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

' -- Initialize lighting
TBGL_UseLighting(%TRUE)
TBGL_UseLightSource(%GL_LIGHT0, %TRUE)

TBGL_SetLightParameter(%GL_LIGHT0, %TBGL_LIGHT_POSITION, 15, 10, 15, 1)

' -- Resets status of all keys
TBGL_ResetKeyState()

TBGL_BindPeriodicFunction( hWnd, "funRender", 10 )
TBGL_ProcessPeriodicFunction(hWnd)

TBGL_DestroyWindow
End Function

' -- Do any calculations here
Function funIdle( hWnd As DWord, frameRate As Double )

angle += 90/FrameRate

End Function

' -- Handle input here
Function funInput( hWnd As DWord, frameRate As Double )

If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then
TBGL_UnBindPeriodicFunction(hWnd)
Exit Function
End If

End Function

Function funRender()

Dim hWnd As DWord = TBGL_CallingWindow
Dim FrameRate As Double

frameRate = TBGL_GetFrameRate

' -- Process input
funIdle( hWnd, FrameRate)

' -- Process calculations
funInput( hWnd, FrameRate )

FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame
TBGL_Camera(15, 15, 15, 0, 0, 0)

TBGL_Rotate(angle,0,1,0)
TBGL_Color(255, 128, 0)
TBGL_Box(1, 1, 1)

TBGL_DrawFrame

End Function

Petr