This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcase2gl.cpp
199 lines (161 loc) · 6.6 KB
/
testcase2gl.cpp
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
#include <windows.h>
#include <windowsx.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "simplewnd.hpp"
#include "utility.hpp"
class CTestWindow
: public CSimpleWindow
{
private:
typedef CTestWindow CSelf;
typedef CSimpleWindow CSuper;
protected:
SIZE m_LastWndSize;
public:
virtual ~CTestWindow()
{
}
CTestWindow(HINSTANCE hInstance)
: CSimpleWindow(hInstance, 640, 480, WS_VISIBLE|WS_POPUPWINDOW|WS_CAPTION|WS_MINIMIZEBOX|WS_CLIPCHILDREN|WS_CLIPSIBLINGS)
, m_LastWndSize()
{
}
void Render()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// R.AddObject(Raytracer::CreatePlane(Raytracer::CreateVector3d(+0.0f, +0.0f, +0.0f), Raytracer::CreateVector3d(+0.0f, +1.0f, +0.0f), Raytracer::CreateColor(1.0f, 1.0f, 1.0f)));
glPopMatrix();
glPushMatrix();
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f, 1.0f, 1.0f);
glNormal3f(+0.0f, +1.0f, +0.0f);
glVertex3f( +0.0f, +0.0f, +0.0f);
glVertex3f(-100.0f, +0.0f, -100.0f);
glVertex3f(-100.0f, +0.0f, +100.0f);
glVertex3f(+100.0f, +0.0f, +100.0f);
glVertex3f(+100.0f, +0.0f, -100.0f);
glVertex3f(-100.0f, +0.0f, -100.0f);
glEnd();
GLUquadricObj* lpQuadric = gluNewQuadric();
if(lpQuadric!=NULL)
{
gluQuadricDrawStyle(lpQuadric, GLU_FILL);
//R.AddObject(Raytracer::CreateSphere(Raytracer::CreateVector3d(+0.0f, +0.0f, +0.0f), 0.1f, Raytracer::CreateColor(1.0f, 0.0f, 0.0f)));
glPopMatrix();
glPushMatrix();
glColor3f(1.0f, 0.0f, 0.0f);
gluSphere(lpQuadric, 0.1f, 20, 20);
// R.AddObject(Raytracer::CreateSphere(Raytracer::CreateVector3d(+2.0f, +1.0f, -3.0f), 1.0f, Raytracer::CreateColor(0.25f, 0.25f, 0.75f)));
glPopMatrix();
glPushMatrix();
glTranslatef(+2.0f, +1.0f, -3.0f);
glColor3f(0.25f, 0.25f, 0.75f);
gluSphere(lpQuadric, 1.0f, 20, 20);
// R.AddObject(Raytracer::CreateSphere(Raytracer::CreateVector3d(-2.0f, +1.0f, -3.0f), 1.0f, Raytracer::CreateColor(0.75f, 0.25f, 0.25f)));
glPopMatrix();
glPushMatrix();
glTranslatef(-2.0f, +1.0f, -3.0f);
glColor3f(0.75f, 0.25f, 0.25f);
gluSphere(lpQuadric, 1.0f, 20, 20);
// R.AddObject(Raytracer::CreateSphere(Raytracer::CreateVector3d(+0.0f, +1.0f, -2.0f), 1.0f, Raytracer::CreateColor(0.25f, 0.75f, 0.25f)));
glPopMatrix();
glPushMatrix();
glTranslatef(+0.0f, +1.0f, -2.0f);
glColor3f(0.25f, 0.75f, 0.25f);
gluSphere(lpQuadric, 1.0f, 20, 20);
gluDeleteQuadric(lpQuadric);
}
glFlush();
}
virtual BOOL WndProcOnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct)
{
PIXELFORMATDESCRIPTOR Pfd = { sizeof(Pfd) };
Pfd.nVersion = 1;
Pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL;
Pfd.iPixelType = PFD_TYPE_RGBA;
Pfd.cColorBits = 32;
HDC hDC = GetDC(hWnd);
int nPixelFormat = ChoosePixelFormat(hDC, &Pfd);
if(nPixelFormat!=0 && SetPixelFormat(hDC, nPixelFormat, &Pfd))
{
HGLRC hGLRC = wglCreateContext(hDC);
if(hGLRC!=NULL)
{
if(wglMakeCurrent(hDC, hGLRC))
{
//R.AddLight(Raytracer::CreateLight(Raytracer::CreateVector3d(+9.0f, +9.0f, +0.0f), Raytracer::CreateColor(1.0f, 1.0f, 1.0f), 50.0f));
GLfloat aLightPos[] = { 9.0f, 9.0f, 0.0f, 1.0f };
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt
(
+0.0f, +1.0f, +10.0f, // eye
+0.0f, +1.0f, +9.0f, // target
+0.0f, +1.0f, +0.0f // up
);
glPushMatrix();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glLightfv(GL_LIGHT0, GL_POSITION, aLightPos);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.0f);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.1f);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0f);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
if(CSuper::WndProcOnCreate(hWnd, lpCreateStruct))
{// this has to come last, because it triggers onWindowPosChanged
SetWindowText(hWnd, "OpenGL TestCase (press ESC to exit)");
return TRUE;
}
wglMakeCurrent(NULL, NULL);
}
wglDeleteContext(hGLRC);
}
}
ReleaseDC(hWnd, hDC);
hDC = NULL;
return FALSE;
}
virtual VOID WndProcOnDestroy(HWND hWnd)
{
HGLRC hGLRC = wglGetCurrentContext();
if(hGLRC!=NULL)
{
HDC hDC = wglGetCurrentDC();
wglMakeCurrent(NULL, NULL);
ReleaseDC(hWnd, hDC);
wglDeleteContext(hGLRC);
}
CSuper::WndProcOnDestroy(hWnd);
}
virtual VOID WndProcOnPaint(HWND hWnd)
{
Render();
ValidateRect(hWnd, NULL);
}
virtual VOID WndProcOnWindowPosChanged(HWND hWnd, const WINDOWPOS* lpWP)
{
RECT rcWnd;
GetClientRect(hWnd, &rcWnd);
if(m_LastWndSize.cx!=rcWnd.right-rcWnd.left || m_LastWndSize.cy!=rcWnd.bottom-rcWnd.top)
{
m_LastWndSize.cx = rcWnd.right-rcWnd.left;
m_LastWndSize.cy = rcWnd.bottom-rcWnd.top;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(180.0f/4, float(m_LastWndSize.cx)/float(m_LastWndSize.cy), 1.0f, 100.0f);
glViewport(0, 0, m_LastWndSize.cx, m_LastWndSize.cy);
InvalidateRect(hWnd, NULL, FALSE);
}
CSuper::WndProcOnWindowPosChanged(hWnd, lpWP);
}
};
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, INT nShowCmd)
{
return CTestWindow(hInstance);
}