在opengl中,如何将鼠标跟随更改为鼠标单击,拖动并在释放时移动? [英] in opengl, how to change mouse follow to mouse click, drag and move on release?

查看:144
本文介绍了在opengl中,如何将鼠标跟随更改为鼠标单击,拖动并在释放时移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,其中对象(三角形)跟随鼠标移动,并沿其移动方向旋转.我要怎么做才能使对象保持静止,直到单击它,将其拖动到一个位置,然后放开鼠标,它就会开始移动到该位置?

I have program in which the object (a triangle) follows the mouse as it moves around and rotates to the direction its moving. What do I have to do to make it so the object stays still, until I click it, drag it to a position and once I release the mouse, it starts to move to that position?

#include <GL/glut.h>
#include <math.h>
# define ANIMATION_STEP (1000/300)
# define PI 3.1415926535897932

struct Globals {
    centre_x, centre_y, rotate;
    float length;
    float mouse_x, mouse_y, speed;
    int animating;
} globals;

void init(void){
    // Starting position of the triangle
    globals.centre_x = 100;
    globals.centre_y = 100;
    globals.rotate = 0.0;
    globals.mouse_x = 300.0;
    globals.mouse_y = 300.0;
    // Animation speed in pixels per second
    globals.speed = 300.0;
    // size of the triangle
    globals.length = 27;
    globals.animating = 1;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); 
    gluOrtho2D(0.0, 1000.0, 0.0, 700.0);
}

void triangle(void){
    glBegin(GL_POLYGON);
    glVertex2f(0.5, 0.0);
    glVertex2f(-0.5, -0.5);
    glVertex2f(-0.5, 0.5);
    glEnd();
}

void display(void){
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(globals.mouse_x, globals.mouse_y, 0.0);
    glRotatef(globals.rotate, 0.0, 0.0, 1.0);
    glScalef(globals.length, globals.length, 1.0);
    triangle();
    glFlush();
    glutSwapBuffers();
}

float limit(float x, float min, float max){
    if (x < min) {
        x = min;
    }
    if (x > max) {
        x = max;
    }
    return x;
    }

void timer(int v){
    // Computing elapsed time for smooth animation.
    int time = glutGet(GLUT_ELAPSED_TIME);
    float angle;
    glutTimerFunc(ANIMATION_STEP, timer, time);
    if (globals.animating) {
        int delta_t = time - v;
        float delta_x, delta_y, length, step_size;
        // Compute vector from current location to mouse
        delta_x = globals.mouse_x - globals.centre_x;
        delta_y = globals.mouse_y - globals.centre_y;
        // Compute length of the vector
        length = sqrt (delta_x*delta_x + delta_y*delta_y);
        // If the triangle is close to the mouse, then no motion is required.
        step_size = globals.speed * delta_t / 1000.0;
            if (length > step_size * 0.55) {
            delta_x = delta_x / length;
            delta_y = delta_y / length;
            globals.centre_x += delta_x * step_size;
            globals.centre_y += delta_y * step_size;
            angle = atan2(delta_y, delta_x);
            globals.rotate = angle * 180.0 / PI;
            // Keep the triangle inside the world window.
            globals.centre_x = limit(globals.centre_x, 0.0 + globals.length/2, 1000.0 - globals.length/2);
            globals.centre_y = limit(globals.centre_y, 0.0 + globals.length/2, 700.0 - globals.length/2);
        }

        glutPostRedisplay();
    }
}

void mousemotion(int x, int yc){
    globals.mouse_x = x;
    globals.mouse_y = 700 - yc;
    glutPostRedisplay();
}

main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(1000, 700);
    glutInitWindowPosition(10, 10);
    glutDisplayFunc(display);
    glutTimerFunc(ANIMATION_STEP, timer, 0);
    glutPassiveMotionFunc(mousemotion);
    init();
    glutMainLoop();
    return 0;
}

我研究了processMouse(),如果state == GLUT_DOWN则记录当前位置和鼠标按下坐标,但最好的ive是它可以立即传送到鼠标单击.有人可以解释一下单击它,拖动,释放然后移到位置时需要做些什么吗?

I have investigated processMouse() where if state == GLUT_DOWN then it records the current position and mouse press cordinates, but the best ive been able to get is that it immediately teleports to the mouse click. Can someone please explain what I would need to do to click on it, drag, release, then move to position?

推荐答案

在opengl中,如何更改鼠标...

in opengl, how to change mouse...

你不知道.鼠标在OpenGL中不存在.也许您应该将问题更改为如何在Windows/GLUT/SDL/任何框架中处理鼠标事件?"其中有很多重复项.

You don't. Mouse does not exist in OpenGL. Maybe you should change the question to "How to process mouse events in windows/GLUT/SDL/whatever-framework?" for which there are dozens of duplicates.

这篇关于在opengl中,如何将鼠标跟随更改为鼠标单击,拖动并在释放时移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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