Skip to content

Commit 79bfe9f

Browse files
committed
Fixed the normals issue by finding the cause in the way the color was stored. Now colors are stored primarly in doubles for each value, then translated to the union when required for writing to the file.
1 parent 64472cd commit 79bfe9f

File tree

6 files changed

+53
-67
lines changed

6 files changed

+53
-67
lines changed

Renderer/Color.cpp

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,74 @@
11
#include "Color.h"
22

3+
void Color::matchRaws()
4+
{
5+
r = R * 255;
6+
g = G * 255;
7+
b = B * 255;
8+
a = A * 255;
9+
}
10+
11+
void Color::matchReals()
12+
{
13+
R = (double)(r) / 255.0;
14+
G = (double)(g) / 255.0;
15+
B = (double)(b) / 255.0;
16+
A = (double)(a) / 255.0;
17+
}
18+
19+
320
Color::Color()
421
{
522
value = 0;
623
bytesPerColor = 1;
724
}
825

26+
927
Color::Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
1028
{
1129
this->r = r;
1230
this->g = g;
1331
this->b = b;
1432
this->a = a;
1533
bytesPerColor = 4;
34+
matchReals();
1635
}
1736

1837
Color::Color(double r, double g, double b, double a)
1938
{
20-
SetR(r);
21-
SetG(g);
22-
SetB(b);
23-
SetA(a);
39+
R = r;
40+
G = g;
41+
B = b;
42+
A = a;
2443
bytesPerColor = 4;
44+
matchRaws();
2545
}
2646

2747
Color::Color(double r, double g, double b)
2848
{
29-
SetR(r);
30-
SetG(g);
31-
SetB(b);
32-
a = 255;
49+
R = r;
50+
G = g;
51+
B = b;
52+
A = 1;
3353
bytesPerColor = 4;
54+
matchRaws();
3455
}
3556

3657
Color::Color(int val, int bpc)
3758
{
3859
value = val;
3960
bytesPerColor = bpc;
61+
matchReals();
4062
}
4163

4264
Color::Color(const Color& c)
4365
{
44-
value = c.value;
66+
R = c.R;
67+
G = c.G;
68+
B = c.B;
69+
A = c.A;
4570
bytesPerColor = c.bytesPerColor;
71+
matchRaws();
4672
}
4773

4874
Color::Color(const unsigned char* p, int bpc)
@@ -53,59 +79,22 @@ Color::Color(const unsigned char* p, int bpc)
5379
{
5480
raw[i] = p[i];
5581
}
82+
matchReals();
5683
}
5784

5885
unsigned char* Color::GetRaw()
5986
{
87+
matchRaws();
6088
return raw;
6189
}
6290

6391
void Color::SetRaw(unsigned char raw[])
6492
{
65-
for (int i = 0; i < bytesPerColor; i++)
93+
for (int i = 0; i < 4; i++)
6694
{
6795
this->raw[i] = raw[i];
6896
}
69-
}
70-
71-
double Color::GetR() const
72-
{
73-
return (double)(r)/255.0f;
74-
}
75-
76-
double Color::GetG() const
77-
{
78-
return (double)(g) / 255.0f;
79-
}
80-
81-
double Color::GetB() const
82-
{
83-
return (double)(b) / 255.0f;
84-
}
85-
86-
double Color::GetA() const
87-
{
88-
return (double)(a) / 255.0f;
89-
}
90-
91-
void Color::SetR(double r)
92-
{
93-
this->r = r * 255;
94-
}
95-
96-
void Color::SetG(double g)
97-
{
98-
this->g = g * 255;
99-
}
100-
101-
void Color::SetB(double b)
102-
{
103-
this->b = b * 255;
104-
}
105-
106-
void Color::SetA(double a)
107-
{
108-
this->a = a * 255;
97+
matchReals();
10998
}
11099

111100
Color& Color::operator=(const Color& c)
@@ -121,11 +110,11 @@ Color& Color::operator=(const Color& c)
121110

122111
Color Color::operator*(double t)
123112
{
124-
return Color(GetR()*t, GetG()*t, GetB()*t);
113+
return Color(R*t, G*t, B*t);
125114
}
126115

127116
Color Color::operator+(const Color& obj)
128117
{
129-
return Color(GetR() + obj.GetR(), GetG() + obj.GetG(), GetB() + obj.GetB());
118+
return Color(R + obj.R, G + obj.G, B + obj.B);
130119
}
131120

Renderer/Color.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class Color
1616
unsigned int value;
1717
};
1818

19+
void matchRaws();
20+
void matchReals();
21+
1922
public:
2023
Color();
2124
Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
@@ -25,19 +28,14 @@ class Color
2528
Color(const Color& c);
2629
Color(const unsigned char* p, int bpc);
2730

31+
double R;
32+
double G;
33+
double B;
34+
double A;
35+
2836
unsigned char* GetRaw();
2937
void SetRaw(unsigned char raw[]);
3038

31-
double GetR() const;
32-
double GetG() const;
33-
double GetB() const;
34-
double GetA() const;
35-
36-
void SetR(double r);
37-
void SetG(double g);
38-
void SetB(double b);
39-
void SetA(double a);
40-
4139
Color& operator =(const Color &c);
4240
Color operator*(double t);
4341
Color operator+(const Color& obj);

Renderer/Ray.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Ray::Ray(const Vector3& origin, const Vector3& direction)
1313

1414
Vector3 Ray::At(double t) const
1515
{
16-
return origin + direction*t;
16+
return origin + (direction*t);
1717
}
1818

1919
Vector3 Ray::GetOrigin() const

Renderer/Renderer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ int main()
7070
Vector3 pixelDeltaV = viewportV / imageHeight;
7171

7272
//Calc location of upper left pixel
73-
Vector3 viewportUpperLeft = cameraCenter - Vector3(0, 0, focalLength) - viewportU / 2 - viewportV / 2;
74-
Vector3 pixel0Center = viewportUpperLeft + (pixelDeltaU + pixelDeltaV) * 0.5;
73+
Vector3 viewportUpperLeft = cameraCenter - Vector3(0, 0, focalLength) - (viewportU / 2) - (viewportV / 2);
74+
Vector3 pixel0Center = viewportUpperLeft + ((pixelDeltaU + pixelDeltaV) * 0.5);
7575

7676
//Render
7777
for (int y = 0; y < imageHeight; y++)

Renderer/Vector3.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ Vector3 Vector3::Cross(const Vector3& v)
4343

4444
Vector3 Vector3::Unit()
4545
{
46-
double length = Length();
47-
return Vector3(X/length, Y/length, Z/length);
46+
return *this / Length();
4847
}
4948

5049
Vector3 Vector3::operator-() const

Renderer/out.tga

-61.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)