-
Notifications
You must be signed in to change notification settings - Fork 75
/
material.c
285 lines (247 loc) · 8.28 KB
/
material.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
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
/*
Copyright (C) 2010 Jeremy Van Grinsven
Author: Jeremy Van Grinsven
This file is part of Spacenerds In Space.
Spacenerds in Space is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Spacenerds in Space is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spacenerds in Space; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "mtwist.h"
#include "quat.h"
#include "snis_graph.h"
#include "material.h"
#include "graph_dev.h"
static unsigned int load_texture(const char *asset_dir, char *filename, int linear_colorspace)
{
char fname[PATH_MAX + 1];
snprintf(fname, sizeof(fname), "%s/textures/%s", asset_dir, filename);
return graph_dev_load_texture(fname, linear_colorspace);
}
static const char *gnu_basename(const char *path)
{
char *base = strrchr(path, '/');
return base ? base + 1 : path;
}
static const char *get_texture_filename(unsigned int texture_id)
{
return gnu_basename(graph_dev_get_texture_filename(texture_id));
}
int material_nebula_read_from_file(const char *asset_dir, const char *filename,
struct material *nebula)
{
FILE *f;
int rc;
int i;
char full_filename[PATH_MAX + 1];
struct material_nebula *mt = &nebula->nebula;
nebula->type = MATERIAL_NEBULA;
nebula->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
nebula->rotate_randomly = 0;
sprintf(full_filename, "%s/materials/%s", asset_dir, filename);
f = fopen(full_filename, "r");
if (!f) {
fprintf(stderr, "material_nebula: Error opening file '%s'\n", full_filename);
return -1;
}
for (i = 0; i < MATERIAL_NEBULA_NPLANES; i++) {
char texture_filename[PATH_MAX+1];
rc = fscanf(f, "texture %s\n", texture_filename);
if (rc != 1) {
fprintf(stderr, "material_nebula: Error reading 'texture' from file '%s'\n", full_filename);
fclose(f);
return -1;
}
mt->texture_id[i] = load_texture(asset_dir, texture_filename, 1);
rc = fscanf(f, "orientation %f %f %f %f\n", &mt->orientation[i].q.q0, &mt->orientation[i].q.q1,
&mt->orientation[i].q.q2, &mt->orientation[i].q.q3);
if (rc != 4) {
fprintf(stderr, "material_nebula: Error reading 'orientation' from file '%s'\n", full_filename);
fclose(f);
return -1;
}
}
rc = fscanf(f, "alpha %f\n", &mt->alpha);
if (rc != 1) {
fprintf(stderr, "material_nebula: Error reading 'alpha' from file '%s'\n", full_filename);
fclose(f);
return -1;
}
rc = fscanf(f, "tint %f %f %f\n", &mt->tint.red, &mt->tint.green, &mt->tint.blue);
if (rc != 3) {
fprintf(stderr, "material_nebula: Error reading 'tint' from file '%s'\n", full_filename);
fclose(f);
return -1;
}
fclose(f);
return 0;
}
int material_nebula_write_to_file(const char *asset_dir, const char *filename,
struct material *nebula)
{
FILE *f;
int i;
char full_filename[PATH_MAX + 1];
struct material_nebula *mt = &nebula->nebula;
sprintf(full_filename, "%s/materials/%s", asset_dir, filename);
f = fopen(full_filename, "w");
if (!f) {
fprintf(stderr, "material_nebula: Error opening file '%s' to write\n", full_filename);
return -1;
}
for (i = 0; i < MATERIAL_NEBULA_NPLANES; i++) {
const char *texture_filename = get_texture_filename(mt->texture_id[i]);
fprintf(f, "texture %s\n", texture_filename);
fprintf(f, "orientation %f %f %f %f\n", mt->orientation[i].q.q0, mt->orientation[i].q.q1,
mt->orientation[i].q.q2, mt->orientation[i].q.q3);
}
fprintf(f, "alpha %f\n", mt->alpha);
fprintf(f, "tint %f %f %f\n", mt->tint.red, mt->tint.green, mt->tint.blue);
fclose(f);
return 0;
}
void material_init_texture_mapped(struct material *m)
{
m->type = MATERIAL_TEXTURE_MAPPED;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->texture_mapped.texture_id = 0;
m->texture_mapped.emit_texture_id = 0;
m->texture_mapped.normalmap_id = 0;
m->texture_mapped.emit_intensity = 1.0;
m->texture_mapped.specular_power = 512.0;
m->texture_mapped.specular_intensity = 0.2;
m->rotate_randomly = 0;
}
void material_init_texture_mapped_unlit(struct material *m)
{
m->type = MATERIAL_TEXTURE_MAPPED_UNLIT;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->texture_mapped_unlit.texture_id = 0;
m->texture_mapped_unlit.do_cullface = 1;
m->texture_mapped_unlit.do_blend = 0;
m->texture_mapped_unlit.alpha = 1.0;
m->texture_mapped_unlit.tint = sng_get_color(WHITE);
m->rotate_randomly = 0;
}
void material_init_texture_cubemap(struct material *m)
{
m->type = MATERIAL_TEXTURE_CUBEMAP;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->texture_cubemap.texture_id = 0;
m->texture_cubemap.do_cullface = 1;
m->texture_cubemap.do_blend = 0;
m->texture_cubemap.alpha = 1.0;
m->texture_cubemap.tint = sng_get_color(WHITE);
m->rotate_randomly = 0;
}
void material_init_nebula(struct material *m)
{
m->type = MATERIAL_NEBULA;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
int i;
for (i = 0; i < MATERIAL_NEBULA_NPLANES; i++) {
m->nebula.texture_id[i] = 0;
m->nebula.orientation[i] = identity_quat;
}
m->nebula.alpha = 1.0;
m->nebula.tint = sng_get_color(WHITE);
m->rotate_randomly = 0;
}
void material_init_textured_particle(struct material *m)
{
m->type = MATERIAL_TEXTURED_PARTICLE;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->textured_particle.texture_id = 0;
m->textured_particle.radius = 1.0;
m->textured_particle.time_base = 1.0;
m->rotate_randomly = 0;
}
void material_init_textured_planet(struct material *m)
{
m->type = MATERIAL_TEXTURED_PLANET;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->textured_planet.texture_id = 0;
m->textured_planet.ring_material = 0;
m->textured_planet.water_color_r = 0.1;
m->textured_planet.water_color_g = 0.33;
m->textured_planet.water_color_b = 1.0;
m->textured_planet.sun_color_r = 1.0;
m->textured_planet.sun_color_g = 1.0;
m->textured_planet.sun_color_b = 0.7;
m->rotate_randomly = 0;
}
void material_init_textured_shield(struct material *m)
{
m->type = MATERIAL_TEXTURED_SHIELD;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->textured_shield.texture_id = 0;
m->rotate_randomly = 0;
}
void material_init_atmosphere(struct material *m)
{
m->type = MATERIAL_ATMOSPHERE;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->atmosphere.r = 81.0f / 255.0f;
m->atmosphere.g = 81.0f / 255.0f;
m->atmosphere.b = 1.0f;
m->atmosphere.scale = 1.03f;
m->atmosphere.brightness = NULL;
m->rotate_randomly = 0;
}
void material_init_alpha_by_normal(struct material *m)
{
m->type = MATERIAL_ALPHA_BY_NORMAL;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->alpha_by_normal.texture_id = -1; /* texture is optional for alpha by normal */
m->alpha_by_normal.do_cullface = 1;
m->alpha_by_normal.texture_id = 0;
m->alpha_by_normal.invert = 0;
m->alpha_by_normal.alpha = 1.0;
m->alpha_by_normal.tint = sng_get_color(WHITE);
m->rotate_randomly = 0;
}
void material_init_textured_planet_ring(struct material *m)
{
m->type = MATERIAL_TEXTURED_PLANET_RING;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->textured_planet_ring.texture_id = 0;
m->textured_planet_ring.alpha = 1.0;
m->textured_planet_ring.texture_v = 0.0f;
m->textured_planet_ring.tint = sng_get_color(WHITE);
m->rotate_randomly = 0;
}
void material_init_wireframe_sphere_clip(struct material *m)
{
m->type = MATERIAL_WIREFRAME_SPHERE_CLIP;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->wireframe_sphere_clip.center = 0;
m->wireframe_sphere_clip.radius = 0;
m->wireframe_sphere_clip.radius_fade = 0;
m->rotate_randomly = 0;
}
void material_init_planetary_lightning(struct material *m)
{
m->type = MATERIAL_PLANETARY_LIGHTNING;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->planetary_lightning.texture_id = 0;
m->rotate_randomly = 0;
}
void material_init_warp_gate_effect(struct material *m)
{
m->type = MATERIAL_WARP_GATE_EFFECT;
m->billboard_type = MATERIAL_BILLBOARD_TYPE_NONE;
m->warp_gate_effect.texture_id = 0;
m->warp_gate_effect.u1 = 0.0;
m->warp_gate_effect.u2 = 0.01;
m->rotate_randomly = 0;
}