andrewm@0
|
1 /*
|
andrewm@0
|
2 TouchKeys: multi-touch musical keyboard control software
|
andrewm@0
|
3 Copyright (c) 2013 Andrew McPherson
|
andrewm@0
|
4
|
andrewm@0
|
5 This program is free software: you can redistribute it and/or modify
|
andrewm@0
|
6 it under the terms of the GNU General Public License as published by
|
andrewm@0
|
7 the Free Software Foundation, either version 3 of the License, or
|
andrewm@0
|
8 (at your option) any later version.
|
andrewm@0
|
9
|
andrewm@0
|
10 This program is distributed in the hope that it will be useful,
|
andrewm@0
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
andrewm@0
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
andrewm@0
|
13 GNU General Public License for more details.
|
andrewm@0
|
14
|
andrewm@0
|
15 You should have received a copy of the GNU General Public License
|
andrewm@0
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
andrewm@0
|
17
|
andrewm@0
|
18 =====================================================================
|
andrewm@0
|
19
|
andrewm@0
|
20 RawSensorDisplay.cpp: simple graph for showing raw TouchKeys sensor values
|
andrewm@0
|
21 */
|
andrewm@0
|
22
|
andrewm@0
|
23 #include "RawSensorDisplay.h"
|
andrewm@0
|
24 #include <iostream>
|
andrewm@0
|
25 #include <cmath>
|
andrewm@0
|
26
|
andrewm@0
|
27 // Class constants
|
andrewm@0
|
28 // Display margins
|
andrewm@0
|
29 const float RawSensorDisplay::kDisplaySideMargin = 0.5;
|
andrewm@0
|
30 const float RawSensorDisplay::kDisplayBottomMargin = 0.5;
|
andrewm@0
|
31 const float RawSensorDisplay::kDisplayTopMargin = 0.5;
|
andrewm@0
|
32
|
andrewm@0
|
33 // Size of bar graphs and spacing
|
andrewm@0
|
34 const float RawSensorDisplay::kDisplayBarWidth = 0.5;
|
andrewm@0
|
35 const float RawSensorDisplay::kDisplayBarSpacing = 0.25;
|
andrewm@0
|
36 const float RawSensorDisplay::kDisplayBarHeight = 10.0;
|
andrewm@0
|
37
|
andrewm@0
|
38
|
andrewm@0
|
39 RawSensorDisplay::RawSensorDisplay() :
|
andrewm@0
|
40 displayPixelWidth_(1.0), displayPixelHeight_(1.0), totalDisplayWidth_(1.0), totalDisplayHeight_(1.0),
|
andrewm@0
|
41 yMin_(-10), yMax_(256), needsUpdate_(true) {
|
andrewm@0
|
42 // Initialize OpenGL settings: 2D only
|
andrewm@0
|
43
|
andrewm@0
|
44 //glMatrixMode(GL_PROJECTION);
|
andrewm@0
|
45 //glDisable(GL_DEPTH_TEST);
|
andrewm@0
|
46
|
andrewm@0
|
47 totalDisplayWidth_ = kDisplaySideMargin*2 + kDisplayBarWidth;
|
andrewm@0
|
48 totalDisplayHeight_ = kDisplayTopMargin + kDisplayBottomMargin + kDisplayBarHeight;
|
andrewm@0
|
49 }
|
andrewm@0
|
50
|
andrewm@0
|
51 void RawSensorDisplay::setDisplaySize(float width, float height) {
|
andrewm@0
|
52 ScopedLock sl(displayMutex_);
|
andrewm@0
|
53
|
andrewm@0
|
54 displayPixelWidth_ = width;
|
andrewm@0
|
55 displayPixelHeight_ = height;
|
andrewm@0
|
56 refreshViewport();
|
andrewm@0
|
57 }
|
andrewm@0
|
58
|
andrewm@0
|
59
|
andrewm@0
|
60 // Render the keyboard display
|
andrewm@0
|
61
|
andrewm@0
|
62 void RawSensorDisplay::render() {
|
andrewm@0
|
63 // Start with a light gray background
|
andrewm@0
|
64 glClearColor(0.8, 0.8, 0.8, 1.0);
|
andrewm@0
|
65 glClear(GL_COLOR_BUFFER_BIT);
|
andrewm@0
|
66 glLoadIdentity();
|
andrewm@0
|
67
|
andrewm@0
|
68 float invAspectRatio = totalDisplayWidth_ / totalDisplayHeight_;
|
andrewm@0
|
69 float scaleValue = 2.0 / totalDisplayWidth_;
|
andrewm@0
|
70
|
andrewm@0
|
71 glScalef(scaleValue, scaleValue * invAspectRatio, scaleValue);
|
andrewm@0
|
72 glTranslatef(-1.0 / scaleValue, -totalDisplayHeight_ / 2.0, 0);
|
andrewm@0
|
73 glTranslatef(0.0, kDisplayBottomMargin, 0.0);
|
andrewm@0
|
74
|
andrewm@0
|
75 ScopedLock sl(displayMutex_);
|
andrewm@0
|
76
|
andrewm@0
|
77 // Draw the line for zero value
|
andrewm@0
|
78 glColor3f(0.5, 0.5, 0.5);
|
andrewm@0
|
79 glBegin(GL_LINES);
|
andrewm@0
|
80 glVertex2f(0, graphToDisplayY(0.0));
|
andrewm@0
|
81 glVertex2f(totalDisplayWidth_, graphToDisplayY(0.0));
|
andrewm@0
|
82 glEnd();
|
andrewm@0
|
83
|
andrewm@0
|
84 glTranslatef(kDisplaySideMargin, 0.0, 0.0);
|
andrewm@0
|
85
|
andrewm@0
|
86 for(int i = 0; i < displayValues_.size(); i++) {
|
andrewm@0
|
87 // Draw each bar in sequence
|
andrewm@0
|
88 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
andrewm@0
|
89 glColor3f(1.0, 0.0, 0.0);
|
andrewm@0
|
90
|
andrewm@0
|
91 glBegin(GL_POLYGON);
|
andrewm@0
|
92 glVertex2f(0, graphToDisplayY(0));
|
andrewm@0
|
93 glVertex2f(0, graphToDisplayY(displayValues_[i]));
|
andrewm@0
|
94 glVertex2f(kDisplayBarWidth, graphToDisplayY(displayValues_[i]));
|
andrewm@0
|
95 glVertex2f(kDisplayBarWidth, graphToDisplayY(0));
|
andrewm@0
|
96 glEnd();
|
andrewm@0
|
97
|
andrewm@0
|
98 glTranslatef(kDisplayBarWidth + kDisplayBarSpacing, 0.0, 0.0);
|
andrewm@0
|
99 }
|
andrewm@0
|
100
|
andrewm@0
|
101 needsUpdate_ = false;
|
andrewm@0
|
102
|
andrewm@0
|
103 glFlush();
|
andrewm@0
|
104 }
|
andrewm@0
|
105
|
andrewm@0
|
106 // Copy new data into the display buffer
|
andrewm@0
|
107 void RawSensorDisplay::setDisplayData(std::vector<int> const& values) {
|
andrewm@0
|
108 displayValues_ = values;
|
andrewm@0
|
109
|
andrewm@0
|
110 // Update display width according to number of data points
|
andrewm@0
|
111 totalDisplayWidth_ = kDisplaySideMargin*2 + (kDisplayBarWidth + kDisplayBarSpacing) * displayValues_.size();
|
andrewm@0
|
112
|
andrewm@0
|
113 needsUpdate_ = true;
|
andrewm@0
|
114 }
|
andrewm@0
|
115
|
andrewm@0
|
116 // Mouse interaction methods
|
andrewm@0
|
117
|
andrewm@0
|
118 void RawSensorDisplay::mouseDown(float x, float y) {
|
andrewm@0
|
119 //Point mousePoint = {x, y};
|
andrewm@0
|
120 //Point scaledPoint = screenToInternal(mousePoint);
|
andrewm@0
|
121
|
andrewm@0
|
122 //needsUpdate_ = true;
|
andrewm@0
|
123 }
|
andrewm@0
|
124
|
andrewm@0
|
125 void RawSensorDisplay::mouseDragged(float x, float y) {
|
andrewm@0
|
126 //Point mousePoint = {x, y};
|
andrewm@0
|
127 //Point scaledPoint = screenToInternal(mousePoint);
|
andrewm@0
|
128
|
andrewm@0
|
129 //needsUpdate_ = true;
|
andrewm@0
|
130 }
|
andrewm@0
|
131
|
andrewm@0
|
132 void RawSensorDisplay::mouseUp(float x, float y) {
|
andrewm@0
|
133 //Point mousePoint = {x, y};
|
andrewm@0
|
134 //Point scaledPoint = screenToInternal(mousePoint);
|
andrewm@0
|
135
|
andrewm@0
|
136 //needsUpdate_ = true;
|
andrewm@0
|
137 }
|
andrewm@0
|
138
|
andrewm@0
|
139 void RawSensorDisplay::rightMouseDown(float x, float y) {
|
andrewm@0
|
140 //Point mousePoint = {x, y};
|
andrewm@0
|
141 //Point scaledPoint = screenToInternal(mousePoint);
|
andrewm@0
|
142
|
andrewm@0
|
143 //needsUpdate_ = true;
|
andrewm@0
|
144 }
|
andrewm@0
|
145
|
andrewm@0
|
146 void RawSensorDisplay::rightMouseDragged(float x, float y) {
|
andrewm@0
|
147 //Point mousePoint = {x, y};
|
andrewm@0
|
148 //Point scaledPoint = screenToInternal(mousePoint);
|
andrewm@0
|
149 }
|
andrewm@0
|
150
|
andrewm@0
|
151 void RawSensorDisplay::rightMouseUp(float x, float y) {
|
andrewm@0
|
152 //Point mousePoint = {x, y};
|
andrewm@0
|
153 //Point scaledPoint = screenToInternal(mousePoint);
|
andrewm@0
|
154 }
|
andrewm@0
|
155
|
andrewm@0
|
156 float RawSensorDisplay::graphToDisplayY(float y) {
|
andrewm@0
|
157 return kDisplayBarHeight*(y - yMin_)/(yMax_ - yMin_);
|
andrewm@0
|
158 }
|
andrewm@0
|
159
|
andrewm@0
|
160 void RawSensorDisplay::refreshViewport() {
|
andrewm@0
|
161 glViewport(0, 0, displayPixelWidth_, displayPixelHeight_);
|
andrewm@0
|
162 }
|
andrewm@0
|
163
|
andrewm@0
|
164 // Conversion from internal coordinate space to external pixel values and back
|
andrewm@0
|
165
|
andrewm@0
|
166 // Pixel values go from 0,0 (lower left) to displayPixelWidth_, displayPixelHeight_ (upper right)
|
andrewm@0
|
167 // Internal values go from -totalDisplayWidth_/2, -totalDisplayHeight_/2 (lower left)
|
andrewm@0
|
168 // to totalDisplayWidth_/2, totalDisplayHeight_/2 (upper right)
|
andrewm@0
|
169
|
andrewm@0
|
170 // Pixel value in --> OpenGL value out
|
andrewm@0
|
171 RawSensorDisplay::Point RawSensorDisplay::screenToInternal(Point& inPoint) {
|
andrewm@0
|
172 Point out;
|
andrewm@0
|
173
|
andrewm@0
|
174 out.x = -totalDisplayWidth_*0.5 + (inPoint.x/displayPixelWidth_) * totalDisplayWidth_;
|
andrewm@0
|
175 out.y = -totalDisplayHeight_*0.5 + (inPoint.y/displayPixelHeight_) * totalDisplayHeight_;
|
andrewm@0
|
176
|
andrewm@0
|
177 return out;
|
andrewm@0
|
178 }
|
andrewm@0
|
179
|
andrewm@0
|
180 // OpenGL value in --> Pixel value out
|
andrewm@0
|
181 RawSensorDisplay::Point RawSensorDisplay::internalToScreen(Point& inPoint) {
|
andrewm@0
|
182 Point out;
|
andrewm@0
|
183
|
andrewm@0
|
184 out.x = ((inPoint.x + totalDisplayWidth_*0.5)/totalDisplayWidth_) * displayPixelWidth_;
|
andrewm@0
|
185 out.y = ((inPoint.y + totalDisplayHeight_*0.5)/totalDisplayHeight_) * displayPixelHeight_;
|
andrewm@0
|
186
|
andrewm@0
|
187 return out;
|
andrewm@0
|
188 }
|