1 | #include "PerformanceGraph.h"
|
---|
2 | #include "glInterface.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | PerformanceGraph::PerformanceGraph(int maxData):
|
---|
6 | mMaxData(maxData), mCurrentIdx(0)
|
---|
7 | {
|
---|
8 | mGraphColor[0] = 1.0f;
|
---|
9 | mGraphColor[1] = 0.0f;
|
---|
10 | mGraphColor[2] = 0.0f;
|
---|
11 | }
|
---|
12 |
|
---|
13 |
|
---|
14 | void PerformanceGraph::DrawBackGround()
|
---|
15 | {
|
---|
16 | glEnable(GL_BLEND);
|
---|
17 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
18 |
|
---|
19 | glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
|
---|
20 |
|
---|
21 | // show a grid
|
---|
22 | glBegin(GL_QUADS);
|
---|
23 | glVertex3f(0, 0, 0.0f);
|
---|
24 | glVertex3f(1, 0, 0.0f);
|
---|
25 | glVertex3f(1, 1, 0.0f);
|
---|
26 | glVertex3f(0, 1, 0.0f);
|
---|
27 | glEnd();
|
---|
28 |
|
---|
29 | glDisable(GL_BLEND);
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | void PerformanceGraph::DrawGrid()
|
---|
34 | {
|
---|
35 | glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
|
---|
36 |
|
---|
37 | // show a grid
|
---|
38 | glBegin(GL_LINE_LOOP);
|
---|
39 | glVertex3f(0, 0, 0.0f);
|
---|
40 | glVertex3f(1, 0, 0.0f);
|
---|
41 | glVertex3f(1, 1, 0.0f);
|
---|
42 | glVertex3f(0, 1, 0.0f);
|
---|
43 | glEnd();
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | void PerformanceGraph::Draw()
|
---|
48 | {
|
---|
49 | glPushAttrib(GL_ALL_ATTRIB_BITS);
|
---|
50 |
|
---|
51 | glLineWidth(2);
|
---|
52 | glDisable(GL_DEPTH_TEST);
|
---|
53 | //glDepthMask(true);
|
---|
54 |
|
---|
55 | glDisable(GL_LIGHTING);
|
---|
56 | glDisable(GL_TEXTURE_2D);
|
---|
57 |
|
---|
58 | // init ortographic projection
|
---|
59 | glMatrixMode(GL_PROJECTION);
|
---|
60 | glPushMatrix();
|
---|
61 | glLoadIdentity();
|
---|
62 |
|
---|
63 | gluOrtho2D(0, 1.0f, 0, 1.0f);
|
---|
64 |
|
---|
65 | glMatrixMode(GL_MODELVIEW);
|
---|
66 | glPushMatrix();
|
---|
67 | glLoadIdentity();
|
---|
68 |
|
---|
69 | glTranslatef(0.668f, 0.668f, 0.0f);
|
---|
70 | glScalef(0.33f, 0.33f, 1.0f);
|
---|
71 |
|
---|
72 | DrawBackGround();
|
---|
73 | DrawCurve();
|
---|
74 | DrawGrid();
|
---|
75 |
|
---|
76 | glPopMatrix();
|
---|
77 |
|
---|
78 | // restore the projection matrix
|
---|
79 | glMatrixMode(GL_PROJECTION);
|
---|
80 | glPopMatrix();
|
---|
81 |
|
---|
82 | glMatrixMode(GL_MODELVIEW);
|
---|
83 |
|
---|
84 | glPopAttrib();
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | void PerformanceGraph::DrawCurve()
|
---|
89 | {
|
---|
90 | const float scalex = 1.0f / mMaxData;
|
---|
91 | const float scaley = 1.0f / 50.0f;
|
---|
92 |
|
---|
93 | glColor3fv(mGraphColor);
|
---|
94 |
|
---|
95 | glBegin(GL_LINE_STRIP);
|
---|
96 |
|
---|
97 | for (size_t i = 0; i < mData.size(); ++ i)
|
---|
98 | {
|
---|
99 | const float x = i * scalex;
|
---|
100 | const float y = mData[i] * scaley;
|
---|
101 |
|
---|
102 | glVertex3f(x, y, 0.0f);
|
---|
103 | }
|
---|
104 |
|
---|
105 | glEnd();
|
---|
106 |
|
---|
107 | // show a grid
|
---|
108 | glBegin(GL_LINES);
|
---|
109 | glVertex3f(mCurrentIdx * scalex, 0, 0.0f);
|
---|
110 | glVertex3f(mCurrentIdx * scalex, 1, 0.0f);
|
---|
111 | glEnd();
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | void PerformanceGraph::AddData(float data)
|
---|
116 | {
|
---|
117 | if ((int)mData.size() < mMaxData)
|
---|
118 | mData.push_back(data);
|
---|
119 | else
|
---|
120 | mData[mCurrentIdx]= data;
|
---|
121 |
|
---|
122 | mCurrentIdx = (mCurrentIdx + 1) % mMaxData;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | void PerformanceGraph::Reset()
|
---|
127 | {
|
---|
128 | mCurrentIdx = 0;
|
---|
129 | mData.clear();
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | void PerformanceGraph::SetGraphColor(float *col)
|
---|
134 | {
|
---|
135 | mGraphColor[0] = col[0];
|
---|
136 | mGraphColor[1] = col[1];
|
---|
137 | mGraphColor[2] = col[2];
|
---|
138 | } |
---|