-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
100 lines (84 loc) · 2.92 KB
/
main.c
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
//
// main.c
// raycast
//
// Created by Liam Westby on 2/6/13.
// This program raycasts an image given a scene file using either perspective or parallel projections.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "ppm.h"
#include "vector.h"
#include "light.h"
#include "object.h"
#include "raycast.h"
#define WORLD_WIDTH 0.5
#define WORLD_HEIGHT 0.3125
void print_usage();
int main(int argc, const char * argv[])
{
long startTime = time(NULL);
if (argc < 4) {
// Requires 3 arguments: l/v, width of image, and output file.
print_usage();
return 0;
}
// Get parallel or perspective
int parallel;
if (argv[1][0] == 'l') parallel = 1;
else if (argv[1][0] == 'v') parallel = 0;
else {
// An invalid option was specified
printf("The parallel/perspective option was invalid.\n");
print_usage();
return 0;
}
// Get the width of the image.
int width = atoi(argv[2]);
if (width == 0) {
// Either they passed in 0 as the width, or atoi failed. Regardless, that won't work.
printf("The given width was invalid.\n");
print_usage();
return 0;
}
// Get the output file, for writing at the end.
char *output_path = argv[3];
// Assemble the image in memory.
printf("Assemble the image in memory...\n");
ppm_image *the_image = (ppm_image*)malloc(sizeof(ppm_image));
the_image->width = width;
the_image->height = width * 5 / 8;
the_image->maxval = 255;
the_image->data = (pixel**)malloc(sizeof(pixel*)*the_image->height);
for (int i = 0; i < the_image->height; i++) {
the_image->data[i] = (pixel*)malloc(sizeof(pixel)*the_image->width);
}
// Set up the scene
printf("Set up the scene...\n");
object **objects = get_objects();
point_light **lights = get_lights();
//Raycast the image
printf("Raycast the image...\n");
if (parallel) {
float *direction = (float*)malloc(sizeof(float)*3);
v_init(0.0, 0.0, -1.0, direction);
raycast_parallel(the_image, objects, OBJECTS_COUNT, lights, LIGHTS_COUNT, the_image->width, the_image->height, WORLD_WIDTH, WORLD_HEIGHT, direction);
}
else {
float *origin = (float*)malloc(sizeof(float)*3);
v_init(0.0, 0.0, 0.8, origin);
raycast_perspective(the_image, objects, OBJECTS_COUNT, lights, LIGHTS_COUNT, the_image->width, the_image->height, WORLD_WIDTH, WORLD_HEIGHT, origin);
}
// Write out the image to disk.
printf("Write out the image to disk...\n");
write_image(the_image, output_path);
printf("Completed. Time: %lds\n", (time(NULL) - startTime));
return 0;
}
/* Prints the correct invocation of this program.
*/
void print_usage() {
printf("USAGE: raycast l|v width outputfile.ppm\n");
printf("Where l means parallel, v means perspective, and width is an integer.\n");
}