为什么 OpenGL 不在此代码中绘制多边形?

Why doesn't OpenGL draw a polygon in this code?

本文关键字:绘制 多边形 代码 OpenGL 为什么      更新时间:2023-10-16

这是最简单的代码,但它不代表任何东西。这是不可能的。
一切似乎都是绝对正确的。但是,我只看到黑色背景。
它曾经一直有效,但现在不行了。
颜色正确,蓝色三角形应该可见。但是什么都没有。

法典:

#include <iostream>
#include <chrono>
#include <GL/glut.h>
using namespace std;
constexpr auto FPS_RATE = 60;
int windowHeight = 600, windowWidth = 600;
void init();
void displayFunction();
void idleFunction();
double getTime();
double getTime()
{
    using Duration = std::chrono::duration<double>;
    return std::chrono::duration_cast<Duration>(
        std::chrono::high_resolution_clock::now().time_since_epoch()
        ).count();
}
const double frame_delay = 1.0 / FPS_RATE;
double last_render = 0;
void init()
{
    glutDisplayFunc(displayFunction);
    glutIdleFunc(idleFunction);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-windowWidth / 2, windowWidth / 2, -windowHeight / 2, windowHeight / 2);
}
void idleFunction()
{
    const double current_time = getTime();
    if ((current_time - last_render) > frame_delay)
    {
        last_render = current_time;
        glutPostRedisplay();
    }
}
void displayFunction()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
    glColor3i(0, 0, 1);
    glVertex2i(-50, 0);
    glVertex2i(50, 0);
    glVertex2i(0, 50);
    glVertex2i(100, 50);
    glEnd();
    glutSwapBuffers();
}
int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(windowWidth, windowHeight);
    glutInitWindowPosition((GetSystemMetrics(SM_CXSCREEN) - windowWidth) / 2, (GetSystemMetrics(SM_CYSCREEN) - windowHeight) / 2);
    glutCreateWindow("Window");
    init();
    glutMainLoop();
    return 0;
}

问题是glColor3i .

何时使用

glColor3f(0, 0, 1.0f);

然后你会看到一个完整的蓝色多边形。但是当你想使用 glColor3i ,那么颜色必须设置为

glColor3i(0, 0, 2147483647); // 2147483647 == 0x7fffffff

以获取具有相同蓝色的多边形。

当您使用带有整数有符号参数的 glColor 版本时,如 glColor3bglColor3sglColor3i ,则整数值的完整范围将映射到浮点范围 [-1.0, 1.0]。因此,对于glColor3i范围 [−2.147.483.648, 2.147.483.647] 中的整数值映射到 [-1.0, 1.0](请参阅常见整数数据类型(。

glColor的无符号版本(如 glColor3ubglColor3usglColor3ui(将整数值映射到范围 [0.0, 1.0]。 例如,glColor3ub将参数从 [0, 255] 映射到 [0.0, 1.0]。