我正在尝试开发一个程序,每次在窗口上执行点击时,一个随机大小的圆圈会出现在以点击发生的位置为中心的窗口上。 之后,我需要使用 glOrtho() 或 gluOrtho2D() 以便以这样的方式设置查看体积:当调整窗口大小时,圆圈不会改变它们的大小或在屏幕上变形。
我尝试用不同的方式编写 reshape 函数,但我无法得到正确的结果。
int winWidth = 800;
int winHeight = 600;
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
glutInitWindowSize( winWidth, winHeight );
glutCreateWindow( "main" );
MyInit();
glutDisplayFunc( MyDisplay );
glutReshapeFunc( MyReshape );
glutMouseFunc( MyMouse );
glutMainLoop();
return 0;
}
void MyReshape( int w, int h )
{
winWidth = w;
winHeight = h;
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w, -20.0, 20.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -20.0, 20.0);
glMatrixMode( GL_MODELVIEW );
}
void MyMouse( int btn, int state, int x, int y )
{
if ( btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
{
disc[numDiscs].pos[0] = x;
disc[numDiscs].pos[1] = y;
disc[numDiscs].speed[0] = ((rand() % 2) * 2 - 1) * (MIN_X_SPEED + rand() / (RAND_MAX / (MAX_X_SPEED - MIN_X_SPEED)));
disc[numDiscs].speed[1] = ((rand() % 2) * 2 - 1) * (MIN_Y_SPEED + rand() / (RAND_MAX / (MAX_Y_SPEED - MIN_Y_SPEED)));
disc[numDiscs].radius = (MIN_RADIUS + rand() / (RAND_MAX / (MAX_RADIUS - MIN_RADIUS)))/10;
disc[numDiscs].color[0] = (char)rand() % 256;
disc[numDiscs].color[1] = (char)rand() % 256;
disc[numDiscs].color[2] = (char)rand() % 256;
numDiscs++;
glutPostRedisplay();
}
}
void MyDisplay( void )
{
glClear( GL_COLOR_BUFFER_BIT );
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
for ( int i = 0; i < numDiscs; i++ ) DrawDisc( &disc[i] );
glFlush();
}
void MyInit( void )
{
glClearColor( 0.0, 0.0, 0.0, 1.0 );
glShadeModel( GL_FLAT );
}
最佳答案
[...] in such a way that when the window is resized, the circles do not change their sizes or get distorted [...]
如果圆应该以像素为单位保持大小,那么您必须定义一个正交投影,它基于窗口的当前大小与窗口初始大小的关系。
由于窗口的大小是 (800, 600),因此您必须根据窗口的高度分别将投影缩放 w/600.0
h/600.0
,因为高度小于宽度,并且 aspect 应用于较长的边:
void MyReshape( int w, int h )
{
winWidth = w;
winHeight = h;
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
if (w <= h)
{
GLdouble aspect = (GLdouble)h / w;
GLdouble scale = w / 600.0;
scale *= 2.0;
project = glm::ortho(-scale, scale, -scale * aspect, scale * aspect, -20.0, 20.0);
}
else
{
GLdouble aspect = (GLdouble)w / h;
GLdouble scale = h / 600.0;
scale *= 2.0;
project = glm::ortho(-scale * aspect, scale * aspect, -scale, scale, -20.0, 20.0);
}
glMatrixMode( GL_MODELVIEW );
}
关于c++ - 无法在窗口中正确 reshape 多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57798628/