반응형

Each vertex of a polygon, separate triangle, or separate quadrilateral specified between a glBegin/glEnd pair is marked as the start of either a boundary or nonboundary edge. If the current edge flag is TRUE when the vertex is specified, the vertex is marked as the start of a boundary edge. If the current edge flag is FALSE, the vertex is marked as the start of a nonboundary edge.


OpenGL에서 선을 그릴때 다각형의 경계선인지 숨겨야할 내부선인지 결정 할 수 있다.


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
55
56
57
58
59
60
#include <gl/glut.h>
 
void DoDisplay();
 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("OpenGL");
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glutDisplayFunc(DoDisplay);
    glutMainLoop();
 
    return 0;
}
 
void DoDisplay()
 
{
    glClear(GL_COLOR_BUFFER_BIT);
 
    glPushMatrix();
 
    glTranslatef(-0.5f, 0.0f, 0.0f);
 
    // 첫 번째 선 Nonboundary 지정
    glBegin(GL_TRIANGLES);
    glEdgeFlag(GL_FALSE);   // 경계선 적용 여부를 선택할 선의 시작점 선언 전에 glEdgeFlag()를 호출.
    // Flags edges as either boundary or nonboundary.
    glVertex2f(0.0f, 0.2f);
    glEdgeFlag(GL_TRUE);
    glVertex2f(-0.2f, -0.2f);
    glVertex2f(0.2f, -0.2f);
    glEnd();
 
    glTranslatef(0.5f, 0.0f, 0.0f);
 
    // 두 번째 선 Nonboundary 지정
    glBegin(GL_TRIANGLES);
    glVertex2f(0.0f, 0.2f);
    glEdgeFlag(GL_FALSE);
    glVertex2f(-0.2f, -0.2f);
    glEdgeFlag(GL_TRUE);
    glVertex2f(0.2f, -0.2f);
    glEnd();
 
    glTranslatef(0.5f, 0.0f, 0.0f);
 
    // 세 번째 선 Nonboundary 지정
    glBegin(GL_TRIANGLES);
    glVertex2f(0.0f, 0.2f);
    glVertex2f(-0.2f, -0.2f);
    glEdgeFlag(GL_FALSE);
    glVertex2f(0.2f, -0.2f);
    glEdgeFlag(GL_TRUE);    
    glEnd();
 
    glPopMatrix();
 
    glFlush();
}



Run the program.


반응형
Posted by J-sean
:
반응형

It describes how to draw the simple solar system that is composed of the Sun, the Earth and the Moon.

OpenGL로 태양, 지구, 달로 구성된 간단한 태양계 그리기


1 day = 10 frames

1 month = 30 days (300 frames)

1 year = 12 months (3,600 frames)


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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <gl/glut.h>
#include <iostream>
 
GLfloat EarthRotation = 0.0f;
GLfloat MoonRevolution = 0.0f;
GLfloat EarthRevolution = 0.0f;
 
GLfloat FramePerYear = 3600.0f;
GLint delay = 10;
 
GLint Year = 2020;
GLint Month = 1;
GLint Day = 1;
 
void MyDisplay();
void MyTimer(int value);
 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutCreateWindow("OpenGL Solar System");
 
    gluLookAt(
        0.01.00.0,    // Eye
        0.00.00.0,    // Center
        1.00.01.0    // Up
    );
    // gluLookAt creates a viewing matrix derived from an eye point, a reference point indicating
    // the center of the scene, and an UP vector.
 
    glutTimerFunc(delay, MyTimer, 1);
    glutDisplayFunc(MyDisplay);
    
    glutMainLoop();
 
    return 0;
}
 
void MyDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);
    
    // The Sun
    glutWireSphere(0.44020);
    
    // The Earth
    glPushMatrix();
    glRotatef(EarthRevolution, 0.0f1.0f0.0f); // Earth's revolution
    glTranslatef(0.7f0.0f0.0f);
    
    glRotatef(-EarthRevolution, 0.0f1.0f0.0f);    // Earth의 Revolution에 의한 Earth의 회전 삭제.
    glRotatef(EarthRotation, 0.0f1.0f0.0f);    // Earth's rotation
    glutWireSphere(0.12010);
 
        // The Moon
        glPushMatrix();
        glRotatef(-EarthRotation, 0.0f1.0f0.0f);    // Earth의 Rotation에 의한 Moon의 회전 삭제.
        glRotatef(MoonRevolution, 0.0f1.0f0.0f); // Moon's revolution
        glTranslatef(0.2f0.0f0.0f);
        glutWireSphere(0.03105);
        glPopMatrix();
 
    glPopMatrix();
    
    glutSwapBuffers();
}
 
void MyTimer(int value)
{
    EarthRotation += (360.0f * 360.0f/ FramePerYear;    // 지구 자전: (360도 * 360일) / FramePerYear(3600) = 36도
    if (EarthRotation >= 360.0f) {
        EarthRotation = 0.0f;
 
        std::cout << Year << "-" << Month << "-" << Day << std::endl;
        // 날짜 출력 코드가 계산 코드(Day++, Month++, Year++)보다 먼저 처리 되야 제대로 표시 된다.
 
        Day++;
        if (Day > 30)
            Day = 1;
    }
 
    MoonRevolution += (360.0f * 12.0f/ FramePerYear;
    // 달의 공전 주기는 약27일 이지만 30일(1달)로 설정.
    // Rotation은 Revolution 과 같은 주기로 따로 계산할 필요 없음.
    // Revolution 처리 만으로 항상 달의 같은면이 지구를 바라 봄.
    if (MoonRevolution >= 360.0f) {
        MoonRevolution = 0.0f;
 
        Month++;
        if (Month > 12)
            Month = 1;
    }
 
    EarthRevolution += 360.0f / FramePerYear;    // 1 프레임에 0.1도 회전. 3600 프레임이 1년.
    if (EarthRevolution >= 360.0f) {
        EarthRevolution = 0.0f;
 
        Year++;
    }
    
    glutPostRedisplay();
    glutTimerFunc(delay, MyTimer, 1);
}



Run the program and see how it works.


반응형
Posted by J-sean
:

Rotation 회전

OpenGL 2019. 12. 13. 14:43 |
반응형

It describes how to rotate objects.

OpenGL에서 오브젝트 회전 하기.


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
55
56
57
#include <gl/glut.h>
 
GLfloat angle = 0.0f;
 
void MyDisplay();
void MyTimer(int value);
 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutCreateWindow("OpenGL Rotation");
 
    glutDisplayFunc(MyDisplay);
    glutTimerFunc(20, MyTimer, 1);
 
    glutMainLoop();
 
    return 0;
}
 
void MyDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);
    
    // 1st teapot, right, CCW
    glPushMatrix();
    glRotatef(45.0f1.0f0.0f0.0f);    // 45 degrees rotation around X-Axis to see the direction of the rotation.
    glTranslatef(0.5f0.0f0.0f);        // Changing the order of calling this Translate()..
    glRotatef(angle, 0.0f1.0f0.0f);    // and this Rotate() will affect how it spins. (rotation and revolution)
    glutWireTeapot(0.3);
    glPopMatrix();
    
    // 2nd teapot, left, CW
    glPushMatrix();
    // glPushMatrix pushes the current matrix stack down by one, duplicating the current matrix. That is, after a
    // glPushMatrix call, the matrix on top of the stack is identical to the one below it.
    glRotatef(45.0f1.0f0.0f0.0f);
    glTranslatef(-0.5f0.0f0.0f);
    glRotatef(-angle, 0.0f1.0f0.0f);
    glutWireTeapot(0.3);
    glPopMatrix();
    //glPopMatrix pops the current matrix stack, replacing the current matrix with the one below it on the stack.
 
    glutSwapBuffers();
}
 
void MyTimer(int value)
{
    angle += 1.0f;
    if (angle >= 360.0f) {
        angle = 0.0f;
    }
 
    glutPostRedisplay();
    glutTimerFunc(20, MyTimer, 1);
}



Run the program and see the directions of each rotation.


