在OpenGL中更改投影

编程入门 行业动态 更新时间:2024-10-07 18:28:22
本文介绍了在OpenGL中更改投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法调整GLUT窗口的大小.我想打印一个矩形,但是当我使用调整大小功能时,我似乎无法在窗口中绘制形状.我有以下代码,当我调整窗口大小时,我想随正方形的宽度而改变,但是如果改变高度,我不希望它的高度改变.

I'm having trouble with resizing a GLUT window. I want to print a rectangle but when i use the resize function i cant seem to draw and shape in the window. I have the following code and i want to change with width of the square when i resize the window but if the height is resized i dont want the height of it to change.

#include <GLUT/glut.h> #include <stdio.h> #include <stdlib.h> /* globals */ GLsizei wh = 500, ww = 500; /* initial window size */ GLfloat size = 3.0; /* half side length of square */ void myinit(void) { glClearColor(0.0,0.0,1.0,1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,1,0,1.0,-1.0,1.0); glViewport(0,0,ww,wh); } /* display callback -- required by GLUT 3.0 */ void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1.0,.5,.7); glBegin(GL_POLYGON); glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0); glEnd(); glFlush(); } void mouse(int button, int state, int x, int y) { if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,500,0.0,500,0.0,1.0); } } /*app quits if q is pressed*/ void keyboard(unsigned char key, int x, int y) { if(key == 'Q' | key == 'q') exit(0); } /* The main program -- note that there is no "flow of control" -- the program merely calls functions to handle interactions */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitWindowSize(ww,wh); glutInitWindowPosition(100,100); glutCreateWindow("Square"); myinit(); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); glutDisplayFunc(renderScene); //glutReshapeFunc(changeSize); glutMainLoop(); return 0; }

我做了一个测试changeSize函数,当我使用它时,它将显示一个空的蓝色窗口,里面没有矩形

i made a test changeSize function and when i used that it would just show an empty blue window without the rectangle in there

推荐答案

您的问题在于对正交矩阵使用固定值:

Your problem lies in the use of a fixed value for the orthographic matrix:

glOrtho(0.0,500,0.0,500,0.0,1.0);

这表示,无论我的窗口有多大,将0的顶点位置映射到窗口底部,而将500的顶点位置映射到窗口顶部.

What this says is, however big my window is, make vertex position of 0 map to the bottom of the window, and 500 map to the top of the window.

因此,如果您有一个500像素高的窗口,并绘制一个覆盖100个单位的正方形,它将映射到该窗口中的100像素.

So if you have a 500 pixel high window, and you draw a square that covers 100 units, this will map to 100 pixels in the window.

但是,如果将窗口拉伸到1000像素高,现在将有一个从0到500的区域映射到一个高度为1000像素的窗口.因此,您以前可以覆盖100个像素的正方形现在覆盖了200个像素.

However if you stretch the window to be 1000 pixels high, now you have a region from 0 to 500 mapping to a window of height 1000 pixels. So your same square that used to cover 100 pixels in height now covers 200 pixels in height.

如果希望在调整大小时始终保持相同大小,则需要更新正交矩阵以将更大的区域映射到新的更大的窗口.因此,如果更改正交矩阵以将区域0 to windowHeight映射到窗口,则100个单位的正方形将始终填充完全相同的高度.

If you want it to always be the same size when you resize, you need to update the orthographic matrix to map a larger area to the new larger window. So if you change your orthographic matrix to map region 0 to windowHeight to the window, a 100 unit square will always fill the exact same amount of height.

glOrtho( 0.0, //left 500, //right 0.0, //bottom height_in_pixels, //top 0.0, //near 1.0); //far

更多推荐

在OpenGL中更改投影

本文发布于:2023-05-28 00:47:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/308249.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:OpenGL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!