1 /*
2 	gl_model_iqm.c
3 
4 	iqm model processing for GLSL
5 
6 	Copyright (C) 2011 Bill Currie <bill@taniwha.org>
7 
8 	Author: Bill Currie <bill@taniwha.org>
9 	Date: 2012/04/27
10 
11 	This program is free software; you can redistribute it and/or
12 	modify it under the terms of the GNU General Public License
13 	as published by the Free Software Foundation; either version 2
14 	of the License, or (at your option) any later version.
15 
16 	This program is distributed in the hope that it will be useful,
17 	but WITHOUT ANY WARRANTY; without even the implied warranty of
18 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 
20 	See the GNU General Public License for more details.
21 
22 	You should have received a copy of the GNU General Public License
23 	along with this program; if not, write to:
24 
25 		Free Software Foundation, Inc.
26 		59 Temple Place - Suite 330
27 		Boston, MA  02111-1307, USA
28 
29 */
30 
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34 
35 #ifdef HAVE_STRING_H
36 # include <string.h>
37 #endif
38 #ifdef HAVE_STRINGS_H
39 # include <strings.h>
40 #endif
41 
42 #include "QF/dstring.h"
43 #include "QF/image.h"
44 #include "QF/quakefs.h"
45 #include "QF/va.h"
46 #include "QF/GL/qf_iqm.h"
47 #include "QF/GL/qf_textures.h"
48 
49 #include "mod_internal.h"
50 
51 static byte null_texture[] = {
52 	204, 204, 204, 255,
53 	204, 204, 204, 255,
54 	204, 204, 204, 255,
55 	204, 204, 204, 255,
56 };
57 
58 static void
gl_iqm_clear(model_t * mod)59 gl_iqm_clear (model_t *mod)
60 {
61 	iqm_t      *iqm = (iqm_t *) mod->aliashdr;
62 	gliqm_t    *gl = (gliqm_t *) iqm->extra_data;
63 
64 	mod->needload = true;
65 
66 	free (gl->blend_palette);
67 	free (gl);
68 	Mod_FreeIQM (iqm);
69 }
70 
71 static void
gl_iqm_load_textures(iqm_t * iqm)72 gl_iqm_load_textures (iqm_t *iqm)
73 {
74 	gliqm_t  *gl = (gliqm_t *) iqm->extra_data;
75 	int         i;
76 	dstring_t  *str = dstring_new ();
77 	tex_t      *tex;
78 
79 	gl->textures = malloc (iqm->num_meshes * sizeof (int));
80 	for (i = 0; i < iqm->num_meshes; i++) {
81 		dstring_copystr (str, iqm->text + iqm->meshes[i].material);
82 		QFS_StripExtension (str->str, str->str);
83 		if ((tex = LoadImage (va ("textures/%s", str->str))))
84 			gl->textures[i] = GL_LoadTexture (str->str, tex->width,
85 											  tex->height, tex->data, true,
86 											  false,
87 											  tex->format > 2 ? tex->format
88 															  : 1);
89 		else
90 			gl->textures[i] = GL_LoadTexture ("", 2, 2, null_texture, true,
91 											  false, 4);
92 	}
93 	dstring_delete (str);
94 }
95 
96 void
gl_Mod_IQMFinish(model_t * mod)97 gl_Mod_IQMFinish (model_t *mod)
98 {
99 	iqm_t      *iqm = (iqm_t *) mod->aliashdr;
100 	gliqm_t    *gl;
101 	int         i;
102 
103 	mod->clear = gl_iqm_clear;
104 	iqm->extra_data = gl = calloc (1, sizeof (gliqm_t));
105 	gl_iqm_load_textures (iqm);
106 	gl->blend_palette = Mod_IQMBuildBlendPalette (iqm, &gl->palette_size);
107 	for (i = 0; i < iqm->num_arrays; i++) {
108 		if (iqm->vertexarrays[i].type == IQM_POSITION)
109 			gl->position = &iqm->vertexarrays[i];
110 		if (iqm->vertexarrays[i].type == IQM_TEXCOORD)
111 			gl->texcoord = &iqm->vertexarrays[i];
112 		if (iqm->vertexarrays[i].type == IQM_NORMAL)
113 			gl->normal = &iqm->vertexarrays[i];
114 		if (iqm->vertexarrays[i].type == IQM_BLENDINDEXES)
115 			gl->bindices = &iqm->vertexarrays[i];
116 		if (iqm->vertexarrays[i].type == IQM_COLOR)
117 			gl->color = &iqm->vertexarrays[i];
118 	}
119 }
120