deepstone

annotate src/scene.c @ 17:1e9f0b3616fa

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