-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassign3.cpp
executable file
·308 lines (261 loc) · 5.99 KB
/
assign3.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
CSCI 480
Assignment 3 Raytracer
Name: <Your name here>
*/
#include <stdlib.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#include <pic.h>
#include <string.h>
#define MAX_TRIANGLES 2000
#define MAX_SPHERES 10
#define MAX_LIGHTS 10
char *filename=0;
//different display modes
#define MODE_DISPLAY 1
#define MODE_JPEG 2
int mode=MODE_DISPLAY;
//you may want to make these smaller for debugging purposes
#define WIDTH 640
#define HEIGHT 480
//the field of view of the camera
#define fov 60.0
unsigned char buffer[HEIGHT][WIDTH][3];
struct Vertex
{
double position[3];
double color_diffuse[3];
double color_specular[3];
double normal[3];
double shininess;
};
typedef struct _Triangle
{
struct Vertex v[3];
} Triangle;
typedef struct _Sphere
{
double position[3];
double color_diffuse[3];
double color_specular[3];
double shininess;
double radius;
} Sphere;
typedef struct _Light
{
double position[3];
double color[3];
} Light;
Triangle triangles[MAX_TRIANGLES];
Sphere spheres[MAX_SPHERES];
Light lights[MAX_LIGHTS];
double ambient_light[3];
int num_triangles=0;
int num_spheres=0;
int num_lights=0;
void plot_pixel_display(int x,int y,unsigned char r,unsigned char g,unsigned char b);
void plot_pixel_jpeg(int x,int y,unsigned char r,unsigned char g,unsigned char b);
void plot_pixel(int x,int y,unsigned char r,unsigned char g,unsigned char b);
//MODIFY THIS FUNCTION
void draw_scene()
{
unsigned int x,y;
//simple output
for(x=0; x<WIDTH; x++)
{
glPointSize(2.0);
glBegin(GL_POINTS);
for(y=0;y < HEIGHT;y++)
{
plot_pixel(x,y,x%256,y%256,(x+y)%256);
}
glEnd();
glFlush();
}
printf("Done!\n"); fflush(stdout);
}
void plot_pixel_display(int x,int y,unsigned char r,unsigned char g,unsigned char b)
{
glColor3f(((double)r)/256.f,((double)g)/256.f,((double)b)/256.f);
glVertex2i(x,y);
}
void plot_pixel_jpeg(int x,int y,unsigned char r,unsigned char g,unsigned char b)
{
buffer[HEIGHT-y-1][x][0]=r;
buffer[HEIGHT-y-1][x][1]=g;
buffer[HEIGHT-y-1][x][2]=b;
}
void plot_pixel(int x,int y,unsigned char r,unsigned char g, unsigned char b)
{
plot_pixel_display(x,y,r,g,b);
if(mode == MODE_JPEG)
plot_pixel_jpeg(x,y,r,g,b);
}
void save_jpg()
{
Pic *in = NULL;
in = pic_alloc(640, 480, 3, NULL);
printf("Saving JPEG file: %s\n", filename);
memcpy(in->pix,buffer,3*WIDTH*HEIGHT);
if (jpeg_write(filename, in))
printf("File saved Successfully\n");
else
printf("Error in Saving\n");
pic_free(in);
}
void parse_check(char *expected,char *found)
{
if(strcasecmp(expected,found))
{
char error[100];
printf("Expected '%s ' found '%s '\n",expected,found);
printf("Parse error, abnormal abortion\n");
exit(0);
}
}
void parse_doubles(FILE*file, char *check, double p[3])
{
char str[100];
fscanf(file,"%s",str);
parse_check(check,str);
fscanf(file,"%lf %lf %lf",&p[0],&p[1],&p[2]);
printf("%s %lf %lf %lf\n",check,p[0],p[1],p[2]);
}
void parse_rad(FILE*file,double *r)
{
char str[100];
fscanf(file,"%s",str);
parse_check("rad:",str);
fscanf(file,"%lf",r);
printf("rad: %f\n",*r);
}
void parse_shi(FILE*file,double *shi)
{
char s[100];
fscanf(file,"%s",s);
parse_check("shi:",s);
fscanf(file,"%lf",shi);
printf("shi: %f\n",*shi);
}
int loadScene(char *argv)
{
FILE *file = fopen(argv,"r");
int number_of_objects;
char type[50];
int i;
Triangle t;
Sphere s;
Light l;
fscanf(file,"%i",&number_of_objects);
printf("number of objects: %i\n",number_of_objects);
char str[200];
parse_doubles(file,"amb:",ambient_light);
for(i=0;i < number_of_objects;i++)
{
fscanf(file,"%s\n",type);
printf("%s\n",type);
if(strcasecmp(type,"triangle")==0)
{
printf("found triangle\n");
int j;
for(j=0;j < 3;j++)
{
parse_doubles(file,"pos:",t.v[j].position);
parse_doubles(file,"nor:",t.v[j].normal);
parse_doubles(file,"dif:",t.v[j].color_diffuse);
parse_doubles(file,"spe:",t.v[j].color_specular);
parse_shi(file,&t.v[j].shininess);
}
if(num_triangles == MAX_TRIANGLES)
{
printf("too many triangles, you should increase MAX_TRIANGLES!\n");
exit(0);
}
triangles[num_triangles++] = t;
}
else if(strcasecmp(type,"sphere")==0)
{
printf("found sphere\n");
parse_doubles(file,"pos:",s.position);
parse_rad(file,&s.radius);
parse_doubles(file,"dif:",s.color_diffuse);
parse_doubles(file,"spe:",s.color_specular);
parse_shi(file,&s.shininess);
if(num_spheres == MAX_SPHERES)
{
printf("too many spheres, you should increase MAX_SPHERES!\n");
exit(0);
}
spheres[num_spheres++] = s;
}
else if(strcasecmp(type,"light")==0)
{
printf("found light\n");
parse_doubles(file,"pos:",l.position);
parse_doubles(file,"col:",l.color);
if(num_lights == MAX_LIGHTS)
{
printf("too many lights, you should increase MAX_LIGHTS!\n");
exit(0);
}
lights[num_lights++] = l;
}
else
{
printf("unknown type in scene description:\n%s\n",type);
exit(0);
}
}
return 0;
}
void display()
{
}
void init()
{
glMatrixMode(GL_PROJECTION);
glOrtho(0,WIDTH,0,HEIGHT,1,-1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
}
void idle()
{
//hack to make it only draw once
static int once=0;
if(!once)
{
draw_scene();
if(mode == MODE_JPEG)
save_jpg();
}
once=1;
}
int main (int argc, char ** argv)
{
if (argc<2 || argc > 3)
{
printf ("usage: %s <scenefile> [jpegname]\n", argv[0]);
exit(0);
}
if(argc == 3)
{
mode = MODE_JPEG;
filename = argv[2];
}
else if(argc == 2)
mode = MODE_DISPLAY;
glutInit(&argc,argv);
loadScene(argv[1]);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutInitWindowPosition(0,0);
glutInitWindowSize(WIDTH,HEIGHT);
int window = glutCreateWindow("Ray Tracer");
glutDisplayFunc(display);
glutIdleFunc(idle);
init();
glutMainLoop();
}