PDA

View Full Version : Passing Parameters To Callback Functions?



larryl
01-27-2017, 02:43 AM
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 (https://learnopengl.com/#!Lighting/Basic-Lighting) series of GREAT tutorials depends on.

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

Thanks
Larry

Cornix
01-27-2017, 03:22 AM
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.

larryl
01-27-2017, 03:27 AM
Thanks Cornix:

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

Larry

Cornix
01-27-2017, 03:43 AM
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?

Silence
01-27-2017, 05:44 AM
I guess this (http://discourse.glfw.org/) is where you should be asking for.

larryl
01-27-2017, 02:55 PM
Thanks Cronix and Silence:

I posted here (http://discourse.glfw.org/t/passing-parameters-to-callbacks/848), which should answer Cronix's questions.

I appreciate your suggestions.

BBeck1
01-29-2017, 09:20 AM
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 \n");
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
_glDebugMessageCallback(OGLGameNameSpace::openglCa llbackFunction, nullptr); //Makes our function the error handler. 2nd parameter unused.
GLuint unusedIds = 0;
}
else
OutputDebugString(L"glDebugMessageCallback not available\n");
#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------------\n");
OutputDebugString(L"message: ");
std::string GLMessage(message);
std::wstring Message;
Message.assign(GLMessage.begin(), GLMessage.end());
OutputDebugString(Message.c_str());
OutputDebugString(L"\n");
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"\n");

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

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"\n");
OutputDebugString(L"---------------------opengl-callback-end--------------\n");

}


If this is the sort of thing you're looking for, the entire project and source code are available for download on my website (http://virtuallyprogramming.com/OGL45/OGL45.html) 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.