Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Using a transparent background now. The gui also works in a 3D scene.…

… We switch from ortho to perspective
  • Loading branch information...
commit 16d72ac02cb9f7f7e51d3bc2ef2097edc86af9f5 1 parent e539ca5
@roxlu authored

Showing 2 changed files with 145 additions and 55 deletions. Show diff stats Hide diff stats

  1. +139 55 src/GGui.cpp
  2. +6 0 src/GGui.h
194 src/GGui.cpp
... ... @@ -1,4 +1,7 @@
1 1 #include "GGui.h"
  2 +
  3 +GGui gui;
  4 +
2 5 //#include "Error.h"
3 6 GGui::GGui()
4 7 :is_data_changed(false)
@@ -7,8 +10,6 @@ GGui::GGui()
7 10 ,x(0)
8 11 ,y(0)
9 12 {
10   - glGenBuffers(1, &vbo_vertices); eglGetError();
11   - glGenBuffers(1, &vbo_texcoords); eglGetError();
12 13 }
13 14
14 15 void GGui::enableEventListeners() {
@@ -20,6 +21,9 @@ void GGui::disableEventListeners() {
20 21
21 22 //void GGui::setup(ofEventArgs& args) {
22 23 void GGui::setup() {
  24 + glGenBuffers(1, &vbo_vertices); eglGetError();
  25 + glGenBuffers(1, &vbo_texcoords); eglGetError();
  26 +
23 27 checkAssetFiles();
24 28 x = 0;
25 29 y = 0;
@@ -28,23 +32,28 @@ void GGui::setup() {
28 32 metrics.label_width = 165;
29 33 metrics.row_width = 400;
30 34 metrics.row_height = 25;
31   - label_font.loadFont("gui/gui_font.ttf",9, true, true);
32   - value_font.loadFont("gui/gui_font.ttf",9, true, true);
  35 + label_font.loadFont("gui/gui_font.ttf",8, true, true);
  36 + value_font.loadFont("gui/gui_font.ttf",8, true, true);
  37 +
33 38 createGuiTexture();
34 39
  40 + createOrthoProjectionMatrix();
  41 +
  42 + // We're not drawing the background anymore... though leave the code
  43 + // here as I might change this in the future.....
35 44 // Create background quad
36 45 // -------------------------------------------------------------------------
37   - float h = 1440; // fmp height
38   - addVertex(x,y);
39   - addVertex(x+metrics.row_width,y);
40   - addVertex(x+metrics.row_width,y+h);
41   - addVertex(x,y+h);
42   -
43   -
44   - addTexCoord(GTCX(48), GTCY(128));
45   - addTexCoord(GTCX(448), GTCY(128));
46   - addTexCoord(GTCX(448), GTCY(128+h));
47   - addTexCoord(GTCX(48), GTCY(128+h));
  46 + float h = 1440; // tmp height
  47 + // addVertex(x,y);
  48 + // addVertex(x+metrics.row_width,y);
  49 + // addVertex(x+metrics.row_width,y+h);
  50 + // addVertex(x,y+h);
  51 +
  52 +
  53 + // addTexCoord(GTCX(48), GTCY(128));
  54 + // addTexCoord(GTCX(448), GTCY(128));
  55 + // addTexCoord(GTCX(448), GTCY(128+h));
  56 + // addTexCoord(GTCX(48), GTCY(128+h));
48 57
49 58 ofAddListener(ofEvents.update, this, &GGui::update);// we need to put this outise enableEventListeners
50 59 enableEventListeners();
@@ -53,6 +62,34 @@ void GGui::setup() {
53 62 show();
54 63 }
55 64
  65 +void GGui::createOrthoProjectionMatrix() {
  66 + float w = ofGetWidth();
  67 + float h = ofGetHeight();
  68 + float n = 0.1;
  69 + float f = 10.0;
  70 + float m[16];
  71 +
  72 + ortho_projection[1] = 0.0f;
  73 + ortho_projection[2] = 0.0f;
  74 + ortho_projection[3] = 0.0f;
  75 + ortho_projection[4] = 0.0f;
  76 + ortho_projection[6] = 0.0f;
  77 + ortho_projection[7] = 0.0f;
  78 + ortho_projection[8] = 0.0f;
  79 + ortho_projection[9] = 0.0f;
  80 + ortho_projection[11] = 0.0f;
  81 + ortho_projection[15] = 1.0f;
  82 +
  83 + float fmn = f - n;
  84 + ortho_projection[0] = 2.0f / w;
  85 + ortho_projection[5] = 2.0f / -h;
  86 + ortho_projection[10] = -2.0f / fmn;
  87 + ortho_projection[12] = -(w)/w;
  88 + ortho_projection[13] = -(h)/-h;
  89 + ortho_projection[14] = -(f+n)/fmn;
  90 +}
  91 +
  92 +
56 93 GSlider& GGui::addFloat(string title, float& value) {
57 94 GSlider* slider = new GSlider(this, &value);
58 95 slider->setLabel(title);
@@ -146,56 +183,103 @@ void GGui::updateVertices() {
146 183 }
147 184
148 185 void GGui::draw() {
149   - glPushMatrix();
150   -// glColor4f(1.0, 1.0, 1.0, 0.8); // could be usefull when stuff drawn underneath
151   - glTranslatef(x,y,0);
152   -
153   - // texture.
154   - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
155   - glEnable(GL_TEXTURE_2D);
156   - glBindTexture(GL_TEXTURE_2D, gui_texture); eglGetError();
157   - glEnable(GL_BLEND); eglGetError();
158   - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); eglGetError()
159   -
160   - // texcoords
161   - glBindBuffer(GL_ARRAY_BUFFER, vbo_texcoords); eglGetError();
162   - glEnableClientState(GL_TEXTURE_COORD_ARRAY); eglGetError();
163   - glTexCoordPointer(2,GL_FLOAT, 0, 0); eglGetError();
  186 + // get current model and projection matrix. We draw the gui in orthographic
  187 + // mode and we will reset the projection and modelview matrices after drawing
  188 + // the gui.
  189 + GLdouble curr_proj[16];
  190 + GLdouble curr_model[16];
  191 + GLdouble curr_texture[16];
  192 + glGetDoublev(GL_PROJECTION_MATRIX, curr_proj);
  193 + glGetDoublev(GL_MODELVIEW_MATRIX, curr_model);
  194 + glGetDoublev(GL_TEXTURE_MATRIX, curr_texture);
164 195
165   - // vertices.
166   - glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices); eglGetError();
167   - glEnableClientState(GL_VERTEX_ARRAY); eglGetError();
168   - glVertexPointer(2, GL_FLOAT,0,0); eglGetError();
  196 + // we use non-normalized texcoords
  197 + glMatrixMode(GL_TEXTURE);
  198 + glLoadIdentity();
169 199
170   - // draw background.
171   - glDrawArrays(GL_QUADS, 0, vertices.size()); eglGetError();
172   -
173   - glBindBuffer(GL_ARRAY_BUFFER, 0); eglGetError();
  200 + glMatrixMode(GL_PROJECTION);
  201 + glLoadMatrixf(ortho_projection);
174 202
175   - glBindTexture(GL_TEXTURE_2D, 0); eglGetError();
  203 + glMatrixMode(GL_MODELVIEW);
  204 + glLoadIdentity();
  205 + glTranslatef(0,0,-5);
176 206
177   - float mx = ofGetMouseX();
178   - float my = ofGetMouseY();
179   - const int num = objects.size();
180   - GObject** ptr = (num != 0) ? &objects.front() : NULL;
181   - glColor3f(0.9,0.9,0.8);
182   - for(int i = 0; i < num; ++i) {
183   - // debug draw
184   -// glBegin(GL_LINE_LOOP);
185   -// glVertex2f(ptr[i]->x, ptr[i]->y);
186   -// glVertex2f(ptr[i]->x+ptr[i]->width, ptr[i]->y);
187   -// glVertex2f(ptr[i]->x+ptr[i]->width, ptr[i]->y+ptr[i]->height);
188   -// glVertex2f(ptr[i]->x, ptr[i]->y+ptr[i]->height);
189   -// glEnd();
190   - ptr[i]->draw();
  207 + bool cull_enabled = false;
  208 + if(glIsEnabled(GL_CULL_FACE)) {
  209 + glDisable(GL_CULL_FACE);
  210 + cull_enabled = true;
191 211 }
192   - glColor3f(1,1,1);
193   - glPopMatrix();
194 212
  213 + bool depth_enabled = glIsEnabled(GL_DEPTH_TEST);
  214 + glDisable(GL_DEPTH_TEST);
  215 +
  216 + // draw gui.
  217 + glPushMatrix();
  218 + glColor4f(1.0, 1.0, 1.0, 1.0);
  219 + glTranslatef(x,y,0);
  220 +
  221 + // texture.
  222 + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  223 + glEnable(GL_TEXTURE_2D);
  224 + glBindTexture(GL_TEXTURE_2D, gui_texture); eglGetError();
  225 + glEnable(GL_BLEND); eglGetError();
  226 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); eglGetError()
  227 +
  228 + // texcoords
  229 + glBindBuffer(GL_ARRAY_BUFFER, vbo_texcoords); eglGetError();
  230 + glEnableClientState(GL_TEXTURE_COORD_ARRAY); eglGetError();
  231 + glTexCoordPointer(2,GL_FLOAT, 0, 0); eglGetError();
  232 +
  233 + // vertices.
  234 + glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices); eglGetError();
  235 + glEnableClientState(GL_VERTEX_ARRAY); eglGetError();
  236 + glVertexPointer(2, GL_FLOAT,0,0); eglGetError();
  237 +
  238 + // draw background.
  239 + glDrawArrays(GL_QUADS, 0, vertices.size()); eglGetError();
  240 +
  241 + glBindBuffer(GL_ARRAY_BUFFER, 0); eglGetError();
  242 +
  243 + glBindTexture(GL_TEXTURE_2D, 0); eglGetError();
  244 +
  245 + float mx = ofGetMouseX();
  246 + float my = ofGetMouseY();
  247 + const int num = objects.size();
  248 + GObject** ptr = (num != 0) ? &objects.front() : NULL;
  249 + // glColor3f(0.9,0.9,0.8);
  250 + for(int i = 0; i < num; ++i) {
  251 + // debug draw
  252 + // glBegin(GL_LINE_LOOP);
  253 + // glVertex2f(ptr[i]->x, ptr[i]->y);
  254 + // glVertex2f(ptr[i]->x+ptr[i]->width, ptr[i]->y);
  255 + // glVertex2f(ptr[i]->x+ptr[i]->width, ptr[i]->y+ptr[i]->height);
  256 + // glVertex2f(ptr[i]->x, ptr[i]->y+ptr[i]->height);
  257 + // glEnd();
  258 + ptr[i]->draw();
  259 + }
  260 + glColor3f(1,1,1);
  261 + glPopMatrix();
  262 +
195 263 glDisable(GL_BLEND); eglGetError();
196 264 glDisableClientState(GL_VERTEX_ARRAY); eglGetError();
197 265 glDisableClientState(GL_TEXTURE_COORD_ARRAY); eglGetError();
198 266 glBindBuffer(GL_ARRAY_BUFFER, 0);
  267 +
  268 + // reset previous projection and modelview matrices
  269 + glMatrixMode(GL_TEXTURE);
  270 + glLoadMatrixd(curr_texture);
  271 +
  272 + glMatrixMode(GL_PROJECTION);
  273 + glLoadMatrixd(curr_proj);
  274 + glMatrixMode(GL_MODELVIEW);
  275 + glLoadMatrixd(curr_model);
  276 +
  277 + if(cull_enabled) {
  278 + glEnable(GL_CULL_FACE);
  279 + }
  280 + if(depth_enabled) {
  281 + glEnable(GL_DEPTH_TEST);
  282 + }
199 283 }
200 284
201 285
6 src/GGui.h
@@ -88,11 +88,13 @@ class GGui {
88 88 void addObject(GObject* obj);
89 89 void updateVertices();
90 90 void positionObjects();
  91 + void createOrthoProjectionMatrix();
91 92 string name;
92 93 string clean_name; // lowercased version of name, no spaces
93 94 bool is_visible;
94 95 int end_tween_on; // we tween in and out of view; this is millis when ended
95 96 int state;
  97 +
96 98 public:
97 99 GMetrics metrics;
98 100 bool is_setup;
@@ -108,8 +110,12 @@ class GGui {
108 110
109 111 ofTrueTypeFont label_font;
110 112 ofTrueTypeFont value_font;
  113 +
  114 + float ortho_projection[16];
111 115 };
112 116
  117 +extern GGui gui;
  118 +
113 119 inline int GGui::addVertex(float x, float y) {
114 120 vertices.push_back(ofVec2f(x,y));
115 121 return vertices.size()-1;

0 comments on commit 16d72ac

Please sign in to comment.
Something went wrong with that request. Please try again.