My Project
scene.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "../stdafx.h"
3 #include "scene.h"
4 
6 {
7  this->hWnd = NULL;
8  this->hdc = NULL;
9  this->hdcMem = NULL;
10 
11  this->X = 0;
12  this->Y = 0;
13  this->width = 0;
14  this->height = 0;
15 
16  this->InitBitmap();
17 
18  this->bricks = nullptr;
19  this->pixels = nullptr;
20  this->render = nullptr;
21  this->cam = nullptr;
22 }
23 
24 Scene::Scene(HWND hWnd, int x, int y, int width, int height)
25 {
26  this->hWnd = hWnd;
27  this->hdc = GetDC(hWnd);
28  this->hdcMem = CreateCompatibleDC(this->hdc);
29 
30  this->X = x;
31  this->Y = y;
32  this->width = width;
33  this->height = height;
34 
35  this->pixels = new unsigned long[this->width * this->height];
36  if (!this->pixels)
37  {
38  throw AllocationMemoryError();
39  }
40 
41  this->InitBitmap();
42 
43  this->bricks = new Composite;
44  if (!this->bricks)
45  {
46  delete this->pixels;
47  this->pixels = nullptr;
48 
49  throw AllocationMemoryError();
50  }
51 
52  this->render = new Render(pixels, this->height, this->width);
53  if (!this->render)
54  {
55  delete this->pixels;
56  delete this->bricks;
57  this->pixels = nullptr;
58  this->bricks = nullptr;
59 
60  throw AllocationMemoryError();
61  }
62 
63  GVector position(1000, 0, 0, 1);
64  GVector target(0, 0, 0, 1);
65 
66  this->cam = new Camera(position, target);
67  if (!this->cam)
68  {
69  delete this->pixels;
70  delete this->bricks;
71  delete this->render;
72  this->pixels = nullptr;
73  this->bricks = nullptr;
74  this->render = nullptr;
75 
76  throw AllocationMemoryError();
77  }
78  //this->light.Z = -1000;
79  this->light.X = -1000;
80 
81 }
82 
84 {
85  delete this->pixels;
86  delete this->bricks;
87  delete this->render;
88  delete this->cam;
89  this->pixels = nullptr;
90  this->bricks = nullptr;
91  this->render = nullptr;
92  this->cam = nullptr;
93 }
94 
95 void Scene::InitBitmap()
96 {
97  this->sBmInfo.bmiHeader.biSize = sizeof(BITMAPINFO);
98  this->sBmInfo.bmiHeader.biWidth = this->width;
99  this->sBmInfo.bmiHeader.biHeight = -this->height;
100  this->sBmInfo.bmiHeader.biPlanes = 1;
101  this->sBmInfo.bmiHeader.biBitCount = 32;
102  this->sBmInfo.bmiHeader.biCompression = BI_RGB;
103  this->sBmInfo.bmiHeader.biSizeImage = 0;
104  this->sBmInfo.bmiHeader.biXPelsPerMeter = 0;
105  this->sBmInfo.bmiHeader.biYPelsPerMeter = 0;
106  this->sBmInfo.bmiHeader.biClrUsed = 0;
107  this->sBmInfo.bmiHeader.biClrImportant = 0;
108 
109  this->sBmInfo.bmiColors[0].rgbBlue = 0;
110  this->sBmInfo.bmiColors[0].rgbGreen = 0;
111  this->sBmInfo.bmiColors[0].rgbRed = 0;
112  this->sBmInfo.bmiColors[0].rgbReserved = 0;
113 
114  this->sBmp = CreateDIBSection(
115  this->hdc, &this->sBmInfo, DIB_RGB_COLORS, (void**)&pixels, NULL, 0
116  );
117 }
118 
120 {
121  int fps;
122  WCHAR fps_buf[11] = { 0 };
123  LARGE_INTEGER sta, fin, frq;
124  QueryPerformanceCounter(&sta);
125 
126 #pragma omp parallel for
127  for (int i = 0; i < this->width; i++)
128  {
129  for (int j = 0; j < this->height; j++)
130  {
131  this->pixels[j*this->width + i] = 0x00586bab;
132  }
133  }
134 
135 #pragma omp parallel for
136  for (int i = 0; i < 3; i++)
137  {
138  for (int j = 0; j < this->height; j++)
139  {
140  this->pixels[j*this->width + i] = 0x00a0aacf;
141  }
142  }
143 
144  this->toCam();
145 
146  this->render->run(bricks, *this->cam, this->slight);
147 
148  SelectObject(this->hdcMem, this->sBmp);
149  BitBlt(this->hdc, X, Y, this->width, this->height, this->hdcMem, 0, 0, SRCCOPY);
150 
151  QueryPerformanceCounter(&fin);
152  QueryPerformanceFrequency(&frq);
153 
154  long double dif = long double(fin.QuadPart - sta.QuadPart) / frq.QuadPart;
155  fps = 1.0 / dif;
156 
157  wsprintf(fps_buf, TEXT(" FPS: %d"), fps);
158  TextOut(this->hdc, this->X + 25, this->height - 50, (LPCWSTR)fps_buf, 11);
159 }
160 
161 void Scene::AddBrick(Brick brick, int X, int Y, int Z, COLORREF color)
162 {
163  Brick* nbrick = new Brick(brick);
164  nbrick->color = color;
165 
166 #pragma omp parallel for
167  for (int vertexIndex = 0; vertexIndex < nbrick->vertexCount(); vertexIndex++)
168  {
169  Vertex v = nbrick->vertex[vertexIndex];
170 
171  int nX = v.X + 1. - nbrick->center.X + X;
172  int nY = v.Y + 1. - nbrick->center.Y + Y;
173  int nZ = v.Z + 1. - nbrick->center.Z + Z;
174 
175  nbrick->vertex[vertexIndex].X = nX;
176  nbrick->vertex[vertexIndex].Y = nY;
177  nbrick->vertex[vertexIndex].Z = nZ;
178  }
179 
180  Vertex center(X, Y, Z);
181  nbrick->center = center;
182 
183  this->bricks->add(nbrick);
184 }
185 
187 {
188  int xCenter = this->width / 2;
189  int yCenter = this->height / 2;
190 
191  GMatrix view = this->cam->cameraview();
192 
193  GMatrix nview = view;
194  nview.transposition();
195  nview.inverse();
196 
197  this->slight = this->light;// *view;
198 
199  this->slight.X += this->width / 2;
200  this->slight.Y += this->height / 2;
201 
202  for (int brickIndex = 0; brickIndex < this->bricks->objects.size(); brickIndex++)
203  {
204  Brick* nbrick = this->bricks->objects[brickIndex];
205 #pragma omp parallel for
206  for (int vertexIndex = 0; vertexIndex < nbrick->vertexCount(); vertexIndex++)
207  {
208  Vertex tmpVertex = nbrick->vertex[vertexIndex];
209  tmpVertex = tmpVertex * view;
210  tmpVertex.X = tmpVertex.X + xCenter;
211  tmpVertex.Y = tmpVertex.Y + yCenter;
212 
213  nbrick->svertex[vertexIndex] = tmpVertex;
214  }
215 
216 #pragma omp parallel for
217  for (int faceIndex = 0; faceIndex < nbrick->facesCount(); faceIndex++)
218  {
219  for (int i = 0; i < 3; i++)
220  {
221  GVector tmpN(nbrick->VNormal[faceIndex][i]);
222  //tmpN = tmpN * nview;
223  nbrick->sVNormal[faceIndex][i] = tmpN;
224  }
225  }
226  }
227 
228 }
Camera * cam
Definition: scene.h:69
double X
Definition: vertex.h:90
void run(Composite *bricks, Camera cam, Vertex light)
Definition: render.cpp:21
Contains loaded bricks.
Definition: composite.h:23
Definition: render.h:6
Define vertex consisting 3 double coordinates.
Definition: vertex.h:24
void AddBrick(Brick brick, int X, int Y, int Z, COLORREF color)
Definition: scene.cpp:161
COLORREF color
Definition: brick.h:105
~Scene()
Definition: scene.cpp:83
Define geometric vector.
Definition: gvector.h:26
Brick archetecture.
Definition: brick.h:25
void DrawScene()
Definition: scene.cpp:119
Camera object.
Definition: camera.h:23
double Z
Definition: vertex.h:92
Scene()
Definition: scene.cpp:5
GMatrix cameraview()
Definition: camera.cpp:52
vector< vector< Normal > > VNormal
Definition: brick.h:98
vector< vector< Normal > > sVNormal
Definition: brick.h:99
vector< Brick * > objects
Definition: composite.h:59
double Y
Definition: vertex.h:91
int facesCount()
Definition: brick.cpp:87
void add(Brick *obj)
Definition: composite.cpp:15
Composite * bricks
Definition: scene.h:68
bool inverse()
Definition: gmatrix.cpp:96
Define geometric matrix.
Definition: gmatrix.h:24
Vertex light
Definition: scene.h:70
vector< Vertex > svertex
Definition: brick.h:96
Vertex center
Definition: brick.h:93
void toCam()
Definition: scene.cpp:186
vector< Vertex > vertex
Definition: brick.h:95
GMatrix transposition()
Definition: gmatrix.cpp:171
Vertex slight
Definition: scene.h:71
int vertexCount()
Definition: brick.cpp:82