-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenGLBaseView.cpp
More file actions
379 lines (304 loc) · 8.15 KB
/
OpenGLBaseView.cpp
File metadata and controls
379 lines (304 loc) · 8.15 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// OpenGLBaseView.cpp : COpenGLBaseView クラスの実装
//
#include "pch.h"
// SHARED_HANDLERS は、プレビュー、縮小版、および検索フィルター ハンドラーを実装している ATL プロジェクトで定義でき、
// そのプロジェクトとのドキュメント コードの共有を可能にします。
#ifndef SHARED_HANDLERS
//#include "OpenGLBase.h"
#endif
//#include "OpenGLBaseDoc.h"
#include "OpenGLBaseView.h"
#include <string>
#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glu32.lib" )
#pragma comment( lib, "freeglutd.lib" )
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// COpenGLBaseView
IMPLEMENT_DYNCREATE(COpenGLBaseView, CView)
BEGIN_MESSAGE_MAP(COpenGLBaseView, CView)
ON_WM_ERASEBKGND()
ON_WM_SIZE()
ON_WM_DESTROY()
END_MESSAGE_MAP()
// COpenGLBaseView コンストラクション/デストラクション
COpenGLBaseView::COpenGLBaseView() :
m_hRC( NULL ),
m_pDC( NULL ),
m_cx( 0 ),
m_cy( 0 )
{
// TODO: 構築コードをここに追加します。
}
COpenGLBaseView::~COpenGLBaseView()
{
}
BOOL COpenGLBaseView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: この位置で CREATESTRUCT cs を修正して Window クラスまたはスタイルを
// 修正してください。
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
// COpenGLBaseView 描画
void COpenGLBaseView::OnDraw(CDC* /*pDC*/)
{
CDocument* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: この場所にネイティブ データ用の描画コードを追加します。
if( !m_pDC )
{
return;
}
// レンダリングコンテキストの設定
// MDI描画において必須。SDIにおいては省略可(InitializeOpenGL関数内で呼び出し済みなので)。
// 以降のOpenGL関数による描画は、指定したレンダリングコンテキス上に描画される。
wglMakeCurrent( m_pDC->GetSafeHdc(), m_hRC );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
PreRenderScene();
glPushMatrix();
RenderStockScene();
glPopMatrix();
glPushMatrix();
RenderScene();
glPopMatrix();
PostRenderScene();
glFinish();
SwapBuffers( m_pDC->GetSafeHdc() );
}
// COpenGLBaseView 診断
#ifdef _DEBUG
void COpenGLBaseView::AssertValid() const
{
CView::AssertValid();
}
void COpenGLBaseView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDocument* COpenGLBaseView::GetDocument() const // デバッグ以外のバージョンはインラインです。
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDocument)));
return (CDocument*)m_pDocument;
}
#endif //_DEBUG
bool COpenGLBaseView::InitializeOpenGL()
{
// デバイスコンテキストの作成
m_pDC = new CClientDC( this );
if( NULL == m_pDC )
{
ASSERT( !"m_pDC is NULL" );
return false;
}
// OpenGL pixel format の設定
if( !SetupPixelFormat() )
{
ASSERT( !"SetupPixelFormat failed" );
return false;
}
// レンダリングコンテキストの作成
m_hRC = wglCreateContext( m_pDC->GetSafeHdc() );
if( 0 == m_hRC )
{
ASSERT( !"wglCreateContext failed" );
return false;
}
// レンダリングコンテキストの設定
if( TRUE != wglMakeCurrent( m_pDC->GetSafeHdc(), m_hRC ) )
{
ASSERT( !"wglMakeCurrent failed" );
return false;
}
// OpenGL拡張機能を使えるようにする
if( !InitOpenGLExtFuncs() )
{
ASSERT( !"InitOpenGLExtFuncs failed" );
return false;
}
// クリア処理
glClearColor(0.5f, 0.5f, 0.5f, 0.0f); // クリアカラー
glClearDepth( 1.0f ); // クリア深度
// デプス処理
glEnable( GL_DEPTH_TEST ); // デプステスト
glDepthFunc( GL_LEQUAL ); //デプスファンク(同じか、手前にあるもので上描いていく)
// ポリゴン処理
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); // ポリゴン(GL_TRIANGLES等)は、両面共塗りつぶす。
glDisable( GL_CULL_FACE ); // 裏を向いている面のカリング(カリングしない=裏面も描画)
glEnable( GL_POLYGON_OFFSET_FILL ); // ポリゴンオフセットフィル(面と線が重なる場合のカスレ防止)
glPolygonOffset( 1.0f, 1.0f ); // ポリゴンオフセット量
// ライティング処理
glDisable( GL_LIGHTING ); // 光源
glEnable( GL_NORMALIZE ); // OpenGL側で法線ベクトルを単位法線ベクトル化するようにする
glDisable( GL_COLOR_MATERIAL ); // カラー設定値をマテリアルとして使用
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return true;
}
bool COpenGLBaseView::SetupPixelFormat()
{
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof( PIXELFORMATDESCRIPTOR ),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int iPixelFormat;
if( 0 == ( iPixelFormat = ChoosePixelFormat( m_pDC->GetSafeHdc(), &pfd ) ) )
{
ASSERT( !"ChoosePixelFormat is failed" );
return FALSE;
}
if( TRUE != SetPixelFormat( m_pDC->GetSafeHdc(), iPixelFormat, &pfd ) )
{
ASSERT( !"SetPixelFormat is failed" );
return FALSE;
}
return true;
}
void COpenGLBaseView::UninitializeOpenGL()
{
wglMakeCurrent( NULL, NULL );
if( m_hRC )
{
wglDeleteContext( m_hRC );
m_hRC = NULL;
}
if( m_pDC )
{
delete m_pDC;
m_pDC = NULL;
}
}
bool COpenGLBaseView::SetupViewport()
{
glViewport( 0,
0,
m_cx,
m_cy );
GLenum error = glGetError();
if (error != GL_NO_ERROR)
{
CString str;
str.Format(_T("ERROR:%i"), error);
AfxMessageBox(str);
return false;
}
return true;
}
bool COpenGLBaseView::SetupProjectionMatrix()
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -m_cx * 0.5 / 10.0,
m_cx * 0.5 / 10.0,
-m_cy * 0.5 / 10.0,
m_cy * 0.5 / 10.0,
0.1,
2000.0 );
// 投影変換行列に対する処理はここで完結。
// 以降の変換行列処理は、モデルビュー変換行列に対して実施されるようにする。
glMatrixMode( GL_MODELVIEW );
return true;
}
bool COpenGLBaseView::SetupModelViewMatrix()
{
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2], // 視点
center[0], center[1], center[2], // 注視点
0.0, 1.0, 0.0 ); // 上ベクトル
return true;
}
void COpenGLBaseView::RenderStockScene()
{
RenderAxis();
}
void COpenGLBaseView::RenderAxis()
{
const double origin_length = 10;
float points[4][3];
points[0][0] = 0.0;
points[0][1] = 0.0;
points[0][2] = 0.0;
points[1][0] = origin_length;
points[1][1] = 0.0;
points[1][2] = 0.0;
points[2][0] = 0.0;
points[2][1] = origin_length;
points[2][2] = 0.0;
points[3][0] = 0.0;
points[3][1] = 0.0;
points[3][2] = origin_length;
glPushAttrib( GL_CURRENT_BIT | GL_LINE_BIT ); // 色 | 線情報
glLineWidth( 2.0 );
glBegin( GL_LINES );
glColor3ub( 255, 0, 0 );
glVertex3fv( points[0] );
glVertex3fv( points[1] );
glColor3ub( 0, 255, 0 );
glVertex3fv( points[0] );
glVertex3fv( points[2] );
glColor3ub( 0, 0, 255 );
glVertex3fv( points[0] );
glVertex3fv( points[3] );
glEnd();
glPopAttrib();
}
// COpenGLBaseView メッセージ ハンドラー
BOOL COpenGLBaseView::OnEraseBkgnd( CDC* pDC )
{
// TODO: ここにメッセージ ハンドラー コードを追加するか、既定の処理を呼び出します。
return TRUE;
//return CView::OnEraseBkgnd( pDC );
}
void COpenGLBaseView::OnSize( UINT nType, int cx, int cy )
{
CView::OnSize( nType, cx, cy );
// TODO: ここにメッセージ ハンドラー コードを追加します。
if( 0 >= cx || 0 >= cy )
{
return;
}
if( NULL == m_hRC )
{
// OpenGL初期化処理
InitializeOpenGL();
}
if( NULL != m_hRC )
{
// 画面大きさの設定
m_cx = cx;
m_cy = cy;
//m_cx = 1200;
//m_cy = 400;
// ビューポートの設定
SetupViewport();
// 投影変換行列の設定
SetupProjectionMatrix();
// モデリング&ビューイング変換行列の設定
SetupModelViewMatrix();
}
}
void COpenGLBaseView::OnDestroy()
{
CView::OnDestroy();
// TODO: ここにメッセージ ハンドラー コードを追加します。
UninitializeOpenGL();
}