Results 1 to 10 of 10

Thread: draw/show function using glutkeyboardfunc c++

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2017
    Location
    germany
    Posts
    16

    draw/show function using glutkeyboardfunc c++

    I have a switch statement which checks for user input using glutkeyboardfunc, but I am wanting to have something drawn when a certain key is pressed, i was thinking something along the lines of this should work, but it doesnt seem to be

    main.cpp

    Code :
    void keyboard(unsigned char key, int x, int y)
    {
    	switch (key)
    	{
    	case 'a': lander.movement(-0.1, 0);
    		break;
    	case 'd': lander.movement(0.1, 0);
    		break;
    	case 'w': lander.movement(0.0, 0.1);
    		thruster.show();
    		break;
    	case 's': lander.movement(0.0, -0.1);
    		break;
    	default:
    		lander.movement(0, 0);
    		break;
    	}
    }

    thruster.cpp

    Code :
    void Thruster::show()
    {
    	draw();
    }
     
    void Thruster::draw()
    {
    	glBegin(GL_POLYGON);
    	glColor3f(1.0, 0.0, 0.0);
    	glVertex2f( 200,13);
    	glColor3f(1.0, 0.0, 0.0);
    	glVertex2f(500, 3);
    	glColor3f(1.0, 0.0, 0.0);
    	glVertex2f( 900, 3);
    	glEnd();
    }

    I have also tried calling it using

    Code :
    void booster(char press) {
    	cin >> press;
    	if (press == 'w')
    		thruster.show();
     
    };

    but none of these ideas seem to be working, is there a way to do this?

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,105
    You shouldn't be issuing drawing commands other than from within the display callback. The display callback needs to call glutSwapBuffers() after everything has been drawn.

    The keyboard and mouse callbacks should just be setting variables which affect what the display callback draws. If you aren't redrawing continuously, then call glutPostRedisplay() from the mouse/keyboard callbacks to schedule a redraw.

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2017
    Location
    germany
    Posts
    16
    I am running a timer, which calls on the display function, would i be able to put an if statement in the display function then that would be say something like if key==65 thruster.draw

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,105
    Quote Originally Posted by Pedrosanchez87 View Post
    I am running a timer, which calls on the display function, would i be able to put an if statement in the display function then that would be say something like if key==65 thruster.draw
    Create a global variable, set it to 1 in the key-press callback, set it to 0 in the key-release callback, have the display callback draw the thruster only if the variable is non-zero.

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2017
    Location
    germany
    Posts
    16
    I am not using a key realise etc only

    Code :
    void keyboard(unsigned char key, int x, int y)
    {
    	switch (key)
    	{
    	case 'a': lander.movement(-0.1, 0);
     
    		break;
    	case 'd': lander.movement(0.1, 0);
    		break;
    	case 'w': lander.movement(0.0, 0.1);
    		G_ThrustShow == 1;
    		break;
    	case 's': lander.movement(0.0, -0.1);
    		break;
    	default:
    		lander.movement(0, 0);
    		break;
    	}
    }

    but i have declared a global var G_ThrustShow set to 0

    You can see in the code above w should set it to 1 then in the display i have

    Code :
    if (G_ThrustShow ==1)
    	{
    		thruster.show();
    	}

    is this what you sort of meant, should this work, as it doesnt seem to be, maybe i have something wrong?

  6. #6
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,105
    Quote Originally Posted by Pedrosanchez87 View Post
    I am not using a key realise etc
    Then how are you going to know when to stop displaying the thruster? You need to use glutKeyboardUpFunc() to register a key-release callback.

    Quote Originally Posted by Pedrosanchez87 View Post
    Code :
    		G_ThrustShow == 1;
    The above should use "=" (assignment) rather than "==" (test for equality). If you compile with warnings enabled, the compiler should warn you about that (e.g. "statement with no effect" with gcc -Wall).

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2017
    Location
    germany
    Posts
    16
    Quote Originally Posted by GClements View Post
    Then how are you going to know when to stop displaying the thruster? You need to use glutKeyboardUpFunc() to register a key-release callback.
    That i was planning on working out when i had gotten it to work

    and i havent actually went over any key release callbacks etc , im just new to programming really, so will need to look it up now

  8. #8
    Junior Member Newbie
    Join Date
    Feb 2017
    Location
    germany
    Posts
    16
    Do you think this should work

    Code :
    void keyboard(unsigned char key, int x, int y)
    {
    	if (key == (77));
    	{
    		lander.movement(0.0, 0.1);
    		G_ThrustShow = 1;
    	}
     
    	if (key == (!77));
    	{
     
    		G_ThrustShow = 0;
    	}
    }

  9. #9
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,105
    Quote Originally Posted by Pedrosanchez87 View Post
    Do you think this should work
    Code :
    	if (key == (!77));
    No.

    That's the same as
    Code :
    	if (key == 0);
    Also, the trailing semicolons shouldn't be there (they'll terminate the "if" statement", making the following statement a separate statement rather than part of the "if" statement).

    To be honest, I think that you need to spend some more time learning C++ before trying to learn OpenGL or GLUT.

  10. #10
    Junior Member Newbie
    Join Date
    Feb 2017
    Location
    germany
    Posts
    16
    I know i really need to learn more c++ but im learning in uni and they sort of only taught the basics of c++ then moved on to opengl, no idea why.

    But I did get it to work, its not perfect. I have the timer function resetting the variable, so it sort of flashes, but it is meant to represent flames so it sort of suits.

Similar Threads

  1. Show function on the surface of a sphere
    By AeRoOo in forum OpenGL: Basic Coding
    Replies: 14
    Last Post: 03-14-2014, 07:23 PM
  2. draw/construct/show a park
    By ma123 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-02-2011, 06:42 AM
  3. Can i add parameters to the GLUT glutKeyboardFunc function?
    By darkenreaper57 in forum OpenGL: Basic Coding
    Replies: 16
    Last Post: 03-07-2004, 07:21 AM
  4. how to show a 3d object with rotation function?
    By xingstar in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-28-2002, 03:24 AM
  5. Cpw visibility function - not seeing Hide or Show event.
    By jpummill in forum OpenGL: General
    Replies: 3
    Last Post: 02-27-2002, 09:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean