-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.c
More file actions
162 lines (131 loc) · 4.33 KB
/
Copy pathtexture.c
File metadata and controls
162 lines (131 loc) · 4.33 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
#include <stdio.h>
#include "header.h"
#include "safe.h"
#include "common.h"
#include "texture.h"
#include "projection.h"
#define PPM_COLOR_VERSION 6
/*
* Loads a ppm file as a texture and stores pixel values.
*
* PARAMETERS:
* tp - texplane objec to store texture struct in
*/
int texture_load(texplane_t *tp) {
int size = 0;
ppm_header *header = NULL;
FILE *texFile = NULL;
texture_t *texture = NULL;
if ((texFile = fopenAndCheck(tp->texname, "r")) == NULL) {
#ifdef DEBUG_TEXTURE
fprintf(stderr, "failed to open texture file:%s|\n", tp->texname);
#endif
return EXIT_FAILURE;
}
header = (ppm_header *)malloc(sizeof(ppm_header));
readHeader(texFile, header);
if (header == NULL) {
fprintf(stderr, "Invalid header, exiting..\n");
#ifdef DEBUG_TEXTURE
fprintf(stderr, "Invalid texture header\n");
#endif
return EXIT_FAILURE;
} else if (header->version != PPM_COLOR_VERSION) {
fprintf(stderr, "Header version must be P6, exiting.\n");
#ifdef DEBUG_TEXTURE
fprintf(stderr, "Invalid header version: %d\n", header->version);
#endif
return EXIT_FAILURE;
}
texture = (texture_t *)smalloc(sizeof(texture_t));
tp->texture = texture;
texture->size[0] = header->width;
texture->size[1] = header->height;
#ifdef DEBUG_TEXTURE
fprintf(stderr, "texture size: %d X %d\n", texture->size[0],
texture->size[1]);
#endif
size = header->width * header->height;
texture->texbuf = (unsigned char *)smalloc(3 * size);
int numRead = fread(texture->texbuf, sizeof(unsigned char),
size * 3, texFile);
if (numRead != size * 3) {
fprintf(stderr, "Corrupt input file, got %d bytes\n", numRead);
return EXIT_FAILURE;
}
free(header);
return EXIT_SUCCESS;
}
/*
* Returns the rgb values for the texture at a given point.
*
* PARAMETERS:
* fp - finite plane object that holds the point to retrieve
* texel - array to store texture rgb values in
*/
int texture_map(fplane_t *fp, double *texel) {
texplane_t *tp = (texplane_t *)fp->priv;
texture_t *tex = (texture_t *)tp->texture;
double xfrac, yfrac;
// scale mode
if (tp->texmode == FIT_TEXTURE) {
#ifdef DEBUG_TEXTURE
fprintf(stderr, "lasthit: (%lf, %lf)\n", fp->lasthit[0],
fp->lasthit[1]);
#endif
xfrac = fp->lasthit[0] / fp->size[0];
yfrac = fp->lasthit[1] / fp->size[1];
texel_get(tex, xfrac, yfrac, texel);
// tile mode
} else {
int pixhit[2];
map_world_to_pix(fp->lasthit, pixhit);
xfrac = (double)(pixhit[0] % tex->size[0]) / tex->size[0];
yfrac = (double)(pixhit[1] % tex->size[1]) / tex->size[1];
texel_get(tex, xfrac, yfrac, texel);
}
return EXIT_SUCCESS;
}
/*
* given a relative x and y coordiante, finds the corrisponding
* texture values.
*
* PARAMETERS:
* tex - texture object
* xrel - x coordinate relative to a point
* yrel - y coordinate relative to a point
* texel - array to store texture rgb values in
*
*/
void texel_get(texture_t *tex, double xrel, double yrel, double *texel) {
unsigned char *texloc;
int xtex; /* x coordinate of the texel */
int ytex; /* y coordinate of the texel */
xtex = xrel * tex->size[0];
ytex = yrel * tex->size[1];
#ifdef DEBUG_TEXTURE
fprintf(stderr, "xrel, yrel = (%lf, %lf)\n", xrel, yrel);
fprintf(stderr, "xtex, ytex = (%d, %d)\n", xtex, ytex);
#endif
// make sure the texture coordinates make sense
if ((xtex < 0) || (ytex < 0) ||
(xtex >= tex->size[0]) || (ytex >= tex->size[1])) {
#ifdef DEBUG_TEXTURE
fprintf(stderr, "got (xtex, ytex) = (%d, %d)\n", xtex, ytex);
#endif
exit(EXIT_FAILURE);
}
#ifdef DEBUG_TEXTURE
fprintf(stderr, "texloc offset = %d\n", offset);
#endif
texloc = tex->texbuf +
(((tex->size[1] - 1 - ytex) * tex->size[0] * 3) + 3 * xtex);
*(texel + 0) = *(texloc + 0) / 255.0;
*(texel + 1) = *(texloc + 1) / 255.0;
*(texel + 2) = *(texloc + 2) / 255.0;
}
void texture_free(texture_t *tex) {
if (tex->texbuf != NULL) {
free(tex->texbuf);
}
}