PDA

View Full Version : problem on handling keyboard in opengl



abubaker
05-31-2013, 12:38 PM
ihave problem in this code


#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

Dark Photon
05-31-2013, 06:57 PM
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?

abubaker
06-01-2013, 11:41 AM
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

tonyo_au
06-01-2013, 08:30 PM
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.