#include "PerformanceGraph.h" #include "glInterface.h" PerformanceGraph::PerformanceGraph(int maxData): mMaxData(maxData), mCurrentIdx(0) { mGraphColor[0] = 1.0f; mGraphColor[1] = 0.0f; mGraphColor[2] = 0.0f; } void PerformanceGraph::DrawBackGround() { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glColor4f(0.0f, 0.0f, 0.0f, 0.5f); // show a grid glBegin(GL_QUADS); glVertex3f(0, 0, 0.0f); glVertex3f(1, 0, 0.0f); glVertex3f(1, 1, 0.0f); glVertex3f(0, 1, 0.0f); glEnd(); glDisable(GL_BLEND); } void PerformanceGraph::DrawGrid() { glColor4f(0.0f, 1.0f, 0.0f, 1.0f); // show a grid glBegin(GL_LINE_LOOP); glVertex3f(0, 0, 0.0f); glVertex3f(1, 0, 0.0f); glVertex3f(1, 1, 0.0f); glVertex3f(0, 1, 0.0f); glEnd(); } void PerformanceGraph::Draw() { glPushAttrib(GL_ALL_ATTRIB_BITS); glLineWidth(2); glDisable(GL_DEPTH_TEST); //glDepthMask(true); glDisable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); // init ortographic projection glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0, 1.0f, 0, 1.0f); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glTranslatef(0.668f, 0.668f, 0.0f); glScalef(0.33f, 0.33f, 1.0f); DrawBackGround(); DrawCurve(); DrawGrid(); glPopMatrix(); // restore the projection matrix glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopAttrib(); } void PerformanceGraph::DrawCurve() { const float scalex = 1.0f / mMaxData; const float scaley = 1.0f / 50.0f; glColor3fv(mGraphColor); glBegin(GL_LINE_STRIP); for (size_t i = 0; i < mData.size(); ++ i) { const float x = i * scalex; const float y = mData[i] * scaley; glVertex3f(x, y, 0.0f); } glEnd(); // show a grid glBegin(GL_LINES); glVertex3f(mCurrentIdx * scalex, 0, 0.0f); glVertex3f(mCurrentIdx * scalex, 1, 0.0f); glEnd(); } void PerformanceGraph::AddData(float data) { if ((int)mData.size() < mMaxData) mData.push_back(data); else mData[mCurrentIdx]= data; mCurrentIdx = (mCurrentIdx + 1) % mMaxData; } void PerformanceGraph::Reset() { mCurrentIdx = 0; mData.clear(); } void PerformanceGraph::SetGraphColor(float *col) { mGraphColor[0] = col[0]; mGraphColor[1] = col[1]; mGraphColor[2] = col[2]; }