为什么 OpenGL 不在此代码中绘制多边形?
Why doesn't OpenGL draw a polygon in this code?
这是最简单的代码,但它不代表任何东西。这是不可能的。
一切似乎都是绝对正确的。但是,我只看到黑色背景。
它曾经一直有效,但现在不行了。
颜色正确,蓝色三角形应该可见。但是什么都没有。
法典:
#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
版本时,如 glColor3b
、 glColor3s
或 glColor3i
,则整数值的完整范围将映射到浮点范围 [-1.0, 1.0]。因此,对于glColor3i
范围 [−2.147.483.648, 2.147.483.647] 中的整数值映射到 [-1.0, 1.0](请参阅常见整数数据类型(。
glColor
的无符号版本(如 glColor3ub
、glColor3us
或 glColor3ui
(将整数值映射到范围 [0.0, 1.0]。 例如,glColor3ub
将参数从 [0, 255] 映射到 [0.0, 1.0]。
相关文章:
- 在 QT C++中绘制/操作多边形
- 如何绘制具有随机顶点数量的多边形并旋转它们
- 为什么 OpenGL 不在此代码中绘制多边形?
- 如何在QGraphicsScene上绘制填充多边形
- OpenGL和GLFW:不绘制多边形
- 即使 OpenGL、SDL 和 GLSL 都没有抛出错误,也没有绘制多边形
- 如何在SDL中绘制多边形
- 通过鼠标单击绘制多边形
- 绘制多边形网格时出现 Opengl 性能问题
- 如何在现代openGL中绘制带有三维点的多边形
- 在C++中使用多线程绘制多边形
- OpenGL 不会为不在命名空间中的对象绘制多边形
- c++opengGL:绘制多边形+图像处理内部像素
- 使用 GL_POLYGON 在 OpenGL 中绘制多边形时获得意外输出
- 使用OpenGL在Maya中绘制多边形导线
- 在OpenCV中绘制多边形
- QGraphicsPolygonItem绘制一个打开(非关闭)的多边形
- 在OpenGL中使用多边形绘制2D粗圆弧
- glDrawElements绘制多边形
- 绘制数以千计的多边形与OpenGL VBO