반응형
Posted by J-sean
:
반응형

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.0f0.0f0.0f0.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.0f0.0f1.0f);
    glVertex3f(0.0f0.5f0.0f);
    glColor3f(1.0f0.0f0.0f);
    glVertex3f(-0.5f-0.5f0.0f);
    glColor3f(0.0f1.0f0.0f);
    glVertex3f(0.5f-0.5f0.0f);
    glEnd();
 
    glFlush();
}
 
void MyReshape(int NewWidth, int NewHeight)
{
    glViewport(00, 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.01.0);
    // glOrtho describes a transformation that produces a parallel projection.
}



Run the program.


Resize the window.


Resize the window.


반응형
Posted by J-sean
:
반응형

freeglut을 설치하고 간단한 OpenGL 예제를 만들어 보자.


http://freeglut.sourceforge.net/ 에서 MSVC용 Prepackaged Release 를 다운 받아 원하는 폴더에 설치 한다.


빈 Console 프로젝트를 만든다.


freeglut 라이브러리의 dll 파일이 있는 폴더를 Environment에 지정 한다.


Include 디렉토리를 지정 한다.


sprintf() 사용을 위해 SDL checks를 No로 바꾼다. Yes로 된 경우 비표준 함수인 sprintf_s()를 써야 한다.


SDL checks: Adds recommended Security Development Lifecycle (SDL) checks. These checks include extra security-relevant warnings as errors, and additional secure code-generation features.


Library 디렉토리를 지정 한다.


외부 라이브러리 사용시 위 사진과 같이 라이브러리 파일들을 추가해야 하지만 freeglut_std.h에 필요한 라이브러리가 모두 추가 되어 있으므로 하지 않아도 된다.



<freeglut_std.h>

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
#   include <windows.h>
 
/* Windows static library */
#   ifdef FREEGLUT_STATIC
 
#error Static linking is not supported with this build. Please remove the FREEGLUT_STATIC preprocessor directive, or download the source code from http://freeglut.sf.net/ and build against that.
 
/* Windows shared library (DLL) */
#   else
 
#       define FGAPIENTRY __stdcall
#       if defined(FREEGLUT_EXPORTS)
#           define FGAPI __declspec(dllexport)
#       else
#           define FGAPI __declspec(dllimport)
 
            /* Link with Win32 shared freeglut lib */
#           if FREEGLUT_LIB_PRAGMAS
#             pragma comment (lib, "freeglut.lib")
#           endif
 
#       endif
 
#   endif
 
/* Drag in other Windows libraries as required by FreeGLUT */
#   if FREEGLUT_LIB_PRAGMAS
#       pragma comment (lib, "glu32.lib")    /* link OpenGL Utility lib     */
#       pragma comment (lib, "opengl32.lib"/* link Microsoft OpenGL lib   */
#       pragma comment (lib, "gdi32.lib")    /* link Windows GDI lib        */
#       pragma comment (lib, "winmm.lib")    /* link Windows MultiMedia lib */
#       pragma comment (lib, "user32.lib")   /* link Windows user lib       */
#   endif
 
#else
 
/* Non-Windows definition of FGAPI and FGAPIENTRY  */
#        define FGAPI
#        define FGAPIENTRY
 
#endif


windows.h 도 포함 되어 있다.


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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <gl/glut.h>
#include <cstdio>
 
GLfloat locationX = 0.0f, locationY = 0.0f;
const GLfloat step = 0.02f;
const GLfloat size = 0.2f;
int winId;
 
void Menu(int value);
void Keyboard(unsigned char key, int x, int y);
void Special(int key, int x, int y);
void Mouse(int button, int state, int x, int y);
void Display();
 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    
    winId = glutCreateWindow("OpenGL");
 
    glutKeyboardFunc(Keyboard);
    glutSpecialFunc(Special);
    glutMouseFunc(Mouse);
 
    // Sub menu
    GLint SubMenu = glutCreateMenu(Menu);
    glutAddMenuEntry("Red"4);
    glutAddMenuEntry("Green"5);
    glutAddMenuEntry("Blue"6);
 
    // Menu
    glutCreateMenu(Menu);
    glutAddMenuEntry("White"1);
    glutAddMenuEntry("Black"2);
    glutAddMenuEntry("Gray"3);
 
    // Add sub menu
    glutAddSubMenu("Triangle Color", SubMenu);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
 
    glutDisplayFunc(Display);
 
    glutMainLoop();
 
    return 0;
}
 
