1 //
2 // Computergraphik II
3 // Prof. Dr. Juergen Doellner
4 // Wintersemester 2001/2002
5 //
6 // Rahmenprogramm fuer Aufgabenzettel 3
7 //
8 // Autoren: Florian Kirsch (kirsch@hpi.uni-potsdam.de)
9 // Marc Nienhaus (nienhaus@hpi.uni-potsdam.de)
10 // Juergen Doellner (doellner@hpi.uni-potsdam.de)
11 //
12
13 #include "cghsvcolors.h"
14
15
16 CGHSVColors::CGHSVColors(int width, int height) : raster_(width,height) {
17 mode_ = DrawGrid;
18 run_ = false;
19 angle_ = 30;
20
21 clrFrom = Color(1.0, 0.0, 0.5);
22 clrTo = Color(0.45, 0.55, 0.4);
23 }
24
25 void CGHSVColors::onInit() {
26 glClearColor(1, 1, 1, 1);
27 // anti-aliased points
28 // glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST);
29 // glEnable(GL_POINT_SMOOTH);
30 // glEnable(GL_BLEND);
31 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
32 }
33
34 void CGHSVColors::onDraw() {
35 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
36
37 glMatrixMode(GL_PROJECTION);
38 glLoadIdentity();
39 glMatrixMode(GL_MODELVIEW);
40 glLoadIdentity();
41
42 // if (mode_ == DrawGrid) {
43
44 // color banner
45 glViewport(0, 0, winWidth_-1, winHeight_/10);
46
47 glDisable(GL_DEPTH_TEST);
48
49 glMatrixMode(GL_PROJECTION);
50 gluOrtho2D(0, raster_.width(), 0, raster_.height());
51 glMatrixMode(GL_MODELVIEW);
52
53 // Hier 2D-Raster zeichnen
54 raster_.clear();
55 const int colors = raster_.width();
56 for (int x=0; x<colors; x++)
57 {
58 // RGB
59 raster_.setPixel(x,0,Color::InterpolateRGB(clrFrom, clrTo, x/(double)colors));
60 // HSV +
61 raster_.setPixel(x,1,Color::InterpolateHSV(clrFrom, clrTo, x/(double)colors, true));
62 // HSV -
63 raster_.setPixel(x,2,Color::InterpolateHSV(clrFrom, clrTo, x/(double)colors, false));
64 }
65 raster_.draw();
66
67 // } else if (mode_ == DrawCube) {
68
69 // cube
70 glViewport(0, winHeight_/10, winWidth_-1, winHeight_*0.9);
71
72 glEnable(GL_DEPTH_TEST);
73
74 glMatrixMode(GL_PROJECTION);
75 glLoadIdentity();
76 gluPerspective(30.0,1.0,2,2000.0);
77 glMatrixMode(GL_MODELVIEW);
78 glLoadIdentity();
79 gluLookAt(0.0, 0.0, 4.0, // from (0,0,4)
80 0.0, 0.0, 0.0, // to (0,0,0)
81 0.0, 1.0, 0.); // up
82
83 glRotatef(angle_, 0.0, 1.0, 0.0);
84 glColor4d(0.0, 0.0, 0.0, 1.0);
85 glutWireCube(1.0);
86 glTranslatef(-0.5, -0.5, -0.5);
87
88 // Hier 3D-Punkte zeichnen
89
90 glPointSize(3);
91 glBegin(GL_POINTS);
92 for (x=0; x<SHOWNCOLORS; x++)
93 {
94 Color clrInterpolate;
95
96 // RGB
97 clrInterpolate = Color::InterpolateRGB(clrFrom, clrTo, x/(double)SHOWNCOLORS);
98 glColor3d(clrInterpolate[0], clrInterpolate[1], clrInterpolate[2]);
99 glVertex3d(clrInterpolate[0], clrInterpolate[1], clrInterpolate[2]);
100
101 // HSV +
102 clrInterpolate = Color::InterpolateHSV(clrFrom, clrTo, x/(double)SHOWNCOLORS, true);
103 glColor3d(clrInterpolate[0], clrInterpolate[1], clrInterpolate[2]);
104 glVertex3d(clrInterpolate[0], clrInterpolate[1], clrInterpolate[2]);
105
106 // HSV -
107 clrInterpolate = Color::InterpolateHSV(clrFrom, clrTo, x/(double)SHOWNCOLORS, false);
108 glColor3d(clrInterpolate[0], clrInterpolate[1], clrInterpolate[2]);
109 glVertex3d(clrInterpolate[0], clrInterpolate[1], clrInterpolate[2]);
110 }
111 glEnd();
112
113 // }
114
115 swapBuffers();
116 }
117
118 void CGHSVColors::onSize(unsigned int newWidth,unsigned int newHeight) {
119 winWidth_ = newWidth;
120 winHeight_ = newHeight;
121 glViewport(0, 0, newWidth - 1, newHeight - 1);
122 }
123
124 void CGHSVColors::onButton(MouseButton button, int x, int y) {
125 onDraw();
126 }
127
128 void CGHSVColors::onIdle() {
129 if (run_) {
130 angle_ += 1.0;
131 onDraw();
132 }
133 }
134
135 void CGHSVColors::onKey(unsigned char key) {
136 switch (key) {
137 case 27: { exit(0); break; }
138 // case 'g': mode_ = DrawGrid; break;
139 // case 'c': mode_ = DrawCube; break;
140
141 // change "from" color
142 case 'a': if (clrFrom[0] <= 0.95) clrFrom[0] += 0.05; break;
143 case 'y': if (clrFrom[0] >= 0.05) clrFrom[0] -= 0.05; break;
144 case 's': if (clrFrom[1] <= 0.95) clrFrom[1] += 0.05; break;
145 case 'x': if (clrFrom[1] >= 0.05) clrFrom[1] -= 0.05; break;
146 case 'd': if (clrFrom[2] <= 0.95) clrFrom[2] += 0.05; break;
147 case 'c': if (clrFrom[2] >= 0.05) clrFrom[2] -= 0.05; break;
148 // change "to" color
149 case 'f': if (clrTo [0] <= 0.95) clrTo [0] += 0.05; break;
150 case 'v': if (clrTo [0] >= 0.05) clrTo [0] -= 0.05; break;
151 case 'g': if (clrTo [1] <= 0.95) clrTo [1] += 0.05; break;
152 case 'b': if (clrTo [1] >= 0.05) clrTo [1] -= 0.05; break;
153 case 'h': if (clrTo [2] <= 0.95) clrTo [2] += 0.05; break;
154 case 'n': if (clrTo [2] >= 0.05) clrTo [2] -= 0.05; break;
155
156 case ' ': run_ = !run_; break;
157 }
158 onDraw();
159 }
160
161 int main(int argc, char* argv[]) {
162 CGHSVColors hsvcolors(32, 3);
163
164 cout << "Tastenbelegung:" << endl
<< "ESC Programm beenden" << endl
<< "Leertaste Objekt drehen" << endl
<< "a Rotanteil der Startfarbe erhöhen" << endl
<< "y Rotanteil der Startfarbe verringern" << endl
<< "s Grünanteil der Startfarbe erhöhen" << endl
<< "x Grünanteil der Startfarbe verringern" << endl
<< "d Blauanteil der Startfarbe erhöhen" << endl
<< "c Blauanteil der Startfarbe verringern" << endl
<< "f Rotanteil der Zielfarbe erhöhen" << endl
<< "v Rotanteil der Zielfarbe verringern" << endl
<< "g Grünanteil der Zielfarbe erhöhen" << endl
<< "b Grünanteil der Zielfarbe verringern" << endl
<< "h Blauanteil der Zielfarbe erhöhen" << endl
<< "n Blauanteil der Zielfarbe verringern" << endl;
165
166 hsvcolors.start("Stephan Brumme, 702544", true, 400, 400);
167
168 return(0);
169 }
170
171