Silverfrost Forums

Welcome to our forums

Keyboard input in OpenGL

2 Nov 2010 8:30 #7112

It's so pity not many use OpenGLwith this compiler. There exist lessons for OpenGL in many languages and compilers (made by Jeff Molofee, Jean-Philippe Perois ('DJIP') and others) but not in this one. Life would be much easier with larger users base, less bugs, problems and conflicts. This and Clearwin (potentially even more with Visual Clearwin) would be no doubt the most shiny selling points of this compiler which is like Swiss army knife is self-sufficient for the what most people do in scientific programming.

Well, besides getting the conflict of <windows.ins> with <opengl.ins> when the functions at the bottom of opengl.ins file are used, i struggle right now to make keyboard handling work in OpenGL.

Two test codes are here, we discussed them on page 2 of this post

https://forums.silverfrost.com/Forum/Topic/1136&start=15

If we just for example change the line

  i=winio@('%^gr[black]&amp;',300,300,gr_func) 

with

  i=winio@('%^og[double,depth32]&amp;',400,400,gr_func)

both these versions of keyboard handling (via WM_KEYDOWN or getwkey@ ) stop handling keyboard.

How make OpenGL react on keyboard input? I've done it before reacting on arrow keyboard keys by adding sliding controls of Clearwin in OpenGL windows, and that was sufficient, but now i need that beautiful OpenGL screen reacted on alphanumeric input, but it keeps mum

3 Nov 2010 1:04 #7113

Dan,

I seem to think that I have had a satisfactory response from openGL for key presses, but I was just experimenting. Here are some code fragments:

         include <clearwin.ins>, nolist
         include <opengl.ins>, nolist

         integer :: i, draw, myreshape, key, specialk, idle, motion,
     &              passivemotion, mouse_press
         external :: draw, myreshape, key, specialk, idle, motion,
     &              passivemotion, mouse_press

...
         call glutInitWindowPosition(1, 1)
         call glutInitWindowSize(iwid, ihgt)
         call glutInitDisplayMode(ior(GLUT_DOUBLE, GLUT_STEREO))
         i = winio@('%ww[no_border]%pv%`og&',0, 0, w1)
...
         call glutSetWindow(w1)
         call glClearColor(0.9,0.9,1.0,1.0)
         call glutDisplayFunc(draw)
         call glutReshapeFunc(myreshape)
         call glutIdleFunc(idle)
         call glutKeyboardFunc(key)
         call glutSpecialFunc(specialk)
         call glutMotionFunc(motion)
         call glutPassiveMotionFunc(passivemotion)
...

      integer function key(k, x, y)
        implicit real*8 (a-h,o-z)
        include <opengl.ins>, nolist

        character :: k
        integer :: x, y
        select case (k)
        case ('z')
          view_rotz = view_rotz + 5.0
        case ('Z')
          view_rotz = view_rotz - 5.0
        case ('1')
          near = ifind_span(x,y,iend)
          if( near .gt. 0)then
            ivert_type(near) = 1
            call generate_model
          endif
        case ('2')
.....
        case default
          return
        end select
        call glutPostRedisplay()
        key = 2
      end function key
....
      integer function specialk(k, x, y)
         implicit real*8 (a-h,o-z)
        include <opengl.ins>, nolist

        integer :: k, x, y
        imodifier_key = glutGetModifiers()
        if(mod(imodifier_key,2) .eq. 1)then
          view_rot_scale = -1
        else
          view_rot_scale = 1
        endif
        if(mod(imodifier_key/4,2) .eq. 1)then
          alt_pressed = 5
        else
          alt_pressed = 1
        endif
        select case (k)
        case (GLUT_KEY_UP)
          if(view_rot_scale .eq. -1)then
            shift_vert = shift_vert + 0.1d0
          else
            view_rotx = view_rotx - 5.0/alt_pressed
          endif
        case (GLUT_KEY_DOWN)
          if(view_rot_scale .eq. -1)then
            shift_vert = shift_vert - 0.1d0
...
        case (GLUT_KEY_F1)
          scale_ortho = scale_ortho * 1.414
        case (GLUT_KEY_F2)
          scale_ortho = scale_ortho / 1.414
        case (GLUT_KEY_F5)
...
        case default
          return
        end select
        call glutPostRedisplay()
        specialk = 2
      end function specialk
...
3 Nov 2010 2:24 #7114

Thanks for help Ian, i'm trying to make working snippet out of this text....so far though unsuccessfully...

A lot of GLUT stuff like GLUTKEYBOARDFUNC is encoded into opengl.ins some commented some uncommented ...it's a bit messy...but nothing freaking works with keyboard so far...

