summaryrefslogtreecommitdiff
path: root/assimp.cpp
blob: 391cd9227bcdb96c1ec3f96cdf8fa6cbf371432b (plain)
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
286
287
288
289
290
291
292
293
294
295
#include <stdexcept>
#include <iostream>

#include "pattern.h"
#include "application.h"

#include "hcoord.h"

#include <wriggle/texturesdl.h>

#include <assimp/assimp.hpp>
#include <assimp/aiScene.h>
#include <assimp/aiPostProcess.h>

Assimp::Importer* importer;

class FiskPattern : public Pattern {
	private:
		const aiScene* scene;
		
		Texture** textures;
		
		int patt_id2;
		
		HMatrix m1_mat;
		
		bool m2_visible;
		
		HVector m2_vec;
		
	public:
		FiskPattern() : Pattern("patterns/m1.patt") {
			patt_id2 = arLoadPatt("patterns/m2.patt");
			
			scene = importer->ReadFile("models/tewi.x", 0);
			
			if(scene) {
				std::cout << "Loaded model: " << scene->mNumMeshes << " meshes, " << scene->mNumMaterials << " materials." << std::endl;
				for(int i = 0; i < scene->mNumMaterials; i++) {
					aiMaterial* material = scene->mMaterials[i];
					
					std::cout << "Material " << i << ": " << material->mNumProperties << " properties." << std::endl;
					
					for(int j = 0; j < material->mNumProperties; j++) {
						aiMaterialProperty* property = material->mProperties[j];
						
						std::cout << "  " << std::string(property->mKey.data, property->mKey.length) << ": " << std::string(property->mData, property->mDataLength) << std::endl;
					}
					
				}
				
				for(int i = 0; i < scene->mNumMeshes; i++) {
					aiMesh* mesh = scene->mMeshes[i];
					
					std::cout << "Mesh " << i << ": mMaterialIndex = " << mesh->mMaterialIndex << "." << std::endl;
					
				}
				
			} else {
				std::cerr << "Failed to import model." << std::endl << importer->GetErrorString() << std::endl;
			}
			
			Texture* tex_face = new TextureSDL("models/tewi.x.data/Tewi_face_01.bmp");
			Texture* tex_hair = new TextureSDL("models/tewi.x.data/TEWI_Hair_01.bmp");
			Texture* tex_body = new TextureSDL("models/tewi.x.data/TEWI_Body_01.bmp");
			Texture* tex_hair2 = new TextureSDL("models/tewi.x.data/TEWI_Hair_02.bmp");
			Texture* tex_body2 = new TextureSDL("models/tewi.x.data/TEWI_Body_02.bmp");
			
			
			textures = new Texture*[10];
			
			textures[1] = tex_face;
			textures[2] = tex_hair;
			textures[3] = tex_body;
			textures[4] = tex_body;
			textures[5] = tex_body;
			textures[6] = tex_body;
			textures[7] = tex_body;
			textures[8] = tex_hair;
			textures[9] = tex_face;
			
			patt_width = 50.0;
			
		}
		
	protected:
		virtual void update(ARMarkerInfo* marker_info, int marker_num) {
			bool m1_visible = m2_visible = false;
			
			for(int j = 0; j < marker_num; j++) {
				if(patt_id == marker_info[j].id) {
					arGetTransMat(&marker_info[j], patt_center, patt_width, m1_mat.m);
					m1_visible = true;
					//else if( marker_info[k].cf < marker_info[j].cf ) k = j;
				}
			}
			
			if(!m1_visible) {
				return;
			}
			
			HMatrix m2_mat;
			
			for(int j = 0; j < marker_num; j++) {
				if(patt_id2 == marker_info[j].id) {
					arGetTransMat(&marker_info[j], patt_center, patt_width, m2_mat.m);
					m2_visible = true;
					m2_vec = m1_mat.invtr() * m2_mat.getTranslation();
				}
			}
			
			draw();
		}
		
		virtual void draw() {
			glMatrixMode(GL_MODELVIEW);
			m1_mat.glLoad();
			
			glPushMatrix();
			
			glRotatef(90, 1, 0, 0);
			
			//glColor4f(1, 0, 0, 0.5);
			glPolygonMode(GL_FRONT, GL_FILL);
			
			for(int i = 0; i < 6; i++) { // scene->mNumMeshes
				aiMesh* mesh = scene->mMeshes[i];
				
				glBindTexture(GL_TEXTURE_2D, textures[mesh->mMaterialIndex]->tex());
				
				for(int j = 0; j < mesh->mNumFaces; j++) {
					aiFace* face = &mesh->mFaces[j];
					
					glBegin(GL_POLYGON);
					for(int k = 0; k < face->mNumIndices; k++) {
						aiVector3D* texcoord = &mesh->mTextureCoords[0][face->mIndices[k]];
						glTexCoord2f(texcoord->x, 2 - texcoord->y);
						aiVector3D* vertice = &mesh->mVertices[face->mIndices[k]];
						glVertex3f(vertice->x, vertice->y, vertice->z);
					}
					glEnd();
				}
			}
			
			glPopMatrix();
			
			if(m2_visible) {
				glLineWidth(5);
				glDisable(GL_TEXTURE_2D);
				glBegin(GL_LINES);
				
				glColor3f(1, 1, 1);
				glVertex3f(0, 0, 0);
				glVertex3f(m2_vec.x, m2_vec.y, m2_vec.z);
				
				glColor3f(1, 0, 0);
				glVertex3f(0, 0, 0);
				glVertex3f(m2_vec.x, 0, 0);
				
				glColor3f(0, 1, 0);
				glVertex3f(0, 0, 0);
				glVertex3f(0, m2_vec.y, 0);
				
				glColor3f(0, 0, 1);
				glVertex3f(0, 0, 0);
				glVertex3f(0, 0, m2_vec.z);
				
				glEnd();
				glEnable(GL_TEXTURE_2D);
			}
		}
};

