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
/
testcase2w.cpp
212 lines (164 loc) · 6.71 KB
/
testcase2w.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
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <vector>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include "simplewnd.hpp"
#include "utility.hpp"
#include "raytracr.hpp"
class CTestWindow
: public CSimpleWindow
{
private:
typedef CTestWindow CSelf;
typedef CSimpleWindow CSuper;
protected:
int m_nImageW;
int m_nImageH;
HBITMAP m_hbmOutput1;
public:
CTestWindow(const HINSTANCE hInstance, const INT nClientW, const INT nClientH, const DWORD dwStyle, const HBITMAP hbmOutput1)
: CSimpleWindow(hInstance, nClientW, nClientH, dwStyle)
, m_nImageW(nClientW)
, m_nImageH(nClientH)
, m_hbmOutput1(hbmOutput1)
{
}
virtual ~CTestWindow()
{
if(m_hbmOutput1!=nullptr)
{
DeleteBitmap(m_hbmOutput1);
m_hbmOutput1 = nullptr;
}
}
virtual BOOL WndProcOnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct)
{
if(CSuper::WndProcOnCreate(hWnd, lpCreateStruct))
{
SetWindowText(hWnd, "TestCase (press ESC to exit)");
return TRUE;
}
return FALSE;
}
virtual VOID WndProcOnPaint(HWND hWnd)
{
if(GetUpdateRect(hWnd, nullptr, FALSE))
{
PAINTSTRUCT Ps = { 0 };
if(BeginPaint(hWnd, &Ps))
{
HDC hDC = CreateCompatibleDC(nullptr);
HGDIOBJ hPrevObj;
if(m_hbmOutput1!=nullptr)
{
hPrevObj = SelectObject(hDC, m_hbmOutput1);
BitBlt(Ps.hdc, 0, 0, m_nImageW, m_nImageH, hDC, 0, 0, SRCCOPY);
SelectObject(hDC, hPrevObj);
}
DeleteDC(hDC);
EndPaint(hWnd, &Ps);
}
}
}
virtual UINT WndProcOnNCHitTest(HWND hWnd, int nX, int nY)
{
UINT uHitTest = FORWARD_WM_NCHITTEST(hWnd, nX, nY, DefWindowProc);
if(uHitTest==HTCLIENT)
{
uHitTest = HTCAPTION;
}
return uHitTest;
}
virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_PAINT, WndProcOnPaint);
HANDLE_MSG(hWnd, WM_NCHITTEST, WndProcOnNCHitTest);
}
return CSuper::WndProc(hWnd, uMsg, wParam, lParam);
}
};
bool TrackProgress(unsigned int uDone, unsigned int uTotal, void* lpContext)
{
printf("%3.1f%%\r", uDone*100.0f/uTotal);
return true;
UNREFERENCED_PARAMETER(lpContext);
}
HBITMAP BitmapFromPixels2(const std::vector< Raytracer::CColor >& aclrPixels, const int nImageW, const int nImageH)
{
HBITMAP hbmOutput = nullptr;
BITMAPINFO bmiOutput = { 0 };
VOID* lpBits = nullptr;
bmiOutput.bmiHeader.biSize = sizeof(bmiOutput.bmiHeader);
bmiOutput.bmiHeader.biWidth = nImageW;
bmiOutput.bmiHeader.biHeight = nImageH; // bottom-up, just like our pixel buffer
bmiOutput.bmiHeader.biPlanes = 1;
bmiOutput.bmiHeader.biBitCount = 24;
bmiOutput.bmiHeader.biCompression = BI_RGB;
hbmOutput = CreateDIBSection(CGetDC(nullptr), &bmiOutput, DIB_RGB_COLORS, &lpBits, nullptr, 0);
if(hbmOutput!=nullptr)
{
BITMAP bmInfo = { 0 };
GetObject(hbmOutput, sizeof(BITMAP), &bmInfo);
const int nWidthBytes = bmInfo.bmWidthBytes;
const int nPixelBytes = bmInfo.bmBitsPixel/8;
for(int nImageY = 0; nImageY<nImageH; nImageY++)
{
for(int nImageX = 0; nImageX<nImageW; nImageX++)
{
const auto& Px = aclrPixels[nImageX+nImageY*nImageW];
unsigned char* ucBits = &static_cast< unsigned char* >(lpBits)[nImageY*nWidthBytes+nImageX*nPixelBytes];
ucBits[0] = static_cast< unsigned char >(Px.B()*255U);
ucBits[1] = static_cast< unsigned char >(Px.G()*255U);
ucBits[2] = static_cast< unsigned char >(Px.R()*255U);
}
}
GdiFlush();
}
return hbmOutput;
}
HBITMAP RenderJob(Raytracer& R)
{
DWORD dwStart;
dwStart = GetTickCount();
//R.Render(&TrackProgress, nullptr);
R.Render(nullptr);
printf("%ums", GetTickCount()-dwStart);
dwStart = GetTickCount();
HBITMAP hbmOutput = BitmapFromPixels2(R.GetResult(), R.GetImageWidth(), R.GetImageHeight());
printf("+%ums\n", GetTickCount()-dwStart);
return hbmOutput;
}
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, INT nShowCmd)
{
const float PI = acos(0.0f)*2.0f;
Raytracer R;
// origin
R.AddObject(Raytracer::CreateSphere(Raytracer::CreateVector3d(+0.0f, +0.0f, +0.0f), 0.1f, Raytracer::CreateColor(1.0f, 0.0f, 0.0f)));
// Simple objects
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)));
R.AddObject(Raytracer::CreateSphere(Raytracer::CreateVector3d(+2.0f, +1.0f, -3.0f), 1.0f, Raytracer::CreateColor(0.25f, 0.25f, 0.75f)));
R.AddObject(Raytracer::CreateSphere(Raytracer::CreateVector3d(-2.0f, +1.0f, -3.0f), 1.0f, Raytracer::CreateColor(0.75f, 0.25f, 0.25f)));
R.AddObject(Raytracer::CreateSphere(Raytracer::CreateVector3d(+0.0f, +1.0f, -2.0f), 1.0f, Raytracer::CreateColor(0.25f, 0.75f, 0.25f)));
// Lights
R.AddLight(Raytracer::CreateLight(Raytracer::CreateVector3d(+9.0f, +9.0f, +0.0f), Raytracer::CreateColor(1.0f, 1.0f, 1.0f), 50.0f, 0.0f, 0.1f, 0.0f));
R.SetImageSize(640, 480);
puts("Rendering...");
const auto ovecEye = Raytracer::CreateVector3d(+0.0f, +1.0f, +10.0f);
const auto rvecLookAt = Raytracer::CreateVector3d(+0.0f, +0.0f, -1.0f);
const auto uvecUp = Raytracer::CreateVector3d(+0.0f, +1.0f, +0.0f);
const auto mtxFront = Raytracer::CreateMatrix3d().SetIdentity().AddYRotation(PI/180.0f*0.0f);
const auto mtxSide = Raytracer::CreateMatrix3d().SetIdentity().AddYRotation(PI/180.0f*90.0f);
const auto mtxTop = Raytracer::CreateMatrix3d().SetIdentity().AddXRotation(PI/180.0f*-90.0f);
const auto mtxIso = Raytracer::CreateMatrix3d().SetIdentity().AddYRotation(PI/180.0f*45.0f).AddXRotation(PI/180.0f*-45.0f);
// Front
R.SetCamera(ovecEye.MatrixProduct(mtxFront), rvecLookAt.MatrixProduct(mtxFront), uvecUp.MatrixProduct(mtxFront));
HBITMAP hbmOutput1 = RenderJob(R);
puts("Present.");
return CTestWindow(hInstance, R.GetImageWidth(), R.GetImageHeight(), WS_VISIBLE|WS_POPUPWINDOW|WS_SYSMENU|WS_CAPTION|WS_BORDER|WS_MINIMIZEBOX, hbmOutput1);
}