1 //
2 // Computergraphik II
3 // Prof. Dr. Juergen Doellner
4 // Wintersemester 2001/02
5 //
6 // Rahmenprogramm zu Aufgabenzettel 2
7 //
8
9 #include "cglinestyle.h"
10 #include "vector.h"
11 #include <fstream.h>
12 #include <stdlib.h>
13
14 CGLinestyle::CGLinestyle() {
15 run_ = false;
16 zoom_ = 0.15;
17 culling_ = false;
18 lighting_ = true;
19 mode_ = SEGMENTED;
20 haloewidth_ = 5.0;
21 segmentwidth_ = 1.0;
22 }
23
24 CGLinestyle::~CGLinestyle( ) {
25 }
26
27 void CGLinestyle::drawScene() {
28
29 glPushMatrix();
30 glScaled(zoom_, zoom_, zoom_);
31
32 static GLuint cache = 0;
33 if (cache == 0) {
34 cache = glGenLists(1);
35 glNewList(cache, GL_COMPILE_AND_EXECUTE);
36 glMatrixMode(GL_MODELVIEW);
37 glPushMatrix();
38
39 //ifstream s("small.txt");
40 //ifstream s("stegarosaurus.txt");
41 ifstream s("triceratops.txt");
42 char buf[100];
43
44 glBegin(GL_TRIANGLES);
45 Vector v[3];
46 while (!s.eof()) {
47 s >> buf;
48
49 while (s.good()) {
50 s >> v[0] >> v[1] >> v[2];
51 Vector n = (v[1]-v[0])*(v[2]-v[0]);
52 glNormal3dv(n.rep());
53 glVertex3dv(v[0].rep());
54 glVertex3dv(v[1].rep());
55 glVertex3dv(v[2].rep());
56 }
57 if (s.rdstate() & ios::failbit) {
58 s.clear(s.rdstate() & ~ios::failbit);
59 }
60 }
61 glEnd();
62 glPopMatrix();
63 glEndList();
64 } else {
65 glCallList(cache);
66 }
67
68 glPopMatrix();
69 }
70
71 void CGLinestyle::onInit() {
72 // OpenGL Lichtquelle
73 static GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; /* diffuse light. */
74 static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */
75 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
76 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
77 glEnable(GL_LIGHTING);
78 glEnable(GL_LIGHT0);
79
80 // automatische Normalisierung
81 glEnable(GL_NORMALIZE);
82
83 glEnable(GL_COLOR_MATERIAL);
84 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
85
86 // Tiefen Test aktivieren
87 glEnable(GL_DEPTH_TEST);
88
89 // Smooth Schattierung aktivieren
90 glShadeModel(GL_SMOOTH);
91
92 // Projection
93 glMatrixMode(GL_PROJECTION);
94 gluPerspective(60.0,1.0,2,2000.0);
95
96 // LookAt
97 glMatrixMode(GL_MODELVIEW);
98 gluLookAt(0.0, 0.0, 4.0, // from (0,0,4)
99 0.0, 0.0, 0.0, // to (0,0,0)
100 0.0, 1.0, 0.); // up
101
102 glClearColor(0.0,0.0,0.0,1.0);
103 }
104
105 void CGLinestyle::onSize(unsigned int newWidth,unsigned int newHeight) {
106 width_ = newWidth;
107 height_ = newHeight;
108 glMatrixMode(GL_PROJECTION);
109 glViewport(0, 0, width_ - 1, height_ - 1);
110 glLoadIdentity();
111 gluPerspective(40.0,float(width_)/float(height_),2.0, 100.0);
112 glMatrixMode(GL_MODELVIEW);
113 }
114
115 void CGLinestyle::onKey(unsigned char key) {
116 switch (key) {
117 case 27: { exit(0); break; }
118 case '+': { zoom_*= 1.1; break; }
119 case '-': { zoom_*= 0.9; break; }
120 case ' ': { run_ = !run_; break; }
121 case 'c': { culling_ = !culling_; break; }
122 case 'l': { lighting_ = !lighting_; break; }
123 case 's': { mode_ = SEGMENTED; break; }
124 case 'h': { mode_ = HALOED; break; }
125 case '1': { haloewidth_*= 0.9; break; }
126 case '2': { haloewidth_*= 1.1; break; }
127 case '3': { segmentwidth_*= 0.9; break; }
128 case '4': { segmentwidth_*= 1.1; break; }
129 }
130 onDraw();
131 }
132
133 void CGLinestyle::onIdle() {
134 if (run_) {
135 glRotatef(1,0.1,1,0.2);
136 onDraw();
137 }
138 }
139
140 void CGLinestyle::onDraw() {
141 // clear buffers
142 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
143 glDepthMask(GL_TRUE);
144 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
145
146
147 if (culling_) glEnable(GL_CULL_FACE);
148 if (!lighting_) glDisable(GL_LIGHTING);
149
150
151 if (mode_ == SEGMENTED)
152 {
153 // SEGMENTED
154
155 // important ! default is GL_LESS
156 glDepthFunc(GL_LESS);
157
158 // wireframe
159 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
160
161 glLineWidth(segmentwidth_);
162 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
163
164 drawScene();
165
166 // filled
167 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
168
169 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
170
171 drawScene();
172 }
173 else
{
174 // HALOED
175
176 // important ! default is GL_LESS
177 glDepthFunc(GL_LEQUAL);
178
179 // wireframe I
180 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
181
182 glLineWidth(haloewidth_);
183 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
184
185 drawScene();
186
187 // wireframe II
188 glLineWidth(1);
189 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
190
191 drawScene();
192 }
193
194 // clean up
195 glEnable(GL_LIGHTING);
196 glDisable(GL_CULL_FACE);
197
198 // Front- und Back-Buffer tauschen:
199 swapBuffers();
200 }
201
202 // Hauptprogramm
203 int main(int argc, char* argv[]) {
204 // Erzeuge eine Instanz der Beispiel-Anwendung:
205 CGLinestyle sample;
206
207 cout << "Tastenbelegung:" << endl
<< "ESC Programm beenden" << endl
<< "Leertaste Objekt drehen" << endl
<< "+ in die Szene hineinzoomen" << endl
<< "- aus der Szene herauszoomen" << endl
<< "c Culling de-/aktivieren" << endl
<< "l Beleuchtung de-/aktivieren" << endl
<< "s segmented surfaces" << endl
<< "h haloed wires" << endl
<< "1 halo verringern" << endl
<< "2 halo verstärken" << endl
<< "3 Abstand der Segmente verringern" << endl
<< "4 Abstand der Segmente vergrößern" << endl;
208
209 // Starte die Beispiel-Anwendung:
210 sample.start("Stephan Brumme, 702544", true, 550, 550);
211 return(0);
212 }
213