void Menu(int value)
{
    switch (value) {
    case 1:
        glClearColor(1.0f1.0f1.0f1.0f);    // 배경 흰색
 
        break;
    case 2:
        glClearColor(0.0f0.0f0.0f1.0f);    // 배경 검정색
 
        break;
    case 3:
        glClearColor(0.5f0.5f0.5f1.0f);    // 배경 회색
 
        break;
    case 4:
        glColor3f(1.0f0.0f0.0f);            // 삼감형 빨간색
 
        break;
    case 5:
        glColor3f(0.0f1.0f0.0f);            // 삼감형 녹색
 
        break;
    case 6:
        glColor3f(0.0f0.0f1.0f);            // 삼감형 파란색
 
        break;
    default:
 
        break;
    }
 
    glutPostRedisplay();
}
 
void Keyboard(unsigned char key, int x, int y)
{
    switch (key) {
    case 'r':
    case 'R':
        glClearColor(1.0f0.0f0.0f1.0f);    // 배경 빨간색
 
        break;
    case 'g':
    case 'G':
        glClearColor(0.0f1.0f0.0f1.0f);    // 배경 녹색
 
        break;
    case 'b':
    case 'B':
        glClearColor(0.0f0.0f1.0f1.0f);    // 배경 파란색
 
        break;
    case 27:
        glutDestroyWindow(winId);                // ESC키 프로그램 종료
        exit(0);
 
        break;
    default:
 
        break;
    }
 
    glutPostRedisplay();
}
 
void Special(int key, int x, int y)
{
    switch (key) {
    case GLUT_KEY_LEFT:        // step 만큼 x감소
        locationX -= step;
 
        break;
    case GLUT_KEY_RIGHT:    // step 만큼 x증가
        locationX += step;
 
        break;
    case GLUT_KEY_UP:        // step 만큼 y증가
        locationY += step;
 
        break;
    case GLUT_KEY_DOWN:        // step 만큼 y감소
        locationY -= step;
 
        break;
    }
    char title[128= {0,};
    sprintf(title, "Triangle location (%.2f, y=%.2f)", locationX, locationY);
    glutSetWindowTitle(title); // 제목표시줄에 삼각형 좌표 표시
 
    glutPostRedisplay();
}
 
void Mouse(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        glColor3f(0.0f1.0f1.0f);    // 삼각형 하늘색
        char title[128= { 0, };
        sprintf(title, "Click (x: %d, y: %d)", x, y);
        glutSetWindowTitle(title); // 제목표시줄에 클릭 좌표 표시
 
        glutPostRedisplay();
    }
    else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP){
        glColor3f(1.0f1.0f0.0f);    // 삼각형 노란색
 
        glutPostRedisplay();
    }
}
 
 
void Display()
{
    glClear(GL_COLOR_BUFFER_BIT);
        
    glBegin(GL_TRIANGLES);
    glVertex2f(locationX, locationY + size);
    glVertex2f(locationX - size, locationY - size);
    glVertex2f(locationX + size, locationY - size);
    glEnd();
 
    glFinish();
}


코드를 입력하고 실행 한다.


오른쪽 버튼을 클릭하면 메뉴가 나온다.


삼각형이 빨간색으로 바뀌었다.


r,g,b, 방향키, ESC키 등을 눌러 본다.


SubSystem을Console에서 Widows로 바꾼다.



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <gl/glut.h>
 
void Menu(int value)
{
    switch (value) {
    case 1:
        glClearColor(1.0f1.0f1.0f1.0f);
 
        break;
    case 2:
        glClearColor(0.0f0.0f0.0f1.0f);
 
        break;
    case 3:
        glClearColor(0.5f0.5f0.5f1.0f);
 
        break;
    case 4:
        glColor3f(1.0f0.0f0.0f);
 
        break;
    case 5:
        glColor3f(0.0f1.0f0.0f);
 
        break;
    case 6:
        glColor3f(0.0f0.0f1.0f);
 
        break;
    default:
 
        break;
    }
 
    glutPostRedisplay();
}
 
