OpenGL- C ++,使用鼠标单击将形状移动到单击位置

魂灵

我需要帮助来了解如何缩放坐标。指令说,修改代码以将三角形移动到鼠标单击位置。有人可以解释这是怎么做的吗?

这是我正在使用的源代码:

#ifdef __APPLE__
#include <GLUT/glut.h> 
#else
#include <GL/glut.h> 
#endif

#include <stddef.h>
#include <iostream>

// values controlled by fast keys
float g_angle = 0.0f;
float g_xoffset = 0.0f;
float g_yoffset = 0.0f;
int x;
int y;

// increments
const float g_angle_step = 32.0f; // degrees
const float g_offset_step = 32.0f; // world coord units

// last cursor click
int g_cursor_x = 0;
int g_cursor_y = 0;

void draw_triangle()
{
    // in model cooridnates centred at (0,0)
    static float vertex[3][2] =
        {
            {-1.0f, -1.0f},
            {1.0f, -1.0f},
            {0.0f, 1.0f}
        };

    glBegin(GL_LINE_LOOP); 
        for (size_t i=0;i<3;i++)
            glVertex2fv(vertex[i]);
    glEnd();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(1.0f, 1.0f, 1.0f); 
    glLineWidth(2.0f);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
        glTranslatef(500.0f+g_xoffset, 500.0f+g_yoffset, 0.0f);
        glScalef(100.0f, 100.0f, 1.0f);
        glRotatef(g_angle, 0.0f, 0.0f, 1.0f); 
        draw_triangle();
    glPopMatrix(); // done with stack
    glutSwapBuffers(); 
}

// handles mouse click events
// button will say which button is presed, e.g. GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON
// state will say if the button is GLUT_UP or GLUT_DOWN
// x and y are the poitner position 
void mouse_click(int button, int state, int x, int y)
{
    if (button==GLUT_LEFT_BUTTON)
    {       
        std::cerr << "\t left mouse button pressed!" << std::endl;

        if (state==GLUT_UP)
        {
            std::cerr << "\t button released...click finished" << std::endl;

            g_cursor_x = x;
            g_cursor_y = y;

            std::cerr << "\t cursor at (" << g_cursor_x << ", " << 
                                             g_cursor_y << ")" << std::endl;
        }

    }
    else
    if (button==GLUT_RIGHT_BUTTON)
    {
        std::cerr << "\t right mouse button pressed!" << std::endl;
    }

// Here is my attempt:
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) 
    {
        float x_min = (-x+500)/512;
        float x_max = (x-500)/512;
        float y_min = (-y+500)/512;
        float y_max = (y-500)/512;
        gluOrtho2D(x_min, x_max, y_min, y_max);
        //glTranslatef(x/512, 1-y/512, 0.0f);
            std::cerr << x << ", " << y << std::endl;
    }
    glutPostRedisplay();
}

void mouse_motion(int x, int y)
{
    std::cerr << "\t mouse is at (" << x << ", " << y << ")" << std::endl;
    glutPostRedisplay();
}


// will get which key was pressed and x and y positions if required
void keyboard(unsigned char key, int, int)
{
    std::cerr << "\t you pressed the " << key << " key" << std::endl;

    switch (key)
    {
        case 'q': exit(1); // quit!

        // clockwise rotate
        case 'r': g_angle += g_angle_step; break;
    }

    glutPostRedisplay(); // force a redraw
}

// any special key pressed like arrow keys
void special(int key, int, int)
{
    // handle special keys
    switch (key)
    {
        case GLUT_KEY_LEFT: g_xoffset -= g_offset_step; break;
        case GLUT_KEY_RIGHT: g_xoffset += g_offset_step; break;
        case GLUT_KEY_UP: g_yoffset += g_offset_step; break;
        case GLUT_KEY_DOWN: g_yoffset -= g_offset_step; break;
    }

    glutPostRedisplay(); // force a redraw
}

void init()
{
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
    gluOrtho2D(0, 1000, 0, 1000);
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f); 

    g_cursor_x = g_cursor_y = 500; // middle of window
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA); 
    glutInitWindowSize(512, 512); 
    glutInitWindowPosition(50, 50); 
    glutCreateWindow("Mouse Test"); 
    glutDisplayFunc(display); 

    // handlers for keyboard input
    glutKeyboardFunc(keyboard); 
    glutSpecialFunc(special); 

    // mouse event handlers
    glutMouseFunc(mouse_click); 
    glutPassiveMotionFunc(mouse_motion); 

    init(); 
    glutMainLoop(); 

    return 0; 
}
瓦伦

