-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleApplication1.cpp
More file actions
71 lines (66 loc) · 2.12 KB
/
ConsoleApplication1.cpp
File metadata and controls
71 lines (66 loc) · 2.12 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
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include "VecFunctions.h"
void SetWindow(int Width, int Height)
{
_COORD coord;
coord.X = Width;
coord.Y = Height;
_SMALL_RECT Rect;
Rect.Top = 0;
Rect.Left = 0;
Rect.Bottom = Height - 1;
Rect.Right = Width - 1;
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleScreenBufferSize(Handle, coord);
SetConsoleWindowInfo(Handle, TRUE, &Rect);
}
int main() {
int width = 120 ;
int height = 30 ;
SetWindow(width, height);
float aspect = (float)width / height;
float pixelAspect = 11.0f / 24.0f;
char gradient[] = " .:;/=123456789@";
int gradientSize = std::size(gradient) - 2;
wchar_t* screen = new wchar_t[width * height];
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
DWORD dwBytesWritten = 0;
for (int t = 0; t < 10000; t++) {
vec3 light = norm(vec3(-0.5, 0.5, -5.0));
vec3 boxPos = vec3(0, 1, 0);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
vec2 uv = vec2(i, j) / vec2(width, height) * 2.0f - 1.0f;uv.x *= aspect * pixelAspect;
vec3 ro = vec3(-6, 0, 0);vec3 rd = norm(vec3(2, uv));
ro = rotateY(ro, 0.25); rd = rotateY(rd, 0.25); ro = rotateZ(ro, t * 0.01); rd = rotateZ(rd, t * 0.01);
float diff = 1;
for (int k = 0; k < 5; k++) {
float minIt = 99999;
vec3 boxN = 0;
vec2 intersection = box(ro-boxPos, rd, 1, boxN);
vec3 n = 0;
float albedo = 1;
if (intersection.x > 0 && intersection.x < minIt) {
minIt = intersection.x;
n = boxN;
}
if (minIt < 99999) {
diff *= (dot(n, light) * 0.5 + 0.5) * albedo;
ro = ro + rd * (minIt - 0.01);
}
else break;
}
int color = (int)(diff * 20);
color = clamp(color, 0, gradientSize);
char pixel = gradient[color];
screen[i + j * width] = pixel;
}
}
screen[width * height - 1] = '\0';
WriteConsoleOutputCharacter(hConsole, screen, width * height, { 0, 0 }, &dwBytesWritten);
}
}