c++ - 如何在蓝色中没有所有感觉颜色的情况下将 QUADS 涂成蓝色

标签 c++ opengl glut

你好,我在 C++ 中使用 glut 和 opengl,我有家,我想在其中绘制蓝色 QUADS 我的问题是,当我将 QUADS 绘制为蓝色时,我的所有感觉颜色都是蓝色的,所以我如何才能仅将 QUADS 着色为蓝色和防止用蓝色着色所有感觉我做错了什么如何从所有感觉中删除蓝色并只为我的 QUAD 着色? 我的尝试:

void drawSquare1()
{
    glBegin(GL_QUADS);
    glColor3d(1,0,0);
    glVertex3f(-0.5,-0.5,-0.5);
    glColor3d(1,1,0);
    glVertex3f(0.5,-0.5,-0.5);
    glColor3d(1,1,1);
    glVertex3f(0.5,0.5,-0.5);
    glColor3d(0,1,1);
    glVertex3f(-0.5,0.5,-0.5);
    glEnd();
}

void render(void)                                                   // Our Rendering Is Done Here
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);             // Clear The Screen And The Depth Buffer
    glLoadIdentity();                                               // Reset The View

    GLfloat xtrans = -g_xpos;
    GLfloat ztrans = -g_zpos;
    GLfloat ytrans = -g_ypos;
    if(g_yrot > 360)
        g_yrot -= 360;
    else if(g_yrot < 0)
        g_yrot += 360;
    GLfloat sceneroty = (360.0f - g_yrot);


    int numpolygons;

    glRotatef(g_lookupdown,1.0f,0,0);
    glRotatef(sceneroty,0,1.0f,0);

    glTranslatef(xtrans, ytrans, ztrans);

    numpolygons = g_sector1.numpolygons;

    for (int loop_m = 0; loop_m < numpolygons; loop_m++)
        texture_object(loop_m); 

    gluQuadricDrawStyle(my_shape[0],GLU_FILL);
    glBindTexture(GL_TEXTURE_2D, textures[1].texID);
    glScalef(0.1,0.1,0.1);
    glTranslatef(0.78,14.3,-4.2);
    gluSphere(my_shape[0], 1.0,50,50);

    gluQuadricDrawStyle(my_shape[1],GLU_FILL);
    glBindTexture(GL_TEXTURE_2D, textures[8].texID);
    glTranslatef(-20,0,0);
    gluSphere(my_shape[1], 1.0,50,50);

    gluQuadricDrawStyle(my_shape[2],GLU_FILL);
    glBindTexture(GL_TEXTURE_2D, textures[22].texID);
    glTranslatef(40,0,0);
    gluSphere(my_shape[2], 1.0,50,50);

    glMatrixMode(GL_PROJECTION);                                    // Select The Projection Matrix
    glPushMatrix();                                                 // Store The Projection Matrix
    glLoadIdentity();                                               // Reset The Projection Matrix

    glOrtho(-10,window_width,0,window_height,-10,10);                   // Set Up An Ortho Screen
    glMatrixMode(GL_MODELVIEW);                                     // Select The Modelview Matrix
drawSquare1();
glMatrixMode(GL_PROJECTION);                                    // Select The Projection Matrix
    glPopMatrix();                                                  // Restore The Old Projection Matrix
    //glPushMatrix();
    drawSquare1();
    //glPopMatrix();
    glMatrixMode(GL_MODELVIEW);                                     // Select The Modelview Matrix
    glutSwapBuffers ( );
}

int main(int argc, char** argv)                                 // Main Function For Bringing It All Together.
{
    //cout << "Hello World!" << endl;
    //cin.get();
    glutInit(&argc, argv);                                      // GLUT Initializtion
    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);  // (CHANGED)
    if (g_gamemode) 
    {
        glutGameModeString("640x480:16");                       // Select The 640x480 In 16bpp Mode
        if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE))
            glutEnterGameMode();                                // Enter Full Screen
        else g_gamemode = false;                                // Cannot Enter Game Mode, Switch To Windowed
    }
    screen_width = glutGet(GLUT_SCREEN_WIDTH);
    screen_height = glutGet(GLUT_SCREEN_HEIGHT);
    window_width = screen_width/1.4;
    window_height = screen_height/1.4;

    if (!g_gamemode) 
    {
        glutInitWindowSize(window_width,window_height);           // Window Size If We Start In Windowed Mode
        glutInitWindowPosition((screen_width-window_width)/2,(screen_height-window_height)/2);
        glutCreateWindow("Frank's 3-D House");                  // Window Title 
    }

    init();


    glutIgnoreKeyRepeat(true);                                  // Disable Auto Repeat (NEW)
    //  glutKeyboardFunc(myKey); // register the key handler.

    glutDisplayFunc(render);                                    // Register The Display Function
    glutReshapeFunc(reshape);                                   // Register The Reshape Handler
    glutKeyboardFunc(keyboard);                                 // Register The Keyboard Handler
    //glRasterPos2f(lineMargin, currentHight); // set the cursor to the initial position.
    glutSpecialFunc(special_keys);                              // Register Special Keys Handler
    glutSpecialUpFunc(special_keys_up);                         // Called When A Special Key Released (NEW)
    glutIdleFunc(game_function);                                // Process User Input And Does Rendering (CHANGED)
    glutMouseFunc(mouse) ;
    glutMainLoop();                                             // Go To GLUT Main Loop
    return 0;
}

还有这张运行我的代码的图片: the output when i run the code

最佳答案

默认情况下纹理环境模式(GL_TEXTURE_ENV_MODE)是GL_MODULATE。参见 glTexEnv .

这意味着如果启用纹理(glEnable(GL_TEXTURE_2D)),那么纹理的颜色将乘以当前由glColor设置的颜色。 .

要解决您的问题,我建议设置 glColor4f(1.0f,1.0f,1.0f,1.0f); 绘制几何图形之前:

void render(void) 
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    .....

    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    for (int loop_m = 0; loop_m < numpolygons; loop_m++)
        texture_object(loop_m); 

    .....                                
}

请注意,当前颜色在函数 drawSquare1 中更改并保持其状态。

关于c++ - 如何在蓝色中没有所有感觉颜色的情况下将 QUADS 涂成蓝色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48972995/

相关文章:

c++ - ## 宏参数连接没有像我预期的那样工作

c++ - 如何使用共享指针作为函数参数

c++ - OpenGL:旋转 90 度后圆柱体变形

python - 来自 PyOpengl 缓冲区的 PIL Image.fromstring 大小错误

c++ - 使用 glutPassiveMotionFunc();在过剩

c++ - 尝试制作我的库的 DLL 并获取未解析的外部符号 __tmainCRTStartup

c++ - opengl 显示白框而不是纹理

java - OpenGL glfwInit()自动执行?

c++ - OpenGL 4.5 直接状态访问渲染一个三角形 - GL_INVALID_VALUE

c++ - 编写跨平台程序