如何确保输出窗口是固定的,或者每次在 OpenGL 中最大化/最小化窗口时,窗口中的圆形输出不会改变形状。我在 Visual Studio 2019 上使用 c++(意味着已经设置了 glew 和 glut 依赖项。)我正在尝试使用中点绘图算法(半径 5cm,起点坐标(0,3))创建一个圆并确保它旋转 90 度之后。
请注意,屏幕截图显示在最小化窗口后圆形变为椭圆形。 Screenshot link...
代码:
#include <Windows.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
using namespace std;
int xForRotation = 000;
int yForRotation = 120;
int rFlag;
double theta = 0.0;
void myinit(void)
{
glClearColor(1, 1, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutInitWindowSize(1000,1000);
gluOrtho2D(-1*640.0, 640.0, -1*640.0, 640.0);
glMatrixMode(GL_MODELVIEW);
}
void circleMidPoint()
{
int xCenter = 000;
int yCenter = 240; //3cm
int radius = 400; //5cm
int x = 0;
int y = radius;
int p = 1 - radius;//5/4 is rounded to 1 for integer radius
while (x < y) {// iterates to draw the first sector
x++;
if (p < 0)// the mid point is inside the circle
p += 2 * x + 1;
else {// the mid point is outside or at the circle
y--;
p += 2 * (x - y) + 1;
}
glBegin(GL_POINTS);
glVertex2i(xCenter + x, yCenter + y);
glVertex2i(xCenter - x, yCenter + y);
glVertex2i(xCenter + x, yCenter - y);
glVertex2i(xCenter - x, yCenter - y);
glVertex2i(xCenter + y, yCenter + x);
glVertex2i(xCenter - y, yCenter + x);
glVertex2i(xCenter + y, yCenter - x);
glVertex2i(xCenter - y, yCenter - x);
glEnd();
}
// OPTIONAL:-> center of the circle
glBegin(GL_POINTS);
glVertex2i(xCenter, yCenter);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glColor3f(1.0, 0.0, 0.0); // red foreground
glPointSize(5.0);// size of points to be drawin (in pixel)
//establish a coordinate system for the image
circleMidPoint();
glFlush(); // send all output to the display
glLoadIdentity();
if (rFlag == 1) //Rotate the circle around fixed point
{
yForRotation;
xForRotation;
theta = 0.25;
}
glTranslatef(xForRotation, yForRotation, 0.0);
glRotatef(theta, 0.0, 0.0, 1.0);
glTranslatef(-xForRotation, -yForRotation, 0.0);
glutPostRedisplay();
glutSwapBuffers();
}
void rotateMenu(int option){
if (option == 1)
rFlag = 1;
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitWindowSize(640, 480); // set the size of the window
glutInitWindowPosition(10, 10); // the position of the top-left of window
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Midpoint Circle Drawing Algorithm. *Right CLick to Rotate");
myinit();
glutDisplayFunc(display); // set the gl display callback function
glutCreateMenu(rotateMenu);
glutAddMenuEntry("Rotate Circle", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop(); // enter the GL event loop
}
最佳答案
反射(reflect)窗口的新大小,你必须添加一个 glutReshapeFunc
打回来。在回调中,您必须通过 glViewport
将视口(viewport)矩形调整为新大小.
此外,您还必须使正交投影适应新的窗口大小。
必须在创建窗口之前设置窗口大小。稍后调用 glutInitWindowSize
没有效果。
由于正交投影设置在reshape
, myinit
中不需要设置.
void myinit(void)
{
glClearColor(1, 1, 1, 0);
}
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-width, width, -height, height);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
// [...]
glutInitWindowSize(1000, 1000);
glutCreateWindow("Midpoint Circle Drawing Algorithm. *Right CLick to Rotate");
// [...]
glutReshapeFunc(reshape);
// [...]
}
关于c++ - OpenGL - 最大化/最小化输出窗口调整内部对象的大小。我怎样才能避免这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60154471/