Browse Source

cooles update, funtzt mehr, materialien noch nich ganz

seba 12 years ago
parent
commit
a853e102eb
4 changed files with 196 additions and 23 deletions
  1. 106
    11
      models/load3ds.cpp
  2. 3
    0
      models/load3ds.h
  3. 60
    11
      models/model.cpp
  4. 27
    1
      models/model.h

+ 106
- 11
models/load3ds.cpp View File

@@ -22,35 +22,35 @@ bool Load3ds::parse(Model *m) {
22 22
 	
23 23
 	unsigned short ident;
24 24
 	unsigned int len;
25
+	
25 26
 	bool modelloaded = false;
27
+	bool materialloaded = false;
28
+	
29
+	Material mat;
26 30
 	ModelObject modobj;
27 31
 	m->clear();
28
-	std::cout << "size: " << sizeof(int) << std::endl;
29 32
 	while(!mfile.eof()) {
30 33
 		ident = len = 0;
31 34
 		mfile.read((char *)&ident, 2);
32 35
 		mfile.read((char *)&len, 4);
33
-// 		mfile.read((char *)&m, 1);
34
-// 		mfile.read((char *)&n, 1);
36
+
35 37
 		std::cout << "Chunk: 0x" << std::hex << ident << " (" << std::dec << len << ")" << std::endl;
36 38
 		switch(ident) {
37 39
 			case 0x4d4d:
38 40
 				// Main Chunk
39
-				std::cout << "yeah!" << std::endl;
40 41
 			break;
41 42
 			case 0x3d3d:
42 43
 				// 3D Editor Chunk
43
-				std::cout << "editorchunk" << std::endl;
44 44
 			break;
45 45
 			case 0x4000:
46 46
 			{
47 47
 				if(modelloaded) {
48
-					modobj.calcNormales();
48
+					modobj.calcNormales();			
49 49
 					m->addObject(modobj);
50 50
 					modobj.clear();
51 51
 				}
52 52
 				modelloaded = true;
53
-				std::cout << "namechunk" << std::endl;
53
+// 				std::cout << "namechunk" << std::endl;
54 54
 				std::string name;
55 55
 				char c;
56 56
 				do {
@@ -67,6 +67,7 @@ bool Load3ds::parse(Model *m) {
67 67
 				unsigned short panz;
68 68
 				Punkt3D p;
69 69
 				mfile.read((char*)&panz, 2);
70
+// 				std::cout << "\t\t\tVertexanz: " << panz << std::endl;
70 71
 				glBegin(GL_LINE_LOOP);
71 72
 				for(unsigned int i=0; i<panz; i++) {
72 73
 					mfile.read((char*)&p.x, sizeof(float));
@@ -84,6 +85,7 @@ bool Load3ds::parse(Model *m) {
84 85
 				unsigned short panz, tp;
85 86
 				Punkt3D p;
86 87
 				mfile.read((char*)&panz, 2);
88
+// 				std::cout << "\t\t\tPolygonanz: " << panz << std::endl;
87 89
 				for(unsigned int i=0; i<panz; i++) {
88 90
 					mfile.read((char*)&tp, 2);
89 91
 					p.x = tp;
@@ -91,16 +93,40 @@ bool Load3ds::parse(Model *m) {
91 93
 					p.y = tp;
92 94
 					mfile.read((char*)&tp, 2);
93 95
 					p.z = tp;
94
-					p.print("Polygon");
96
+					mfile.read((char*)&tp, 2);
97
+					tp; // flags
98
+// 					p.print("Polygon");
95 99
 					modobj.polygon.push_back(p);
96 100
 				}
97 101
 			}
98 102
 			break;
103
+			// Komischer Chunk, ein haufen Zahlen sind sich selber zugewiesen
104
+
105
+			case 0x4130:
106
+			{
107
+				std::string name;
108
+				char c;
109
+				do {
110
+					mfile.read(&c, 1);
111
+					name += c;
112
+				} while(c!=0);
113
+				modobj.mat = m->findMaterial(name);
114
+				std::cout << "Mat Name: " << name << std::endl;
115
+				unsigned short panz, tp;
116
+				mfile.read((char*)&panz, 2);
117
+				mfile.ignore(panz*2);
118
+// 				for(unsigned int i=0; i<panz; i++) {
119
+// 					mfile.read((char*)&tp, 2);
120
+// 					std::cout << i << " ==> " << tp << std::endl;
121
+// 				}
122
+			}
123
+			break;
99 124
 			case 0x4140:
100 125
 			{
101 126
 				unsigned short panz;
102 127
 				Punkt2D p;
103 128
 				mfile.read((char*)&panz, 2);
129
+// 				std::cout << "\t\t\tUV-Anz: " << panz << std::endl;
104 130
 				for(unsigned int i=0; i<panz; i++) {
105 131
 					mfile.read((char*)&p.x, sizeof(float));
106 132
 					mfile.read((char*)&p.y, sizeof(float));
@@ -108,25 +134,94 @@ bool Load3ds::parse(Model *m) {
108 134
 				}
109 135
 			}
110 136
 			break;
137
+			case 0xafff:
138
+				// Materialchunk
139
+			break;
140
+			case 0xa000:
141
+			{
142
+				if(materialloaded) {
143
+					m->addMaterial(mat);
144
+					mat.clear();
145
+				}
146
+				materialloaded = true;
147
+				std::string name;
148
+				char c;
149
+				do {
150
+					mfile.read(&c, 1);
151
+					name += c;
152
+				} while(c!=0);
153
+				mat.name = name;
154
+				std::cout << "Mat Name: " << name << std::endl;
155
+			}
156
+			break;
157
+			case 0xa010:
158
+			{
159
+				GLColor col;
160
+				col = readColorChunk(&mfile);
161
+				mat.ambient = col;
162
+				std::cout << col.r << ", " << col.g << ", " << col.b << std::endl;
163
+			}
164
+			break;
165
+			case 0xa020:
166
+			{
167
+				GLColor col;
168
+				col = readColorChunk(&mfile);
169
+				mat.diffuse = col;
170
+				std::cout << col.r << ", " << col.g << ", " << col.b << std::endl;
171
+			}
172
+			break;
173
+			case 0xa030:
174
+			{
175
+				GLColor col;
176
+				col = readColorChunk(&mfile);
177
+				mat.specular = col;
178
+				std::cout << col.r << ", " << col.g << ", " << col.b << std::endl;
179
+			}
180
+			break;
111 181
 			default:
112 182
 				//Switch Chunk
113
-				std::cout << "einfach ignorieren..." << std::endl;
183
+// 				std::cout << "einfach ignorieren..." << std::endl;
114 184
 				mfile.ignore(len-6);
185
+// 				std::cout << "moep" << std::endl;
115 186
 			break;
116 187
 		}
117 188
 	}
189
+	if(materialloaded)
190
+		m->addMaterial(mat);
118 191
 
119 192
 	if(modelloaded) {
120 193
 		modobj.calcNormales();
194
+		modobj.mat = m->findMaterial(modobj.getName());
121 195
 		m->addObject(modobj);
122
-		modobj.clear();
123 196
 	}
124
-	
197
+	std::cout << "Ende Load" << std::endl;
125 198
 	mfile.close();
126 199
 
127 200
 	return true;
128 201
 }
129 202
 
203
+GLColor Load3ds::readColorChunk(std::ifstream *file) {
204
+	GLColor col;
205
+	unsigned short id, tp;
206
+	unsigned int clen;
207
+	file->read((char*)&id, 2); // Color, warscheinlich nur bytes, wenn nich implementiere funktion
208
+	file->read((char*)&clen, 4); // len, gleiches wie oben
209
+	switch(id)  {
210
+		case 0x11:
211
+				file->read((char*)&tp, 1);
212
+				col.r = tp/255.0f;
213
+				file->read((char*)&tp, 1);
214
+				col.g = tp/255.0f;
215
+				file->read((char*)&tp, 1);
216
+				col.b = tp/255.0f;
217
+		break;
218
+		default:
219
+			file->ignore(clen-6);
220
+		break;
221
+	}
222
+	return col;
223
+}
224
+
130 225
 void Load3ds::unload() {
131 226
 	parsed = false;
132 227
 	error = false;

+ 3
- 0
models/load3ds.h View File

@@ -6,6 +6,7 @@
6 6
 #include <string>
7 7
 #include <vector>
8 8
 #include "model.h"
9
+#include "../glcolor.h"
9 10
 
10 11
 class Chunk {
11 12
 	private:
@@ -24,6 +25,8 @@ class Load3ds {
24 25
 		
25 26
 		bool parsed;
26 27
 		bool error;
28
+		
29
+		GLColor readColorChunk(std::ifstream *file);
27 30
 	public:
28 31
 		Load3ds(std::string _fname, bool parse=true);
29 32
 		

+ 60
- 11
models/model.cpp View File

@@ -13,35 +13,70 @@ void ModelObject::clear() {
13 13
 
14 14
 void ModelObject::calcNormales() {
15 15
 	normal.clear();
16
-	Punkt3D a, b;
16
+	Punkt3D a, b, c;
17 17
 	for(unsigned int i=0; i<polygon.size(); i++) {
18 18
 		a = vertex.at((unsigned short)polygon[i].x) - vertex.at((unsigned short)polygon[i].z);
19 19
 		b = vertex.at((unsigned short)polygon[i].y) - vertex.at((unsigned short)polygon[i].z);
20
-		normal.push_back(a.kreuzprodukt(b));
20
+		c = a.kreuzprodukt(b);
21
+		c.normalize();
22
+		normal.push_back(c);
23
+// 		c.print("Normale");
21 24
 	}
22 25
 	
23 26
 }
24 27
 
25 28
 void ModelObject::render() {
26
-	std::cout << "render " << name << std::endl;
29
+// 	std::cout << "render " << name << " mit " << polygon.size()<< std::endl;
30
+// 	std::cout << "Vertex: " << vertex.size() << std::endl;
27 31
 	glBegin(GL_TRIANGLES);
28 32
 		for(unsigned int i=0; i<polygon.size(); i++) {
29
-	// 		glNormal3f(normal.at(    (unsigned short)polygon[i].x));
30
-	// 		glTexCoord2f(mapcoord.at((unsigned short)polygon[i].x));
33
+// 			std::cout << "whee nr " <<i << std::endl;
34
+// 			polygon[i].print();
35
+// 			std::cout << "Stat: " << polygon.size() << " " << vertex.size() << " " << mapcoord.size() << " " << normal.size() << std::endl;
36
+// 			glNormal3f(normal.at(    (unsigned short)polygon[i].x));
37
+// 			glColor3f(mat.diffuse.r, mat.diffuse.g, mat.diffuse.b);
38
+			glColorGLC(mat.ambient);
39
+// 			std::cout << mat.ambient.r << ", " << mat.ambient.g << ", " << mat.ambient.b << std::endl;
40
+
41
+			glNormal3f(normal.at(i));
42
+				
43
+			glTexCoord2f(mapcoord.at((unsigned short)polygon[i].x));
31 44
 			glVertex3f(vertex.at(    (unsigned short)polygon[i].x));
32 45
 			
33
-	// 		glNormal3f(normal.at(    (unsigned short)polygon[i].y));
34
-	// 		glTexCoord2f(mapcoord.at((unsigned short)polygon[i].y));
46
+
47
+			glTexCoord2f(mapcoord.at((unsigned short)polygon[i].y));
35 48
 			glVertex3f(vertex.at(    (unsigned short)polygon[i].y));
36 49
 	
37
-	// 		glNormal3f(normal.at(    (unsigned short)polygon[i].z));
38
-	// 		glTexCoord2f(mapcoord.at((unsigned short)polygon[i].z));
50
+
51
+			glTexCoord2f(mapcoord.at((unsigned short)polygon[i].z));
39 52
 			glVertex3f(vertex.at(    (unsigned short)polygon[i].z));
40
-			vertex.at(    (unsigned short)polygon[i].x).print();
53
+// 			vertex.at(    (unsigned short)polygon[i].x).print();
41 54
 		}
42 55
 	glEnd();
43 56
 }
44 57
 
58
+std::string ModelObject::getName() {
59
+	return name;
60
+}
61
+
62
+Material::Material() {
63
+	clear();
64
+}
65
+		
66
+void Material::clear() {
67
+	name = "";
68
+		
69
+	ambient.set(0.0f, 0.0f, 0.0f);
70
+	diffuse.set(0.0f, 0.0f, 0.0f);
71
+	specular.set(0.0f, 0.0f, 0.0f);
72
+	percent = 0.0f;
73
+}
74
+
75
+std::string Material::getName() {
76
+	return name;
77
+}
78
+
79
+
45 80
 Model::Model() {
46 81
 	
47 82
 }
@@ -55,7 +90,21 @@ void Model::addObject(ModelObject c) {
55 90
 }
56 91
 
57 92
 void Model::render() {
58
-	std::cout << "Render Model " << objects.size() << std::endl;
93
+// 	std::cout << "Render Model " << objects.size() << std::endl;
59 94
 	for(unsigned int i=0; i<objects.size(); i++) 
60 95
 		objects[i].render();
61 96
 }
97
+
98
+void Model::addMaterial(Material m) {
99
+	materials.push_back(m);
100
+}
101
+
102
+Material Model::findMaterial(std::string name) {
103
+	for(unsigned int i=0; i<materials.size(); i++) {
104
+		if(name==materials[i].getName()) {
105
+			std::cout<<"found!"<<std::endl;
106
+			return materials[i];
107
+		}
108
+	}
109
+	return Material();
110
+}

+ 27
- 1
models/model.h View File

@@ -6,8 +6,28 @@
6 6
 #include "../punkt2d.h"
7 7
 #include "../punkt3d.h"
8 8
 #include "../gltexture.h"
9
+#include "../glcolor.h"
9 10
 // #include "load3ds.h"
10 11
 
12
+
13
+class Material {
14
+	friend class Load3ds;
15
+	friend class ModelObject;
16
+	private:
17
+		std::string name;
18
+		
19
+		GLColor ambient;
20
+		GLColor diffuse;
21
+		GLColor specular;
22
+		float percent;
23
+	public:
24
+		Material();
25
+		
26
+		void clear();
27
+		std::string getName();
28
+};
29
+
30
+
11 31
 class ModelObject {
12 32
 	friend class Load3ds;
13 33
 	private:
@@ -17,9 +37,12 @@ class ModelObject {
17 37
 		std::vector<Punkt3D> polygon;
18 38
 		void calcNormales();
19 39
 		std::string name;
40
+		
41
+		Material mat;
20 42
 	public:
21 43
 		ModelObject();
22 44
 		
45
+		std::string getName();
23 46
 		void render();
24 47
 		void clear();
25 48
 	
@@ -28,13 +51,16 @@ class ModelObject {
28 51
 class Model {
29 52
 	private:
30 53
 		std::vector<ModelObject> objects;
31
-		
54
+		std::vector<Material> materials;
32 55
 		
33 56
 	public:
34 57
 		Model();
35 58
 		void addObject(ModelObject c);
59
+		void addMaterial(Material m);
36 60
 		void clear();
37 61
 		void render();
62
+		
63
+		Material findMaterial(std::string name);
38 64
 };
39 65
 
40 66
 #endif

Loading…
Cancel
Save