1 //
2 // Computergraphik I
3 // Prof. Dr. Juergen Doellner
4 // Sommersemester 2001
5 //
6 // Rahmenprogramm zu Aufgabenzettel 7
7 //
8
9 #include "cgmultiview.h"
10 #include "vector.h"
11 #include <fstream.h>
12 #include <stdlib.h>
13
14 CGMultiview::CGMultiview() {
15 run_ = false;
16
17 mainfilled_ = true;
18 childfilled_ = false;
19
20 mainzoom_ = 1;
21 zoomxy_ = zoomxz_ = zoomyz_ = 1;
22
23 rotate_ = rotate2_ = 0;
24 }
25
26 CGMultiview::~CGMultiview( ) {
27 }
28
29 void CGMultiview::drawScene() {
30 glRotatef(rotate_,-1,1,0);
31 glRotatef(rotate2_,0,1,0);
32
33 static GLuint cache = 0;
34 if (cache == 0) {
35 cache = glGenLists(1);
36 glNewList(cache,GL_COMPILE_AND_EXECUTE);
37 glMatrixMode(GL_MODELVIEW);
38 glPushMatrix();
39 glRotated(-90,1,0,0);
40
41 ifstream s("big.txt"); // fuer den Porsche
42 //ifstream s("small.txt");
43 char buf[100];
44
45 int nTriangleCount = 0;
46
47 glBegin(GL_TRIANGLES);
48 Vector v[3];
49 while (!s.eof()) {
50 s >> buf;
51
52 while (s.good()) {
53 s >> v[0] >> v[1] >> v[2];
54 Vector n = (v[1]-v[0])*(v[2]-v[0]);
55 glNormal3dv(n.rep());
56 glVertex3dv(v[0].rep());
57 glVertex3dv(v[1].rep());
58 glVertex3dv(v[2].rep());
59
60 nTriangleCount++;
61 }
62 if (s.rdstate() & ios::failbit) {
63 s.clear(s.rdstate() & ~ios::failbit);
64 }
65 }
66 glEnd();
67 glPopMatrix();
68 glEndList();
69
70 cout << "triangles: " << nTriangleCount << endl;
71 } else {
72 glCallList(cache);
73 }
74 }
75
76 void CGMultiview::onInit() {
77 // OpenGL Lichtquelle
78 static GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; /* diffuse light. */
79 static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */
80 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
81 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
82 glEnable(GL_LIGHTING);
83 glEnable(GL_LIGHT0);
84
85 // automatische Normalisierung
86 glEnable(GL_NORMALIZE);
87
88 glEnable(GL_COLOR_MATERIAL);
89 glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
90
91 // Tiefen Test aktivieren
92 glEnable(GL_DEPTH_TEST);
93
94 // Smooth Schattierung aktivieren
95 glShadeModel(GL_SMOOTH);
96
97 // Projection
98 glMatrixMode(GL_PROJECTION);
99 gluPerspective(60.0,1.0,2,2000.0);
100
101 // LookAt
102 glMatrixMode(GL_MODELVIEW);
103 // gluLookAt(0.0, 0.0, 4.0, // from (0,0,4)
104 // 0.0, 0.0, 0.0, // to (0,0,0)
105 // 0.0, 1.0, 0.); // up
106
107 glClearColor(0.95,0.95,0.95,1);
108 }
109
110 void CGMultiview::onSize(unsigned int newWidth,unsigned int newHeight) {
111 width_ = newWidth;
112 height_ = newHeight;
113
114 glutPostRedisplay();
115 }
116
117 void CGMultiview::onKey(unsigned char key) {
118 switch (key) {
119 case 27: { exit(0); break; }
120 case ' ': { run_ = !run_; break; }
121
122 case '+': { mainzoom_*= 1.1; break; }
123 case '-': { mainzoom_*= 0.9; break; }
124
125 case '1': { zoomxy_*= 1.1; break; }
126 case '2': { zoomxy_*= 0.9; break; }
127 case '3': { zoomxz_*= 1.1; break; }
128 case '4': { zoomxz_*= 0.9; break; }
129 case '5': { zoomyz_*= 1.1; break; }
130 case '6': { zoomyz_*= 0.9; break; }
131
132 case 'l': { childfilled_ = !childfilled_; break; }
133 case 'L': { mainfilled_ = !mainfilled_; break; }
134 }
135
136 glutPostRedisplay();
137 }
138
139 void CGMultiview::onIdle() {
140 if (run_) {
141 rotate_ += 1;
142 glutPostRedisplay();
143 }
144 }
145
146 void CGMultiview::onDrag(double dx, double dy)
147 {
148 rotate2_ += dx*20;
149 // glRotatef(-dy*20, 1, 0, 0);
150
151 glutPostRedisplay();
152 }
153
154 void CGMultiview::onDraw() {
155 // Loesche den Farb- und Tiefenspeicher
156 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
157
158 // Fentergröße
159 int maxx = width_;
160 int maxy = height_;
161 int orthosize = maxy/3;
162
163 // zeichne perspektivische Hauptansicht
164 glViewport(0,0, maxx-1, maxy-1);
165 glMatrixMode(GL_PROJECTION);
166 glLoadIdentity();
167 gluPerspective(45/mainzoom_, (float)maxx/maxy, 0.1, 100.0);
168 glMatrixMode(GL_MODELVIEW);
169 glLoadIdentity();
170 gluLookAt(0,0,10, 0,0,0, 0,1,0);
171
172 // Füllmodus
173 glPolygonMode(GL_FRONT_AND_BACK, mainfilled_ ? GL_FILL:GL_LINE);
174 glColor3f(1,1,1);
175 drawScene();
176
177
178 glPolygonMode(GL_FRONT_AND_BACK, childfilled_ ? GL_FILL:GL_LINE);
179 // zeichne Ansicht x,y
180 glViewport(maxx-orthosize, 2*orthosize, orthosize, orthosize);
181 glMatrixMode(GL_PROJECTION);
182 // orthogonale Sicht
183 glLoadIdentity();
184 glOrtho(-1/zoomxy_, 1/zoomxy_, -1/zoomxy_, 1/zoomxy_, 0.1, 100);
185 // entlang z-Achse
186 glMatrixMode(GL_MODELVIEW);
187 glLoadIdentity();
188 gluLookAt(0,0,10, 0,0,0, 0,1,0);
189 glColor3f(0,0,1);
190 drawScene();
191
192 // zeichne Ansicht x,z
193 glViewport(maxx-orthosize, orthosize, orthosize, orthosize);
194 glMatrixMode(GL_PROJECTION);
195 // orthogonale Sicht
196 glLoadIdentity();
197 glOrtho(-1/zoomxz_, 1/zoomxz_, -1/zoomxz_, 1/zoomxz_, 0.1, 100);
198 // entlang y-Achse
199 glMatrixMode(GL_MODELVIEW);
200 glLoadIdentity();
201 gluLookAt(0,10,0, 0,0,0, -1,0,0);
202 glColor3f(0,1,0);
203 drawScene();
204
205 // zeichne Ansicht y,z
206 glViewport(maxx-orthosize, 0, orthosize, orthosize);
207 glMatrixMode(GL_PROJECTION);
208 // orthogonale Sicht
209 glLoadIdentity();
210 glOrtho(-1/zoomyz_, 1/zoomyz_, -1/zoomyz_, 1/zoomyz_, 0.1, 100);
211 // entlang x-Achse
212 glMatrixMode(GL_MODELVIEW);
213 glLoadIdentity();
214 gluLookAt(10,0,0, 0,0,0, 0,1,0);
215 glColor3f(1,0,0);
216 drawScene();
217
218 // Nicht vergessen! Front- und Back-Buffer tauschen:
219 swapBuffers();
220 }
221
222 // Hauptprogramm
223 int main(int argc, char* argv[]) {
224 // Erzeuge eine Instanz der Beispiel-Anwendung:
225 CGMultiview sample;
226
227 // Starte die Beispiel-Anwendung:
228 sample.start("CGMultiview, Stephan Brumme, 702544");
229 return(0);
230 }
231