-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
280 lines (238 loc) · 8.94 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
///
/// \file main.cpp
/// \brief SDL Demo Code
/// \author Kairong Jiang <[email protected]>
/// \date 03/11/18
///
///
/*
***********************************************************************
Copyright (C) 2018, Kairong Jiang
University of Arizona
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
***********************************************************************
*/
//include SDL2 libraries
#include <SDL.h>
//C++ includes
#include <iostream>
#include <fstream>
#include <cfloat>
#include <cmath>
#include <vector>
#include <algorithm>
#include "Renderer.h"
using namespace std;
///
/// Log an SDL error with some error message to the output stream of our
/// choice
///
/// \param os The output stream to write the message to
/// \param msg The error message to write, SDL_GetError() appended to it
///
void logSDLError(std::ostream &os, const std::string &msg) {
os << msg << " error: " << SDL_GetError() << std::endl;
}
///
/// Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
/// the texture's width and height
///
/// \param tex The source texture we want to draw
/// \param ren The renderer we want to draw to
/// \param x The x coordinate to draw to
/// \param y The y coordinate to draw to
///
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y) {
//Setup the destination rectangle to be at the position we want
SDL_Rect dst;
dst.x = x;
dst.y = y;
//Query the texture to get its width and height to use
SDL_QueryTexture(tex, nullptr, nullptr, &dst.w, &dst.h);
SDL_RenderCopy(ren, tex, nullptr, &dst);
}
///
/// Main function. Initializes an SDL window, renderer, and texture,
/// and then goes into a loop to listen to events and draw the texture.
///
/// \param argc Number of command line arguments
/// \param argv Array of command line arguments
/// \return integer indicating success (0) or failure (nonzero)
///
int main(int argc, char **argv) {
if (argc == 1) {
cout << "please set the relative path of the scene file as the argument of the program." << endl;
return 0;
}
string inputFileName = argv[1];
Renderer rasterizeRenderer(inputFileName);
auto result = rasterizeRenderer.renderForDisplay();
ofstream out;
//Integers specifying the width (number of columns) and height (number
//of rows) of the image
int num_cols = static_cast<int>(result->cols());
int num_rows = static_cast<int>(result->rows());
//Start up SDL and make sure it went ok
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
logSDLError(std::cout, "SDL_Init");
return 1;
}
//Setup our window and renderer
SDL_Window *window = SDL_CreateWindow("Ray Tracer", 100, 100, num_cols, num_rows, SDL_WINDOW_SHOWN);
if (window == nullptr) {
logSDLError(std::cout, "CreateWindow");
SDL_Quit();
return 1;
}
SDL_Renderer
*renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr) {
logSDLError(std::cout, "CreateRenderer");
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
//The textures we'll be using
SDL_Texture *background;
//A raw data array of characters. Each column is drawn using the same
//color that is a grayscale ramp from the leftmost to rightmost pixel.
// unsigned char *data = new unsigned char[num_cols * num_rows * 3];
// //r is row, c is column, and ch is channel
// for (int r = 0; r < num_rows; r++) {
// for (int c = 0; c < num_cols; c++) {
// for (int ch = 0; ch < 3; ch++) {
// data[3 * (r * num_cols + c) + ch] = 255 * float(c) / num_cols;
// }
// }
// }
auto *data = ImageUtils::getRawData(*result);
//Initialize the texture. SDL_PIXELFORMAT_RGB24 specifies 3 bytes per
//pixel, one per color channel
background =
SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STATIC, num_cols, num_rows);
//Copy the raw data array into the texture.
SDL_UpdateTexture(background, nullptr, data, 3 * num_cols);
if (background == nullptr) {
logSDLError(std::cout, "CreateTextureFromSurface");
}
//Make sure they both loaded ok
if (background == nullptr) {
SDL_DestroyTexture(background);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
//Variables used in the rendering loop
SDL_Event event;
bool quit = false;
bool leftMouseButtonDown = false;
int start_mouseX;
int start_mouseY;
float orig_x_angle;
float orig_y_angle;
while (!quit) {
//Grab the time for frame rate computation
const Uint64 start = SDL_GetPerformanceCounter();
//Clear the screen
SDL_RenderClear(renderer);
//Event Polling
//This while loop responds to mouse and keyboard commands.
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
//Use number input to select which clip should be drawn
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:break;
case SDLK_f:rasterizeRenderer.setShadingPolicy(Renderer::FLAT_SHADING);
delete[] data;
result = rasterizeRenderer.renderForDisplay();
data = ImageUtils::getRawData(*result);
break;
case SDLK_g:rasterizeRenderer.setShadingPolicy(Renderer::GOURAUD_SHADING);
delete[] data;
result = rasterizeRenderer.renderForDisplay();
data = ImageUtils::getRawData(*result);
break;
case SDLK_p:rasterizeRenderer.setShadingPolicy(Renderer::PHONG_SHADING);
delete[] data;
result = rasterizeRenderer.renderForDisplay();
data = ImageUtils::getRawData(*result);
break;
case SDLK_i:break;
case SDLK_n:break;
case SDLK_m:break;
case SDLK_b:break;
case SDLK_s:cout << "saving image to ppm" << endl;
out.open(inputFileName + ".ppm", std::ios::out | std::ios::trunc | std::ios::binary);
out << "P6" << std::endl;
out << std::to_string(num_cols).data() << " " << std::to_string(num_rows).data() << std::endl;
out << std::to_string(255).data() << std::endl;
for (int i = 0; i < num_rows; i++) {
for (int j = 0; j < num_cols; j++) {
out << (*result)(i, j)(0);
out << (*result)(i, j)(1);
out << (*result)(i, j)(2);
}
}
out.close();
break;
default:break;
}
} else if (event.type == SDL_MOUSEBUTTONUP) {
if (event.button.button == SDL_BUTTON_LEFT)
leftMouseButtonDown = false;
} else if (event.type == SDL_MOUSEBUTTONDOWN) {
if (event.button.button == SDL_BUTTON_LEFT) {
leftMouseButtonDown = true;
}
} else if (event.type == SDL_MOUSEMOTION) {
if (leftMouseButtonDown) {
int mouseX = event.motion.x;
int mouseY = event.motion.y;
data[3 * (mouseY * num_cols + mouseX) + 0] = 255;
data[3 * (mouseY * num_cols + mouseX) + 1] = 0;
data[3 * (mouseY * num_cols + mouseX) + 2] = 0;
}
}
}
//Update the texture, assuming data has changed.
SDL_UpdateTexture(background, nullptr, data, 3 * num_cols);
//display the texture on the screen
renderTexture(background, renderer, 0, 0);
//Update the screen
SDL_RenderPresent(renderer);
//Display the frame rate to stdout
const Uint64 end = SDL_GetPerformanceCounter();
const static Uint64 freq = SDL_GetPerformanceFrequency();
const double seconds = (end - start) / static_cast< double >( freq );
//You may want to comment this line out for debugging purposes
// std::cout << "Frame time: " << seconds * 1000.0 << "ms" << std::endl;
}
//After the loop finishes (when the window is closed, or escape is
//pressed, clean up the data that we alloc/**/ated.
delete[] data;
SDL_DestroyTexture(background);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}