void display() {
    glClear(GL_COLOR_BUFFER_BIT);
 
    glBegin(GL_TRIANGLES);
    glVertex2f(0.0f0.5f);
    glVertex2f(-0.5f-0.5f);
    glVertex2f(0.5f-0.5f);
    glEnd();
 
    glFinish();
}
 
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
    glutCreateWindow("OpenGL");
 
    // Sub menu
    GLint SubMenu = glutCreateMenu(Menu);
    glutAddMenuEntry("Red"4);
    glutAddMenuEntry("Green"5);
    glutAddMenuEntry("Blue"6);
 
    // Menu
    glutCreateMenu(Menu);
    glutAddMenuEntry("White"1);
    glutAddMenuEntry("Black"2);
    glutAddMenuEntry("Gray"3);
 
    // Add sub menu
    glutAddSubMenu("Triangle Color", SubMenu);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
 
    glutDisplayFunc(display);
 
    glutMainLoop();
 
    return 0;
}


위 코드와 같이 Windows 프로그램에 맞게 바꾸고 빌드하면 에러없이 빌드는 되지만 아무것도 실행 되지는 않는다.


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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <Windows.h>
#include <gl/gl.h>
 
# pragma comment (lib, "opengl32.lib")
# pragma comment(lib, "glu32.lib")
 
HINSTANCE g_hInst;
HWND hWndMain;
LPCTSTR lpszClass = TEXT("OpenGL");
HDC hdc;
HGLRC hrc;
 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void Display();
 
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
    g_hInst = hInstance;
 
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClass.hInstance = hInstance;
    WndClass.lpfnWndProc = WndProc;
    WndClass.lpszClassName = lpszClass;
    WndClass.lpszMenuName = NULL;
    WndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    RegisterClass(&WndClass);
 
    hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, (HMENU)NULL, hInstance, NULL);
    ShowWindow(hWnd, nCmdShow);
 
    while (GetMessage(&Message, NULL00)) {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
 
    return (int)Message.wParam;
}
 
void Display()
{
    glClear(GL_COLOR_BUFFER_BIT);
 
    glBegin(GL_TRIANGLES);
    glVertex2f(0.0f0.5f);
    glVertex2f(-0.5f-0.5f);
    glVertex2f(0.5f-0.5f);
    glEnd();
 
    glFinish();
}
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
    switch (iMessage) {
    case WM_CREATE:
        hWndMain = hWnd;
        PIXELFORMATDESCRIPTOR pfd;
        int nPixelFormat;
 
        hdc = GetDC(hWnd);
        memset(&pfd, 0sizeof(pfd));
 
        pfd.nSize = sizeof(pfd);
        pfd.nVersion = 1;
        pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = 32;
 
        nPixelFormat = ChoosePixelFormat(hdc, &pfd);
        SetPixelFormat(hdc, nPixelFormat, &pfd);
 
        hrc = wglCreateContext(hdc);
        wglMakeCurrent(hdc, hrc);
 
        return 0;
 
    case WM_PAINT:
        Display();
 
        SetTextColor(hdc, RGB(2552550));
        SetBkMode(hdc, TRANSPARENT);
        TextOutA(hdc, 1010"Windows OpenGL"14);
        
        ValidateRect(hWnd, NULL);
 
        return 0;
 
    case WM_SIZE:
        glViewport(00, LOWORD(lParam), HIWORD(lParam));
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
 
        glOrtho(-11-111-1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
 
        return 0;
    case WM_DESTROY:
        wglMakeCurrent(hdc, NULL);
        wglDeleteContext(hrc);
        ReleaseDC(hWnd, hdc);
        PostQuitMessage(0);
 
        return 0;
    }
 
    return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}


glut을 사용하지 않고 Windows에서 지원하는 OpenGL 관련 wgl함수를 이용해 작성한 코드이다.



※ Reference

http://soen.kr/lecture/library/opengl/opengl-5.htm


반응형
Posted by J-sean
: