Click to See Complete Forum and Search --> : Keyboard events and OpenGL (without GLUT)


alexin
January 31st, 2008, 10:00 AM
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.

Lindley
January 31st, 2008, 10:29 AM
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.

alexin
January 31st, 2008, 10:49 AM
I feel pretty easy at programming GUI, but using Java.
So you think I can use GTK? I'll check it out.

Thanks.

Lindley
January 31st, 2008, 01:37 PM
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.

Hnefi
January 31st, 2008, 03:45 PM
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 (http://libsdl.org). 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 (http://www.wxwidgets.org). 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.

ejac
February 3rd, 2008, 08:33 AM
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.

alexin
February 3rd, 2008, 10:40 AM
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:
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:
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...

ejac
February 4th, 2008, 05:36 AM
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

Lindley
February 4th, 2008, 11:16 AM
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/ALMA/STL/html/group__s20__3__8__memadaptors.html

alexin
February 4th, 2008, 03:12 PM
Thanks guys!

dudebot
February 12th, 2008, 06:22 PM
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

rh_galaxy
January 5th, 2012, 11:02 AM
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

rh_galaxy
January 5th, 2012, 11:11 AM
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.