/*
class MarisaPattern : public Pattern {
	private:
		const aiScene* scene;
		
		Texture** textures;
		
	public:
		MarisaPattern() : Pattern("patterns/m2.patt") {
			scene = importer->ReadFile("kirisame_marisa.x", 0);
			
			if(scene != NULL) {
				std::cout << "Loaded model: " << scene->mNumMeshes << " meshes, " << scene->mNumMaterials << " materials." << std::endl;
				for(int i = 0; i < scene->mNumMaterials; i++) {
					aiMaterial* material = scene->mMaterials[i];
					
					std::cout << "Material " << i << ": " << material->mNumProperties << " properties." << std::endl;
					
					for(int j = 0; j < material->mNumProperties; j++) {
						aiMaterialProperty* property = material->mProperties[j];
						
						std::cout << "  " << std::string(property->mKey.data, property->mKey.length) << ": " << std::string(property->mData, property->mDataLength) << std::endl;
					}
					
				}
				
				for(int i = 0; i < scene->mNumMeshes; i++) {
					aiMesh* mesh = scene->mMeshes[i];
					
					std::cout << "Mesh " << i << ": mMaterialIndex = " << mesh->mMaterialIndex << ", mNumBones = " << mesh->mNumBones << std::endl;
					
				}
				
			} else {
				std::cerr << "Failed to import model." << std::endl << importer->GetErrorString() << std::endl;
			}

			Texture* tex_head = new TextureSDL("Ma-head.tga");
			Texture* tex_1 = new TextureSDL("Marisa-1.bmp");
			Texture* tex_B = new TextureSDL("B.spa");
			Texture* tex_2 = new TextureSDL("Marisa-2.tga");
			Texture* tex_S = new TextureSDL("S.spa");
			
			textures = new Texture*[8];
			
			//textures[0] = tex_head;
			//textures[1] = tex_1;
			//textures[2] = tex_B;
			//textures[3] = tex_1;
			//textures[4] = tex_B;
			//textures[5] = tex_2;
			//textures[6] = tex_1;
			//textures[7] = tex_2;

			textures[0] = tex_head; // face
			textures[1] = tex_1; // body + hat
			textures[2] = tex_B; // buttons
			textures[3] = tex_2; // skirt, arms, hair, legs
			textures[4] = tex_S; // accessories

			patt_width = 10.0;
		}
		
	protected:
		virtual void draw() {
			double    gl_para[16];
			arglCameraView(patt_trans_kake, gl_para, 1.0);
			
			glMatrixMode(GL_MODELVIEW);
			glLoadMatrixd(gl_para);
			
			//glTranslatef(0.0, -8.0, 0.0);
			
			glRotatef(90, 1, 0, 0);
			glRotatef(180, 0, 1, 0);
			
			//glColor4f(1, 0, 0, 0.5);
			glPolygonMode(GL_FRONT, GL_FILL);
			
			for(int i = 0; i < scene->mNumMeshes; i++) { // scene->mNumMeshes
				aiMesh* mesh = scene->mMeshes[i];
				
				glBindTexture(GL_TEXTURE_2D, textures[i]->tex());
				
				for(int j = 0; j < mesh->mNumFaces; j++) {
					aiFace* face = &mesh->mFaces[j];
					
					glBegin(GL_POLYGON);
					for(int k = 0; k < face->mNumIndices; k++) {
						aiVector3D* texcoord = &mesh->mTextureCoords[0][face->mIndices[k]];
						glTexCoord2f(texcoord->x, 2 - texcoord->y);
						aiVector3D* vertice = &mesh->mVertices[face->mIndices[k]];
						glVertex3f(vertice->x, vertice->y, vertice->z);
					}
					glEnd();
				}
			}
			
		}
};
*/

int main(int argc, char **argv) {
	try {
		Application* app = new Application();
		
		Assimp::Importer _importer;
		importer = &_importer;
		
		app->patt = new FiskPattern();
		
		//Assimp::Importer _importer2;
		//importer = &_importer2;
		
		//app->patt2 = new MarisaPattern();
		
		app->run();
	} catch(std::runtime_error e) {
		std::cerr << "Exception caught: " << e.what() << std::endl;
	}
	return 0;
}