-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDriver.cpp
185 lines (162 loc) · 5.9 KB
/
Driver.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "Driver.hpp"
Driver::Driver()
: Driver{ 400, 300, 3 }
{
}
Driver::Driver(int width, int height, int scale)
: width{ width }, height{ height }, scale{ scale },
windowWidth{ width * scale }, windowHeight{ height * scale }
{
}
void Driver::start()
{
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "3D Software Renderer", sf::Style::Close);
window.setMouseCursorVisible(false);
Raster raster{ width, height };
sf::Texture texture;
texture.create(width, height);
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setScale(scale, scale);
Renderer renderer{ &raster };
Mesh* bricks = Mesh::loadFromFile("bricks.obj", Mesh::Shading::KEEP_NORMALS);
Raster bricksTex{ 728, 473 };
sf::Image image;
image.loadFromFile("bricks.jpg");
bricksTex.loadFromBuffer(image.getPixelsPtr());
Camera camera(false, 1.57, width / (double) height, 0.1, Vector3{ 0.0, 0.0, 5.0 });
std::vector<LightSource> lights;
lights.push_back(LightSource{ AmbientLight{ Vector3{ 0.4, 0.4, 0.5 } } });
lights.push_back(LightSource{ DirectionalLight{ Vector3{ 0.6, 0.6, 0.8 }, Vector3{ 1.0, -1.0, -1.0 } } });
sf::Clock timer;
sf::Time lastTime = timer.getElapsedTime();
bool cursorFree = true;
bool wDown = false;
bool aDown = false;
bool sDown = false;
bool dDown = false;
bool qDown = false;
bool eDown = false;
double dmx;
double dmy;
sf::Mouse::setPosition(sf::Vector2i{ windowWidth / 2, windowHeight / 2 }, window);
sf::Time fpsTimer;
int frames = 0;
while (window.isOpen())
{
sf::Time now = timer.getElapsedTime();
sf::Time delta = now - lastTime;
lastTime = now;
frames++;
fpsTimer += delta;
while (fpsTimer.asSeconds() >= 1.0)
{
std::cout << "FPS: " << frames << std::endl;
frames = 0;
fpsTimer -= sf::seconds(1.0);
}
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::W)
wDown = true;
if (event.key.code == sf::Keyboard::A)
aDown = true;
if (event.key.code == sf::Keyboard::S)
sDown = true;
if (event.key.code == sf::Keyboard::D)
dDown = true;
if (event.key.code == sf::Keyboard::Q)
qDown = true;
if (event.key.code == sf::Keyboard::E)
eDown = true;
if (event.key.code == sf::Keyboard::Escape)
window.close();
}
if (event.type == sf::Event::KeyReleased)
{
if (event.key.code == sf::Keyboard::W)
wDown = false;
if (event.key.code == sf::Keyboard::A)
aDown = false;
if (event.key.code == sf::Keyboard::S)
sDown = false;
if (event.key.code == sf::Keyboard::D)
dDown = false;
if (event.key.code == sf::Keyboard::Q)
qDown = false;
if (event.key.code == sf::Keyboard::E)
eDown = false;
}
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
{
cursorFree = !cursorFree;
if (!cursorFree)
sf::Mouse::setPosition(sf::Vector2i{ windowWidth / 2, windowHeight / 2 }, window);
}
}
if (!cursorFree)
{
dmx = sf::Mouse::getPosition(window).x - windowWidth / 2;
dmy = sf::Mouse::getPosition(window).y - windowHeight / 2;
sf::Mouse::setPosition(sf::Vector2i{ windowWidth / 2, windowHeight / 2 }, window);
window.setMouseCursorVisible(false);
}
else
{
window.setMouseCursorVisible(true);
}
double camSpeed = 3.0;
if (wDown)
{
Vector3 moveVec = camera.getFrontVec();
moveVec.scl(camSpeed * delta.asSeconds());
camera.translate(moveVec);
}
if (aDown)
{
Vector3 moveVec = camera.getRightVec();
moveVec.scl(-camSpeed * delta.asSeconds());
camera.translate(moveVec);
}
if (sDown)
{
Vector3 moveVec = camera.getFrontVec();
moveVec.scl(-camSpeed * delta.asSeconds());
camera.translate(moveVec);
}
if (dDown)
{
Vector3 moveVec = camera.getRightVec();
moveVec.scl(camSpeed * delta.asSeconds());
camera.translate(moveVec);
}
if (qDown)
{
Vector3 moveVec = Vector3{ 0.0, 1.0, 0.0 };
moveVec.scl(-camSpeed * delta.asSeconds());
camera.translate(moveVec);
}
if (eDown)
{
Vector3 moveVec = Vector3{ 0.0, 1.0, 0.0 };
moveVec.scl(camSpeed * delta.asSeconds());
camera.translate(moveVec);
}
double camRotSpeed = 0.5;
camera.rotateYaw(camRotSpeed * -dmx * 0.016);
camera.rotatePitch(camRotSpeed * -dmy * 0.016);
renderer.clearColorDepth(Color{ 0, 0, 0, 255 });
Combined c;
renderer.renderMesh(*bricks, bricksTex, c, camera, lights, Renderer::Lighting::DIFFUSE);
texture.update(raster.getData());
window.clear(sf::Color::Black);
window.draw(sprite);
window.display();
}
delete bricks;
}