[英]OpenGL draws outside window bounds
在繪制 GL_LINE_STRIP 時,我注意到該圖在 window 邊界之外,我看不到圖的一半。
我想在 window 中居中並自動調整大小以適應它。 有人可以重定向我如何實現這一目標嗎?
這就是我所看到的:
我希望它是這樣的尺寸和 position:
這是我的代碼:
int main(int argc, char** argv)
{
// GUI Stuff
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(600, 600);
glutCreateWindow("Graph showing the waveform after hanning window");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void display()
{
glDisable(GL_DEPTH_TEST); glDisable(GL_BLEND);
glDisable(GL_LINE_SMOOTH); glLineWidth(2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-6, 6, -6, 6, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor4f(0.4, 1.0, 0.6, 1);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < N / 2; i++)
{
glVertex2f(frequencies[i], mag[i]);
}
glEnd();
glutSwapBuffers();
}
這是我的完整代碼
要解決問題,您需要知道曲線的界限。 您必須更改正交投影:
glOrtho(-6, 6, -6, 6, -1, 1);
在正交投影中,視圖空間坐標線性映射到裁剪空間坐標。 觀看量由 6 個距離(左、右、下、上、近、遠)定義。 left、right、bottom、top、near 和 far 的值定義了一個長方體(框)。
如果要使視圖在曲線上居中,則投影的中心必須是曲線的中心。 例如:
glOrtho(min_x-1, max_x+1, min_y-1, min_y+1, -1, 1);
( min_x
, min_y
) 是左下角, ( max_x
, max_y
) 是曲線周圍邊界矩形的右上角。
#include <vector>
#include <algorithm>
std::vector<float> x_corrds(frequencies, frequencies + N/2);
std::vector<float> y_corrds(mag, mag + N/2);
float min_x = std::min_element(x_corrds.begin(), x_corrds.end());
float max_x = std::max_element(x_corrds.begin(), x_corrds.end());
float min_y = std::min_element(y_corrds.begin(), y_corrds.end());
float max_y = std::max_element(y_corrds.begin(), y_corrds.end());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.