在程序中,扩大了形状的大小是成功的,但是改变颜色效果不好

Posted

技术标签:

【中文标题】在程序中,扩大了形状的大小是成功的,但是改变颜色效果不好【英文标题】:In the program, it was successful to expand the size of the shape, but changing the color is not working well 【发布时间】:2020-04-03 07:33:34 【问题描述】:

我正在编写一个程序来改变形状的大小和颜色。我已经到了可以改变当前形状的大小,但颜色没有改变的阶段。我想将颜色更改为 R、G、B 形式。谢谢你。请原谅我的英语不合语法。英语不是我的第一语言

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

constexpr auto RED = 1;
constexpr auto GREEN = 2;
constexpr auto BLUE = 3;
float red = 1.0, green = 1.0, blue = 1.0;

GLboolean IsSphere = true;
GLboolean IsSmall = true;

void MyDisplay() 
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.5, 0.0, 0.5);
    if ((IsSphere) && (IsSmall))
        glutWireSphere(0.2, 15, 15);
    else if ((IsSphere) && (!IsSmall))
        glutWireSphere(0.4, 15, 15);
    else if ((!IsSphere) && (IsSmall))
        glutWireTorus(0.1, 0.3, 40, 20);
    else glutWireTorus(0.2, 0.5, 40, 20);
    glFlush();


void MyMainMenu(int entryID) 
    if (entryID == 1)
        IsSphere = true;
    else if (entryID == 2)
        IsSphere = false;
    else if (entryID == 3)
        exit(0);
    glutPostRedisplay();


void MySubMenu(int entryID) 
    if (entryID == 1)
        IsSmall = true;
    else if (entryID == 2)
        IsSmall = false;
    glutPostRedisplay();


void MySubMenu2(int entryID)

    switch (entryID) 
    case RED: red = 1.0; green = 0.0; blue = 0.0; break;
    case GREEN: red = 0.0; green = 1.0; blue = 0.0; break;
    case BLUE: red = 0.0; green = 0.0; blue = 1.0; break;
    


int main(int argc, char** argv) 
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(300, 300);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("OpenGL Example Drawing");
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    GLint MySubMenuID = glutCreateMenu(MySubMenu);
    glutAddMenuEntry("Small One", 1);
    glutAddMenuEntry("Big One", 2);
    GLint MySubMenu2ID = glutCreateMenu(MySubMenu2);
    glutAddMenuEntry("RED", RED);
    glutAddMenuEntry("GREEN", GREEN);
    glutAddMenuEntry("BLUE", BLUE);
    GLint MyMainMenuID = glutCreateMenu(MyMainMenu);
    glutAddMenuEntry("Draw Sphere", 1);
    glutAddMenuEntry("Draw Torus", 2);
    glutAddSubMenu("Change Size", MySubMenuID);
    glutAddSubMenu("Change Color", MySubMenu2ID);
    glutAddMenuEntry("Exit", 3);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutDisplayFunc(MyDisplay);
    glutMainLoop();
    return 0;

【问题讨论】:

【参考方案1】:

MyDisplay 中使用定义颜色的变量(redgreenblue):

float red = 0.5f, green = 0.0f, blue = 0.5f; // <--- init 0.5, 0, 0.5

void MyDisplay() 
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(red, green, blue); // <----- red, green, blue

    // [...]

在菜单事件回调中添加glutPostRedisplay,当颜色改变时执行:

void MySubMenu2(int entryID)

    switch (entryID) 
        case RED: red = 1.0; green = 0.0; blue = 0.0; break;
        case GREEN: red = 0.0; green = 1.0; blue = 0.0; break;
        case BLUE: red = 0.0; green = 0.0; blue = 1.0; break;
    
    glutPostRedisplay(); // <---- this is missing

【讨论】:

以上是关于在程序中,扩大了形状的大小是成功的,但是改变颜色效果不好的主要内容,如果未能解决你的问题,请参考以下文章

Android使用selector切换enable状态遇到的问题:只改变了颜色但是没改变形状

AE 动效工作流技巧 —— 减少 Bodymovin 导出的 JSON 大小并提升性能

Android - 可绘制形状样式中的多种颜色和大小

如何在android中改变可绘制形状的颜色

如何在代码中动态的设置shape

如何在代码中动态的设置shape