在opengl中处理键盘的问题 [英] problem on handling keyboard in opengl

查看:137
本文介绍了在opengl中处理键盘的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是如果按下''a''键iwant点在显示屏上呈现但是它不起作用



为什么我不知道有些身体告诉我codeproject解决这个问题



the problem if am pressing ''a'' key iwant point render in display screen but it is not work

why i dont know some body tell me about codeproject to solution this problem

#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();
 
}

推荐答案

我没有按下但不能工作


这篇关于在opengl中处理键盘的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