c++ - glColor不会改变opengl中其他三角形的颜色

标签 c++ opengl

所以我还是opengl的新手,我用这个教程作为引用

https://www.youtube.com/watch?v=8p76pJsUP44

我现在正在尝试绘制一组由 4 个三角形组成的正方形(这是我的项目任务),这是我的代码

#include <Windows.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>

using namespace std;

void changeViewPort(int w, int h)
{
    glViewport(0, 0, w, h);
}


void draw() {
    glBegin(GL_TRIANGLES);

        glColor3f(0.0, 0.0, 255.0);
        glVertex3f(0.0, 0.0, 0.0); // top triangle
        glVertex3f(-1.0,1.0, 0.0);
        glVertex3f(1.0, 1.0, 0.0);

        glColor3f(50.0, 50.0, 255.0);
        glVertex3f(0.0, 0.0, 0.0); // right triangle
        glVertex3f(1.0, 1.0, 0.0);
        glVertex3f(1.0, -1.0, 0.0);

        glColor3f(75.0, 75.0, 255.0);
        glVertex3f(0.0, 0.0, 0.0);//bottom triangle
        glVertex3f(1.0, -1.0, 0.0);
        glVertex3f(-1.0, -1.0, 0.0);

        glColor3f(50.0, 50.0, 255.0);
        glVertex3f(0.0, 0.0, 0.0); //left triangle
        glVertex3f(-1.0, -1.0, 0.0);
        glVertex3f(-1.0, 1.0, 0.0);
        glEnd();
}

void render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    draw();
    glutSwapBuffers();
}




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

    // Initialize GLUT
    glutInit(&argc, argv);
    // Set up some memory buffers for our display
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    // Set the window size
    glutInitWindowSize(350, 300);
    // Create the window with the title "Hello,GL"
    glutCreateWindow("Hello, GL");
    // Bind the two functions (above) to respond when necessary
    glutReshapeFunc(changeViewPort);
    glutDisplayFunc(render);
    //glutDisplayFunc(draw);

    // Very important!  This initializes the entry points in the OpenGL driver so we can 
    // call all the functions in the API.
    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "GLEW error");
        return 1;
    }


    glutMainLoop();
    return 0;
}

请注意,在我的绘制函数中,我只是试图创建 4 个具有不同蓝色阴影的不同三角形。 然而,每当执行此代码时,只有顶部三角形是蓝色的,而其余的则保持白色

有谁知道为什么这行不通?我什至尝试创建绘图函数的 for 循环版本,但它也没有用

void draw() {
    double x = 1.0;
    double y = 1.0;
    double coords[8] = { -x,y,x,y,x,-y,-x,-y };

    for (int i = 0; i < 7; i=i+2) { // i = [0,2,4,6]
        switch (i) {
        case 0:
            glColor3f(0, 0, 255);
            break;
        case 2:
            glColor3f(50, 50, 255);
            break;
        case 4:
            glColor3f(75, 75, 255);
            break;
        case 6:
            glColor3f(50, 50, 255);
            break;
        }

        if (i == 6) { //4th and 1st coordinate
            glBegin(GL_TRIANGLES);
            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(coords[i], coords[i + 1], 0.0);
            glVertex3f(coords[0], coords[1], 0.0);
            glEnd();
        }
        else {
            glBegin(GL_TRIANGLES);
            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(coords[i], coords[i + 1], 0.0);
            glVertex3f(coords[i + 2], coords[i + 3], 0.0);
            glEnd();
        }

    }


}

最佳答案

glColor3f 将 [0, 1] 范围内的颜色分量作为输入,而不是当前使用的 [0, 255]。

关于c++ - glColor不会改变opengl中其他三角形的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40710555/

相关文章:

c++ - “lock cmpxchg”如何在汇编中工作?

c++ - C/C++ API设计困境

opengl - 为什么我应该使用常量顶点属性而不是制服?

opengl - gluPerspective、glViewport、gluLookAt 以及 GL_PROJECTION 和 GL_MODELVIEW 矩阵

c++ - 为 VBO 创建动态对象数组

c++ - 为 Windows 内置拼写检查提供程序添加拼写检查语言

c++ - 如何从字符串转换为 double *

c++ - 使 "Makefile"看起来更好

c - OpenGL glutWireCube 可以工作,但 glutWireCylinder 不能。我究竟做错了什么?

c++ - 在opengl中绘制图形