annotate Source/Display/RawSensorDisplay.cpp @ 56:b4a2d2ae43cf tip

merge
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Fri, 23 Nov 2018 15:48:14 +0000
parents eef567a60146
children
rev   line source
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@27 24 #include "OpenGLJuceCanvas.h"
andrewm@0 25 #include <iostream>
andrewm@0 26 #include <cmath>
andrewm@0 27
andrewm@0 28 // Class constants
andrewm@0 29 // Display margins
andrewm@0 30 const float RawSensorDisplay::kDisplaySideMargin = 0.5;
andrewm@0 31 const float RawSensorDisplay::kDisplayBottomMargin = 0.5;
andrewm@0 32 const float RawSensorDisplay::kDisplayTopMargin = 0.5;
andrewm@0 33
andrewm@0 34 // Size of bar graphs and spacing
andrewm@0 35 const float RawSensorDisplay::kDisplayBarWidth = 0.5;
andrewm@0 36 const float RawSensorDisplay::kDisplayBarSpacing = 0.25;
andrewm@0 37 const float RawSensorDisplay::kDisplayBarHeight = 10.0;
andrewm@0 38
andrewm@0 39
andrewm@27 40 RawSensorDisplay::RawSensorDisplay() : canvas_(0),
andrewm@0 41 displayPixelWidth_(1.0), displayPixelHeight_(1.0), totalDisplayWidth_(1.0), totalDisplayHeight_(1.0),
andrewm@27 42 yMin_(-10), yMax_(256) {
andrewm@0 43 // Initialize OpenGL settings: 2D only
andrewm@0 44
andrewm@0 45 //glMatrixMode(GL_PROJECTION);
andrewm@0 46 //glDisable(GL_DEPTH_TEST);
andrewm@0 47
andrewm@0 48 totalDisplayWidth_ = kDisplaySideMargin*2 + kDisplayBarWidth;
andrewm@0 49 totalDisplayHeight_ = kDisplayTopMargin + kDisplayBottomMargin + kDisplayBarHeight;
andrewm@0 50 }
andrewm@0 51
andrewm@27 52 // Tell the underlying canvas to repaint itself
andrewm@27 53 void RawSensorDisplay::tellCanvasToRepaint() {
andrewm@27 54 if(canvas_ != 0)
andrewm@27 55 canvas_->triggerRepaint();
andrewm@27 56 }
andrewm@27 57
andrewm@0 58 void RawSensorDisplay::setDisplaySize(float width, float height) {
andrewm@0 59 ScopedLock sl(displayMutex_);
andrewm@0 60
andrewm@0 61 displayPixelWidth_ = width;
andrewm@0 62 displayPixelHeight_ = height;
andrewm@0 63 refreshViewport();
andrewm@0 64 }
andrewm@0 65
andrewm@0 66
andrewm@0 67 // Render the keyboard display
andrewm@0 68
andrewm@0 69 void RawSensorDisplay::render() {
andrewm@0 70 // Start with a light gray background
andrewm@0 71 glClearColor(0.8, 0.8, 0.8, 1.0);
andrewm@0 72 glClear(GL_COLOR_BUFFER_BIT);
andrewm@0 73 glLoadIdentity();
andrewm@0 74
andrewm@0 75 float invAspectRatio = totalDisplayWidth_ / totalDisplayHeight_;
andrewm@0 76 float scaleValue = 2.0 / totalDisplayWidth_;
andrewm@0 77
andrewm@0 78 glScalef(scaleValue, scaleValue * invAspectRatio, scaleValue);
andrewm@0 79 glTranslatef(-1.0 / scaleValue, -totalDisplayHeight_ / 2.0, 0);
andrewm@0 80 glTranslatef(0.0, kDisplayBottomMargin, 0.0);
andrewm@0 81
andrewm@0 82 ScopedLock sl(displayMutex_);
andrewm@0 83
andrewm@0 84 // Draw the line for zero value
andrewm@0 85 glColor3f(0.5, 0.5, 0.5);
andrewm@0 86 glBegin(GL_LINES);
andrewm@0 87 glVertex2f(0, graphToDisplayY(0.0));
andrewm@0 88 glVertex2f(totalDisplayWidth_, graphToDisplayY(0.0));
andrewm@0 89 glEnd();
andrewm@0 90
andrewm@0 91 glTranslatef(kDisplaySideMargin, 0.0, 0.0);
andrewm@0 92
andrewm@0 93 for(int i = 0; i < displayValues_.size(); i++) {
andrewm@0 94 // Draw each bar in sequence
andrewm@0 95 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
andrewm@0 96 glColor3f(1.0, 0.0, 0.0);
andrewm@0 97
andrewm@0 98 glBegin(GL_POLYGON);
andrewm@0 99 glVertex2f(0, graphToDisplayY(0));
andrewm@0 100 glVertex2f(0, graphToDisplayY(displayValues_[i]));
andrewm@0 101 glVertex2f(kDisplayBarWidth, graphToDisplayY(displayValues_[i]));
andrewm@0 102 glVertex2f(kDisplayBarWidth, graphToDisplayY(0));
andrewm@0 103 glEnd();
andrewm@0 104
andrewm@0 105 glTranslatef(kDisplayBarWidth + kDisplayBarSpacing, 0.0, 0.0);
andrewm@0 106 }
andrewm@0 107
andrewm@0 108 glFlush();
andrewm@0 109 }
andrewm@0 110
andrewm@0 111 // Copy new data into the display buffer
andrewm@0 112 void RawSensorDisplay::setDisplayData(std::vector<int> const& values) {
andrewm@0 113 displayValues_ = values;
andrewm@0 114
andrewm@0 115 // Update display width according to number of data points
andrewm@0 116 totalDisplayWidth_ = kDisplaySideMargin*2 + (kDisplayBarWidth + kDisplayBarSpacing) * displayValues_.size();
andrewm@0 117
andrewm@27 118 tellCanvasToRepaint();
andrewm@0 119 }
andrewm@0 120
andrewm@0 121 // Mouse interaction methods
andrewm@0 122
andrewm@0 123 void RawSensorDisplay::mouseDown(float x, float y) {
andrewm@0 124 //Point mousePoint = {x, y};
andrewm@0 125 //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 126 }
andrewm@0 127
andrewm@0 128 void RawSensorDisplay::mouseDragged(float x, float y) {
andrewm@0 129 //Point mousePoint = {x, y};
andrewm@0 130 //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 131 }
andrewm@0 132
andrewm@0 133 void RawSensorDisplay::mouseUp(float x, float y) {
andrewm@0 134 //Point mousePoint = {x, y};
andrewm@0 135 //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 136 }
andrewm@0 137
andrewm@0 138 void RawSensorDisplay::rightMouseDown(float x, float y) {
andrewm@0 139 //Point mousePoint = {x, y};
andrewm@0 140 //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 141 }
andrewm@0 142
andrewm@0 143 void RawSensorDisplay::rightMouseDragged(float x, float y) {
andrewm@0 144 //Point mousePoint = {x, y};
andrewm@0 145 //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 146 }
andrewm@0 147
andrewm@0 148 void RawSensorDisplay::rightMouseUp(float x, float y) {
andrewm@0 149 //Point mousePoint = {x, y};
andrewm@0 150 //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 151 }
andrewm@0 152
andrewm@0 153 float RawSensorDisplay::graphToDisplayY(float y) {
andrewm@0 154 return kDisplayBarHeight*(y - yMin_)/(yMax_ - yMin_);
andrewm@0 155 }
andrewm@0 156
andrewm@0 157 void RawSensorDisplay::refreshViewport() {
andrewm@0 158 glViewport(0, 0, displayPixelWidth_, displayPixelHeight_);
andrewm@0 159 }
andrewm@0 160
andrewm@0 161 // Conversion from internal coordinate space to external pixel values and back
andrewm@0 162
andrewm@0 163 // Pixel values go from 0,0 (lower left) to displayPixelWidth_, displayPixelHeight_ (upper right)
andrewm@0 164 // Internal values go from -totalDisplayWidth_/2, -totalDisplayHeight_/2 (lower left)
andrewm@0 165 // to totalDisplayWidth_/2, totalDisplayHeight_/2 (upper right)
andrewm@0 166
andrewm@0 167 // Pixel value in --> OpenGL value out
andrewm@0 168 RawSensorDisplay::Point RawSensorDisplay::screenToInternal(Point& inPoint) {
andrewm@0 169 Point out;
andrewm@0 170
andrewm@0 171 out.x = -totalDisplayWidth_*0.5 + (inPoint.x/displayPixelWidth_) * totalDisplayWidth_;
andrewm@0 172 out.y = -totalDisplayHeight_*0.5 + (inPoint.y/displayPixelHeight_) * totalDisplayHeight_;
andrewm@0 173
andrewm@0 174 return out;
andrewm@0 175 }
andrewm@0 176
andrewm@0 177 // OpenGL value in --> Pixel value out
andrewm@0 178 RawSensorDisplay::Point RawSensorDisplay::internalToScreen(Point& inPoint) {
andrewm@0 179 Point out;
andrewm@0 180
andrewm@0 181 out.x = ((inPoint.x + totalDisplayWidth_*0.5)/totalDisplayWidth_) * displayPixelWidth_;
andrewm@0 182 out.y = ((inPoint.y + totalDisplayHeight_*0.5)/totalDisplayHeight_) * displayPixelHeight_;
andrewm@0 183
andrewm@0 184 return out;
andrewm@0 185 }