Reputation: 1069
I've just started learning about OpenGL/Glut and I'm facing some issues. I want to print a string key: a
whenever I click the key a
, but it seems like I don't know what I'm doing.
I'm calling glutKeyboardFunc()
from the main()
function, and I'm passing it keyinput()
, which tests whether key
is ESC
or a
. Another thing is, when I try to print something from inside scene1()
it works just great! hmm wonder why?
void printstr(void* font, char* str, GLfloat x, GLfloat y, GLfloat z)
{
char* c = str;
// set the raster position
glRasterPos3f(x, y, z);
for(; *c != '\0'; c++)
glutBitmapCharacter(font, *c);
}
void keyinput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
killsound();
exit(0);
break;
case 'a':
glLoadIdentity();
glColor3f(0.0f, 1.0f, 0.0f); // Green
printstr(GLUT_BITMAP_8_BY_13, "Key: a", 0.0f, 0.9f, 0.0f);
break;
}
}
int main(int argc, char** argv)
{
// initialization and stuff
glutInit(&argc, argv);
.
.
.
glutDisplayFunc(scene1);
glutIdleFunc(scene1);
.
.
.
glutKeyboardFunc(keyinput);
.
.
.
glutMainLoop();
}
Upvotes: 0
Views: 2976
Reputation: 2065
It's probably because GLUT is calling keyinput() outside of your rendering code.
I'm assuming your drawing function (glutDisplayFunc or glutIdleFunc) starts by calling glClear() and ends by calling glutSwapBuffers()? If so then any draw calls that take place inside of keyinput() would be done before glClear() is called, so the drawing won't be seen.
On top of that, keyinput isn't called continuously. It gets called when your key is pressed. So your text will only display for one instant, not all the time.
I suggest you make a global variable,
int aIsPressed=0;
In keyinput, set aIsPressed to the number of frames you want your message to be displayed (if you're limiting your framerate, try 60, if not, try 1000)
Then, in your drawing function, do this along with your code for drawing everything else:
if(aIsPressed>0)
{
glLoadIdentity();
glColor3f(0.0f, 1.0f, 0.0f); // Green
printstr(GLUT_BITMAP_8_BY_13, "Key: a", 0.0f, 0.9f, 0.0f);
aIsPressed--; //this way aIsPressed will count down to zero, and the message will disappear again
}
Upvotes: 1