OpenGL
[OpenGL] Keeping the aspect ratio 종횡비 유지하기
J-sean
2019. 12. 12. 21:03
반응형
It describes how to keep the aspect ratio.
OpenGL에서 종횡비를 유지하기 위해서는 Viewport와 Projection의 비율을 같게 해야 한다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#include <gl/glut.h>
const int InitWidth = 300;
const int InitHeight = 300;
void MyDisplay();
void MyReshape(int NewWidth, int NewHeight);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(InitWidth, InitHeight);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glutCreateWindow("OpenGL Aspect Ratio");
glutReshapeFunc(MyReshape);
// glutReshapeFunc sets the reshape callback for the current window.
glutDisplayFunc(MyDisplay);
glutMainLoop();
return 0;
}
void MyDisplay() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glEnd();
glFlush();
}
void MyReshape(int NewWidth, int NewHeight)
{
glViewport(0, 0, NewWidth, NewHeight);
// glViewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates.
GLfloat WidthFactor = (GLfloat)NewWidth / (GLfloat)InitWidth;
GLfloat HeightFactor = (GLfloat)NewHeight / (GLfloat)InitHeight;
glMatrixMode(GL_PROJECTION); // glMatrixMode sets the current matrix mode.
glLoadIdentity(); // glLoadIdentity replaces the current matrix with the identity matrix.
glOrtho(-1.0 * WidthFactor, 1.0 * WidthFactor, -1.0 * HeightFactor, 1.0 * HeightFactor, -1.0, 1.0);
// glOrtho describes a transformation that produces a parallel projection.
}
|
Run the program.
Resize the window.
Resize the window.
반응형