如何在不缩放其内容的情况下调整opengl视口大小(How do you adjust an opengl viewport size without scaling its contents)

当我的窗口调整大小时,我不希望内容扩展,只是为了增加视图端口大小。 我在stackoverflow(http://stackoverflow.com/questions/5894866/resize-viewport-crop-scene)上搜索时发现了这一点,这与我的问题几乎相同。 但是我很困惑如何设置缩放到哪里,我用1.0f试了但是然后什么都没有显示:s

这是调整功能代码,目前正在进行缩放:

void GLRenderer::resize() { RECT rect; int width, height; GLfloat aspect; GetClientRect(hWnd, &rect); width = rect.right; height = rect.bottom; if (height == 0) { height = 1; } aspect = (GLfloat) width / height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, aspect, 0.1, 100.0); glMatrixMode(GL_MODELVIEW); }

我的功能是渲染一个简单的三角形:

void GLRenderer::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslated(0, 0, -20); glBegin(GL_TRIANGLES); glColor3d(1, 0, 0); glVertex3d(0, 1, 0); glVertex3d(1, -1, 0); glVertex3d(-1, -1, 0); glEnd(); SwapBuffers(hDC); }

When my window is resized, i don't want the contents to scale but just to increase the view port size. I found this while searching on stackoverflow (http://stackoverflow.com/questions/5894866/resize-viewport-crop-scene) which is pretty much the same as my problem. However I'm confused as to what to set the Zoom to and where, i tried it with 1.0f but then nothing was shown at all :s

This is resize function code at the moment which does scaling:

void GLRenderer::resize() { RECT rect; int width, height; GLfloat aspect; GetClientRect(hWnd, &rect); width = rect.right; height = rect.bottom; if (height == 0) { height = 1; } aspect = (GLfloat) width / height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, aspect, 0.1, 100.0); glMatrixMode(GL_MODELVIEW); }

And my function to render a simple triangle:

void GLRenderer::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslated(0, 0, -20); glBegin(GL_TRIANGLES); glColor3d(1, 0, 0); glVertex3d(0, 1, 0); glVertex3d(1, -1, 0); glVertex3d(-1, -1, 0); glEnd(); SwapBuffers(hDC); }

最满意答案

您可以使用“视野”参数将缩放y(高度)更改为gluPerspective。 代码中45度的那个。 由于它目前始终为45度,因此您将始终获得相同的视角(以y为单位)。 如何根据窗口的高度更改此值并不明显。 对于较大的值(180度及以上),线性关系将失败。 我会尝试使用arctan(height / k),其中'k'就像500。

另请注意,当您在x中扩展窗口时,您将获得所需内容(源代码当前的方式)。 也就是说,您可以获得更广阔的视野。 这是因为您根据x和y之间的比率将方面(第二个参数)更改为值。

高度和宽度以像素为单位测量,因此值1不好。

请注意,您使用的是旧版OpenGL。 有关更多信息,请参阅旧版OpenGL 。

You can change the zoom in y (height) with the "field of view" parameter to gluPerspective. The one that is 45 degrees in your code. As it is currently always 45 degrees, you will always get the same view angle (in y). How to change this value as a function of the height of the window is not obvious. A linear relation would fail for big values (180 degrees and up). I would try to use arctan(height/k), where 'k' is something like 500.

Notice also that when you extend the window in x, you will already get what you want (the way your source code currently is). That is, you get a wider field of view. That is because you change the aspect (second argument) to a value depending on the ratio between x and y.

Height and Width is measured in pixels, so a value of 1 is not good.

Notice that you are using deprecated legacy OpenGL. See Legacy OpenGL for more information.

如何在不缩放其内容的情况下调整opengl视口大小(How do you adjust an opengl viewport size without scaling its contents)

当我的窗口调整大小时,我不希望内容扩展,只是为了增加视图端口大小。 我在stackoverflow(http://stackoverflow.com/questions/5894866/resize-viewport-crop-scene)上搜索时发现了这一点,这与我的问题几乎相同。 但是我很困惑如何设置缩放到哪里,我用1.0f试了但是然后什么都没有显示:s

这是调整功能代码,目前正在进行缩放:

void GLRenderer::resize() { RECT rect; int width, height; GLfloat aspect; GetClientRect(hWnd, &rect); width = rect.right; height = rect.bottom; if (height == 0) { height = 1; } aspect = (GLfloat) width / height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, aspect, 0.1, 100.0); glMatrixMode(GL_MODELVIEW); }

我的功能是渲染一个简单的三角形:

void GLRenderer::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslated(0, 0, -20); glBegin(GL_TRIANGLES); glColor3d(1, 0, 0); glVertex3d(0, 1, 0); glVertex3d(1, -1, 0); glVertex3d(-1, -1, 0); glEnd(); SwapBuffers(hDC); }

When my window is resized, i don't want the contents to scale but just to increase the view port size. I found this while searching on stackoverflow (http://stackoverflow.com/questions/5894866/resize-viewport-crop-scene) which is pretty much the same as my problem. However I'm confused as to what to set the Zoom to and where, i tried it with 1.0f but then nothing was shown at all :s

This is resize function code at the moment which does scaling:

void GLRenderer::resize() { RECT rect; int width, height; GLfloat aspect; GetClientRect(hWnd, &rect); width = rect.right; height = rect.bottom; if (height == 0) { height = 1; } aspect = (GLfloat) width / height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, aspect, 0.1, 100.0); glMatrixMode(GL_MODELVIEW); }

And my function to render a simple triangle:

void GLRenderer::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslated(0, 0, -20); glBegin(GL_TRIANGLES); glColor3d(1, 0, 0); glVertex3d(0, 1, 0); glVertex3d(1, -1, 0); glVertex3d(-1, -1, 0); glEnd(); SwapBuffers(hDC); }

最满意答案

您可以使用“视野”参数将缩放y(高度)更改为gluPerspective。 代码中45度的那个。 由于它目前始终为45度,因此您将始终获得相同的视角(以y为单位)。 如何根据窗口的高度更改此值并不明显。 对于较大的值(180度及以上),线性关系将失败。 我会尝试使用arctan(height / k),其中'k'就像500。

另请注意,当您在x中扩展窗口时,您将获得所需内容(源代码当前的方式)。 也就是说,您可以获得更广阔的视野。 这是因为您根据x和y之间的比率将方面(第二个参数)更改为值。

高度和宽度以像素为单位测量,因此值1不好。

请注意,您使用的是旧版OpenGL。 有关更多信息,请参阅旧版OpenGL 。

You can change the zoom in y (height) with the "field of view" parameter to gluPerspective. The one that is 45 degrees in your code. As it is currently always 45 degrees, you will always get the same view angle (in y). How to change this value as a function of the height of the window is not obvious. A linear relation would fail for big values (180 degrees and up). I would try to use arctan(height/k), where 'k' is something like 500.

Notice also that when you extend the window in x, you will already get what you want (the way your source code currently is). That is, you get a wider field of view. That is because you change the aspect (second argument) to a value depending on the ratio between x and y.

Height and Width is measured in pixels, so a value of 1 is not good.

Notice that you are using deprecated legacy OpenGL. See Legacy OpenGL for more information.