c++ - 添加鼠标事件来改变简单圆圈的颜色

标签 c++ opengl glut

关闭。这个问题需要details or clarity .它目前不接受答案。












想改进这个问题?通过 editing this post 添加详细信息并澄清问题.

2年前关闭。




Improve this question




我画了一个圆圈并尝试添加鼠标事件,这改变了圆圈的颜色我找不到好的来源

这是整个代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define window_width  1080  
#define window_height 720 
void drawFilledSun() {
    //static float angle;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0, 0, -10);
    int i, x, y;
    double radius = 0.30;
    //glColor3ub(253, 184, 19);     
    glColor3ub(255, 0, 0);
    double twicePi = 2.0 * 3.142;
    x = 0, y = 0;
    glBegin(GL_TRIANGLE_FAN); //BEGIN CIRCLE
    glVertex2f(x, y); // center of circle
    for (i = 0; i <= 20; i++) {
        glVertex2f(
            (x + (radius * cos(i * twicePi / 20))), (y + (radius * sin(i * twicePi / 20)))
        );
    }
    glEnd(); //END
}
void main_loop_function() {
    int c;
    drawFilledSun();
    glutSwapBuffers();
    c = getchar();
}
void GL_Setup(int width, int height) {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glEnable(GL_DEPTH_TEST);
    gluPerspective(45, (float)width / height, .1, 100);
    glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(window_width, window_height);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("GLUT Example!!!");
    glutIdleFunc(main_loop_function);
    GL_Setup(window_width, window_height);
    glutMainLoop();
}

最佳答案

为太阳的红色绿色和蓝色分量添加一个数组和一个颜色索引:

int color_i = 0;
GLubyte color[][3] = { {255, 0, 0 }, {253, 184, 19} };

使用颜色数组设置颜色属性

void drawFilledSun() {
     // [...]

     glColor3ubv(color[color_i]);

摆脱getcharmain_loop_function ,它会阻止窗口响应。但请调用 glutPostRedisplay 持续更新窗口:

void main_loop_function() {
    drawFilledSun();
    glutSwapBuffers();
    glutPostRedisplay();
}

使用 glutDisplayFunc 而不是 glutIdleFunc 并添加 glutMouseFunc 打回来:

int main(int argc, char** argv) {
    // [...]

    glutDisplayFunc(main_loop_function);
    glutMouseFunc( mouseFunc );


在鼠标回调中更改颜色索引:

void mouseFunc(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        color_i = (color_i == 0) ? 1 : 0;
    }
}

请参阅示例:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define window_width  1080  
#define window_height 720

int color_i = 0;
GLubyte color[][3] = { {255, 0, 0 }, {253, 184, 19} };

void drawFilledSun() {
    //static float angle;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0, 0, -10);
    int i, x, y;
    double radius = 0.30;
    glColor3ubv(color[color_i]);
    double twicePi = 2.0 * 3.142;
    x = 0, y = 0;
    glBegin(GL_TRIANGLE_FAN); //BEGIN CIRCLE
    glVertex2f(x, y); // center of circle
    for (i = 0; i <= 20; i++) {
        glVertex2f(
            (x + (radius * cos(i * twicePi / 20))), (y + (radius * sin(i * twicePi / 20)))
        );
    }
    glEnd(); //END
}

void main_loop_function() {
    drawFilledSun();
    glutSwapBuffers();
    glutPostRedisplay();
}

void GL_Setup(int width, int height) {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glEnable(GL_DEPTH_TEST);
    gluPerspective(45, (float)width / height, .1, 100);
    glMatrixMode(GL_MODELVIEW);
}

void mouseFunc(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        color_i = (color_i == 0) ? 1 : 0;
    }
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(window_width, window_height);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("GLUT Example!!!");
    glutDisplayFunc(main_loop_function);
    glutMouseFunc( mouseFunc );
    GL_Setup(window_width, window_height);
    glutMainLoop();
}

关于c++ - 添加鼠标事件来改变简单圆圈的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61491993/

相关文章:

c++ - 为什么 CMakeLists 文件需要位于 CLion 项目文件夹的根目录中?

c++ - OpenGL 中一致的手绘线

c++ - glGenTextures 段错误?

opengl - 如何呈现为无符号整数格式

java - 正交投影 - 使物体适合屏幕?

c++ - 用鼠标在 OpenGL GLUT 中绘制多边形

c++ - 你如何在 osx 上使用 waf 链接 opengl 和 glut?

c++ - 为 cout 做一个包装器?

c++ - 二维矩阵 STATUS_ACCESS_VIOLATION 错误

c++ - 派生类私有(private)方法被调用