对象在全屏中变得拉伸
Objects become stretched in full screen
我遇到的问题是当我使用全屏时我的对象被拉伸了。我希望它适合全屏坐标,因此它看起来与图像 A 完全相同。我知道 glViewport
确定 OpenGL 绘制到的 window 部分,它可以帮助设置我反对整个window。但是,我没有使用glViewport
,而是使用gluOrtho2D
。
Click here to see the full code
图像 A(屏幕尺寸:700、600)
图像 B(全屏尺寸)
gluOrtho2D代码
// this is the initialisation function, called once only
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0); // set what colour you want the background to be
glMatrixMode(GL_PROJECTION); // set the matrix mode
gluOrtho2D(0.0, winWidth, 0.0, winHeight); // set the projection window size in x and y.
}
我原来用的是gluOrtho2D
,用来设置二维正交视区
glViewport
定义渲染到的(默认)帧缓冲区区域。
如果你不想渲染到全屏,那么你可以缩小渲染到的区域,glViewport
。你可以在边框上放一些黑色条纹。
您的应用程序的宽高比是 winWidth
: winHeight
与glutGet
分别使用参数GLUT_WINDOW_WIDTH
GLUT_WINDOW_HEIGHT
可以得到当前window的大小并计算出当前的宽高比:
int currWidth = glutGet( GLUT_WINDOW_WIDTH );
int currHeight = glutGet( GLUT_WINDOW_HEIGHT );
float window_aspcet = (float)currWidth / (float)currHeight;
有了这些信息,视图可以完美地居中于视口:
void display() {
float app_aspcet = (float)winWidth / (float)winHeight;
int currWidth = glutGet( GLUT_WINDOW_WIDTH );
int currHeight = glutGet( GLUT_WINDOW_HEIGHT );
float window_aspcet = (float)currWidth / (float)currHeight;
if ( window_aspcet > app_aspcet )
{
int width = (int)((float)currWidth * app_aspcet / window_aspcet + 0.5f);
glViewport((currWidth - width) / 2, 0, width, currHeight);
}
else
{
int height = (int)((float)currHeight * window_aspcet / app_aspcet + 0.5f);
glViewport(0, (currHeight - height) / 2, currWidth, height);
}
// [...]
}
或者你可以掌握纵横比和正交投影的中心
void display() {
float app_aspcet = (float)winWidth / (float)winHeight;
int currWidth = glutGet( GLUT_WINDOW_WIDTH );
int currHeight = glutGet( GLUT_WINDOW_HEIGHT );
float window_aspcet = (float)currWidth / (float)currHeight;
glViewport(0, 0, currWidth, currHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( window_aspcet > app_aspcet )
{
float delta_width = (float)currWidth * (float)winHeight / (float)currHeight - (float)winWidth;
gluOrtho2D(-delta_width/2.0f, (float)winWidth + delta_width/2.0f, 0.0, (float)winHeight);
}
else
{
float delta_height = (float)currHeight * (float)winWidth / (float)currWidth - (float)winHeight;
gluOrtho2D(0.0, (float)winWidth, -delta_height/2.0f, (float)winHeight + delta_height/2.0f);
}
我遇到的问题是当我使用全屏时我的对象被拉伸了。我希望它适合全屏坐标,因此它看起来与图像 A 完全相同。我知道 glViewport
确定 OpenGL 绘制到的 window 部分,它可以帮助设置我反对整个window。但是,我没有使用glViewport
,而是使用gluOrtho2D
。
Click here to see the full code
图像 A(屏幕尺寸:700、600)
图像 B(全屏尺寸)
gluOrtho2D代码
// this is the initialisation function, called once only
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0); // set what colour you want the background to be
glMatrixMode(GL_PROJECTION); // set the matrix mode
gluOrtho2D(0.0, winWidth, 0.0, winHeight); // set the projection window size in x and y.
}
我原来用的是gluOrtho2D
,用来设置二维正交视区
glViewport
定义渲染到的(默认)帧缓冲区区域。
如果你不想渲染到全屏,那么你可以缩小渲染到的区域,glViewport
。你可以在边框上放一些黑色条纹。
您的应用程序的宽高比是 winWidth
: winHeight
与glutGet
分别使用参数GLUT_WINDOW_WIDTH
GLUT_WINDOW_HEIGHT
可以得到当前window的大小并计算出当前的宽高比:
int currWidth = glutGet( GLUT_WINDOW_WIDTH );
int currHeight = glutGet( GLUT_WINDOW_HEIGHT );
float window_aspcet = (float)currWidth / (float)currHeight;
有了这些信息,视图可以完美地居中于视口:
void display() {
float app_aspcet = (float)winWidth / (float)winHeight;
int currWidth = glutGet( GLUT_WINDOW_WIDTH );
int currHeight = glutGet( GLUT_WINDOW_HEIGHT );
float window_aspcet = (float)currWidth / (float)currHeight;
if ( window_aspcet > app_aspcet )
{
int width = (int)((float)currWidth * app_aspcet / window_aspcet + 0.5f);
glViewport((currWidth - width) / 2, 0, width, currHeight);
}
else
{
int height = (int)((float)currHeight * window_aspcet / app_aspcet + 0.5f);
glViewport(0, (currHeight - height) / 2, currWidth, height);
}
// [...]
}
或者你可以掌握纵横比和正交投影的中心
void display() {
float app_aspcet = (float)winWidth / (float)winHeight;
int currWidth = glutGet( GLUT_WINDOW_WIDTH );
int currHeight = glutGet( GLUT_WINDOW_HEIGHT );
float window_aspcet = (float)currWidth / (float)currHeight;
glViewport(0, 0, currWidth, currHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( window_aspcet > app_aspcet )
{
float delta_width = (float)currWidth * (float)winHeight / (float)currHeight - (float)winWidth;
gluOrtho2D(-delta_width/2.0f, (float)winWidth + delta_width/2.0f, 0.0, (float)winHeight);
}
else
{
float delta_height = (float)currHeight * (float)winWidth / (float)currWidth - (float)winHeight;
gluOrtho2D(0.0, (float)winWidth, -delta_height/2.0f, (float)winHeight + delta_height/2.0f);
}