glutKeyboardFunc is pretty powerful function, but things are not very intuitive for just experimenting blindly. It shows also x,y position where key was pressed, not just ascii of the pressed key, but it has to be initialized accordingly. Doc says

'glutKeyboardFunc sets the keyboard callback for the current window. When a user types into the window, each key press generating an ASCII character will generate a keyboard callback. The key callback parameter is the generated ASCII character. The state of modifier keys such as Shift cannot be determined directly; their only effect will be on the returned ASCII data. The x and y callback parameters indicate the mouse location in window relative coordinates when the key was pressed. When a new window is created, no keyboard callback is initially registered, and ASCII key strokes in the window are ignored. Passing NULL to glutKeyboardFunc disables the generation of keyboard callbacks. During a keyboard callback, glutGetModifiers may be called to determine the state of modifier keys when the keystroke generating the callback occurred. Also, see glutSpecialFunc for a means to detect non-ASCII key strokes.'

so it's a lot of sh#t initially has to be done to get it work. Damn, it's so easy with mouse handling! Just a call to get_opengl_mouse_state@ or clearwin_string@('CALL_BACK_REASON')

5 Nov 2010 8:19 (Edited: 5 Nov 2010 2:21) #7117

More headaches. In the search of keyboard functions working with OpenGL i tried these two-three

reason01=clearwin_string@('BUTTON_PRESS')
reason02=clearwin_string@('KEY_DOWN')
reason01=clearwin_string@('MESSAGE_HOOK')

Got error 'Is not known to clearwin_string@'. Checked same function with parameter 'CALL_BACK_REASON' it works fine.

5 Nov 2010 11:37 #7118

'BUTTON_PRESS' is a value returned by clearwin_string@('CALLBACK_REASON') and nothing more. It is not an input value to any routine.

These 'reasons' only apply to certain controls. You can see what reasons are provided for a given control by printing the results of clearwin_string@('callback_reason') from within your callback function. For some controls the available reasons depend on the options provided. For example, with %gr you can add %gr[full_mouse_input].

5 Nov 2010 1:35 #7119

Got it. But from your respond follows that there is no FTN95-specific library functions for keyboard handling with OpenGL, and this has to be done via OpenGL own functions?

5 Nov 2010 4:30 #7120

It looks like there is some undocumented stuff in the library for handling keyboard input for OpenGL but I am not sure if it is easily accessible from Fortran. Give me a few days and I will see if I can get it to work.

5 Nov 2010 4:34 #7121

Correction. This is the stuff used by Ian above (glutKeyboardFunc). You need to provide a callback function (key) like that in Ian's code. Now to find the documentation...

7 Nov 2010 12:38 #7122

I will appreciate any further help, tried many things but still can not solve this damn problems of selecting 3D object by keyboard (i plan make straight on screen numbering of objects and then chose them by picking numbers from keyboard, why i need keyboard handling).

Or same can be possibly done by selecting the same objects by mouse. The mouse handling itself works with FTN95 own utilities great, but there exist another problem how select the 3D object on our actually 2D screens...You can not do that by colors (do such utilities exist?) plus the colors change in 3D due to shadowing, fog, reflections etc.(if such utilities existed and if they would ignore the color change taking just the original color of the object, then it could be possible to select objects by giving them slight variations of RGB colors)

8 Nov 2010 12:51 #7125

Dan, You have to use display lists, giving each object a unique number. The when you click the mouse, you re-render the display in a GL_SELECTION mode and a small screen size of say 1 pixel by 1 pixels. This put a list of items rendered onto the reduced 'screen' size into a result list which is the number of the item's display list position and hence the selected item.
I've looked at it, but never got it to work as all the examples are in C Have a look at:

http://www.opengl.org/resources/faq/technical/selection.htm#sele0020

http://www.lighthouse3d.com/opengl/picking/

Regards Ian

9 Nov 2010 11:51 #7127

Yea, the damn thing is in C unfortunately. And the next section after display lists explains the method i described above which they call color coding.

http://www.lighthouse3d.com/opengl/picking/index.php3?color1

It has source code and explanations but again it's in C. All that easy for those who knows C and OpenGL and know this compiler OpenGL. Great would be if Paul encode all these 500 lines of C in just one single simple call to their efficient FTN95 library functions like ones CLEARWIN_INFO@ or similar:

CLEARWIN_INFO@(OpenGL_Colors) which return 3 RGB 0-255 or better typical OpenGL 4 relative 0-1 colors (one is transparency) of the point on screen with coordinates x,y (which is already provided by existing FTN95 OpenGL functions)

Please login to reply.