Repository navigation
Expand file tree
/
Copy pathtbars.cpp
More file actions
151 lines (124 loc) · 4.03 KB
/
Copy pathtbars.cpp
File metadata and controls
151 lines (124 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "tbars.h"
#include "esShader.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define BAR_WIDTH 0.5
#define VERTEX_POS_LOCATION 0
#define MVP_LOCATION 1
#define COLOR_LOCATION 2
TBars::TBars(void)
{
for(int i=0;i<N_BARS;i++){
data[i] = 0.0f;
}
i_current = 0;
program = 0;
vboIds[0] = 0;
vboIds[1] = 0;
vaoId = 0;
}
TBars::~TBars(void)
{
glDeleteProgram( program );
glDeleteBuffers( 2, vboIds );
glDeleteVertexArrays( 1, &vaoId );
}
bool TBars::Init(void)
{
const char vShaderStr[] =
"#version 310 es \n"
"layout(location = 0) in vec2 a_position; \n"
"layout(location = 1) uniform mat4 MVP; \n"
"void main() \n"
"{ \n"
" gl_Position = MVP * vec4(a_position,0.0,1.0); \n"
"}\n";
const char fShaderStr[] =
"#version 310 es \n"
"precision mediump float; \n"
"layout(location = 2) uniform vec4 color; \n"
"out vec4 o_fragColor; \n"
"void main() \n"
"{ \n"
" o_fragColor = color; \n"
"}\n";
GLfloat vertices[4*2] =
{
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
GLushort indices[2*3] = {0, 1, 2, 0, 2, 3};
program = esLoadProgram( vShaderStr, fShaderStr );
if( program == 0 ){
return false;
}
glGenBuffers( 2, vboIds );
glBindBuffer(GL_ARRAY_BUFFER, vboIds[0] );
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
vertices, GL_STATIC_DRAW );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[1] );
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),
indices, GL_STATIC_DRAW );
glGenVertexArrays( 1, &vaoId );
glBindVertexArray( vaoId );
glBindBuffer(GL_ARRAY_BUFFER, vboIds[0] );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[1] );
glEnableVertexAttribArray( VERTEX_POS_LOCATION );
glVertexAttribPointer( VERTEX_POS_LOCATION, 2,
GL_FLOAT, GL_FALSE, 2*sizeof(float),
(const void *)0);
glBindVertexArray( 0 );
return true;
}
void TBars::Draw(int width, int height)
{
if(program == 0)return;
glViewport(0, 0, width, height );
glUseProgram( program );
glBindVertexArray( vaoId );
float aspect = (float)width/(float)height;
glm::mat4 proj;
if(aspect >= 1.0){
float border = (aspect - 1.0f)/2.0f;
proj = glm::ortho(-border, 1.0f+border,
0.0f, 1.0f, -1.0f, 1.0f );
}else{
float border = (1.0f/aspect - 1.0f)/2.0f;
proj = glm::ortho(0.0f, 1.0f,
-border, 1.0f+border,
-1.0f, 1.0f);
}
int i = i_current - 1;
if(i<0)i+=N_BARS;
float bar_seperation = 1.0/N_BARS;
float bar_height = bar_seperation*BAR_WIDTH;
float bar_offset = (bar_seperation-bar_height)/2.0f;
for(int n=0;n<N_BARS;n++){
glm::mat4 model(1.0f);
glm::vec3 trans_v(0.0f, n*bar_seperation+bar_offset, 0.0f);
model = glm::translate( model, trans_v );
glm::vec3 scale_v(data[i]*0.5f,bar_height,1.0f);
model = glm::scale( model, scale_v );
glUniformMatrix4fv(MVP_LOCATION,
1,
GL_FALSE,
glm::value_ptr(proj*model));
glUniform4f(COLOR_LOCATION,
1.0f, 1.0f, 0.0f, 1.0f);
glDrawElements( GL_TRIANGLES, 6,
GL_UNSIGNED_SHORT, (const void *)0 );
i--;
if(i<0)i+=N_BARS;
}
glUseProgram( 0 );
glBindVertexArray( 0 );
}
void TBars::Update(float x)
{
data[i_current] = x;
i_current++;
i_current%=N_BARS;
}