Passing Parameters To Callback Functions?

Hi Folks:

I’m sorry if this is a re-post. I had a thread on this topic open as I spent this evening trying to, apparently, make OpenGL do something OpenGL really doesn’t want to do. When I pressed the “Submit New Thread” button the page hung, so it might pop up eventually.

I like to use parameters with callbacks, like Windows dialog procedures. It allows the calling program to affect the callback’s behavior.

If you’re not familiar with the concept these parameters are pointer sized, 32 or 64 bit, values that might be something as simple as binary, or a pointer into an instance of a class the callback can then access.

I don’t see any obvious means to pass parameters into OpenGL keyboard, mouse or scroll wheel callbacks.

I try to avoid passing global parameters, a practice the sample code in this series of GREAT tutorials depends on.

Has anybody come up with a way to pass a parameter to one of these callbacks?

  Thanks
  Larry

OpenGL doesnt have anything to do with keyboard, mouse or other devices. OpenGL is a standard for outputting graphics only. What you are probably using is some kind of framework that handles the windowing, context creation and input. That is not OpenGL.

Thanks Cornix:

Sorry, I’m new here. I’m referring to GLFW callback functions, like GLFWkeyfun().

Larry

And what programming language are you using with GLFW?
What kind of parameters do you want to pass?
Can you give us a specific example of what you would like to do? Perhaps with code or pseudo code?

I guess this is where you should be asking for.

Thanks Cronix and Silence:

I posted here, which should answer Cronix’s questions.

I appreciate your suggestions.

I’m not sure if this is what you’re looking for but here’s something I do in my code to do callbacks within Object Oriented Code:


MainWindow = glfwCreateWindow(WindowWidth, WindowHeight, APP_TITLE, NULL, NULL);
		if (MainWindow)
		{
			glfwMakeContextCurrent(MainWindow);
			glfwSetKeyCallback(MainWindow, Keyboard.glfw_OnKey);
			glfwSetFramebufferSizeCallback(MainWindow, OnFrameBufferSize);
			InitializedOK = true;
#if _DEBUG
			PFNGLDEBUGMESSAGECALLBACKPROC _glDebugMessageCallback = (PFNGLDEBUGMESSAGECALLBACKPROC)wglGetProcAddress("glDebugMessageCallback");
			if (_glDebugMessageCallback)
			{
				OutputDebugString(L"Register OpenGL debug callback 
");
				glEnable(GL_DEBUG_OUTPUT);
				glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
				_glDebugMessageCallback(OGLGameNameSpace::openglCallbackFunction, nullptr);		//Makes our function the error handler. 2nd parameter unused.
				GLuint unusedIds = 0;
			}
			else
				OutputDebugString(L"glDebugMessageCallback not available
");
#endif
		}

void KeyboardClass::glfw_OnKey(GLFWwindow* Window, int Key, int ScanCode, int Action, int Mode)
{
	ReceivingWindow = Window;
	KeyPressed = Key;
	ScanCodePressed = ScanCode;
	ActionPressed = Action;
	ModePressed = Mode;
}


static void APIENTRY OGLGameNameSpace::openglCallbackFunction(GLenum source,
	GLenum type,
	GLuint id,
	GLenum severity,
	GLsizei length,
	const GLchar* message,
	const void* userParam) {


	OutputDebugString(L"---------------------opengl-callback-start------------
");
	OutputDebugString(L"message: ");
	std::string GLMessage(message);
	std::wstring Message;
	Message.assign(GLMessage.begin(), GLMessage.end());
	OutputDebugString(Message.c_str());
	OutputDebugString(L"
");
	OutputDebugString(L"type: ");
	switch (type) 
	{
	case GL_DEBUG_TYPE_ERROR:
		OutputDebugString(L"ERROR");
		break;
	case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
		OutputDebugString(L"DEPRECATED BEHAVIOR");
		break;
	case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
		OutputDebugString(L"UNDEFINED BEHAVIOR");
		break;
	case GL_DEBUG_TYPE_PORTABILITY:
		OutputDebugString(L"PORTABILITY");
		break;
	case GL_DEBUG_TYPE_PERFORMANCE:
		OutputDebugString(L"PERFORMANCE");
		break;
	case GL_DEBUG_TYPE_OTHER:
		OutputDebugString(L"OTHER");
		break;
	}
	OutputDebugString(L"
");

	OutputDebugString(L"id: ");
	std::wstring ID = std::to_wstring(id);
	OutputDebugString(ID.c_str());
	OutputDebugString(L"
");

	OutputDebugString(L"severity: ");
	switch (severity) 
	{
	case GL_DEBUG_SEVERITY_LOW:
		OutputDebugString(L"LOW");
		break;
	case GL_DEBUG_SEVERITY_MEDIUM:
		OutputDebugString(L"MEDIUM");
		break;
	case GL_DEBUG_SEVERITY_HIGH:
		OutputDebugString(L"HIGH");
		break;
	}
	OutputDebugString(L"
");
	OutputDebugString(L"---------------------opengl-callback-end--------------
");

}

If this is the sort of thing you’re looking for, the entire project and source code are available for download on my website as BaseEngine.Zip, which would allow you to see it in context. I’m no expert on this, but I did get the callbacks working.