这应该可以解决您的问题。将您的代码(您的尝试)替换为下面的代码。

 if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) 
 { 

    g_xoffset = x;
    g_yoffset = 1000-y;

   } glutPostRedisplay();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

OpenGL- C ++,使用鼠标单击将形状移动到单击位置

来自分类Dev

如何将2D角色(带有动画)移动到C#中的鼠标单击位置?

来自分类Dev

使用鼠标单击OPENGL绘制线条

来自分类Dev

使用鼠标单击与鼠标光标移动的回调时的OpenGL未处理的异常

来自分类Dev

如何在C ++中使用自定义形状的光标检测图像上的鼠标单击位置

来自分类Dev

如何使用 C#.net 将鼠标单击事件添加到图片框

来自分类Dev

使用C ++的OpenCV中的鼠标单击功能

来自分类Dev

SharpGL-使用选择和拾取来检测OpenGL元素上的鼠标单击

来自分类Dev

如何将统一预制件移动到鼠标单击位置?

来自分类Dev

如何将统一预制件移动到鼠标单击的位置?

来自分类Dev

使用OpenGL C ++绘制圆

来自分类Dev

使用OpenGL C ++绘制圆

来自分类Dev

在使用C ++的OpenGL中移动形状时按时间自动旋转形状

来自分类Dev

仅对OpenGL使用C(不使用C ++)?

来自分类Dev

仅对OpenGL使用C(不使用C ++)?

来自分类Dev

将鼠标单击发送到WebEngineView Qt C ++

来自分类Dev

将鼠标单击发送到WebEngineView Qt C ++

来自分类Dev

鼠标单击(mouseFunc)时OpenGL无法关闭-glutKeyboardFunc正常吗?

来自分类Dev

使用C ++在OpenGL中使用键盘移动3D形状

来自分类Dev

如何使用SDL在C中的鼠标单击上调用函数?

来自分类Dev

在Opengl中通过鼠标移动形状

来自分类Dev

在Opengl中通过鼠标移动形状

来自分类Dev

如何朝相机,OpenGL,C ++的方向移动

来自分类Dev

在C#Silverlight中从鼠标单击事件收集分数

来自分类Dev

C#WPF ListBox鼠标单击晚

来自分类Dev

C#WPF ListBox鼠标单击晚

来自分类Dev

C#复选框和鼠标单击事件

来自分类Dev

在C#中捕获鼠标单击事件

来自分类Dev

在Java中将JButton组件平稳地移动到鼠标单击位置

Related 相关文章

  1. 1

    OpenGL- C ++,使用鼠标单击将形状移动到单击位置

  2. 2

    如何将2D角色(带有动画)移动到C#中的鼠标单击位置?

  3. 3

    使用鼠标单击OPENGL绘制线条

  4. 4

    使用鼠标单击与鼠标光标移动的回调时的OpenGL未处理的异常

  5. 5

    如何在C ++中使用自定义形状的光标检测图像上的鼠标单击位置

  6. 6

    如何使用 C#.net 将鼠标单击事件添加到图片框

  7. 7

    使用C ++的OpenCV中的鼠标单击功能

  8. 8

    SharpGL-使用选择和拾取来检测OpenGL元素上的鼠标单击

  9. 9

    如何将统一预制件移动到鼠标单击位置?

  10. 10

    如何将统一预制件移动到鼠标单击的位置?

  11. 11

    使用OpenGL C ++绘制圆

  12. 12

    使用OpenGL C ++绘制圆

  13. 13

    在使用C ++的OpenGL中移动形状时按时间自动旋转形状

  14. 14

    仅对OpenGL使用C(不使用C ++)?

  15. 15

    仅对OpenGL使用C(不使用C ++)?

  16. 16

    将鼠标单击发送到WebEngineView Qt C ++

  17. 17

    将鼠标单击发送到WebEngineView Qt C ++

  18. 18

    鼠标单击(mouseFunc)时OpenGL无法关闭-glutKeyboardFunc正常吗?

  19. 19

    使用C ++在OpenGL中使用键盘移动3D形状

  20. 20

    如何使用SDL在C中的鼠标单击上调用函数?

  21. 21

    在Opengl中通过鼠标移动形状

  22. 22

    在Opengl中通过鼠标移动形状

  23. 23

    如何朝相机,OpenGL,C ++的方向移动

  24. 24

    在C#Silverlight中从鼠标单击事件收集分数

  25. 25

    C#WPF ListBox鼠标单击晚

  26. 26

    C#WPF ListBox鼠标单击晚

  27. 27

    C#复选框和鼠标单击事件

  28. 28

    在C#中捕获鼠标单击事件

  29. 29

    在Java中将JButton组件平稳地移动到鼠标单击位置

热门标签

归档