My Project
render.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "../stdafx.h"
3 #include "render.h"
4 
5 Render::Render(unsigned long* pixels, int height, int width)
6 {
7  this->pixels = pixels;
8  this->height = height;
9  this->width = width;
10  this->zbuffer = new int[this->width * this->height];
11  if (!zbuffer)
12  {
13  throw AllocationMemoryError();
14  }
15 }
16 
18 {
19 }
20 
21 void Render::run(Composite* bricks, Camera cam, Vertex light)
22 {
23 #pragma omp parallel
24  for (int i = 0; i<this->width * this->height; i++) {
25  this->zbuffer[i] = -9999999;
26  }
27 
28 
29  for (int brickIndex = 0; brickIndex < bricks->objects.size(); brickIndex++)
30  {
31  Brick* brick = bricks->objects[brickIndex];
32  COLORREF color = brick->color;
33 
34 #pragma omp parallel for schedule(dynamic, 1)
35  for (int faceIndex = 0; faceIndex < brick->facesCount(); faceIndex++)
36  {
37  Face face = brick->faces[faceIndex];
38 
39  Vertex A(brick->svertex[face.A() - 1]);
40  Vertex B(brick->svertex[face.B() - 1]);
41  Vertex C(brick->svertex[face.C() - 1]);
42 
43  Normal nA = brick->sVNormal[faceIndex][0];
44  Normal nB = brick->sVNormal[faceIndex][1];
45  Normal nC = brick->sVNormal[faceIndex][2];
46  if (faceIndex == 43)
47  {
48  int Aaa = 0;
49  Aaa++;
50  int Bbb = Aaa;
51  }
52 
53  this->fillFaces(A, B, C, nA, nB, nC, color, light, cam);
54  }
55  }
56 }
57 
58 void Render::line(int x0, int y0, int x1, int y1)
59 {
60  bool step = false;
61  if (abs(x0 - x1) < abs(y0 - y1)) {
62  std::swap(x0, y0);
63  std::swap(x1, y1);
64  step = true;
65  }
66 
67  if (x0 > x1) {
68  std::swap(x0, x1);
69  std::swap(y0, y1);
70  }
71 
72  int dx = x1 - x0;
73  int dy = y1 - y0;
74  int derror2 = abs(dy) * 2;
75  int error2 = 0;
76 
77  int y = y0;
78 
79  for (int x = x0; x <= x1; x++) {
80  if (step) {
81  this->pixels[x*this->width + y] = 0x00ffffff;
82  }
83  else {
84  this->pixels[y*this->width + x] = 0x00ffffff;
85  }
86  error2 += derror2;
87 
88  if (error2 > dx) {
89  y += (y1 > y0 ? 1 : -1);
90  error2 -= dx * 2;
91  }
92  }
93 }
94 
95 void Render::fillFaces(Vertex A, Vertex B, Vertex C, Normal normA, Normal normB, Normal normC, COLORREF color, Vertex light, Camera cam)
96 {
97  if (A.Y == B.Y && A.Y == C.Y) return;
98 
99  if (A.Y > B.Y) { std::swap(A, B); std::swap(normA, normB); }
100  if (A.Y > C.Y) { std::swap(A, C); std::swap(normA, normC); }
101  if (B.Y > C.Y) { std::swap(B, C); std::swap(normB, normC); }
102 
103  if (int(A.Y + .5) == int(B.Y + .5) && B.X < A.X) { std::swap(A, B); std::swap(normA, normB); }
104 
105  int x1 = int(A.X + .5);
106  int x2 = int(B.X + .5);
107  int x3 = int(C.X + .5);
108  int y1 = int(A.Y + .5);
109  int y2 = int(B.Y + .5);
110  int y3 = int(C.Y + .5);
111  int z1 = int(A.Z + .5);
112  int z2 = int(B.Z + .5);
113  int z3 = int(C.Z + .5);
114 
115  if ((y1 == y2) && (x1 > x2)) { std::swap(A, B); std::swap(normA, normB); }
116  x1 = int(A.X + .5);
117  x2 = int(B.X + .5);
118  y1 = int(A.Y + .5);
119  y2 = int(B.Y + .5);
120 
121  double dx13 = 0, dx12 = 0, dx23 = 0;
122  double dz13 = 0, dz12 = 0, dz23 = 0;
123  Normal dn13;
124  Normal dn12;
125  Normal dn23;
126 
127  if (y3 != y1)
128  {
129  dz13 = (z3 - z1) / (double)(y3 - y1);
130  dx13 = (x3 - x1) / (double)(y3 - y1);
131  dn13 = (normC - normA) / (y3 - y1);
132  }
133  if (y2 != y1)
134  {
135  dz12 = (z2 - z1) / (double)(y2 - y1);
136  dx12 = (x2 - x1) / (double)(y2 - y1);
137  dn12 = (normB - normA) / (double)(y2 - y1);
138  }
139  if (y3 != y2)
140  {
141  dz23 = (z3 - z2) / (double)(y3 - y2);
142  dx23 = (x3 - x2) / (double)(y3 - y2);
143  dn23 = (normC - normB) / (double)(y3 - y2);
144  }
145 
146  double z = 0;
147  double dz = 0;
148 
149  Normal normP;
150  Normal dnorm;
151 
152  double wx1 = x1;
153  double wx2 = x1;
154  double wz1 = z1;
155  double wz2 = z1;
156  Normal wn1(normA);
157  Normal wn2(normA);
158 
159  Normal _dn13(dn13);
160  double _dx13 = dx13;
161  double _dz13 = dz13;
162 
163  if (dx13 > dx12)
164  {
165  std::swap(dn13, dn12);
166  std::swap(dx13, dx12);
167  std::swap(dz13, dz12);
168  }
169 
170  if (y1 == y2) {
171  wx1 = x1;
172  wx2 = x2;
173  wz1 = z1;
174  wz2 = z2;
175  wn1 = normA;
176  wn2 = normB;
177  }
178 
179  if (_dx13 < dx23)
180  {
181  std::swap(_dn13, dn23);
182  std::swap(_dx13, dx23);
183  std::swap(_dz13, dz23);
184  }
185 
186  for (int yCoord = y1; yCoord < y3; yCoord++)
187  {
188  z = wz1;
189  normP = wn1;
190 
191  if (wx1 != wx2)
192  {
193  dz = (wz2 - wz1) / (double)(wx2 - wx1);
194  dnorm = (wn2 - wn1) / (double)(wx2 - wx1);
195  }
196 
197  for (int xCoord = wx1; xCoord < wx2; xCoord++)
198  {
199  int pix = yCoord * this->width + xCoord;
200  if (pix >= 0 && pix <= this->width * this->height)
201  {
202  if (this->zbuffer[pix] < z)
203  {
204  double I = this->intencity(xCoord, yCoord, z, normP, light, cam);
205  if (this->zbuffer[pix] < z)
206  {
207  this->zbuffer[pix] = z;
208  this->pixels[pix] = RGB(GetBValue(color) * I, GetGValue(color) * I, GetRValue(color) * I);
209  }
210  }
211  }
212  z += dz;
213  normP = normP + dnorm;
214  }
215  if (yCoord < y2)
216  {
217  wx1 += dx13;
218  wx2 += dx12;
219  wz1 += dz13;
220  wz2 += dz12;
221  wn1 = wn1 + dn13;
222  wn2 = wn1 + dn12;
223  }
224  else
225  {
226  wx1 += _dx13;
227  wx2 += dx23;
228  wz1 += _dz13;
229  wz2 += dz23;
230  wn1 = wn1 + _dn13;
231  wn2 = wn1 + dn23;
232  }
233  }
234 }
235 
236 double Render::intencity(double X, double Y, double Z, GVector N, Vertex light, Camera cam)
237 {
238  GVector D(X - light.X, Y - light.Y, Z - light.Z, 1);
239  D.normalize();
240  N.normalize();
241  double I;
242  double Iconst = 0.20;
243  double Idiff = 0.60 * max(0, GVector::scalar(N, D));
244 
245  GVector v(cam.position);
246  v.normalize();
247  GVector h(v);
248  h = h + D;
249  h.normalize();
250  double Iblinn = 0.20 * pow(max(0, GVector::scalar(h, N)), 1000);
251 
252  I = Iconst + Idiff + Iblinn;
253  return I;
254 }
double X
Definition: vertex.h:90
int C()
Definition: face.cpp:72
void run(Composite *bricks, Camera cam, Vertex light)
Definition: render.cpp:21
Contains loaded bricks.
Definition: composite.h:23
Define vertex consisting 3 double coordinates.
Definition: vertex.h:24
GVector normalize()
Definition: gvector.cpp:115
COLORREF color
Definition: brick.h:105
Define face consisting 3 links to vetices.
Definition: face.h:25
Define geometric vector.
Definition: gvector.h:26
Define normal (object container for GVector)
Definition: normal.h:24
Brick archetecture.
Definition: brick.h:25
Camera object.
Definition: camera.h:23
double Z
Definition: vertex.h:92
GVector position
Definition: camera.h:57
vector< vector< Normal > > sVNormal
Definition: brick.h:99
vector< Brick * > objects
Definition: composite.h:59
int A()
Definition: face.cpp:62
double Y
Definition: vertex.h:91
int facesCount()
Definition: brick.cpp:87
~Render()
Definition: render.cpp:17
int B()
Definition: face.cpp:67
static double scalar(GVector first, GVector second)
Definition: gvector.cpp:143
Render(unsigned long *pixels, int height, int width)
Definition: render.cpp:5
vector< Vertex > svertex
Definition: brick.h:96
vector< Face > faces
Definition: brick.h:103