dos3d

view src/scene.c @ 19:c10f62b2bd56

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 30 Nov 2011 07:17:09 +0200
parents 1e9f0b3616fa
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <errno.h>
6 #include <assert.h>
7 #include "scene.h"
8 #include "cvec.h"
9 #include "palman.h"
11 #ifdef USE_GL
12 #include <GL/gl.h>
13 #else
14 #include "mingl.h"
15 #endif
18 struct face {
19 int v[3], n[3], t[3];
20 };
22 static int load_materials(struct scene *scn, const char *fname);
23 static int parse_face(struct face *face, char *buf);
26 /* --- scene --- */
27 int scn_init(struct scene *scn)
28 {
29 memset(scn, 0, sizeof *scn);
30 return 0;
31 }
33 void scn_destroy(struct scene *scn)
34 {
35 while(scn->matlist) {
36 struct material *tmp = scn->matlist;
37 scn->matlist = scn->matlist->next;
39 mtl_destroy(tmp);
40 free(tmp);
41 }
42 while(scn->meshlist) {
43 struct mesh *tmp = scn->meshlist;
44 scn->meshlist = scn->meshlist->next;
46 mesh_destroy(tmp);
47 free(tmp);
48 }
49 }
52 void scn_add_mesh(struct scene *scn, struct mesh *m)
53 {
54 printf("adding mesh: %d faces\n", m->nface);
55 m->next = scn->meshlist;
56 scn->meshlist = m;
57 }
59 void scn_add_material(struct scene *scn, struct material *m)
60 {
61 printf("adding material: %s\n", m->name);
62 m->next = scn->matlist;
63 scn->matlist = m;
64 }
66 struct material *scn_find_material(struct scene *scn, const char *name)
67 {
68 struct material *mtl = scn->matlist;
70 while(mtl) {
71 if(strcmp(mtl->name, name) == 0) {
72 break;
73 }
74 mtl = mtl->next;
75 }
76 return mtl;
77 }
79 #define SEP " \t\n\r"
80 int scn_load(struct scene *scn, const char *fname)
81 {
82 FILE *fp;
83 char buf[256];
84 struct mesh *m;
85 vec3_t *varr, *vnarr;
86 vec2_t *vtarr;
88 if(!(fp = fopen(fname, "rb"))) {
89 fprintf(stderr, "failed to open scene file: %s: %s\n", fname, strerror(errno));
90 return -1;
91 }
93 varr = cvec_alloc(0, sizeof *varr);
94 vnarr = cvec_alloc(0, sizeof *vnarr);
95 vtarr = cvec_alloc(0, sizeof *vtarr);
97 if(!(m = malloc(sizeof *m)) || mesh_init(m) == -1) {
98 fprintf(stderr, "meshed up\n");
99 fclose(fp);
100 return -1;
101 }
103 while(fgets(buf, sizeof buf, fp)) {
104 char *line = buf;
105 char *tok, *rest, *tmp;
107 while(*line && isspace(*line)) {
108 line++;
109 }
110 if(*line == 0 || *line == '#') {
111 continue;
112 }
114 if(!(tok = strtok(line, SEP))) {
115 continue;
116 }
117 rest = tok + strlen(tok) + 1;
119 if((tmp = strchr(rest, '\r')) || (tmp = strchr(rest, '\n'))) {
120 *tmp = 0;
121 }
123 if(strcmp(tok, "mtllib") == 0) {
124 if(!rest) {
125 fprintf(stderr, "invalid mtllib directive\n");
126 continue;
127 }
128 load_materials(scn, rest);
130 } else if(strcmp(tok, "usemtl") == 0) {
131 if(rest) {
132 m->mtl = scn_find_material(scn, rest);
133 }
135 } else if(strcmp(tok, "o") == 0 || strcmp(tok, "g") == 0) {
136 if(cvec_size(m->vert) > 0) {
137 scn_add_mesh(scn, m);
139 if(!(m = malloc(sizeof *m)) || mesh_init(m) == -1) {
140 fprintf(stderr, "meshed up\n");
141 fclose(fp);
142 return -1;
143 }
144 }
146 } else if(strcmp(tok, "v") == 0) {
147 vec3_t v;
149 if(!rest || sscanf(rest, "%f %f %f\n", &v.x, &v.y, &v.z) != 3) {
150 continue;
151 }
152 varr = cvec_append(varr, &v);
154 } else if(strcmp(tok, "vn") == 0) {
155 vec3_t v;
157 if(!rest || sscanf(rest, "%f %f %f\n", &v.x, &v.y, &v.z) != 3) {
158 continue;
159 }
160 vnarr = cvec_append(vnarr, &v);
162 } else if(strcmp(tok, "vt") == 0) {
163 vec2_t v;
165 if(!rest || sscanf(rest, "%f %f\n", &v.x, &v.y) != 2) {
166 continue;
167 }
168 v.y = 1.0 - v.y;
169 vtarr = cvec_append(vtarr, &v);
171 } else if(strcmp(tok, "f") == 0) {
172 int i;
173 struct face face;
175 if(!rest || parse_face(&face, rest) == -1) {
176 continue;
177 }
179 for(i=0; i<3; i++) {
180 int vidx = face.v[i];
181 int nidx = face.n[i];
182 int tidx = face.t[i];
184 mesh_add_vertex(m, varr[vidx]);
185 if(nidx >= 0) {
186 mesh_add_normal(m, vnarr[nidx]);
187 }
188 if(tidx >= 0) {
189 mesh_add_texcoord(m, vtarr[tidx]);
190 }
191 m->nface++;
192 }
193 }
195 }
196 fclose(fp);
198 if(cvec_size(m->vert) > 0) {
199 scn_add_mesh(scn, m);
200 }
202 cvec_free(varr);
203 cvec_free(vnarr);
204 cvec_free(vtarr);
206 return 0;
207 }
209 static int load_materials(struct scene *scn, const char *fname)
210 {
211 FILE *fp;
212 struct material *m = 0;
213 char buf[256];
215 if(!(fp = fopen(fname, "r"))) {
216 fprintf(stderr, "failed to load material file: %s: %s\n", fname, strerror(errno));
217 return -1;
218 }
220 while(fgets(buf, sizeof buf, fp)) {
221 char *line = buf;
222 char *tok, *rest, *tmp;
224 while(*line && isspace(*line)) {
225 line++;
226 }
227 if(*line == 0 || *line == '#') {
228 continue;
229 }
231 if(!(tok = strtok(line, SEP))) {
232 continue;
233 }
234 rest = tok + strlen(tok) + 1;
236 if((tmp = strchr(rest, '\r')) || (tmp = strchr(rest, '\n'))) {
237 *tmp = 0;
238 }
240 if(strcmp(tok, "newmtl") == 0) {
241 if(m) {
242 if(!m->tex) {
243 palm_add_color(m->kd[0], m->kd[1], m->kd[2]);
244 }
245 scn_add_material(scn, m);
246 }
247 if(!(m = malloc(sizeof *m)) || mtl_init(m) == -1) {
248 continue;
249 }
250 mtl_set_name(m, rest);
252 } else if(strcmp(tok, "Kd") == 0) {
253 float r, g, b;
254 if(sscanf(rest, "%f %f %f", &r, &g, &b) != 3) {
255 continue;
256 }
257 m->kd[0] = (int)(r * 255.0);
258 m->kd[1] = (int)(g * 255.0);
259 m->kd[2] = (int)(b * 255.0);
261 } else if(strcmp(tok, "map_Kd") == 0) {
262 if(!(m->tex = load_texture(rest))) {
263 fprintf(stderr, "failed to load texture: `%s'\n", rest);
264 }
265 }
266 }
268 if(m) {
269 if(!m->tex) {
270 palm_add_color(m->kd[0], m->kd[1], m->kd[2]);
271 }
272 scn_add_material(scn, m);
273 }
275 fclose(fp);
276 return 0;
277 }
279 static int parse_face(struct face *face, char *buf)
280 {
281 int i, found;
282 char *tok;
284 for(i=0; i<3; i++) {
285 tok = strtok(i > 0 ? 0 : buf, SEP);
286 found = sscanf(tok, "%d/%d/%d", &face->v[i], &face->t[i], &face->n[i]);
288 face->v[i]--;
290 if(found > 1) {
291 face->t[i]--;
292 } else {
293 face->t[i] = -1;
294 }
295 if(found > 2) {
296 face->n[i]--;
297 } else {
298 face->n[i] = -1;
299 }
300 }
301 return 0;
302 }
304 void scn_render(struct scene *scn)
305 {
306 struct mesh *m;
308 if(!scn->ready) {
309 struct material *mtl = scn->matlist;
310 while(mtl) {
311 if(mtl->tex) {
312 get_texture_pixels(mtl->tex);
313 #ifdef USE_GL
314 {
315 int i, npix = mtl->tex->width * mtl->tex->height;
316 int range = palm_color_range();
317 unsigned char *rgb = malloc(npix * 3);
318 struct palm_color *pal = palm_palette();
320 for(i=0; i<npix; i++) {
321 int idx = mtl->tex->pixels[i] + range - 1;
322 rgb[i * 3] = pal[idx].r;
323 rgb[i * 3 + 1] = pal[idx].g;
324 rgb[i * 3 + 2] = pal[idx].b;
325 }
326 free(mtl->tex->pixels);
327 mtl->tex->pixels = rgb;
328 }
329 #endif /* USE_GL */
330 } else {
331 mtl->kd_base = palm_color_base(mtl->kd[0], mtl->kd[1], mtl->kd[2]);
332 }
333 mtl = mtl->next;
334 }
335 scn->ready = 1;
336 }
338 m = scn->meshlist;
339 while(m) {
340 mesh_draw(m);
341 m = m->next;
342 }
344 #if 0
345 {
346 int i;
347 struct palm_color *pal = palm_palette();
348 float dx = 1.8 / 256;
349 struct material *mtl = scn->matlist;
351 glMatrixMode(GL_MODELVIEW);
352 glPushMatrix();
353 glLoadIdentity();
354 glMatrixMode(GL_PROJECTION);
355 glPushMatrix();
356 glLoadIdentity();
358 glPushAttrib(GL_ENABLE_BIT);
359 glDisable(GL_LIGHTING);
360 glDisable(GL_DEPTH_TEST);
362 glBegin(GL_QUADS);
363 for(i=0; i<255; i++) {
364 float x = i * dx - 0.9;
365 glColor3ub(pal[i].r, pal[i].g, pal[i].b);
366 glVertex2f(x, 0.98);
367 glVertex2f(x, 0.9);
368 glColor3ub(pal[i + 1].r, pal[i + 1].g, pal[i + 1].b);
369 glVertex2f(x + dx, 0.9);
370 glVertex2f(x + dx, 0.98);
371 }
372 glEnd();
374 glPopAttrib();
376 glMatrixMode(GL_PROJECTION);
377 glPopMatrix();
378 glMatrixMode(GL_MODELVIEW);
379 glPopMatrix();
380 }
381 #endif
382 }
384 /* --- material --- */
385 int mtl_init(struct material *mtl)
386 {
387 memset(mtl, 0, sizeof *mtl);
388 return 0;
389 }
391 void mtl_destroy(struct material *mtl)
392 {
393 free(mtl->name);
395 if(mtl->tex) {
396 free_texture(mtl->tex);
397 }
398 }
401 int mtl_set_name(struct material *mtl, const char *name)
402 {
403 char *tmp;
404 int len = strlen(name);
406 if(!(tmp = malloc(len + 1))) {
407 perror("failed to allocate material name");
408 return -1;
409 }
410 memcpy(tmp, name, len);
411 tmp[len] = 0;
413 free(mtl->name);
414 mtl->name = tmp;
415 return 0;
416 }
419 /* --- mesh --- */
420 int mesh_init(struct mesh *m)
421 {
422 memset(m, 0, sizeof *m);
424 m->vert = cvec_alloc(0, sizeof *m->vert);
425 m->norm = cvec_alloc(0, sizeof *m->norm);
426 m->texcoord = cvec_alloc(0, sizeof *m->texcoord);
428 if(!m->vert || !m->norm || !m->texcoord) {
429 return -1;
430 }
431 return 0;
432 }
434 void mesh_destroy(struct mesh *m)
435 {
436 cvec_free(m->vert);
437 cvec_free(m->norm);
438 cvec_free(m->texcoord);
439 }
441 void mesh_add_vertex(struct mesh *m, vec3_t v)
442 {
443 m->vert = cvec_append(m->vert, &v);
444 }
446 void mesh_add_normal(struct mesh *m, vec3_t n)
447 {
448 m->norm = cvec_append(m->norm, &n);
449 }
451 void mesh_add_texcoord(struct mesh *m, vec2_t tc)
452 {
453 m->texcoord = cvec_append(m->texcoord, &tc);
454 }
456 void mesh_draw(struct mesh *m)
457 {
458 int i, numv;
459 struct material *mtl = m->mtl;
461 numv = cvec_size(m->vert);
463 #ifdef USE_GL
464 if(mtl->tex) {
465 assert(mtl->tex->pixels);
466 glEnable(GL_TEXTURE_2D);
467 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
468 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
469 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
470 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
472 glTexImage2D(GL_TEXTURE_2D, 0, 3, mtl->tex->width, mtl->tex->height, 0, GL_RGB, GL_UNSIGNED_BYTE, mtl->tex->pixels);
473 }
475 glBegin(GL_TRIANGLES);
476 for(i=0; i<numv; i++) {
477 glNormal3f(m->norm[i].x, m->norm[i].y, m->norm[i].z);
478 glTexCoord2f(m->texcoord[i].x, m->texcoord[i].y);
479 glVertex3f(m->vert[i].x, m->vert[i].y, m->vert[i].z);
480 }
481 glEnd();
483 if(mtl->tex) {
484 glDisable(GL_TEXTURE_2D);
485 }
486 #else
487 if(mtl->tex) {
488 mgl_enable(MGL_TEXTURE_2D);
489 mgl_teximage(mtl->tex->width, mtl->tex->height, mtl->tex->pixels);
490 } else {
491 mgl_index(mtl->kd_base);
492 }
494 mgl_begin(MGL_TRIANGLES);
496 for(i=0; i<numv; i++) {
497 mgl_normal(m->norm[i].x, m->norm[i].y, m->norm[i].z);
498 mgl_texcoord2f(m->texcoord[i].x, m->texcoord[i].y);
499 mgl_vertex3f(m->vert[i].x, m->vert[i].y, m->vert[i].z);
500 }
501 mgl_end();
503 if(mtl->tex) {
504 mgl_disable(MGL_TEXTURE_2D);
505 }
506 #endif
507 }