Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: problem on handling keyboard in opengl

  1. #1
    Newbie Newbie
    Join Date
    May 2013
    Posts
    3

    Lightbulb problem on handling keyboard in opengl

    ihave problem in this code

    Code cpp:
    #include <stdio.h>
    #include <stdlib.h>
     
    #include <cmath>
    #include <cstdlib>
    #include <glut.h>
    #include <iostream>
     
     
    void createMenu(void);
    void menu(int value);
    float translateX = 0,translateY = 0;
    static int win;
    static int menyid;
    static int transformation;
    static int objectcolor;
    static int Backgroundcolor;
    static int val = 0;
    GLsizei wh =720; 
    GLsizei ww = 900 ;
    bool  Point=false;
     
    void drawLine()
     
    {
          glPointSize(5);
              glTranslatef(translateX, translateY, 0);
              glColor3f(1.0,0.0,1.0);
              glLineWidth(6);
        glBegin(GL_LINES);
       glVertex2f(600.0,20.0);
       glVertex2f(30.0,20.0);
        glEnd();
        glFlush();
     
    }   
     
    void display(void)
    {   
        glClearColor (1.0, 1.0, 1.0, 1.0);
        glClear (GL_COLOR_BUFFER_BIT);
        glLoadIdentity(); 
        drawLine(); // call function to draw line and Using ARROW UP,DOWN ,LEFT,RIGHT to move this Line 
     
      glPointSize(10);
     glColor3f(1.0,0.0,1.0);
     // Drawing Point 
     if(Point)
     {
     
               glPointSize(10);
     glColor3f(1.0,0.0,1.0);
     
        glBegin(GL_POINTS);
       glVertex2f(55.0,60.0);
        glEnd();
        glFlush();
     
     }
        glFlush();
    }
     
    void reshape(int w, int h)
    {   
        glViewport(0,0,(GLsizei)w,(GLsizei)h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
         //gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
      gluOrtho2D ( 0.0, (GLdouble)ww, 0.0, (GLdouble)wh ); //Display area
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
     
     
     void keypress (unsigned  char key, int x, int y)
    {
        int modifiers = glutGetModifiers();
     
     
     
     
         switch (key) 
         {
           case 'a':
           case 27:
                   Point=true;
                   glutPostRedisplay();
                   break;
         }
     
     
     
    }
    void specialfun(int k, int x, int y)
        {
        if (k == GLUT_KEY_LEFT)
            translateX -=1;// lef direction 
         else if (k == GLUT_KEY_RIGHT)//right direction 
            translateX +=1;
        else if (k == GLUT_KEY_UP)
            translateY +=1; // up direction 
        else if (k == GLUT_KEY_DOWN)
            translateY -=1; // down direction
         glutPostRedisplay();
    }
     
    void spindisplay(void)
    {       
        glutPostRedisplay();
    }
    void createMenu(void){
        //////////
        // MENU //
        //////////
     
        // Create a submenu, this has to be done first.
        transformation = glutCreateMenu(menu);
     
        // Add sub menu entry
        glutAddMenuEntry("scaling", 1);
        glutAddMenuEntry("rotation", 2);
     
        objectcolor = glutCreateMenu(menu);
     
        glutAddMenuEntry("yellow", 3);
        glutAddMenuEntry("black", 4);
    Backgroundcolor = glutCreateMenu(menu);
     
        glutAddMenuEntry("red", 3);
        glutAddMenuEntry("blue", 4);
        // Create the menu, this menu becomes the current menu
        menyid = glutCreateMenu(menu);
     
        // Create an entry
        glutAddSubMenu("Transformation", transformation);
        glutAddSubMenu("Object color", objectcolor);
        glutAddSubMenu("Background  color", Backgroundcolor);
        // Create an entry
        glutAddMenuEntry("Quit", 0);
     
        // Let the menu respond on the right mouse button
        glutAttachMenu(GLUT_RIGHT_BUTTON);
     
     
    }
     
     
     void menu(int value){
        if(value == 0){
            exit(0);
            glutDestroyWindow(win);
     
        }else{
            val=value;
        }
     
        // you would want to redraw now
        glutPostRedisplay();
    }
    int main(int argc, char **argv)
    {
     
        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
        glutInitWindowSize(800,768);
        glutInitWindowPosition(0,0);
        createMenu();
        glutCreateWindow("ASSIGNMENT2");
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
     
            glutKeyboardFunc(keypress);
           glutSpecialFunc(specialfun);
        // ste the function to handle the updates for our program
     
         // Create our popup menu
    createMenu();
      glutAttachMenu (GLUT_RIGHT_BUTTON);
     
        glutMainLoop();
     
    }

    ineed if iam press keyboard key like A or a draw shape i screen >> in this code not work
    Last edited by Dark Photon; 05-31-2013 at 05:52 PM.

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,126
    Quote Originally Posted by abubaker View Post
    ineed if iam press keyboard key like A or a draw shape i screen >> in this code not work
    Huh? Press the 'a' key, and you get your point. What's the problem?

  3. #3
    Newbie Newbie
    Join Date
    May 2013
    Posts
    3

    if iam press 'a' the point not show

    Quote Originally Posted by Dark Photon View Post
    Huh? Press the 'a' key, and you get your point. What's the problem?
    are u try the code
    if iam press a the point not show why idont now plz help me
    if iam use the ascii code for a again not work
    if iam change the ascii code form a nubmber to esc it work

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,106
    I tested your code and it works fine for me. Set a breakpoint in the keyboard routine and see what character arrives on your system.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •