Browse Source

OBJ Loader

Neue Quaternionklasse
header in emath fuer punkt3d rausgenommen
Sebastian 12 years ago
parent
commit
1c613ca59c
30 changed files with 845 additions and 79 deletions
  1. 3
    3
      Makefile
  2. 0
    1
      emath.cpp
  3. 0
    1
      emath.h
  4. 1
    0
      emath_opengl.h
  5. 47
    0
      extstring.cpp
  6. 12
    0
      extstring.h
  7. 22
    1
      glcamera.cpp
  8. 2
    0
      glcamera.h
  9. 1
    0
      matrix.h
  10. 13
    0
      model/Makefile
  11. 235
    0
      model/loadobj.cpp
  12. 16
    0
      model/loadobj.h
  13. 153
    0
      model/model.cpp
  14. 89
    0
      model/model.h
  15. 5
    0
      model/modelloader.cpp
  16. 17
    0
      model/modelloader.h
  17. 0
    0
      model/old/Makefile
  18. 0
    0
      model/old/load3ds.cpp
  19. 0
    0
      model/old/load3ds.h
  20. 0
    0
      model/old/model.cpp
  21. 0
    0
      model/old/model.h
  22. 11
    3
      punkt2d.cpp
  23. 5
    1
      punkt2d.h
  24. 7
    1
      punkt3d.cpp
  25. 2
    2
      punkt3d.h
  26. 10
    0
      quader.cpp
  27. 15
    0
      quader.h
  28. 143
    55
      quaternion.cpp
  29. 35
    11
      quaternion.h
  30. 1
    0
      rotationsmatrix.h

+ 3
- 3
Makefile View File

@@ -1,9 +1,9 @@
1 1
 CC = g++
2 2
 AR = ar
3
-OBJECTS = punkt3d.o punkt2d.o emath.o emath_opengl.o glcolor.o gldrawhelper.o glfontengine.o glrect.o gltexture.o matrix.o quaternion.o rotationsmatrix.o glsdlscreen.o sdlfuncs.o fpsmanager.o glcamera.o catmullromspline.o
3
+OBJECTS = punkt3d.o punkt2d.o emath.o emath_opengl.o glcolor.o gldrawhelper.o glfontengine.o glrect.o gltexture.o matrix.o quaternion.o rotationsmatrix.o glsdlscreen.o sdlfuncs.o fpsmanager.o glcamera.o catmullromspline.o extstring.o
4 4
 OBJOPT = -Wall -c `sdl-config --cflags`
5
-SUBDIRS = glgui glmenu models
6
-SUBDIROBJECTS = glgui/*.o glmenu/*.o models/*.o
5
+SUBDIRS = glgui glmenu model
6
+SUBDIROBJECTS = glgui/*.o glmenu/*.o model/*.o
7 7
 VERSION = 0.0.1
8 8
 LIBNAME = libsegl
9 9
 

+ 0
- 1
emath.cpp View File

@@ -27,7 +27,6 @@ float ssin(float c) {
27 27
 		case 270:
28 28
 			return -1.0f;
29 29
 		default:
30
-// 			std::cout << "ssin sollte keinen defaultwert zurückliefern. c=" << c << ", t=" << t << std::endl;
31 30
 			return sin(deg2rad(c));
32 31
 	}
33 32
 }

+ 0
- 1
emath.h View File

@@ -5,7 +5,6 @@
5 5
 #include <cmath>
6 6
 #include <SDL_opengl.h>
7 7
 
8
-#include "punkt3d.h" /* aus kompatibilitätsgründen */
9 8
 
10 9
 float deg2rad(float deg);
11 10
 float rad2deg(float rad);

+ 1
- 0
emath_opengl.h View File

@@ -2,6 +2,7 @@
2 2
 #define __EMATH_OPENGL
3 3
 
4 4
 #include <cmath>
5
+#include "punkt3d.h"
5 6
 #include "emath.h"
6 7
 #include "rotationsmatrix.h"
7 8
 #include <SDL_opengl.h>

+ 47
- 0
extstring.cpp View File

@@ -0,0 +1,47 @@
1
+#include "extstring.h"
2
+
3
+int explode(std::string str, std::string delim, std::vector<std::string> *erg, bool clean) {
4
+	erg->clear();
5
+	
6
+	int treffer=0;
7
+	std::string tmp;
8
+	unsigned int pos;
9
+	
10
+	while((pos = str.find(delim))!=std::string::npos) {
11
+		tmp = str.substr(0, pos);
12
+		
13
+		if(!clean || tmp!="") {
14
+			// tmp nicht leer, hinzufügen
15
+			erg->push_back(tmp);
16
+			treffer++;
17
+		}
18
+		
19
+		str = str.substr(pos+1);	// alten kram entfernen
20
+	}
21
+	
22
+	if(str.length()>0) {
23
+		treffer++;
24
+		erg->push_back(str);
25
+	}
26
+	
27
+	return treffer;
28
+}
29
+
30
+std::string trim(std::string t) {
31
+	while(t.length() && isspace(t[0])) {
32
+		t = t.substr(1);
33
+	}
34
+	
35
+	while(t.length() && ( isspace(t[t.length()-1]) )) {
36
+		t = t.substr(0, t.length()-2);
37
+	}
38
+	return t;
39
+}
40
+
41
+std::string basename(std::string str) {
42
+	return str.substr( str.rfind("/"));
43
+}
44
+
45
+std::string rbasename(std::string str) {
46
+	return str.substr(0, str.rfind("/")+1);
47
+}

+ 12
- 0
extstring.h View File

@@ -0,0 +1,12 @@
1
+#ifndef __EXTSTRING_H
2
+#define __EXTSTRING_H
3
+
4
+#include <string>
5
+#include <vector>
6
+
7
+int explode(std::string str, std::string delim, std::vector<std::string> *erg, bool clean=false);
8
+std::string trim(std::string t);
9
+std::string basename(std::string str);
10
+std::string rbasename(std::string str);
11
+
12
+#endif

+ 22
- 1
glcamera.cpp View File

@@ -100,7 +100,7 @@ void GLCamera::rotateUp(float sec) {
100 100
 		rotmatX.set(rotx);
101 101
 		doupdate = true;
102 102
 	} else  {
103
-		//std::cout << "zu groß" << std::endl;
103
+		//std::cout << "zu gro�" << std::endl;
104 104
 	}
105 105
 // 	rotmat.set(dir.kreuzprodukt(norm), -apersec*sec);
106 106
 // 	norm = rotmat * norm;
@@ -122,6 +122,27 @@ void GLCamera::rotateDown(float sec) {
122 122
 // 	dir.normalize();
123 123
 }
124 124
 
125
+void GLCamera::handleKeys(float sec) {
126
+	Uint8 *keys = SDL_GetKeyState(NULL);
127
+	
128
+	if(keys[SDLK_w])
129
+		moveForward(sec);
130
+	if(keys[SDLK_s])
131
+		moveBackward(sec);
132
+	if(keys[SDLK_UP])
133
+		rotateUp(sec);
134
+	if(keys[SDLK_DOWN])
135
+		rotateDown(sec);
136
+	if(keys[SDLK_LEFT])
137
+		rotateLeft(sec);
138
+	if(keys[SDLK_RIGHT])
139
+		rotateRight(sec);
140
+	if(keys[SDLK_a])
141
+		moveLeft(sec);
142
+	if(keys[SDLK_d])
143
+		moveRight(sec);
144
+}
145
+
125 146
 float GLCamera::getXrot() {
126 147
 	return rotx;
127 148
 }

+ 2
- 0
glcamera.h View File

@@ -44,6 +44,8 @@ class GLCamera {
44 44
 		void rotateUp(float sec);
45 45
 		void rotateDown(float sec);
46 46
 		
47
+		void handleKeys(float sec);
48
+		
47 49
 		float getXrot();
48 50
 		float getYrot();
49 51
 		

+ 1
- 0
matrix.h View File

@@ -4,6 +4,7 @@
4 4
 #include <iostream>
5 5
 #include <iomanip>
6 6
 
7
+#include "punkt3d.h"
7 8
 #include "emath.h"
8 9
 
9 10
 class Matrix {

+ 13
- 0
model/Makefile View File

@@ -0,0 +1,13 @@
1
+CC = g++
2
+OBJOPT = -c `sdl-config --cflags` -Wall
3
+OBJECTS = model.o modelloader.o loadobj.o
4
+
5
+glguilib: $(OBJECTS)
6
+# 	rm glgui.a -f
7
+	ar crus glmenu.a $(OBJECTS)
8
+
9
+%.o: %.cpp %.h
10
+	$(CC) $(OBJOPT) $<
11
+
12
+clean:
13
+	rm -f $(OBJECTS)

+ 235
- 0
model/loadobj.cpp View File

@@ -0,0 +1,235 @@
1
+#include "loadobj.h"
2
+
3
+LoadOBJ::LoadOBJ(std::string _filename) : Modelloader(_filename) {
4
+	
5
+}
6
+
7
+bool LoadOBJ::load(Model *m) {
8
+	// Filestreams
9
+	std::ifstream file(filename.c_str());
10
+	std::ifstream matfile;
11
+	std::string inbuf;			// TMP-Var
12
+	std::vector<std::string> v;
13
+	std::vector<std::string> v2;
14
+	int anz;
15
+	unsigned int vertanz=0, polyanz=0, matanz=0, texanz=0, normanz=0;
16
+	std::string materials;
17
+// 	Punkt2D *texdata = 0;
18
+// 	texdataanz = 0;
19
+// 	Punkt3D *normdata = 0;
20
+// 	normdata = 0;
21
+	Material *usemat;
22
+	
23
+	if(!file)
24
+		return false;
25
+	
26
+	// start counting items, read file
27
+	while(getline(file, inbuf)) {
28
+		anz = explode(inbuf, " ", &v);
29
+		if(!anz)
30
+			continue;
31
+		
32
+		if(v[0]=="f")
33
+			polyanz++;			// polygon
34
+		else if(v[0]=="v")
35
+			vertanz++;			// vertex
36
+		else if(v[0]=="mtllib") {
37
+			if(anz>1)
38
+				materials = trim(v[1]);
39
+// 			std::cout << "V1: " << v[1] << std::endl;
40
+		} else if(v[0]=="vt")
41
+			texanz++;			// texcoord
42
+		else if(v[0]=="vn")
43
+			normanz++;			// normal
44
+	}
45
+	
46
+	if(materials!="") {
47
+		matfile.open((rbasename(filename)+materials).c_str());
48
+		if(matfile) {
49
+			while(getline(matfile, inbuf)) {
50
+				anz = explode(inbuf, " ", &v);
51
+				if(!anz)
52
+					continue;
53
+				if(v[0]=="newmtl")
54
+					matanz++;
55
+			}
56
+		}
57
+	}
58
+	
59
+// 	std::cout << "Vert: " << vertanz << " Poly: " << polyanz << " Mat: " << matanz << " (Matfile: " << materials << ") Norm: " << normanz << std::endl;
60
+	
61
+	if(!vertanz || !polyanz)
62
+		return false;
63
+	
64
+	if(m->isLoaded())
65
+		m->unload();
66
+	
67
+	// Allocate memory for filecontent
68
+	m->meshdataanz = vertanz;
69
+	m->meshdata = new Punkt3D[vertanz];
70
+	
71
+	m->polydataanz = polyanz;
72
+	m->polydata = new Meshpolygon[polyanz];
73
+	
74
+	if(matanz) {
75
+		m->matdataanz = matanz;
76
+		m->matdata = new Material[matanz];
77
+	}
78
+	
79
+	m->texdataanz = texanz;
80
+	m->texdata = new Punkt2D[matanz];
81
+	
82
+	m->normdataanz = normanz;
83
+	m->normdata = new Punkt3D[normanz];
84
+	
85
+	
86
+	// Readout Material
87
+	matfile.clear();
88
+	matfile.seekg(0);
89
+	if(matanz&&matfile) {		// Got milk.. eh.. material?
90
+		matanz = 0;
91
+		Material mat;
92
+		while(getline(matfile, inbuf)) {
93
+			anz = explode(inbuf, " ", &v);
94
+			if(!anz)
95
+				continue;
96
+			
97
+			if(v[0]=="newmtl") {
98
+				if(matanz!=0)
99
+					m->matdata[matanz-1] = mat;		// Push material
100
+				matanz++;
101
+				mat = Material();
102
+				mat.name = trim(v[1]);
103
+			} else if(v[0]=="Ka") {
104
+				if(anz<4)
105
+					continue;
106
+				mat.ambient.set(std::atof(v[1].c_str()), std::atof(v[2].c_str()), std::atof(v[3].c_str()));
107
+			} else if(v[0]=="Kd") {
108
+				if(anz<4)
109
+					continue;
110
+				mat.diffuse.set(std::atof(v[1].c_str()), std::atof(v[2].c_str()), std::atof(v[3].c_str()));
111
+			} else if(v[0]=="Ks") {
112
+				if(anz<4)
113
+					continue;
114
+				mat.specular.set(std::atof(v[1].c_str()), std::atof(v[2].c_str()), std::atof(v[3].c_str()));
115
+			}
116
+		}
117
+		// Last material wasnt pushed
118
+		if(matanz!=0)
119
+			m->matdata[matanz-1] = mat;
120
+	}
121
+	
122
+	
123
+	// Readout File
124
+	usemat = &(m->backupmat);		// If material required but not avilable use backup
125
+	
126
+	file.clear();
127
+	file.seekg(0);
128
+	polyanz = vertanz = normanz = texanz = 0;	// Reset counters
129
+	
130
+	while(getline(file, inbuf)) {
131
+		anz = explode(inbuf, " ", &v);
132
+		if(!anz)
133
+			continue;
134
+		
135
+		if(v[0]=="v") {
136
+			if(anz<4)
137
+				continue;
138
+			m->meshdata[vertanz].set(std::atof(v[1].c_str()), std::atof(v[2].c_str()), std::atof(v[3].c_str()));
139
+			vertanz++;
140
+		} else if(v[0]=="vn") {
141
+			if(anz<4)
142
+				continue;
143
+			m->normdata[normanz].set(std::atof(v[1].c_str()), std::atof(v[2].c_str()), std::atof(v[3].c_str()));
144
+			normanz++;
145
+		} else if(v[0]=="vt") {
146
+			if(anz<3)
147
+				continue;
148
+			m->texdata[texanz].set(std::atof(v[1].c_str()), std::atof(v[2].c_str()));
149
+			texanz++;
150
+		} else if(v[0]=="f") {
151
+			if(anz<4)
152
+				continue;
153
+			
154
+			unsigned int arr[3][3] = { {0,0,0}, {0,0,0}, {0,0,0} };
155
+			for(unsigned int i=0; i<3; i++) {
156
+				int zanz = explode(v[i+1], "/", &v2);
157
+// 				std::cout << "explode " << v[i+1] << " anz " << zanz << std::endl;
158
+				if(zanz<3) {
159
+					if(zanz==1)
160
+						arr[i][0] = std::atoi(v2[0].c_str());
161
+					continue;
162
+				}
163
+				arr[i][0] = std::atoi(v2[0].c_str());
164
+				arr[i][1] = std::atoi(v2[1].c_str());
165
+				arr[i][2] = std::atoi(v2[2].c_str());	
166
+// 				std::cout << "Loaded: " << arr[i][0] << " " << arr[i][1] << " " << arr[i][2] << " " << std::endl;
167
+// 				std::cout << "From:   " << v2[0] << " " << v2[1] << " " << v2[2] << " " << std::endl;
168
+			}
169
+			
170
+			if(arr[0][0])
171
+				m->polydata[polyanz].m1.point  = &(m->meshdata[arr[0][0]-1]);
172
+			if(arr[0][1])
173
+				m->polydata[polyanz].m1.tex    = &(m->texdata[arr[0][1]-1]);
174
+			if(arr[0][2])
175
+				m->polydata[polyanz].m1.normal = &(m->normdata[arr[0][2]-1]);
176
+			
177
+			if(arr[1][0])
178
+			m->polydata[polyanz].m2.point  = &(m->meshdata[arr[1][0]-1]);
179
+			if(arr[1][1])
180
+				m->polydata[polyanz].m2.tex    = &(m->texdata[arr[1][1]-1]);
181
+			if(arr[1][2])
182
+				m->polydata[polyanz].m2.normal = &(m->normdata[arr[1][2]-1]);
183
+			
184
+			if(arr[2][0])
185
+				m->polydata[polyanz].m3.point  = &(m->meshdata[arr[2][0]-1]);
186
+			if(arr[2][1])
187
+				m->polydata[polyanz].m3.tex    = &(m->texdata[arr[2][1]-1]);
188
+			if(arr[2][2])
189
+				m->polydata[polyanz].m3.normal = &(m->normdata[arr[2][2]-1]);
190
+// 			std::cout << "POLY " << polyanz << std::endl;
191
+			polyanz++;
192
+		} else if(v[0]=="usemtl") {
193
+			if(anz<2)
194
+				continue;
195
+			std::string fterm = trim(v[1]);
196
+			for(unsigned int i=0; i < m->matdataanz; i++) {
197
+				if(m->matdata[i].name==fterm) {
198
+					usemat = &(m->matdata[i]);
199
+					break;
200
+				}
201
+			}
202
+		}
203
+		
204
+		v.clear();
205
+	}
206
+	
207
+	// Calculate bounding box
208
+	float minx=m->meshdata[0].x, maxx=m->meshdata[0].x, miny=m->meshdata[0].y, maxy=m->meshdata[0].y, minz=m->meshdata[0].z, maxz=m->meshdata[0].z;
209
+	for(unsigned int i=0; i<m->meshdataanz; i++) {
210
+		minx = std::min(minx, m->meshdata[i].x);
211
+		maxx = std::max(maxx, m->meshdata[i].x);
212
+		miny = std::min(miny, m->meshdata[i].y);
213
+		maxy = std::max(maxy, m->meshdata[i].y);
214
+		minz = std::min(minz, m->meshdata[i].z);
215
+		maxz = std::max(maxz, m->meshdata[i].z);
216
+	}
217
+	std::cout << "(" << minx << ", " << maxx << ") " << "(" << miny << ", " << maxy << ") " << "(" << minz << ", " << maxz << ") " << std::endl;
218
+	m->boundingbox.d.set(minx, miny, minz);
219
+	m->boundingbox.a.set(minx, miny, maxz);
220
+	m->boundingbox.b.set(maxx, miny, maxz);
221
+	m->boundingbox.c.set(maxx, miny, minz);
222
+	
223
+	m->boundingbox.h.set(minx, maxy, minz);
224
+	m->boundingbox.e.set(minx, maxy, maxz);
225
+	m->boundingbox.f.set(maxx, maxy, maxz);
226
+	m->boundingbox.g.set(maxx, maxy, minz);
227
+
228
+	m->boundingrad = std::max( std::abs(minx)+std::abs(maxx),
229
+					 std::max( std::abs(miny)+std::abs(maxy),
230
+							   std::abs(minz)+std::abs(maxz)));
231
+	
232
+	m->loaded = true;
233
+// 	std::cout << "Loaded!" << std::endl;
234
+	return true;
235
+}

+ 16
- 0
model/loadobj.h View File

@@ -0,0 +1,16 @@
1
+#ifndef __LOADOBJ_H
2
+#define __LOADOBJ_H
3
+
4
+#include "modelloader.h"
5
+#include "model.h"
6
+#include "../extstring.h"
7
+
8
+class LoadOBJ : public Modelloader {
9
+	private:
10
+		
11
+	public:
12
+		LoadOBJ(std::string _filename);
13
+		bool load(Model *m);
14
+};
15
+
16
+#endif

+ 153
- 0
model/model.cpp View File

@@ -0,0 +1,153 @@
1
+#include "model.h"
2
+
3
+// Class Meshpoint
4
+
5
+Meshpoint::Meshpoint() {
6
+	
7
+}
8
+
9
+Meshpoint::Meshpoint(Punkt3D v, Punkt2D vt) {
10
+	set(v, vt);
11
+}
12
+
13
+void Meshpoint::set(Punkt3D v, Punkt2D vt) {
14
+	vertex = v;
15
+	texcoord = vt;
16
+// 	normal = n;
17
+}
18
+
19
+void Meshpoint::use() {
20
+	glTexCoordP2D(texcoord);
21
+// 	glNormalP3D(normal);
22
+	glVertexP3D(vertex);
23
+}
24
+
25
+// Class Material
26
+
27
+Material::Material() {
28
+	name = "none";
29
+	ambient.set(1.0f, 1.0f, 1.0f);
30
+	diffuse.set(1.0f, 1.0f, 1.0f);
31
+	specular.set(1.0f, 1.0f, 1.0f);
32
+}
33
+
34
+Material::Material(std::string _name, GLColor _a, GLColor _d, GLColor _s) {
35
+	set(_name, _a, _d, _s);
36
+}
37
+
38
+void Material::set(std::string _name, GLColor _a, GLColor _d, GLColor _s) {
39
+	name = _name;
40
+	ambient = _a;
41
+	diffuse = _d;
42
+	specular = _s;
43
+}
44
+
45
+void Material::use() {
46
+	glColorGLC(diffuse);
47
+}
48
+
49
+// Polygonpoint
50
+Polygonpoint::Polygonpoint() {
51
+	point = 0;
52
+	tex = 0;
53
+	normal = 0;
54
+}
55
+
56
+void Polygonpoint::use() {
57
+	if(normal)
58
+		glNormalP3D(*normal);
59
+	if(tex)
60
+		glTexCoordP2D(*tex);
61
+	if(point)
62
+		glVertexP3D(*point);
63
+}
64
+
65
+// Class Meshpolygon
66
+	
67
+Meshpolygon::Meshpolygon() {
68
+	a = b = c = 0;
69
+}
70
+
71
+Meshpolygon::Meshpolygon(Meshpoint *_a, Meshpoint *_b, Meshpoint *_c) {
72
+	set(_a, _b, _c);
73
+}
74
+
75
+void Meshpolygon::set(Meshpoint *_a, Meshpoint *_b, Meshpoint *_c) {
76
+	a = _a;
77
+	b = _b;
78
+	c = _c;	
79
+}
80
+
81
+void Meshpolygon::render(GLenum mode) {
82
+// 	if(!a || !b || !c)
83
+// 		return;
84
+	
85
+	glBegin(mode);
86
+		m1.use();
87
+		m2.use();
88
+		m3.use();
89
+	glEnd();
90
+// 	std::cout << "Pos 1 " << *m1.point << std::endl;
91
+// 	std::cout << "Pos 2 " << *m2.point << std::endl;
92
+// 	std::cout << "Pos 3 " << *m3.point << std::endl;
93
+}
94
+// Class Model
95
+
96
+Model::Model() {
97
+	meshdata = 0;
98
+	meshdataanz = 0;
99
+	matdata = 0;
100
+	matdataanz = 0;
101
+	polydata = 0;
102
+	polydataanz = 0;
103
+	texdata = 0;
104
+	texdataanz = 0;
105
+	normdata = 0;
106
+	normdataanz = 0;
107
+	loaded = false;
108
+	
109
+	backupmat.diffuse.set(1.0f, 1.0f, 1.0f);
110
+}
111
+
112
+bool Model::isLoaded() {
113
+	return loaded;
114
+}
115
+
116
+void Model::unload() {
117
+	if(meshdata)
118
+		delete[](meshdata);
119
+	if(polydata)
120
+		delete[](polydata);
121
+	if(matdata)
122
+		delete[](matdata);
123
+	if(texdata)
124
+		delete[](texdata);
125
+	if(normdata)
126
+		delete[](normdata);
127
+	meshdataanz = polydataanz = matdataanz = texdataanz = normdataanz = 0;
128
+	meshdata = 0;
129
+	polydata = 0;
130
+	matdata = 0;
131
+	texdata = 0;
132
+	normdata = 0;
133
+	loaded = false;
134
+}
135
+
136
+void Model::render() {
137
+	if(!loaded) {
138
+// 		std::cout << "Model not loaded" << std::endl;
139
+		return;
140
+	}
141
+	glPushMatrix();
142
+// 	std::cout << " --- begin --- " << std::endl;
143
+	for(unsigned int i=0; i<polydataanz; i++) {
144
+// 		glColor3f((float)i/polydataanz, 0.0f, 1.0f);
145
+		polydata[i].render(GL_LINE_LOOP);
146
+	}
147
+	glPopMatrix();
148
+// 	std::cout << " --- end --- " << std::endl;
149
+}
150
+
151
+Model::~Model() {
152
+	unload();
153
+}

+ 89
- 0
model/model.h View File

@@ -0,0 +1,89 @@
1
+#ifndef __MODEL_H
2
+#define __MODEL_H
3
+
4
+#include <iostream>
5
+#include <SDL_opengl.h>
6
+#include "../punkt3d.h"
7
+#include "../punkt2d.h"
8
+#include "../glcolor.h"
9
+#include "../quader.h"
10
+
11
+class Meshpoint {
12
+	public:
13
+		Meshpoint();
14
+		Meshpoint(Punkt3D v, Punkt2D vt);
15
+		void set(Punkt3D v, Punkt2D vt);
16
+		void use();
17
+		
18
+		Punkt3D vertex;
19
+		Punkt2D texcoord;
20
+};
21
+
22
+class Material {
23
+	public:
24
+		Material();
25
+		Material(std::string _name, GLColor _a, GLColor _d, GLColor _s);
26
+		void set(std::string _name, GLColor _a, GLColor _d, GLColor _s);
27
+		void use();
28
+		
29
+		std::string name;
30
+		GLColor ambient, diffuse, specular;
31
+};
32
+
33
+class Polygonpoint {
34
+	public:
35
+		Polygonpoint();
36
+		void use();
37
+		Punkt3D *point;
38
+		Punkt2D *tex;
39
+		Punkt3D *normal;
40
+};
41
+
42
+class Meshpolygon {
43
+	public:
44
+		Meshpolygon();
45
+		Meshpolygon(Meshpoint *a, Meshpoint *b, Meshpoint *c);
46
+		void set(Meshpoint *a, Meshpoint *b, Meshpoint *c);
47
+		void render(GLenum mode=GL_TRIANGLES);
48
+		
49
+		Polygonpoint m1, m2, m3;
50
+		Meshpoint *a, *b, *c;
51
+		Material *mat;
52
+};
53
+
54
+class Model {
55
+	friend class Modelloader;
56
+	friend class LoadOBJ;
57
+	private:
58
+		bool loaded;
59
+		
60
+		
61
+		unsigned int meshdataanz;
62
+		unsigned int polydataanz;
63
+		unsigned int matdataanz;
64
+		unsigned int texdataanz;
65
+		unsigned int normdataanz;
66
+		
67
+		Punkt3D 	*meshdata;
68
+		Meshpolygon *polydata;
69
+		Material	*matdata;
70
+		
71
+		Punkt2D *texdata;
72
+		Punkt3D *normdata;
73
+		
74
+		Material backupmat;
75
+		
76
+		Quader boundingbox;
77
+		float boundingrad;
78
+	public:
79
+		Model();
80
+		~Model();
81
+		bool isLoaded();
82
+		void unload();
83
+		void render();
84
+		
85
+		Quader getBoundingBox() { return boundingbox; }
86
+		float getBoundingRadius() { return boundingrad; }
87
+};
88
+
89
+#endif

+ 5
- 0
model/modelloader.cpp View File

@@ -0,0 +1,5 @@
1
+#include "modelloader.h"
2
+
3
+Modelloader::Modelloader(std::string _filename) {
4
+	filename = _filename;
5
+}

+ 17
- 0
model/modelloader.h View File

@@ -0,0 +1,17 @@
1
+#ifndef __MODELLOADER_H
2
+#define __MODELLOADER_H
3
+
4
+#include <string>
5
+#include <fstream>
6
+#include "model.h"
7
+
8
+
9
+class Modelloader {
10
+	protected:
11
+		std::string filename;
12
+	public:
13
+		Modelloader(std::string _filename);
14
+		virtual bool load(Model *m)=0;
15
+};
16
+
17
+#endif

models/Makefile → model/old/Makefile View File


models/load3ds.cpp → model/old/load3ds.cpp View File


models/load3ds.h → model/old/load3ds.h View File


models/model.cpp → model/old/model.cpp View File


models/model.h → model/old/model.h View File


+ 11
- 3
punkt2d.cpp View File

@@ -19,6 +19,9 @@ void Punkt2D::print(std::string coordname) {
19 19
 	std::cout << coordname << "Coord: (" << x << ", " << y << ")" << std::endl;
20 20
 }
21 21
 
22
+float Punkt2D::abs() {
23
+	return sqrt(x*x + y*y);
24
+}
22 25
 
23 26
 Punkt2D Punkt2D::operator+(const Punkt2D &b) {
24 27
 	Punkt2D c;
@@ -131,6 +134,11 @@ void glTexCoord2f(Punkt2D p) {
131 134
 	glTexCoord2f(p.x, p.y);
132 135
 }
133 136
 
134
-// float abs(Punkt2D p) {
135
-// 	return p.abs();
136
-// }
137
+float abs(Punkt2D p) {
138
+	return p.abs();
139
+}
140
+
141
+// Fixed Headers
142
+void glTexCoordP2D(Punkt2D p) {
143
+	glTexCoord2f(p.x, p.y);
144
+}

+ 5
- 1
punkt2d.h View File

@@ -14,6 +14,7 @@ class Punkt2D {
14 14
 		
15 15
 		void set(float, float);
16 16
 		void print(std::string="");
17
+		float abs();
17 18
 		
18 19
 		// Operatoren
19 20
 		Punkt2D operator+(const Punkt2D&);
@@ -45,6 +46,9 @@ class Punkt2D {
45 46
 };
46 47
 
47 48
 void glTexCoord2f(Punkt2D);
48
-// float abs(Punkt3D);
49
+float abs(Punkt2D);
50
+
51
+// Fixed Headers
52
+void glTexCoordP2D(Punkt2D p);
49 53
 
50 54
 #endif

+ 7
- 1
punkt3d.cpp View File

@@ -205,7 +205,7 @@ float abs(Punkt3D p) {
205 205
 	return p.abs();
206 206
 }
207 207
 
208
-// OpenGL Funktionen für Punkt3D
208
+// OpenGL Funktionen f�r Punkt3D
209 209
 
210 210
 void glVertex3f(Punkt3D p) {
211 211
 	glVertex3f(p.x, p.y, p.z);
@@ -239,3 +239,9 @@ void glNormalP3D(Punkt3D p) {
239 239
 void glRotateP3D(float deg, Punkt3D vec) {
240 240
 	glRotatef(deg, vec.x, vec.y, vec.z);
241 241
 }
242
+
243
+void gluLookAt(Punkt3D pos, Punkt3D viewport, Punkt3D normal) {
244
+	gluLookAt( pos.x, pos.y, pos.z,
245
+			   viewport.x, viewport.y, viewport.z,
246
+			   normal.x, normal.y, normal.z );
247
+}

+ 2
- 2
punkt3d.h View File

@@ -57,7 +57,7 @@ class Punkt3D {
57 57
 
58 58
 float abs(Punkt3D);
59 59
 
60
-// OpenGL-Funktionen für Punkt3D
60
+// OpenGL-Functions for Punkt3D
61 61
 
62 62
 void glVertex3f(Punkt3D);
63 63
 void glTranslatef(Punkt3D);
@@ -71,6 +71,6 @@ void glTranslateP3D(Punkt3D);
71 71
 void glNormalP3D(Punkt3D);
72 72
 
73 73
 void glRotateP3D(float, Punkt3D);
74
-
74
+void gluLookAt(Punkt3D pos, Punkt3D viewport, Punkt3D normal);
75 75
 
76 76
 #endif

+ 10
- 0
quader.cpp View File

@@ -0,0 +1,10 @@
1
+#include "quader.h"
2
+
3
+void Quader::clean() {
4
+	a.set(0.0f, 0.0f, 0.0f);
5
+	b = c = d = e = f = g = h = a;
6
+}
7
+
8
+void Quader::render() {
9
+	glB
10
+}

+ 15
- 0
quader.h View File

@@ -0,0 +1,15 @@
1
+#ifndef __QUADER_H
2
+#define __QUADER_H
3
+
4
+#include <SDL_opengl.h>
5
+#include "punkt3d.h"
6
+
7
+
8
+class Quader {
9
+	public:
10
+		Punkt3D a, b, c, d, e, f, g, h;
11
+		void clean();
12
+		void render();
13
+};
14
+
15
+#endif

+ 143
- 55
quaternion.cpp View File

@@ -1,77 +1,165 @@
1 1
 #include "quaternion.h"
2 2
 
3 3
 Quaternion::Quaternion() {
4
-	w = 1.0f;
4
+	w = 1.0f,
5 5
 	x = y = z = 0.0f;
6 6
 }
7 7
 
8
-void Quaternion::createFromRotation(float deg, float _x, float _y, float _z) {
9
-	float tmpres = sin(deg2rad(deg) / 2.0f);
10
-	
11
-	w = cos(deg2rad(deg) / 2.0f);
12
-	x = _x * tmpres;
13
-	y = _y * tmpres;
14
-	z = _z * tmpres;
8
+Quaternion::Quaternion(float _x, float _y, float _z, float _w) {
9
+	x = _x;
10
+	y = _y;
11
+	z = _z;
12
+	w = _w;
15 13
 }
16 14
 
17
-Quaternion Quaternion::operator*(const Quaternion &q) {
18
-	Quaternion ret;
19
-
20
-	ret.w = w*q.w - x*q.x - y*q.y - z*q.z;
21
-	ret.x = w*q.x + x*q.w + y*q.z - z*q.y;
22
-	ret.y = w*q.y + y*q.w + z*q.x - x*q.z;
23
-	ret.z = w*q.z + z*q.w + x*q.y - y*q.x;
24
-
25
-	return ret;
15
+Quaternion::Quaternion(float deg, Punkt3D p) {
16
+	set(deg, p);
26 17
 }
27 18
 
28
-void Quaternion::createGlMatrix(GLfloat *matrix) {
29
-	if(!matrix)
30
-		return;
31
-
32
-	matrix[0]  = 1.0f - 2.0f * ( y*y + z*z);
33
-	matrix[1]  = 2.0f * ( x*y + z*w);
34
-	matrix[2]  = 2.0f * ( x*z - y*w);
35
-	matrix[3]  = 0.0f;
36
-
37
-	matrix[4]  = 2.0f * ( x*y - z*w);
38
-	matrix[5]  = 1.0f - 2.0f * ( x*x + z*z);
39
-	matrix[6]  = 2.0f * ( z*y + x*w);
40
-	matrix[7]  = 0.0f;
41
-
42
-	matrix[8]  = 2.0f * ( x*z + y*w);
43
-	matrix[9]  = 2.0f * ( y*z - x*w);
44
-	matrix[10] = 1.0f - 2.0f * ( x*x + y*y);
45
-	matrix[11] = 0.0f;
19
+Quaternion::Quaternion(float _x, float _y, float _z) {
20
+	set(_x, _y, _z);
21
+}
46 22
 
47
-	matrix[12] = 0.0f;
48
-	matrix[13] = 0.0f;
49
-	matrix[14] = 0.0f;
50
-	matrix[15] = 1.0f;
23
+void Quaternion::set(float rotx, float roty, float rotz) {
24
+	rotx = deg2rad(rotx);
25
+	roty = deg2rad(roty);
26
+	rotz = deg2rad(rotz);
27
+	
28
+	float sinx = sin(rotx);
29
+	float siny = sin(roty);
30
+	float sinz = sin(rotz);
31
+	float cosx = cos(rotx);
32
+	float cosy = cos(roty);
33
+	float cosz = cos(rotz);
34
+	
35
+	x = sinz * cosx * cosy - cosz * sinx * siny;
36
+	y = cosz * sinx * cosy + sinz * cosx * siny;
37
+	z = cosz * cosx * siny - sinz * sinx * cosy;
38
+	w = cosz * cosx * cosy - sinz * sinx * siny;
51 39
 	
40
+	normalize();
52 41
 }
53 42
 
54
-Punkt3D Quaternion::getDirectionVector() {
55
-	GLfloat matrix[16];
56
-	createGlMatrix(matrix);
43
+void Quaternion::set(float deg, Punkt3D p) {
44
+	float angle = deg2rad(0.5f*deg);
45
+	float sinangle = sin(angle);
57 46
 	
58
-	return getDirectionVector(matrix);
47
+	x = p.x * sinangle;
48
+	y = p.y * sinangle;
49
+	z = p.z * sinangle;
50
+	w = cos(angle);
51
+	
52
+	normalize();
53
+}
54
+
55
+void Quaternion::normalize() {
56
+	float len = x*x+y*y+z*z+w*w;
57
+	if(std::fabs(len-1.0f)>0.00001f) {
58
+		len = sqrt(len);
59
+		x /= len;
60
+		y /= len;
61
+		z /= len;
62
+		w /= len;
63
+	}
64
+}
65
+
66
+Quaternion Quaternion::getConjugate() {
67
+	return Quaternion(-x, -y, -z, w);
59 68
 }
60 69
 
61
-Punkt3D Quaternion::getDirectionVector(GLfloat *matrix) {
62
-	if(!matrix)
63
-		return Punkt3D();
64
-	Punkt3D tmp;
70
+// Rotationsmatrix Quaternion::getRotMat() {
71
+// 	
72
+// }
73
+
74
+Punkt3D Quaternion::rotP3D(const Punkt3D &p) {
75
+	Quaternion q(p.x, p.y, p.z, 0.0f);
65 76
 	
66
-	tmp.x = matrix[8];
67
-	tmp.y = matrix[9];
68
-	tmp.z = matrix[10];
77
+	Quaternion erg = *this * q * this->getConjugate();
69 78
 	
70
-	return tmp;
79
+	return Punkt3D(erg.x, erg.y, erg.z);
71 80
 }
72 81
 
73
-void glMultMatrixf(Quaternion q) {
74
-	GLfloat matrix[16];
75
-	q.createGlMatrix(matrix);
76
-	glMultMatrixf(matrix);
82
+Quaternion Quaternion::operator*(const Quaternion &q) {
83
+	return Quaternion(w*q.x - x*q.w + y*q.z - z*q.y,
84
+					  w*q.y - y*q.w + z*q.x - x*q.z,
85
+					  w*q.z - z*q.w + x*q.y - y*q.x,
86
+					  w*q.w - x*q.x - y*q.y - z*q.z);
77 87
 }
88
+
89
+
90
+
91
+// Quaternion::Quaternion() {
92
+// 	w = 1.0f;
93
+// 	x = y = z = 0.0f;
94
+// }
95
+// 
96
+// void Quaternion::createFromRotation(float deg, float _x, float _y, float _z) {
97
+// 	float tmpres = sin(deg2rad(deg) / 2.0f);
98
+// 	
99
+// 	w = cos(deg2rad(deg) / 2.0f);
100
+// 	x = _x * tmpres;
101
+// 	y = _y * tmpres;
102
+// 	z = _z * tmpres;
103
+// }
104
+// 
105
+// Quaternion Quaternion::operator*(const Quaternion &q) {
106
+// 	Quaternion ret;
107
+// 
108
+// 	ret.w = w*q.w - x*q.x - y*q.y - z*q.z;
109
+// 	ret.x = w*q.x + x*q.w + y*q.z - z*q.y;
110
+// 	ret.y = w*q.y + y*q.w + z*q.x - x*q.z;
111
+// 	ret.z = w*q.z + z*q.w + x*q.y - y*q.x;
112
+// 
113
+// 	return ret;
114
+// }
115
+// 
116
+// void Quaternion::createGlMatrix(GLfloat *matrix) {
117
+// 	if(!matrix)
118
+// 		return;
119
+// 
120
+// 	matrix[0]  = 1.0f - 2.0f * ( y*y + z*z);
121
+// 	matrix[1]  = 2.0f * ( x*y + z*w);
122
+// 	matrix[2]  = 2.0f * ( x*z - y*w);
123
+// 	matrix[3]  = 0.0f;
124
+// 
125
+// 	matrix[4]  = 2.0f * ( x*y - z*w);
126
+// 	matrix[5]  = 1.0f - 2.0f * ( x*x + z*z);
127
+// 	matrix[6]  = 2.0f * ( z*y + x*w);
128
+// 	matrix[7]  = 0.0f;
129
+// 
130
+// 	matrix[8]  = 2.0f * ( x*z + y*w);
131
+// 	matrix[9]  = 2.0f * ( y*z - x*w);
132
+// 	matrix[10] = 1.0f - 2.0f * ( x*x + y*y);
133
+// 	matrix[11] = 0.0f;
134
+// 
135
+// 	matrix[12] = 0.0f;
136
+// 	matrix[13] = 0.0f;
137
+// 	matrix[14] = 0.0f;
138
+// 	matrix[15] = 1.0f;
139
+// 	
140
+// }
141
+// 
142
+// Punkt3D Quaternion::getDirectionVector() {
143
+// 	GLfloat matrix[16];
144
+// 	createGlMatrix(matrix);
145
+// 	
146
+// 	return getDirectionVector(matrix);
147
+// }
148
+// 
149
+// Punkt3D Quaternion::getDirectionVector(GLfloat *matrix) {
150
+// 	if(!matrix)
151
+// 		return Punkt3D();
152
+// 	Punkt3D tmp;
153
+// 	
154
+// 	tmp.x = matrix[8];
155
+// 	tmp.y = matrix[9];
156
+// 	tmp.z = matrix[10];
157
+// 	
158
+// 	return tmp;
159
+// }
160
+// 
161
+// void glMultMatrixf(Quaternion q) {
162
+// 	GLfloat matrix[16];
163
+// 	q.createGlMatrix(matrix);
164
+// 	glMultMatrixf(matrix);
165
+// }

+ 35
- 11
quaternion.h View File

@@ -4,27 +4,51 @@
4 4
 #include <iostream>
5 5
 #include <cmath>
6 6
 #include <SDL_opengl.h>
7
-
7
+// #include "rotationsmatrix.h"
8
+#include "punkt3d.h"
8 9
 #include "emath.h"
9 10
 
10 11
 class Quaternion {
11 12
 	private:
12
-		float w;
13
-		float x,y,z;
13
+		float x, y, z, w;
14
+	
14 15
 	public:
15 16
 		Quaternion();
16
-		void createFromRotation(float, float, float, float);
17
+		Quaternion(float _x, float _y, float _z, float _w);
18
+		Quaternion(float deg, Punkt3D p);
19
+		Quaternion(float _x, float _y, float _z);
17 20
 		
18
-
19
-		void createGlMatrix(GLfloat*);
20
-		Punkt3D getDirectionVector();
21
-		Punkt3D getDirectionVector(GLfloat*);
21
+		void set(float rotx, float roty, float rotz);
22
+		void set(float deg, Punkt3D axis);
23
+		void normalize();
24
+		Quaternion getConjugate();
25
+// 		Rotationsmatrix getRotMat();
26
+		Punkt3D rotP3D(const Punkt3D &p);
22 27
 		
23 28
 		Quaternion operator*(const Quaternion&);
24
-		
25
-
26 29
 };
27 30
 
28
-void glMultMatrixf(Quaternion);
31
+
32
+
33
+
34
+// class Quaternion {
35
+// 	private:
36
+// 		float w;
37
+// 		float x,y,z;
38
+// 	public:
39
+// 		Quaternion();
40
+// 		void createFromRotation(float, float, float, float);
41
+// 		
42
+// 
43
+// 		void createGlMatrix(GLfloat*);
44
+// 		Punkt3D getDirectionVector();
45
+// 		Punkt3D getDirectionVector(GLfloat*);
46
+// 		
47
+// 		Quaternion operator*(const Quaternion&);
48
+// 		
49
+// 
50
+// };
51
+// 
52
+// void glMultMatrixf(Quaternion);
29 53
 
30 54
 #endif

+ 1
- 0
rotationsmatrix.h View File

@@ -2,6 +2,7 @@
2 2
 #define __ROTATIONSMATRIX_H
3 3
 
4 4
 #include <cmath>
5
+#include "punkt3d.h"
5 6
 #include "emath.h"
6 7
 #include "matrix.h"
7 8
 

Loading…
Cancel
Save