-
Notifications
You must be signed in to change notification settings - Fork 0
/
loaders.cpp
268 lines (216 loc) · 7.38 KB
/
loaders.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
#include "loaders.h"
//printf-s fpr shaders debuggin on lines: 17,45,52
// "framework-procedures" for shaders;
bool checkOpenGLError ()
{
bool retCode = true;
for ( ; ; )
{
GLenum glErr = glGetError ();
if ( glErr == GL_NO_ERROR )
return retCode;
printf ( "glError: %s\n", gluErrorString ( glErr ) );
retCode = false;
glErr = glGetError ();
}
return retCode;
}
void printInfoLog ( GLhandleARB object )
{
GLint logLength = 0;
GLsizei charsWritten = 0;
GLcharARB * infoLog;
checkOpenGLError (); // check for OpenGL errors
glGetObjectParameterivARB ( object, GL_OBJECT_INFO_LOG_LENGTH_ARB, &logLength );
if ( !checkOpenGLError() ) // check for OpenGL errors
exit ( 1 );
if ( logLength > 0 )
{
infoLog = (GLcharARB*) malloc ( logLength );
if ( infoLog == NULL )
{
printf("ERROR: Could not allocate InfoLog buffer\n");
exit ( 1 );
}
glGetInfoLogARB ( object, logLength, &charsWritten, infoLog );
printf ( "InfoLog:\n%s\n\n", infoLog );
free ( infoLog );
}
if ( !checkOpenGLError () ) // check for OpenGL errors
exit ( 1 );
}
bool loadShader ( GLhandleARB shader, const char * fileName )
{
printf ( "Loading %s\n", fileName );
FILE * file = fopen ( fileName, "rb" );
if ( file == NULL )
{
printf ( "Error opening %s\n", fileName );
exit ( 1 );
}
fseek ( file, 0, SEEK_END );
GLint size = ftell ( file );
if ( size < 1 )
{
fclose ( file );
printf ( "Error loading file %s\n", fileName );
exit ( 1 );
}
char * buf = (char *) malloc ( size );
fseek ( file, 0, SEEK_SET );
if ( fread ( buf, 1, size, file ) != size )
{
fclose ( file );
printf ( "Error loading file %s\n", fileName );
exit ( 1 );
}
fclose ( file );
GLint compileStatus;
glShaderSourceARB ( shader, 1, (const char **) &buf, &size );
free ( buf );
// compile the particle vertex shader, and print out
glCompileShaderARB ( shader );
if ( !checkOpenGLError() ) // check for OpenGL errors
return false;
glGetObjectParameterivARB ( shader, GL_OBJECT_COMPILE_STATUS_ARB, &compileStatus );
printInfoLog ( shader );
return compileStatus != 0;
}
GLhandleARB prepareShader (GLhandleARB vert, GLhandleARB frag, const char * path_vert, const char * path_frag)
{
GLint local_linked;
GLhandleARB local;
// create a vertex shader object and a fragment shader object
vert = glCreateShaderObjectARB ( GL_VERTEX_SHADER_ARB );
frag = glCreateShaderObjectARB ( GL_FRAGMENT_SHADER_ARB );
// load source code strings into shaders
if ( !loadShader ( vert, path_vert ) )
exit ( 1 );
if ( !loadShader ( frag, path_frag ) )
exit ( 1 );
// create a program object and attach the
// two compiled shaders
local = glCreateProgramObjectARB ();
glAttachObjectARB ( local, vert );
glAttachObjectARB ( local, frag );
// link the program object and print out the info log
glLinkProgramARB ( local );
if ( !checkOpenGLError() ) // check for OpenGL errors
exit ( 1 );
glGetObjectParameterivARB ( local, GL_OBJECT_LINK_STATUS_ARB, &local_linked );
printInfoLog ( local);
if ( !local_linked )
exit(0);
return local;
}
/* procedure to load textures from bmp-files */
texture_info* TextureObs(const char* path) {
FILE* input;
FILE* test;
unsigned char tmp;
texture_info* output;
int other;
int idx_1, idx_2;
input = fopen(path, "rb");
if (input == NULL) {
exit(3);
return NULL;
}
test = fopen("test.txt","w");
/*preparing the structure where the texture
and its parametres will be stored*/
output = (texture_info *) malloc (sizeof(texture_info));
/*analysing the image*/
/*reading BM*/
fgetc(input);
fgetc(input);
/*reading filesize*/
tmp = fgetc(input);
other = tmp;
tmp = fgetc(input);
other += (tmp << 8);
tmp = fgetc(input);
other += (tmp << 16);
tmp = fgetc(input);
other += (tmp << 24);
fprintf(test,"%d%s",other,"\n");
/*reading two reserved spaces, an offset and the size of info header*/
for (idx_1 = 0; idx_1 <= 11; idx_1++) fgetc(input);
/*reading width of the image*/
tmp = fgetc(input);
output->width = tmp;
tmp = fgetc(input);
output->width += (tmp << 8);
tmp = fgetc(input);
output->width += (tmp << 16);
tmp = fgetc(input);
output->width += (tmp << 24);
fprintf(test,"%d%s",output->width,"\n");
/*reading height of the image*/
tmp = fgetc(input);
output->height = tmp;
tmp = fgetc(input);
output->height += (tmp << 8);
tmp = fgetc(input);
output->height += (tmp << 16);
tmp = fgetc(input);
output->height += (tmp << 24);
fprintf(test,"%d%s",output->height,"\n");
/*reserved field*/
fgetc(input);
fgetc(input);
/*read color depth*/
tmp = fgetc(input);
other = tmp;
tmp = fgetc(input);
other += (tmp << 8);
fprintf(test,"%d%s",other,"\n");
//if (other != 24) return NULL;
/*read compression*/
tmp = fgetc(input);
other = tmp;
tmp = fgetc(input);
other += (tmp << 8);
tmp = fgetc(input);
other += (tmp << 16);
tmp = fgetc(input);
other += (tmp << 24);
fprintf(test,"%d%s",other,"\n");
if (other != 0) {
printf("%s","Comression!!!");
return NULL;
}
for (idx_1 = 0; idx_1 <= 19; idx_1++) fgetc(input);
/*reading the image itself*/
output->img_data = (unsigned char*) malloc (output->width * output->height * 3 * sizeof(unsigned char));
for (idx_1 = 0; idx_1 <= output->height-1; idx_1++)
for (idx_2 = 0; idx_2 <= output->width-1; idx_2++) {
output->img_data[(idx_1 * output->width * 3) + (idx_2 * 3) + 2] = fgetc(input);
output->img_data[(idx_1 * output->width * 3) + (idx_2 * 3) + 1] = fgetc(input);
output->img_data[(idx_1 * output->width * 3) + (idx_2 * 3)] = fgetc(input);
}
return output;
fclose(input);
fclose(test);
}
GLuint loadTexture (const char * fileName )
{
texture_info* raw_image;
GLuint texture_stor;
glGenTextures(1,&texture_stor);
/*Create cracked concrete texture*/
raw_image = TextureObs(fileName);
if (raw_image == NULL) exit(2);
glBindTexture(GL_TEXTURE_2D, texture_stor);
//glTexImage2D(GL_TEXTURE_2D, 0, 3, raw_image->width, raw_image->height, 0, GL_RGB, GL_UNSIGNED_BYTE, raw_image->img_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gluBuild2DMipmaps ( GL_TEXTURE_2D, GL_RGB,
raw_image -> width, raw_image -> height,
GL_RGB, GL_UNSIGNED_BYTE,
raw_image -> img_data );
free(raw_image);
return texture_stor;
}