-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.h
More file actions
155 lines (130 loc) · 3.01 KB
/
Utility.h
File metadata and controls
155 lines (130 loc) · 3.01 KB
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
#pragma once
#include <algorithm>
#include "MathHelper.h"
#include "LodePNG\lodepng.h"
enum RenderTechnique
{
POINTGRAPH,
WIREFRAME,
RASTER,
RAYTRACE
};
enum Shader
{
NOSHADER,
FLAT,
SMOOTH,
PHONGFLAT,
PHONGSMOOTH
};
enum Sampler
{
NOSAMPLE,
POINTSAMPLE
};
enum RasterizationTechnique
{
BARYCENTRIC,
SCANLINE
};
class Color
{
public:
unsigned char r;
unsigned char g;
unsigned char b;
Color() : r(0), g(0), b(0)
{
}
Color(unsigned char r, unsigned char g, unsigned char b)
{
Color::r = r;
Color::g = g;
Color::b = b;
}
Color operator+(const Color& a)
{
return Color(r + a.r, g + a.g, b + a.b);
}
Color operator-(const Color& a)
{
return Color(r - a.r, g - a.g, b - a.b);
}
Color operator*(const float& a)
{
return Color((char)(r * a), (char)(g * a), (char)(b * a));
}
Color operator*(const Color& a)
{
return Color(r * a.r, g * a.g, b * a.b);
}
};
class Utility
{
public:
// Gets the index in 1 dimensional array of size w * h
static int Index2DTo1D(int x, int y, UINT w)
{
return y * w + x;
}
static float Saturate(float a)
{
if (a < 0.0f)
a = 0.0f;
else if (a > 1.0f)
a = 1.0f;
return a;
}
static Color Saturate(Color color)
{
Color ans = Color(0, 0, 0);
if (color.r > 255)
color.r = 255;
if (color.g > 255)
color.g = 255;
if (color.b > 255)
color.b = 255;
return ans;
}
// Returns true if successful
static bool SaveScreenshot(unsigned char* image, UINT width, UINT height, std::string fileName = "output.png")
{
// Convert Screen Buffer to LodePng compatiable format
std::vector<unsigned char> convertedImage = std::vector<unsigned char>(width * height * 4u);
// image starts from the bottom left going right, up
// convertedImage starts from the top left going right, down
// So we need to flip the y coordinate (hence height - y)
for (UINT y = 0; y < height; y++)
{
for (UINT x = 0; x < width; x++)
{
convertedImage[4 * (width * y + x)] = image[3 * (width * (height - y - 1) + x)] - 50;
convertedImage[4 * (width * y + x) + 1] = image[3 * (width * (height - y - 1) + x) + 1] - 50;
convertedImage[4 * (width * y + x) + 2] = image[3 * (width * (height - y - 1) + x) + 2] - 50;
convertedImage[4 * (width * y + x) + 3] = 255;
}
}
// Saves the image
UINT error = lodepng::encode(fileName.c_str(), convertedImage, width, height);
if (error)
return false;
else
return true;
}
//static Color sampleBilinear(Texture* tex, float2 pos)
//{
// // We first select the 4 nearest neighbors
// int xFloor = (int)pos.x;
// int yFloor = (int)pos.y;
// int xCeil = ceil(pos.x);
// int yCeil = ceil(pos.y);
// Color col1 = tex->GetPixel(xFloor, yFloor);
// Color col2 = tex->GetPixel(xFloor, yCeil);
// Color col3 = tex->GetPixel(xCeil, yFloor);
// Color col4 = tex->GetPixel(xCeil, yCeil);
//
//}
//static Color sampleTrilinear()
//{
//}
};