CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jan 2008
    Posts
    78

    Keyboard events and OpenGL (without GLUT)

    I need to create a program using OpenGL without GLUT. I saw an OpenGL example where the window is created with 'WinMain', but I don't know how to listen to keyboard, or even mouse events.

    Can someone point me to the good direction, please? Have in mind that I'm fairly new to C++ and WinAPi programming.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Keyboard events and OpenGL (without GLUT)

    You know glutMainLoop()? It turns out that every GUI system is based around a similar function: A loop that does nothing except process and dispatch "events" to their appropriate handlers.

    These handlers are things you define yourself. If you want your handler to be invoked when the mouse is clicked within a window you've created, you attach it to the button-press event associated with that window.

    I'm using GTK to do this (it's cross-platform), so I'm not entirely sure how you'd do it with WinAPI directly.

    In any event, it's not an OpenGL question. OpenGL exists somewhere off to the side of all of this stuff----it's pure GUI programming at this level.

  3. #3
    Join Date
    Jan 2008
    Posts
    78

    Re: Keyboard events and OpenGL (without GLUT)

    I feel pretty easy at programming GUI, but using Java.
    So you think I can use GTK? I'll check it out.

    Thanks.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Keyboard events and OpenGL (without GLUT)

    You'll need gtkglext if you want to use OpenGL in a GTK window----in particular, I used it to make a gtk_draw_space widget OpenGL-capable. Just FYI.

    I don't know if gtk has Java bindings or not.

  5. #5
    Join Date
    Jul 2003
    Location
    Linköping, Sweden
    Posts
    261

    Re: Keyboard events and OpenGL (without GLUT)

    I recommend against using WinAPI. I did that when I started doing graphics programming, and it was a whole lot of effort learning a system I never use.

    GTK is one option, as previously mentioned, but if you want to do graphics applications that consist of no more GUI elements than the OpenGL window itself (typical for things such as games), I strongly recommend SDL. It's multiplatform, extremely easy to use and quite flexible. Use it to set up an OpenGL window and handle all your input. It will save you many hours of work.

    If you want a GUI built to look like a native Windows/X11/MacOS application with or without one or more OpenGL windows, I recommend wxWidgets. Like SDL, it is multiplatform, but with some pros and cons. It is a very complete window handling library, allowing you to create advanced user interfaces that look native to whatever platform you compile it on. The cost is increased complexity - it is not nearly as easy to use as SDL.
    Errare humanum est, ergo non sum humanus.

  6. #6
    Join Date
    Aug 2007
    Posts
    78

    Re: Keyboard events and OpenGL (without GLUT)

    I like to use even another gui library: Qt. Also cross platform, and imo easy to use. (only the opensource version is free though)

    If you haven't already, be sure to take a loot at:

    http://nehe.gamedev.net/

    it has a lot of code in their lessons, which are ported to almost any gui library that can be used with opengl.

  7. #7
    Join Date
    Jan 2008
    Posts
    78

    Re: Keyboard events and OpenGL (without GLUT)

    Thank you all for the input.

    I won't use GTK+ because it requires additional libraries and since this is an assignment I shouldn't rely on them. I still have to take a look at SDL.

    My teacher prefer that I use GLUT so I'm giving it a try, but the problems already appeared.
    I have this:
    PHP Code:
    class Application{
             ...
             public:
                      
    void start();
                      
    void display();
             ...
    }

    void Application::start(){
             ...
             
    glutDisplayFunc(display); // As in all the tutorials I've seen.
    }

    void Application::display(){
             ...

    The compiler yields 'error C3867: 'Application::run': function call missing argument list; use '&Application::run' to create a pointer to member'.

    So, I do the following:
    PHP Code:
    glutDisplayFunc(&Application::display); // As the compiler suggests. 
    Then again, the compiler prints 'error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from 'void (__thiscall Application::* )(void)' to 'void (__cdecl *)(void)''.
    I'm clueless...

    I've started to work on this several days ago and I have virtually nothing done, so far. Most of my time is spent learning VC++ and resolving irritating problems like the one above, instead of focusing in the relevant problem.

    I'm gonna check out SDL, right now.

    EDIT: SDL looks great! Unfortunately, I doubt my teacher allows me to use it. I guess I'll have to stick to GLUT or win32...
    Last edited by alexin; February 3rd, 2008 at 11:46 AM.

  8. #8
    Join Date
    Aug 2007
    Posts
    78

    Re: Keyboard events and OpenGL (without GLUT)

    Dont know exactly why this wont work, try making the function (Application::display()) static so the callback can access the function without trouble.


    Found some more info on the subject:
    http://www.newty.de/fpt/callback.html#static
    Last edited by ejac; February 4th, 2008 at 06:39 AM.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Keyboard events and OpenGL (without GLUT)

    Passing member functions as function pointers is tricky. Making them static should do the trick; if you can't, for some reason, then you can try using one of these:
    http://www.aoc.nrao.edu/~tjuerges/AL...madaptors.html

  10. #10
    Join Date
    Jan 2008
    Posts
    78

    Re: Keyboard events and OpenGL (without GLUT)

    Thanks guys!

  11. #11
    Join Date
    Feb 2008
    Posts
    2

    Re: Keyboard events and OpenGL (without GLUT)

    Hey Alexin,

    Don't know if on the right track yet, but I have done some
    OpenGL programming in C and the WIN32 API.

    In Win32, each window is assingned a Callback function
    when the window class in created/registered.

    Messages are passed to this callback and then handled in
    a switch case. case WM_LBUTTONDOWN is, as you can tell,
    is for the mouse left button down.

    I've used GLUT before, and its great, but its no longer maintained.

    Win32 can seem pretty freaky compared to GLUT, but soon the
    initial shock will wear off.

    Do a google serach for Forgers Win32 tutorials to get ramped up
    on that. Check out NeHe for Win32 OpenGL tutorials in C and C++


    Cheers

  12. #12
    Join Date
    Jan 2012
    Posts
    2

    Re: Keyboard events and OpenGL (without GLUT)

    Check out the code downloadable from http://galaxy-forces.com/

    It initializes and uses OpenGL on Windows, Linux and Mac. It does not use glut or any other library. All is native. Also the code is public domain.

    There is an input handler class included also (keyboard, mouse, joystick). Keyboard and mouse works on all 3 OS. Joystick only on Windows and Linux. It's most suitable in games since it is implemented by polling, but it might be possible to fix it to be event driven.

    Hope this helps someone.

    /rh_galaxy

  13. #13
    Join Date
    Jan 2012
    Posts
    2

    Re: Keyboard events and OpenGL (without GLUT)

    Check out the code at http://galaxy-forces.com

    It initializes and uses OpenGL without the use of glut or other external libraries. All is native. It works on Windows, Linux and Mac.

    There is also an input library, used to get input from keyboard, mouse and joystick.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured