View Full Version : GLUT callback in GLFW
12-05-2011, 04:52 AM
Hi, I want to ask a question on function callback. as we know, in glut, there is callback function such as glutReshapeFunc, glutDisplayFunc and glutIdleFunc. for these callback in glut, what is it in GLFW and how to implement it? I'm just working with GLFW a few days ago and I don't quite understand how to use it even after studying the manual and there is not many online example. can some one help me?
thank you :)
12-05-2011, 05:22 AM
Be sure to read the user guide, it is explained.
Idle and display callbacks are not needed, as you simply call your code in sequence.
How does your rendering loop with OpenCV looks like ? Please post some code with proper code blocks, and keep it small.
12-05-2011, 08:34 PM
this is my main method. it is still not finished and I still have to add other function to use the x-coordinate from hand detection to move the game.
here I encounter a problem, when I run the program, the hand detection program run slowly with huge delay and it eat up a lot of memory. but when I exit the game, the hand detection program can run smoothly. any idea?
int main(int argc, char* argv)
int key = cvWaitKey(33);
cvNamedWindow( "imageSkinBlobs" );
cvNamedWindow( "source" );
while(!glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS || key != 27)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
cvShowImage( "source", imageSkinPixels );
cvShowImage( "imageSkinBlobs", imageSkinBlobs );
key = cvWaitKey(1);
cvDestroyWindow( "source" );
cvDestroyWindow( "imageSkinBlobs" );
12-05-2011, 10:25 PM
is there any command for glutPostRedisplay() in GLFW? my game window keeps blinking..i wonder why..
12-06-2011, 12:39 AM
is there any command for glutPostRedisplay() in GLFW?
There doesn't need to be one; you control the message processing loop. You decide when to render stuff.
12-06-2011, 09:06 AM
glfwSwapInterval(1) can help to reduce the consumption of resources (ie. it will limit the number of renderings to 60 per second).
If you need to better balance CPU between programs, add some sleep time inside the loop. Not sure how that fits with all your cvWaitKey
Why do you have a separate program for hand detection ? It would be easier to have a simple program doing both tasks, no ?
About the blinking, try to simplify your program to a minimal code, you will see what is the source of the blinking. Do you request a double buffered window ? You should.
12-06-2011, 09:17 PM
can I email you my code? can u consult me what to do? I think I really need your consultation on this graphic programming..
12-06-2011, 11:55 PM
You can post more of your code but USE THE PROPER CODE TAGS FOR THIS FORUM ! There a button for that !
For the blinking problem :
- what is in initializeGLFW() ?
- what is in Draw() ?
- do you try with GL and CV to draw on the same window ? You must not do that. If you use GL, use only that to draw. Just upload cv image data to a gl texture, and draw a textured quad with it.
12-10-2011, 10:37 PM
hi, sorry for bothering, I am new here, please don't get mad :)
this is my code for initialization:
if (glfwInit() != GL_TRUE)
if (glfwOpenWindow(WindowWidth, WindowHeight, 5, 6, 5,
0, 0, 0, GLFW_WINDOW) != GL_TRUE)
and this is the code for Draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(CurrentState == GameState::MainMenu)
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(-10.0f, 10.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); // Top left
glVertex3f(10.0f, 10.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); // Top right
glVertex3f(10.0f, -10.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); // Bottom right
glVertex3f(-10.0f, -10.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); // Bottom left
else if(CurrentState == GameState::InGame)
----draw game interface-------
----set up camera-------------
----draw hockey table, bat and puck-----
and this is the code for Setup2DOrthoSpace
void Setup2DOrthoSpace(int w, int h)
glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, 0.0f, zFarDist);
I am not drawing GL and CV in one window, they are in different window actually. GL is for the game and CV is just to display the image detected by webcam.
12-11-2011, 02:41 AM
Flicker is because you swapbuffers twice for each frame.
Remove the one which is in Setup2DOrthoSpace.
glfwOpenWindow(WindowWidth, WindowHeight, 5, 6, 5, 0, 0, 0, GLFW_WINDOW);
Try a more realistic call like :
glfwOpenWindow(WindowWidth, WindowHeight, 8, 8, 8, 0, 0, 0, GLFW_WINDOW);
Are you sure you don't need a depth buffer ?
12-11-2011, 03:16 AM
that means I just have to put only 1 swapbuffers. ok, problem solved. thank you. then, if i want to use the depth buffer, where to put it? and the glfwSwapInterval(1) that you mention before. is it in initialize function or main function?
12-11-2011, 12:47 PM
swap interval : call it once, anywhere after GL window has been created.
For the depth buffer, just ask for 24 bits of depth buffer in the create window, then clear depth any time you need it, ie. at the same time you clear color.
12-11-2011, 09:13 PM
my physical memory still increasing when the game starts. the swapinterval seems got no effect on it. anyways, can you give me some basic idea how to move the game. right now, what I have done is:
1. set the region where the player state will change. eg: if hand position is larger than 319(x-coordinate), player1state is 1. then, it will be pass to other function for bat movement.
suppose it will move, but it doesn't. somebody told me that I need to send key value to keyboard function to move it. I've done it like this:
void GLFWCALL keyboardFunc(int key, int action)
key = GLFW_KEY_RIGHT;
action = GLFW_PRESS;
key = GLFW_KEY_LEFT;
action = GLFW_PRESS;
if(key == GLFW_KEY_LEFT && action == GLFW_PRESS)
if(key == GLFW_KEY_RIGHT && action == GLFW_PRESS)
and this one also cannot move the game. what is the right way to do it?
12-12-2011, 01:38 AM
Did you verify that your CheckBatMovement is actually working ?
It looks you do not really understand what you are doing.
To properly troubleshoot, try to simplify to the maximum the code, so that no doubt is left to where is the problem.
- add printf traces to check code paths and runtime values
- use a debugger (a bit more complex so try it later)
- use a code versioning system (svn, git, or at least frequent manual backups on different files). That way you can remove code until it works as intended, and diff with the non-working code to see the errors.
12-12-2011, 02:31 AM
it is functioning well if i use keyboard to change the state. actually I do not understand about the sending key value thing. anyway, I'm just trying to do what they thought as the solution and it doesn't work. maybe I should map the value directly.
12-12-2011, 02:39 AM
Add printf traces inside your tests for xPos, and do not forget to add an else for all others cases, to really see what happens. Print the xPos value as well.
Is is intended that that exact value 319 will not pass in any of your current test branches ? Need "greater or equal" operator >=
12-12-2011, 04:39 AM
the bat can move now! I'm so happy!
ok, my next question. how to render string in GLFW. previously, my glut code is
RenderString(-9.5f, 8.25f, "Player 1", GLUT_BITMAP_TIMES_ROMAN_24);
do glfw have the same thing as "GLUT_BITMAP_TIMES_ROMAN_24"?
12-12-2011, 05:08 AM
Displaying text in OpenGL is not straightforward.
I would suggest if it is enough for you, keep the glut method just for displaying text (unless you absolutely want to not link with glut).
Otherwise there are various libs and tools available to help in the domain of text rendering in OpenGL.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.