taisei/src/resource/model.c

257 lines
5.8 KiB
C
Raw Normal View History

2012-07-15 08:15:47 +02:00
/*
* This software is licensed under the terms of the MIT-License
* See COPYING for further information.
2012-07-15 08:15:47 +02:00
* ---
2018-01-04 18:14:31 +01:00
* Copyright (c) 2011-2018, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2018, Andrei Alexeyev <akari@alienslab.net>.
2012-07-15 08:15:47 +02:00
*/
#include "taisei.h"
2012-07-15 08:15:47 +02:00
#include "model.h"
#include "list.h"
#include "resource.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static void parse_obj(const char *filename, ObjFileData *data);
static void free_obj(ObjFileData *data);
char* model_path(const char *name) {
return strjoin(MDL_PATH_PREFIX, name, MDL_EXTENSION, NULL);
}
bool check_model_path(const char *path) {
return strendswith(path, MDL_EXTENSION);
}
2017-03-13 06:44:39 +01:00
typedef struct ModelLoadData {
ObjFileData *obj;
Vertex *verts;
Model *model;
} ModelLoadData;
2017-03-13 06:44:39 +01:00
void* load_model_begin(const char *path, unsigned int flags) {
Model *m = malloc(sizeof(Model));
ObjFileData *data = malloc(sizeof(ObjFileData));
Vertex *verts;
2017-03-13 06:44:39 +01:00
parse_obj(path, data);
2017-03-13 06:44:39 +01:00
m->fverts = data->fverts;
m->indices = calloc(data->icount, sizeof(unsigned int));
m->icount = data->icount;
2017-03-13 06:44:39 +01:00
verts = calloc(data->icount, sizeof(Vertex));
2017-03-13 06:44:39 +01:00
#define BADREF(filename,aux,n) { \
log_warn("OBJ file '%s': Index %d: bad %s index reference\n", filename, n, aux); \
goto fail; \
2017-03-13 06:44:39 +01:00
}
2017-03-13 06:44:39 +01:00
memset(verts, 0, data->icount*sizeof(Vertex));
for(unsigned int i = 0; i < data->icount; i++) {
int xi, ni, ti;
2017-03-13 06:44:39 +01:00
xi = data->indices[i][0]-1;
if(xi < 0 || xi >= data->xcount)
BADREF(path, "vertex", i);
2017-03-13 06:44:39 +01:00
memcpy(verts[i].x, data->xs[xi], sizeof(Vector));
2017-03-13 06:44:39 +01:00
if(data->tcount) {
ti = data->indices[i][1]-1;
if(ti < 0 || ti >= data->tcount)
BADREF(path, "texcoord", i);
2017-03-13 06:44:39 +01:00
verts[i].s = data->texcoords[ti][0];
verts[i].t = data->texcoords[ti][1];
}
2017-03-13 06:44:39 +01:00
if(data->ncount) {
ni = data->indices[i][2]-1;
if(ni < 0 || ni >= data->ncount)
BADREF(path, "normal", ni);
2017-03-13 06:44:39 +01:00
memcpy(verts[i].n, data->normals[ni], sizeof(Vector));
}
2017-03-13 06:44:39 +01:00
m->indices[i] = i;
}
#undef BADREF
2017-03-13 06:44:39 +01:00
ModelLoadData *ldata = malloc(sizeof(ModelLoadData));
ldata->obj = data;
ldata->verts = verts;
ldata->model = m;
return ldata;
fail:
free(m->indices);
free(m);
free(verts);
free_obj(data);
free(data);
return NULL;
2017-03-13 06:44:39 +01:00
}
void* load_model_end(void *opaque, const char *path, unsigned int flags) {
ModelLoadData *ldata = opaque;
unsigned int ioffset = _vbo.offset;
if(!ldata) {
return NULL;
}
for(int i = 0; i < ldata->obj->icount; ++i) {
ldata->model->indices[i] += ioffset;
}
vbo_add_verts(&_vbo, ldata->verts, ldata->obj->icount);
2017-03-13 06:44:39 +01:00
free(ldata->verts);
free_obj(ldata->obj);
free(ldata->obj);
Model *m = ldata->model;
free(ldata);
return m;
}
void unload_model(void *model) { // Does not delete elements from the VBO, so doing this at runtime is leaking VBO space
free(((Model*)model)->indices);
free(model);
}
static void free_obj(ObjFileData *data) {
free(data->xs);
free(data->normals);
free(data->texcoords);
free(data->indices);
}
2017-02-28 18:47:47 +01:00
static void parse_obj(const char *filename, ObjFileData *data) {
2017-04-18 21:48:18 +02:00
SDL_RWops *rw = vfs_open(filename, VFS_MODE_READ);
2017-03-12 22:22:43 +01:00
if(!rw) {
2017-04-18 21:48:18 +02:00
log_warn("VFS error: %s", vfs_get_error());
2017-03-12 22:22:43 +01:00
return;
}
2017-03-13 06:44:39 +01:00
char line[256], *save;
2012-07-15 08:15:47 +02:00
Vector buf;
char mode;
int linen = 0;
2012-07-15 08:15:47 +02:00
memset(data, 0, sizeof(ObjFileData));
2017-03-12 22:22:43 +01:00
while(SDL_RWgets(rw, line, sizeof(line))) {
2012-07-15 08:15:47 +02:00
linen++;
2012-07-15 08:15:47 +02:00
char *first;
2017-03-13 06:44:39 +01:00
first = strtok_r(line, " \n", &save);
2012-07-15 08:15:47 +02:00
if(strcmp(first, "v") == 0)
mode = 'v';
else if(strcmp(first, "vt") == 0)
mode = 't';
else if(strcmp(first, "vn") == 0)
mode = 'n';
else if(strcmp(first, "f") == 0)
mode = 'f';
else
mode = 0;
if(mode != 0 && mode != 'f') {
2017-03-13 06:44:39 +01:00
buf[0] = atof(strtok_r(NULL, " \n", &save));
char *wtf = strtok_r(NULL, " \n", &save);
buf[1] = atof(wtf);
2012-07-15 08:15:47 +02:00
if(mode != 't')
2017-03-13 06:44:39 +01:00
buf[2] = atof(strtok_r(NULL, " \n", &save));
2012-07-15 08:15:47 +02:00
switch(mode) {
case 'v':
data->xs = realloc(data->xs, sizeof(Vector)*(++data->xcount));
memcpy(data->xs[data->xcount-1], buf, sizeof(Vector));
break;
case 't':
data->texcoords = realloc(data->texcoords, sizeof(Vector)*(++data->tcount));
memcpy(data->texcoords[data->tcount-1], buf, sizeof(Vector));
break;
case 'n':
data->normals = realloc(data->normals, sizeof(Vector)*(++data->ncount));
memcpy(data->normals[data->ncount-1], buf, sizeof(Vector));
break;
}
} else if(mode == 'f') {
char *segment, *seg;
int j = 0, jj;
IVector ibuf;
memset(ibuf, 0, sizeof(ibuf));
2017-03-13 06:44:39 +01:00
while((segment = strtok_r(NULL, " \n", &save))) {
2012-07-18 13:42:04 +02:00
seg = segment;
j++;
2012-07-15 08:15:47 +02:00
jj = 0;
2012-07-18 13:42:04 +02:00
while(jj < 3) {
2012-07-15 08:15:47 +02:00
ibuf[jj] = atoi(seg);
jj++;
2012-07-18 13:42:04 +02:00
while(*seg != '\0' && *(++seg) != '/');
2012-07-18 13:42:04 +02:00
if(*seg == '\0')
break;
else
seg++;
2012-07-15 08:15:47 +02:00
}
2012-07-15 08:15:47 +02:00
if(strstr(segment, "//")) {
ibuf[2] = ibuf[1];
ibuf[1] = 0;
}
2012-07-15 08:15:47 +02:00
if(jj == 0 || jj > 3 || segment[0] == '/')
2017-03-13 17:03:51 +01:00
log_fatal("OBJ file '%s:%d': Parsing error: Corrupt face definition", filename,linen);
2012-07-15 08:15:47 +02:00
data->indices = realloc(data->indices, sizeof(IVector)*(++data->icount));
memcpy(data->indices[data->icount-1], ibuf, sizeof(IVector));
}
2012-07-18 13:42:04 +02:00
2012-07-15 08:15:47 +02:00
if(data->fverts == 0)
data->fverts = j;
2012-07-15 08:15:47 +02:00
if(data->fverts != j)
2017-03-13 17:03:51 +01:00
log_fatal("OBJ file '%s:%d': Parsing error: face vertex count must stay the same in the whole file", filename, linen);
2012-07-15 08:15:47 +02:00
if(data->fverts != 3 && data->fverts != 4)
2017-03-13 17:03:51 +01:00
log_fatal("OBJ file '%s:%d': Parsing error: face vertex count must be either 3 or 4", filename, linen);
2012-07-15 08:15:47 +02:00
}
}
2017-03-12 22:22:43 +01:00
SDL_RWclose(rw);
2012-07-15 08:15:47 +02:00
}
Model* get_model(const char *name) {
return get_resource(RES_MODEL, name, RESF_DEFAULT)->model;
2012-07-15 08:15:47 +02:00
}
void draw_model_p(Model *model) {
GLenum flag = model->fverts == 3 ? GL_TRIANGLES : GL_QUADS;
2012-07-16 18:13:58 +02:00
glMatrixMode(GL_TEXTURE);
glScalef(1,-1,1); // every texture in taisei is actually read vertically mirrored. and I noticed that just now.
2012-07-15 08:15:47 +02:00
glDrawElements(flag, model->icount, GL_UNSIGNED_INT, model->indices);
2012-07-16 18:13:58 +02:00
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
2012-07-15 08:15:47 +02:00
}
2017-02-28 18:47:47 +01:00
void draw_model(const char *name) {
2012-07-15 08:15:47 +02:00
draw_model_p(get_model(name));
}