-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
130 lines (110 loc) · 3.69 KB
/
main.cpp
File metadata and controls
130 lines (110 loc) · 3.69 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
//
// main.cpp
// CppTarget
//
// Created by Suriya Dakshina Murthy on 10/2/25.
//
#include <iostream>
#include <vector>
#include <fstream>
#include <filesystem>
//#include "RenderGaussian.h"
using namespace std;
struct float2 {
float x, y;
float2(float x, float y) : x(x), y(y) {};
float2 operator-(const float2& other) const {
return float2(x-other.x, y-other.y);
}
};
struct float3 {
float x, y, z;
float3() : x(0), y(0), z(0) {};
float3(float x, float y, float z) : x(x), y(y), z(z) {};
float3(float i) : x(i), y(i), z(i) {};
float3 operator*(const float3& other) const {
return float3(x*other.x, y*other.y, z*other.z);
}
float3& operator+=(const float3& other) {
x+=other.x;
y+=other.y;
z+=other.z;
return *this;
}
};
struct gaussian {
float2 mu; // center (x,y)
float sigma; // radius/spread
float3 color; // RGB
float alpha; // opacity weight
gaussian(float2 mu, float sigma, float3 color, float alpha): mu(mu), sigma(sigma), color(color), alpha(alpha) {};
};
float squaredDistance(float2 a) {
return (a.x*a.x) + (a.y*a.y);
}
// Sampling a Gaussian
void splat(const gaussian& gauss, int x, int y, float3& pColor, float& pAlpha) {
float2 pixelCenter = float2(x+0.5f, y+0.5f);
float2 offset = pixelCenter - gauss.mu;
float r2 = squaredDistance(offset);
float gaussianFallOff = expf(-0.5f * r2 / (gauss.sigma*gauss.sigma));
pAlpha = gauss.alpha * gaussianFallOff; // per-pixel alpha
pColor = gauss.color * pAlpha; // pre-multiplied color with alpha
return;
}
vector<float3> renderGaussian(gaussian gauss, int width, int height) {\
vector<float3> accumRGB(width*height, float3(0));
vector<float> accumAlpha(width*height, float(0));
float3 pixelColor;
float pixelAlpha;
for(int j=0; j<height; j++) {
for(int i=0; i<width; i++) {
splat(gauss, i, j, pixelColor, pixelAlpha);
accumRGB[j*width + i] += pixelColor;
accumAlpha[i*width + i] += pixelAlpha;
}
}
return accumRGB;
}
void writePPM(const std::vector<float3>& img, int W, int H, const std::string& filename)
{
std::ofstream f(filename, std::ios::binary);
f << "P6\n" << W << " " << H << "\n255\n";
for (const auto& c : img) {
unsigned char rgb[3] = {
(unsigned char)(255 * std::clamp(c.x, 0.0f, 1.0f)),
(unsigned char)(255 * std::clamp(c.y, 0.0f, 1.0f)),
(unsigned char)(255 * std::clamp(c.z, 0.0f, 1.0f))
};
f.write((char*)rgb, 3);
}
}
int main(int argc, const char * argv[]) {
//renderGaussian();
int WIDTH = 500, HEIGHT = 500;
gaussian firstGaussian(float2(250, 250), 100, float3(1.0, 0.05, 0.05), 0.5);
auto image = renderGaussian(firstGaussian, WIDTH, HEIGHT);
writePPM(image, WIDTH, HEIGHT, "gaussian.ppm");
std::cout << "Current working directory: "
<< std::filesystem::current_path() << "\n";
cout << "image written";
return 0;
}
/* Reference: Triangle - Raterization:
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
// 1. Compute barycentrics for pixel (x + 0.5, y + 0.5)
float alpha, beta, gamma;
computeBarycentrics(A, B, C, x + 0.5f, y + 0.5f, alpha, beta, gamma);
// 2. Check inside triangle
if (alpha >= 0 && beta >= 0 && gamma >= 0) {
// 3. Interpolate attributes
vec3 color = alpha * colorA + beta * colorB + gamma * colorC;
float depth = alpha * zA + beta * zB + gamma * zC;
// 4. Draw
framebuffer[y][x] = color;
zbuffer[y][x] = depth;
}
}
}
*/