gpt4 book ai didi

c - 如何使用 OpenGL 将正方形居中?

转载 作者:行者123 更新时间:2023-11-30 16:45:07 25 4
gpt4 key购买 nike

我希望我的 2 个方 block 位于下方并位于窗口中央。

我有以下代码:

glBegin(GL_QUADS);
glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(-0.8f, 0.1f);
glVertex2f(-0.2f, 0.1f);
glVertex2f(-0.2f, 0.7f);
glVertex2f(-0.8f, 0.7f);

glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(-0.7f, -0.6f);
glVertex2f(-0.1f, -0.6f);
glVertex2f(-0.1f, 0.0f);
glVertex2f(-0.7f, 0.0f);
glEnd();

结果是:

result

但是,它并不完全在下面。

怎么做?

最佳答案

为了澄清问题,您应该首先考虑形状的位置和尺寸:

/* It seems that your windows lies on [-1,1][-1,1] square. */
/* dimension of squares: {width, height}
here rectangle since width is equal to height
The two rectangles have the same dimension.
*/
float dim[2] = {0.25f, 0.25f};

/* position of the rectangle centers, here you can see that the first is above
this array must be read like {{x1, y1}, {x2, y2}}
*/
float pos[2][2] = {{0.0f, 0.4f},{0.0f, -0.4f}};


glBegin(GL_QUADS);
glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(pos[0][0] - dim[0], pos[0][1] - dim[1]);
glVertex2f(pos[0][0] + dim[0], pos[0][1] - dim[1]);
glVertex2f(pos[0][0] + dim[0], pos[0][1] + dim[1]);
glVertex2f(pos[0][0] - dim[0], pos[0][1] + dim[1]);

glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(pos[1][0] - dim[0], pos[1][1] - dim[1]);
glVertex2f(pos[1][0] + dim[0], pos[1][1] - dim[1]);
glVertex2f(pos[1][0] + dim[0], pos[1][1] + dim[1]);
glVertex2f(pos[1][0] - dim[0], pos[1][1] + dim[1]);
glEnd();

关于c - 如何使用 OpenGL 将正方形居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44222723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com