0

我有这个代码:

#include <iostream>
#include <GL/glut.h>

using namespace std;
bool* Keys = new bool[256];

void keyboardDown(unsigned char key, int x, int y) 
{
    Keys[key] = true;
}

void keyboardUp(unsigned char key, int x, int y) 
{
    Keys[key] = false;
}

void reshape(int width, int height)
{
    GLfloat fieldOfView = 90.0f;
    glViewport(0, 0, (GLsizei)width, (GLsizei)height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fieldOfView, (GLfloat)width / (GLfloat)height, 0.1, 500.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void draw() 
{
    if (Keys['e'])
        cout << "e" << endl;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glBegin(GL_QUADS);
    glColor3f(0.1, 0.1, 0.1); glVertex3f(1.0, 1.0, -1.0);
    glColor3f(0.1, 0.9, 0.1); glVertex3f(1.0, -1.0, -1.0);
    glColor3f(0.9, 0.1, 0.1); glVertex3f(-1.0, -1.0, -1.0);
    glColor3f(0.1, 0.1, 0.9); glVertex3f(-1.0, 1.0, -1.0);
    glEnd();
    glFlush();
    glutSwapBuffers();
}

void initGL(int width, int height)
{
    reshape(width, height);
    glClearColor(0.1f, 0.5f, 0.7f, 1.0f);
    glClearDepth(1.0f);
    glOrtho(0, 1, 0, 1, 1, 10);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
}

/* initialize GLUT settings, register callbacks, enter main loop */
int main(int argc, char** argv) 
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(800, 800);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Perspective's GLUT Template");
    glutKeyboardFunc(keyboardDown);
    glutKeyboardUpFunc(keyboardUp);
    glutSpecialFunc(keyboardSpecialDown);
    glutSpecialUpFunc(keyboardSpecialUp);
    glutReshapeFunc(reshape);
    glutDisplayFunc(draw);
    glutIgnoreKeyRepeat(true); // ignore keys held down
    initGL(800, 600);
    glutMainLoop();
    return 0;
}

当我启动程序时,只有当我按下鼠标时它才会写“e”。我找不到问题。为什么只有按住鼠标才有效?

4

2 回答 2

0

您的代码中没有鼠标功能,这就是它不起作用的原因。从这里插入:http ://www.zeuscmd.com/tutorials/glut/03-MouseInput.php

#include <iostream>
#include <gl/glut.h>

using namespace std;

bool lbuttonDown = false;

bool init()
{
    return true;
}

void display()
{

}

void mouse(int button, int state, int x, int y)
{
    if (button == GLUT_RIGHT_BUTTON)
    {
        if (state == GLUT_DOWN)
            cout << "Right button pressed"
            << endl;
        else
            cout << "Right button lifted "
            << "at (" << x << "," << y
            << ")" << endl;
    }
    else if (button == GLUT_LEFT_BUTTON)
    {
        if (state == GLUT_DOWN)
            lbuttonDown = true;
        else
            lbuttonDown = false;
    }
}

void motion(int x, int y)
{
    if (lbuttonDown)
        cout << "Mouse dragged with left button at "
        << "(" << x << "," << y << ")" << endl;
}

void motionPassive(int x, int y)
{
    cout << "Mouse moved at "
        << "(" << x << "," << y << ")" << endl;
}

void entry(int state)
{
    if (state == GLUT_ENTERED)
        cout << "Mouse Entered" << endl;
    else
        cout << "Mouse Left" << endl;
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);

    glutInitWindowPosition(200, 200);
    glutInitWindowSize(200, 200);

    glutCreateWindow("03 - Mouse Input");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutPassiveMotionFunc(motionPassive);
    glutEntryFunc(entry);

    if (!init())
        return 1;

    glutMainLoop();

    return 0;
}
于 2014-05-18T06:56:13.763 回答
0

我遇到了同样的问题 [使用 freeglut] 并在键回调函数中的 glutPostRedisplay() 强制它进行重绘之后立即调用 'draw()' [或您使用的任何显示回调]。我实际上不知道为什么它会这样。在通过键盘输入更改数据并返回键回调后,需要重绘但不会执行。

于 2015-09-09T01:36:29.683 回答