6

我想将用户的鼠标点击位置存储在两个变量上

float x,y;

我将 openGL 与C. 我已经有一个使用 glut 的鼠标功能,但是当我尝试打印 x 和 y 时,它给了我类似的值x = 134; y = 150,而我的屏幕正交在 0 和 1 之间。

我想得到确切的点来在那里画一个点。

4

2 回答 2

7

您需要注册一个鼠标回调函数,它具有以下签名:

void glutMouseFunc(void (*func)(int button, int state,
                                int x, int y));

这里有一个涵盖一些基础知识的教程

编辑:如果您希望将位置标准化(0.0 - 1.0)除以宽度和高度:

float x1 = x /(float) width;
float y1 = y /(float) height;
于 2012-11-15T14:16:45.597 回答
-1

这是我写的一个简单的程序,希望它能帮助像它帮助我的每个人。

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

#include "Vec2.h"

Vec2 pos(0.0, 0.0);
Vec2 go(1.0, 1.0);

float mouseX;
float mouseY;
float angle = 0.0f;

void changeSize(int w, int h) {

    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    if (h == 0)
        h = 1;

    float ratio =  w * 1.0 / h;

    // Use the Projection Matrix
    glMatrixMode(GL_PROJECTION);

    // Reset Matrix
    glLoadIdentity();

    // Set the viewport to be the entire window
    glViewport(0, 0, w, h);

    // Set the correct perspective.
    gluPerspective(45.0f, ratio, 0.1f, 100.0f);

    // Get Back to the Modelview
    glMatrixMode(GL_MODELVIEW);
}

void renderScene(void) {

    // Clear Color and Depth Buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Reset transformations
    glLoadIdentity();
    // Set the camera
    gluLookAt(  0.0f, 0.0f, 10.0f,
                0.0f, 0.0f,  0.0f,
                0.0f, 1.0f,  0.0f);



    glPushMatrix();
    glRotatef(angle, 0.0f, 0.0f, 1.0f);
    glTranslatef(0.0, 0.0, 0.0);
    glBegin(GL_LINE_LOOP);
        glVertex3f( 0.0f, 2.0f, 0.0f);
        glVertex3f( -1.0f, -1.0f, 0.0f);
        glVertex3f( 1.0f, -1.0f, 0.0f);
    glEnd();
    glPopMatrix();

    //angle+=0.01f;

    glutSwapBuffers();
}

void mouseMove(int x, int y) 
{
    mouseX = -1.0 + 2.0 * x / 320 ;
    mouseY = 1.0 - 2.0 * y / 320 ;

    angle = 90 + atan2(pos.y-mouseY, pos.x-mouseX) * 180 / 3.1415926;

    //std::cout << mouseX << ", " << mouseY << std::endl;
    //std::cout << x << ", " << y << std::endl;
    std::cout << angle << std::endl;
}

int main(int argc, char **argv) {

    // init GLUT and create window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Lighthouse3D- GLUT Tutorial");

    // register callbacks
    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);
    glutIdleFunc(renderScene);

    glutMotionFunc(mouseMove);

    // enter GLUT event processing cycle
    glutMainLoop();

    return 1;
}
于 2014-12-13T19:03:37.373 回答