Opengl鼠标点击矩形

时间:2016-02-11 09:18:44

标签: c++ opengl

伙计我通过使用过剩鼠标功能在此程序中遇到问题。我正在做什么矩形在屏幕上绘制,在鼠标功能我将像素坐标转换为世界坐标宽度为600和高度500我想要的当用户点击给定的矩形颜色将是红色但我如何执行此任务如何用这些矩形确定鼠标点击。
代码:

#include <cstdlib>
#include<GL\freeglut.h>
#include <iostream>
using namespace std;

    float xWorldCoordinate = 0.0f;
    float yWorldCoordinate = 0.0f;
    float r = 0.0f;
    float g = 0.0f;
    float b = 0.0f;
    float r1 = 0.0f;
    float g1 = 0.0f;
    float b1 = 0.0f;
    bool isRed = false;

    void init(void) {

        glClearColor(31.0f / 255, 28.0f / 255, 44.0f / 255, 0.0f);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    }

    void MouseButton(int button, int action, int xPixel, int yPixel) {

        if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) {
            yPixel = 500 - yPixel;
            xWorldCoordinate = (xPixel / 600.0f) * 2;
            yWorldCoordinate = (yPixel / 400.0f) * 2;
            xWorldCoordinate = -1 + xWorldCoordinate;
            yWorldCoordinate = -1 + yWorldCoordinate;

            int index1 = 0;
            int index2 = 0;
            //Index1
            glReadPixels(xPixel, yPixel, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_INT, &index1);// Read pixel under mouse's cursor and read information from stencil buffer to index when it rectangle index should be equal to 1


            if (index1 == 1) {
                isRed = !isRed;
                if (isRed) {
                    r = 0.0f;

                }
                else {
                    r = 1.0f;
                }


                //Index 2
            //  glReadPixels(xPixel, yPixel, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_INT, &index2);
            }
        }

        glutPostRedisplay();
    }

    void Rectangles(void) {

        glStencilFunc(GL_ALWAYS, 1, -1); //set front and back function and reference value for stencil testing
        glBegin(GL_POLYGON);

        glColor3f(r, g, b);
        glVertex2f(-0.1f, 0.1f);
        glVertex2f(-0.1f, -0.1f);
        glVertex2f(0.1f, -0.1f);
        glVertex2f(0.1f, 0.1f);
        glEnd();

        //2nd Rectangle
        glStencilFunc(GL_ALWAYS, 2, -1);
        glBegin(GL_POLYGON);
        glColor3f(r1, g1, b1);
        glVertex2f(0.66f, 0.84f);
        glVertex2f(0.87f, 0.84f);
        glVertex2f(0.87f, 0.66f);
        glVertex2f(0.66f, 0.66f);
        glEnd();
    }
    void Display(void) {
        glClearStencil(0); // specifies the index used by glClear to clear the stencil buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        glEnable(GL_STENCIL_TEST);
        glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        Rectangles();
        glFlush();
    }

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

        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_STENCIL); //Bit mask to select a window with a stencil buffer.
        glutInitWindowSize(600, 500);
        glutInitWindowPosition(100, 100);
        glutCreateWindow("Mouse");
        init();
        glutDisplayFunc(&Display);
        glutMouseFunc(&MouseButton);
        glutMainLoop();

        return EXIT_SUCCESS;
    }

1 个答案:

答案 0 :(得分:0)

我想你可以使用GLUT_STENCIL,你可以通过索引指示你现场的所有对象

 #if defined(__APPLE__) || defined(MACOSX)
  #include <GLUT/glut.h>
#else
  #include <GL\freeglut.h>
#endif
#include <cstdlib>
#include <iostream>
using namespace std;

using namespace std;

    float xWorldCoordinate = 0.0f;
    float yWorldCoordinate = 0.0f;
    float r = 0.0f;
    float g = 0.0f;
    float b = 0.0f;
    float r1 = 0.0f;
    float g1 = 0.0f;
    float b1 = 0.0f;
    bool isRed = false;
    bool isBlue = false;
    void init(void) {

        glClearColor(31.0f / 255, 28.0f / 255, 44.0f / 255, 0.0f);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    }

    void MouseButton(int button, int action, int xPixel, int yPixel) {

        if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) {
            yPixel = glutGet(GLUT_WINDOW_HEIGHT) - yPixel;

            int * index = new int[1];
            //Index1
            glReadPixels(xPixel, yPixel, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_INT, index);// Read pixel under mouse's cursor and read information from stencil buffer to index when it rectangle index should be equal to 1


            if (index[0] == 1) {
                isRed = !isRed;
                if (isRed) {
                    r = 1.0f;

                }
                else {
                    r = 0.0f;
                }
                //Index 2
            //  glReadPixels(xPixel, yPixel, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_INT, &index2);
          }else if(index[0] == 2){
            isBlue = !isBlue;
            if (isBlue) {
                b1 = 1.0f;

            }
            else {
                b1 = 0.0f;
            }
          }
          delete index;
        }

        glutPostRedisplay();
    }

    void Rectangles(void) {

        glStencilFunc(GL_ALWAYS, 1, -1); //set front and back function and reference value for stencil testing
        glBegin(GL_POLYGON);

        glColor3f(r, g, b);
        glVertex2f(-0.1f, 0.1f);
        glVertex2f(-0.1f, -0.1f);
        glVertex2f(0.1f, -0.1f);
        glVertex2f(0.1f, 0.1f);
        glEnd();

        //2nd Rectangle
        glStencilFunc(GL_ALWAYS, 2, -1);
        glBegin(GL_POLYGON);
        glColor3f(r1, g1, b1);
        glVertex2f(0.66f, 0.84f);
        glVertex2f(0.87f, 0.84f);
        glVertex2f(0.87f, 0.66f);
        glVertex2f(0.66f, 0.66f);
        glEnd();
    }
    void Display(void) {
        glClearStencil(0); // specifies the index used by glClear to clear the stencil buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        glEnable(GL_STENCIL_TEST);
        glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        Rectangles();
        glFlush();
    }

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

        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_STENCIL); //Bit mask to select a window with a stencil buffer.
        glutInitWindowSize(600, 500);
        glutInitWindowPosition(100, 100);
        glutCreateWindow("Mouse");
        init();
        glutDisplayFunc(&Display);
        glutMouseFunc(&MouseButton);
        glutMainLoop();

        return EXIT_SUCCESS;
    }

它适用于Mac,但我认为也应该使用freeglut

相关问题