c++ - OpenGL - 调用不同颜色的方形函数

标签 c++ opengl visual-studio-2015

我制作了一个 drawSquare() 函数,可以在我的屏幕上绘制几个 2D 正方形:

void drawSquare(GLfloat length, GLfloat x, GLfloat y, GLfloat outline){
    // x1,y1 is the top left-hand corner coordinate
    // and so on...
    GLfloat x1, y1, x2, y2, x3, y3, x4, y4;

    x1 = x - length / 2;
    y1 = y + length / 2;
    x2 = x + length / 2;
    y2 = y + length / 2;
    x3 = x + length / 2;
    y3 = y - length / 2;
    x4 = x - length / 2;
    y4 = y - length / 2;

    // ACTUAL SQUARE OBJECT
    glColor3f(0.0, 1.0, 1.0); // Colour: Cyan
    glBegin(GL_POLYGON);
    glVertex2f(x1, y1);     // vertex for BLUE SQUARES
    glVertex2f(x2, y2);
    glVertex2f(x3, y3);
    glVertex2f(x4, y4);
    glEnd()
}

当我点击一个正方形时,我需要改变它的颜色。我已经设置了一个鼠标功能,可以在我右键单击时显示鼠标位置:

void processMouse(int button, int state, int x, int y)
{
    if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)){
        // gets mouse location
        printf("Clicked on pixel %d - %d", x, y);
        // prints to console
    }
}

在上面的 if 语句 中有一个 if 语句,如下所示:

    if (x > 0 && x < 95 && y > 302 && y < 395) { 
                // there's a square at this location!
                // change the square colour!
    }

当我将 exit(0); 放在这个语句中时:

if (x > 0 && x < 95 && y > 302 && y < 395) { 
                exit(0);
}

我的程序正常退出,所以条件有效,我只是想知道我如何以某种方式用不同的颜色再次调用我的drawSquare()函数。。 p>

最初当我调用我的 drawSquare() 时,它在我的显示函数中是这样调用的:

void display(){
    glClear(GL_COLOR_BUFFER_BIT);   /* clear window */
    // there are some other primatives here too
    drawSquare(100,150, 750,true);  // square drawn
}

解决方案

这是我解决问题的方法。我制作了一个全局 bool 变量 boolean 变量 areaClicked = false; 检查用户是否点击过,我们默认设置为 false。

在我的鼠标函数中,我检查是否点击了一个正方形,如果是,则将 bool 值设置为 true:

if (x >= 0 && x <= 95 && y >= 302 && y <= 380) { // area of box
            areaClicked = true;
} 

现在在我的显示函数中,我们检查 bool 值是否已被触发,如果已触发,则显示我重新着色的方 block ,否则,什么都不做:

if (areaClicked != false) {
        drawRecolour(100, 50, 350, true);   // 4th square drawn
}

else areaClicked = false;

最佳答案

在您的事件处理程序中设置一个变量,然后触发重绘。

if (x > 0 && x < 95 && y > 302 && y < 395) { 
            // there's a square at this location!
            // change the square colour!
            square_color = ...;
            glutPostRedisplay();
}

在您的显示函数中检查变量并使用它来确定颜色:

// you should probably make the color a parameter of drawSquare
void drawSquare(GLfloat length, GLfloat x, GLfloat y, GLfloat outline){

    // OpenGL doesn't do "objects". It **DRAWS** things. Like pen on paper

    glColor3f(square_color); // <---- uses variable
    glBegin(GL_POLYGON);
...
    glEnd()
}

关于c++ - OpenGL - 调用不同颜色的方形函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40408075/

相关文章:

opengl - 在 OpenGL 中重复纹理

opengl - OpenGL 中的预乘 Alpha 和多重采样

c++ - VS2015支持魔法静态,那么为什么会出现这个警告呢?

c++ - Visual Studio 2015 中的其他包含目录不起作用

visual-studio-2013 - 锁定计算机或启动另一个实例时 Visual Studio 崩溃

c++ - 静态成员函数初始化静态成员变量 - 用法和风险?

c++ - 访问集合中的元素?

opengl - 用 glDrawElements 很难理解索引

c++ - 如何在 spidermonkey 嵌入中提供 js-ctypes?

c++ - 将 cv::Mat 转换为无符号整型像素