andrewm@0: /* andrewm@0: TouchKeys: multi-touch musical keyboard control software andrewm@0: Copyright (c) 2013 Andrew McPherson andrewm@0: andrewm@0: This program is free software: you can redistribute it and/or modify andrewm@0: it under the terms of the GNU General Public License as published by andrewm@0: the Free Software Foundation, either version 3 of the License, or andrewm@0: (at your option) any later version. andrewm@0: andrewm@0: This program is distributed in the hope that it will be useful, andrewm@0: but WITHOUT ANY WARRANTY; without even the implied warranty of andrewm@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrewm@0: GNU General Public License for more details. andrewm@0: andrewm@0: You should have received a copy of the GNU General Public License andrewm@0: along with this program. If not, see . andrewm@0: andrewm@0: ===================================================================== andrewm@0: andrewm@0: RawSensorDisplay.cpp: simple graph for showing raw TouchKeys sensor values andrewm@0: */ andrewm@0: andrewm@0: #include "RawSensorDisplay.h" andrewm@27: #include "OpenGLJuceCanvas.h" andrewm@0: #include andrewm@0: #include andrewm@0: andrewm@0: // Class constants andrewm@0: // Display margins andrewm@0: const float RawSensorDisplay::kDisplaySideMargin = 0.5; andrewm@0: const float RawSensorDisplay::kDisplayBottomMargin = 0.5; andrewm@0: const float RawSensorDisplay::kDisplayTopMargin = 0.5; andrewm@0: andrewm@0: // Size of bar graphs and spacing andrewm@0: const float RawSensorDisplay::kDisplayBarWidth = 0.5; andrewm@0: const float RawSensorDisplay::kDisplayBarSpacing = 0.25; andrewm@0: const float RawSensorDisplay::kDisplayBarHeight = 10.0; andrewm@0: andrewm@0: andrewm@27: RawSensorDisplay::RawSensorDisplay() : canvas_(0), andrewm@0: displayPixelWidth_(1.0), displayPixelHeight_(1.0), totalDisplayWidth_(1.0), totalDisplayHeight_(1.0), andrewm@27: yMin_(-10), yMax_(256) { andrewm@0: // Initialize OpenGL settings: 2D only andrewm@0: andrewm@0: //glMatrixMode(GL_PROJECTION); andrewm@0: //glDisable(GL_DEPTH_TEST); andrewm@0: andrewm@0: totalDisplayWidth_ = kDisplaySideMargin*2 + kDisplayBarWidth; andrewm@0: totalDisplayHeight_ = kDisplayTopMargin + kDisplayBottomMargin + kDisplayBarHeight; andrewm@0: } andrewm@0: andrewm@27: // Tell the underlying canvas to repaint itself andrewm@27: void RawSensorDisplay::tellCanvasToRepaint() { andrewm@27: if(canvas_ != 0) andrewm@27: canvas_->triggerRepaint(); andrewm@27: } andrewm@27: andrewm@0: void RawSensorDisplay::setDisplaySize(float width, float height) { andrewm@0: ScopedLock sl(displayMutex_); andrewm@0: andrewm@0: displayPixelWidth_ = width; andrewm@0: displayPixelHeight_ = height; andrewm@0: refreshViewport(); andrewm@0: } andrewm@0: andrewm@0: andrewm@0: // Render the keyboard display andrewm@0: andrewm@0: void RawSensorDisplay::render() { andrewm@0: // Start with a light gray background andrewm@0: glClearColor(0.8, 0.8, 0.8, 1.0); andrewm@0: glClear(GL_COLOR_BUFFER_BIT); andrewm@0: glLoadIdentity(); andrewm@0: andrewm@0: float invAspectRatio = totalDisplayWidth_ / totalDisplayHeight_; andrewm@0: float scaleValue = 2.0 / totalDisplayWidth_; andrewm@0: andrewm@0: glScalef(scaleValue, scaleValue * invAspectRatio, scaleValue); andrewm@0: glTranslatef(-1.0 / scaleValue, -totalDisplayHeight_ / 2.0, 0); andrewm@0: glTranslatef(0.0, kDisplayBottomMargin, 0.0); andrewm@0: andrewm@0: ScopedLock sl(displayMutex_); andrewm@0: andrewm@0: // Draw the line for zero value andrewm@0: glColor3f(0.5, 0.5, 0.5); andrewm@0: glBegin(GL_LINES); andrewm@0: glVertex2f(0, graphToDisplayY(0.0)); andrewm@0: glVertex2f(totalDisplayWidth_, graphToDisplayY(0.0)); andrewm@0: glEnd(); andrewm@0: andrewm@0: glTranslatef(kDisplaySideMargin, 0.0, 0.0); andrewm@0: andrewm@0: for(int i = 0; i < displayValues_.size(); i++) { andrewm@0: // Draw each bar in sequence andrewm@0: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); andrewm@0: glColor3f(1.0, 0.0, 0.0); andrewm@0: andrewm@0: glBegin(GL_POLYGON); andrewm@0: glVertex2f(0, graphToDisplayY(0)); andrewm@0: glVertex2f(0, graphToDisplayY(displayValues_[i])); andrewm@0: glVertex2f(kDisplayBarWidth, graphToDisplayY(displayValues_[i])); andrewm@0: glVertex2f(kDisplayBarWidth, graphToDisplayY(0)); andrewm@0: glEnd(); andrewm@0: andrewm@0: glTranslatef(kDisplayBarWidth + kDisplayBarSpacing, 0.0, 0.0); andrewm@0: } andrewm@0: andrewm@0: glFlush(); andrewm@0: } andrewm@0: andrewm@0: // Copy new data into the display buffer andrewm@0: void RawSensorDisplay::setDisplayData(std::vector const& values) { andrewm@0: displayValues_ = values; andrewm@0: andrewm@0: // Update display width according to number of data points andrewm@0: totalDisplayWidth_ = kDisplaySideMargin*2 + (kDisplayBarWidth + kDisplayBarSpacing) * displayValues_.size(); andrewm@0: andrewm@27: tellCanvasToRepaint(); andrewm@0: } andrewm@0: andrewm@0: // Mouse interaction methods andrewm@0: andrewm@0: void RawSensorDisplay::mouseDown(float x, float y) { andrewm@0: //Point mousePoint = {x, y}; andrewm@0: //Point scaledPoint = screenToInternal(mousePoint); andrewm@0: } andrewm@0: andrewm@0: void RawSensorDisplay::mouseDragged(float x, float y) { andrewm@0: //Point mousePoint = {x, y}; andrewm@0: //Point scaledPoint = screenToInternal(mousePoint); andrewm@0: } andrewm@0: andrewm@0: void RawSensorDisplay::mouseUp(float x, float y) { andrewm@0: //Point mousePoint = {x, y}; andrewm@0: //Point scaledPoint = screenToInternal(mousePoint); andrewm@0: } andrewm@0: andrewm@0: void RawSensorDisplay::rightMouseDown(float x, float y) { andrewm@0: //Point mousePoint = {x, y}; andrewm@0: //Point scaledPoint = screenToInternal(mousePoint); andrewm@0: } andrewm@0: andrewm@0: void RawSensorDisplay::rightMouseDragged(float x, float y) { andrewm@0: //Point mousePoint = {x, y}; andrewm@0: //Point scaledPoint = screenToInternal(mousePoint); andrewm@0: } andrewm@0: andrewm@0: void RawSensorDisplay::rightMouseUp(float x, float y) { andrewm@0: //Point mousePoint = {x, y}; andrewm@0: //Point scaledPoint = screenToInternal(mousePoint); andrewm@0: } andrewm@0: andrewm@0: float RawSensorDisplay::graphToDisplayY(float y) { andrewm@0: return kDisplayBarHeight*(y - yMin_)/(yMax_ - yMin_); andrewm@0: } andrewm@0: andrewm@0: void RawSensorDisplay::refreshViewport() { andrewm@0: glViewport(0, 0, displayPixelWidth_, displayPixelHeight_); andrewm@0: } andrewm@0: andrewm@0: // Conversion from internal coordinate space to external pixel values and back andrewm@0: andrewm@0: // Pixel values go from 0,0 (lower left) to displayPixelWidth_, displayPixelHeight_ (upper right) andrewm@0: // Internal values go from -totalDisplayWidth_/2, -totalDisplayHeight_/2 (lower left) andrewm@0: // to totalDisplayWidth_/2, totalDisplayHeight_/2 (upper right) andrewm@0: andrewm@0: // Pixel value in --> OpenGL value out andrewm@0: RawSensorDisplay::Point RawSensorDisplay::screenToInternal(Point& inPoint) { andrewm@0: Point out; andrewm@0: andrewm@0: out.x = -totalDisplayWidth_*0.5 + (inPoint.x/displayPixelWidth_) * totalDisplayWidth_; andrewm@0: out.y = -totalDisplayHeight_*0.5 + (inPoint.y/displayPixelHeight_) * totalDisplayHeight_; andrewm@0: andrewm@0: return out; andrewm@0: } andrewm@0: andrewm@0: // OpenGL value in --> Pixel value out andrewm@0: RawSensorDisplay::Point RawSensorDisplay::internalToScreen(Point& inPoint) { andrewm@0: Point out; andrewm@0: andrewm@0: out.x = ((inPoint.x + totalDisplayWidth_*0.5)/totalDisplayWidth_) * displayPixelWidth_; andrewm@0: out.y = ((inPoint.y + totalDisplayHeight_*0.5)/totalDisplayHeight_) * displayPixelHeight_; andrewm@0: andrewm@0: return out; andrewm@0: }