Repository navigation
Expand file tree
/
Copy pathspriteMaker.c
More file actions
299 lines (265 loc) · 8.43 KB
/
Copy pathspriteMaker.c
File metadata and controls
299 lines (265 loc) · 8.43 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
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
/*
Zack Diller
CSCI 5229 FINAL PROJECT
Files:
knightArmy.c
> spriteMaker.c
knightDrawer.c
knightDrawer.h
File Description: This program creates the knight particles for the knightArmy program. NOTE: Must run before running knightArmy!
Please see README.txt for more info
*/
#include <stdlib.h>
#include "CSCIx239.h"
#include "knightDrawer.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <math.h>
#ifdef _WIN32
#include <GL/glew.h>
#else
#include <GL/glut.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include "SDL/SDL.h"
// Used to convert the bmp data into a human readable form
union
{
unsigned int integer;
unsigned char byte[4];
} conversion;
// Application variables
char _title[200]; // Title of our application
// Projection variables
int _fov = 20; // Field of view
float _worldSize = 1.0; // Size of world
float _asp; // Aspect ratio
// Number of iterations for each axis
int _maxThIters;
int _maxPhIters;
char _currDir[300];
// User-set variables
int _object[10];
unsigned int _numObjects;
char _fileName[100];
int _thIter; // How much th increases per iteration
int _phIter; // How much ph increases per iteration
int _width; // Screen width
int _height; // Screen height
float _scale; // Object size
int _displays = 0;
// Lighting
static const float _lightPosition[] = {0.17365,0.7,0.98481,0.0};
static const float _lightEmission[] = {0.3,0.3,0.3,1.0};
static const float _lightAmbient[] = {0.1,0.1,0.1,1.0};
static const float _lightDiffuse[] = {1.0,1.0,1.0,1.0};
static const float _lightSpecular[] = {1.0,1.0,1.0,1.0};
static const float _lightShinyness[] = {16};
// Set the caption
void setCaption(int th, int ph)
{
sprintf(_title, "(%d,%d) Parameters(%s,%d,%d,%d,%d,%.2f)", th, ph, _fileName, _thIter, _phIter, _width, _height, _scale);
SDL_WM_SetCaption(_title, 0);
}
// Draw the current frame and write to the image
void displayAndPrint(int th, int ph)
{
// Increment counter
_displays++;
//printf("%d\n", _displays); // For testing
// Set up projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(_fov, _asp, _worldSize/16.0, 16.0*_worldSize);
// Set up modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
double Ex = -2 * Sin(th) * Cos(ph);
double Ey = +2 * Sin(ph);
double Ez = -2 * Cos(th) * Cos(ph);
gluLookAt(Ex,Ey,Ez, 0,0,0, 0,Cos(ph),0);
// Set up lighting
// ----- Lighting -----
// OpenGL should normalize normal vectors
glEnable(GL_NORMALIZE);
// Enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Set ambient, diffuse, specular components and position of light 0
glLightfv(GL_LIGHT0, GL_POSITION, _lightPosition);
glLightfv(GL_LIGHT0, GL_AMBIENT, _lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, _lightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, _lightSpecular);
// Clean slate
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// ----- DRAWING OF OBJECT -----
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glScaled(_scale,_scale,_scale);
drawKnightModel(0,0,0,0,0.0,0.0,_scale);
glPopMatrix();
glDisable(GL_DEPTH_TEST);
// Disables
glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);
glDisable(GL_NORMALIZE);
// Set the caption
setCaption(th, ph);
// Render the scene
glFlush();
SDL_GL_SwapBuffers();
// ----- WRITE TO IMAGE -----
int i;
// Create the pixel buffer of our scene
GLubyte *buffer = malloc(3 * _width * _height * sizeof(GLubyte));
if (buffer) glReadPixels(0,0,_width,_height,GL_BGR,GL_UNSIGNED_BYTE,buffer);
// Read the "bmpPreClone.bmp" in the project's main directory
FILE *fREAD;
FILE *fWRITE;
char readFile[300] = "";
char writeFile[300] = "";
// Get the name of the read file
strcat(readFile, _currDir);
strcat(readFile, "/bmpPreClone.bmp");
// Create the name of the file we will write to
strcat(writeFile, _currDir);
strcat(writeFile, "/generated/");
char smallFileName[50] = "";
char curVal[10];
i = 0;
while (_fileName[i] != '.' && _fileName[i] != '\0') {
smallFileName[i] = _fileName[i];
i++;
}
strcat(writeFile, smallFileName);
strcat(writeFile, "_");
sprintf(curVal, "%d", ph);
strcat(writeFile, curVal);
strcat(writeFile, "_");
sprintf(curVal, "%d", th);
strcat(writeFile, curVal);
strcat(writeFile, ".bmp");
// We got the file names we want, now open them
fREAD = fopen(readFile, "rb");
fWRITE = fopen(writeFile, "wb");
// After we are done copying the file, edit it to show our scene
unsigned char info[100];
// Get the current file size - make the size match our scene
fread(info, sizeof(unsigned char), 54, fREAD);
// Image size is at 18 & 24 bytes in
conversion.integer = _width;
info[18] = conversion.byte[0]; info[19] = conversion.byte[1];
info[20] = conversion.byte[2]; info[21] = conversion.byte[3];
conversion.integer = _height;
info[22] = conversion.byte[0]; info[23] = conversion.byte[1];
info[24] = conversion.byte[2]; info[25] = conversion.byte[3];
fwrite(info, sizeof(unsigned char), 54, fWRITE);
// Info is copied over, now we need to fill in the pixel information
fread(info, sizeof(unsigned char), 3, fREAD);
fwrite(buffer, sizeof(GLubyte), 3 * _width * _height, fWRITE);
// Free the file & buffer
free(buffer);
fclose(fREAD);
fclose(fWRITE);
// ----- END OF IMAGE WRITING -----
// Error check
ErrCheck("displayAndPrint");
}
// Main function
int main(int argc, char* argv[])
{
// Check given # arguements
if ((argc != 7) && (argc != 1)) {
Fatal("Invalid # of arguments given.");
}
// Read in all arguements
else if (argc == 7) {
strncpy(_fileName, argv[1], strlen(argv[1]));
_thIter = atoi(argv[2]);
_phIter = atoi(argv[3]);
_width = atoi(argv[4]);
_height = atoi(argv[5]);
_scale = strtod(argv[6], NULL);
}
// Use my 'default settings'
else {
strncpy(_fileName, "knight.obj", 10);
_thIter = 5;
_phIter = 5;
_width = 128;
_height = 128;
_scale = 0.42;
}
// No negative or zero values
if ((_thIter <= 0) || (_phIter <= 0) || (_width <= 0) || (_height <= 0) || (_scale <= 0.0))
Fatal("Values must be postive and non-zero.");
// th & ph iterations okay?
if (360 % _thIter != 0) Fatal("Arguement 1 must be divisible by 180");
// Arguements pass - set # of iterations
_maxThIters = 360 / _thIter;
_maxPhIters = 90 / _phIter;
// Set aspect ratio
_asp = ((float)_width/(float)_height);
// Initialize window
SDL_Surface* window; // The window pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL
window = SDL_SetVideoMode(_width, _height, 0, SDL_OPENGL|SDL_DOUBLEBUF);
if (!window) Fatal("SDL init failed.");
#ifdef _WIN32
GLenum err = glewInit();
if (GLEW_OK != err) Fatal("GLEW init failed.\n");
#endif
// Chroma key
glClearColor(0, 1, 0, 1.0);
// Set the caption for the first time
setCaption(0,0);
// Load the objects and the image, and create the programs
loadKnightObjects();
setUpKnightImages();
compileKnightPrograms();
// See if the 'generated' directory exists
getcwd(_currDir, 300); // Store the current directory
char command[300] = "";
strcat(command, _currDir);
strcat(command, "/generated");
struct stat st = {0};
// Doesn't exist - create it. Note that ubuntu and windows use different versions.
if (stat(command, &st) == -1) {
#ifdef _WIN32
mkdir(command);
#else
mkdir(command, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH);
#endif
}
// Does exist - clean it out
else {
char rmCommand[300] = "rm -r ";
strcat(rmCommand, command);
strcat(rmCommand, "/*");
//printf("%s\n\n\n", rmCommand); // For testing
system(rmCommand);
}
// Write the settings we used into the settings file for knightArmy.exe
char settingsFile[300] = "";
strcat(settingsFile, _currDir);
strcat(settingsFile, "/settings.txt");
FILE *fSETTINGS;
fSETTINGS = fopen(settingsFile, "w");
char settings[10];
sprintf(settings, "%d_%d_", _thIter, _phIter);
int u = 0;
while (settings[u] != '\0') u++;
fwrite(settings, sizeof(char), u, fSETTINGS);
fclose(fSETTINGS);
// While the program is running
// Note that this program has no user interaction during runtime
int ph,th;
for (ph = 90; ph >= 0; ph -= _phIter)
for (th = 0; th < 360; th += _thIter)
displayAndPrint(th, ph);
// Shut down
SDL_Quit();
return 0;
}