iPlug2 - C++ Audio Plug-in Framework
TestGLControl.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 #pragma once
12 
19 #include "IControl.h"
20 #include "IGraphics_select.h"
21 
22 #ifdef IGRAPHICS_NANOVG
23 
24 class TestGLControl : public IControl
25 {
26 public:
27  TestGLControl(const IRECT& rect)
28  : IControl(rect, kNoParameter)
29  {
30  SetTooltip("TestGLControl");
31 
32 // SetActionFunction([&](IControl* pCaller)
33 // {
34 // SetAnimation([&](IControl* pCaller)
35 // {
36 // auto progress = pCaller->GetAnimationProgress();
37 //
38 // mXRotation += progress * 5.;
39 // mYRotation += progress * 10.;
40 //
41 // pCaller->SetDirty(false);
42 //
43 // if (progress > 1.) {
44 // pCaller->OnEndAnimation();
45 // return;
46 // }
47 //
48 // }, 1000);
49 // });
50  }
51 
52  ~TestGLControl()
53  {
54  if (mFBO)
55  nvgDeleteFramebuffer(mFBO);
56  }
57 
58  void Draw(IGraphics& g) override
59  {
60  NVGcontext* vg = static_cast<NVGcontext*>(g.GetDrawContext());
61  int w = static_cast<int>(mRECT.W() * g.GetDrawScale());
62  int h = static_cast<int>(mRECT.H() * g.GetDrawScale());
63 
64  if(invalidateFBO)
65  {
66  if (mFBO)
67  nvgDeleteFramebuffer(mFBO);
68 
69  mFBO = nvgCreateFramebuffer(vg, w, h, 0);
70 
71  invalidateFBO = false;
72  }
73 
74  g.DrawDottedRect(COLOR_BLACK, mRECT);
75  g.FillRect(mMouseIsOver ? COLOR_TRANSLUCENT : COLOR_TRANSPARENT, mRECT);
76 
77 #ifdef IGRAPHICS_GL
78  nvgEndFrame(vg);
79  glGetIntegerv(GL_FRAMEBUFFER_BINDING, &mInitialFBO);
80 
81  nvgBindFramebuffer(mFBO);
82  nvgBeginFrame(vg, static_cast<float>(w), static_cast<float>(h), static_cast<float>(g.GetScreenScale()));
83  GLint vp[4];
84  glGetIntegerv(GL_VIEWPORT, vp);
85 
86  glViewport(0, 0, w, h);
87 
88  glScissor(0, 0, w, h);
89  glClearColor(0.f, 0.f, 0.f, 0.f);
90  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
91 
92  DrawGL();
93  glViewport(vp[0], vp[1], vp[2], vp[3]);
94 
95  nvgEndFrame(vg);
96  glBindFramebuffer(GL_FRAMEBUFFER, mInitialFBO);
97  nvgBeginFrame(vg, static_cast<float>(g.WindowWidth()), static_cast<float>(g.WindowHeight()), static_cast<float>(g.GetScreenScale()));
98 
99  APIBitmap apibmp {mFBO->image, w, h, 1, 1.};
100  IBitmap bmp {&apibmp, 1, false};
101 
102  g.DrawFittedBitmap(bmp, mRECT);
103 #else
104  g.DrawText(mText, "UNSUPPORTED", mRECT);
105 #endif
106  }
107 
108  void OnResize() override
109  {
110  invalidateFBO = true;
111  }
112 
113  void OnRescale() override
114  {
115  invalidateFBO = true;
116  }
117 
118 #ifdef IGRAPHICS_GL
119  void DrawGL()
120  {
121  // code from emscripten tests
122 
123  auto compileShader = [](GLenum shaderType, const char *src) {
124  GLuint shader = glCreateShader(shaderType);
125  glShaderSource(shader, 1, &src, NULL);
126  glCompileShader(shader);
127 
128  GLint isCompiled = 0;
129  glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
130  if (!isCompiled)
131  {
132  GLint maxLength = 0;
133  glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
134  char *buf = (char*)malloc(maxLength+1);
135  glGetShaderInfoLog(shader, maxLength, &maxLength, buf);
136  printf("%s\n", buf);
137  free(buf);
138  return GLuint(0);
139  }
140 
141  return shader;
142  };
143 
144  auto createProgram = [](GLuint vertexShader, GLuint fragmentShader) {
145  GLuint program = glCreateProgram();
146  glAttachShader(program, vertexShader);
147  glAttachShader(program, fragmentShader);
148  glBindAttribLocation(program, 0, "apos");
149  glBindAttribLocation(program, 1, "acolor");
150  glLinkProgram(program);
151  return program;
152  };
153 
154  static const char vs_str[] =
155  "attribute vec4 apos;"
156  "attribute vec4 acolor;"
157  "varying vec4 color;"
158  "void main() {"
159  "color = acolor;"
160  "gl_Position = apos;"
161  "}";
162  GLuint vs = compileShader(GL_VERTEX_SHADER, vs_str);
163 
164  static const char fs_str[] =
165 #ifdef OS_WEB
166  "precision lowp float;"
167 #endif
168  "varying vec4 color;"
169  "uniform vec4 color2;"
170  "void main() {"
171  "gl_FragColor = color*color2;"
172  "}";
173  GLuint fs = compileShader(GL_FRAGMENT_SHADER, fs_str);
174 
175  GLuint program = createProgram(vs, fs);
176  glUseProgram(program);
177 
178  static const float posAndColor[] = {
179  // x, y, r, g, b
180  -0.6f, -0.6f, 1, 0, 0,
181  0.6f, -0.6f, 0, 1, 0,
182  0.f, 0.6f, 0, 0, 1,
183  };
184 
185  GLuint vbo;
186  glGenBuffers(1, &vbo);
187  glBindBuffer(GL_ARRAY_BUFFER, vbo);
188  glBufferData(GL_ARRAY_BUFFER, sizeof(posAndColor), posAndColor, GL_STATIC_DRAW);
189  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 20, 0);
190  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 20, (void*)8);
191  glEnableVertexAttribArray(0);
192  glEnableVertexAttribArray(1);
193 
194  float color2[4] = { 0.0f, 1.f, 0.0f, 1.0f };
195  glUniform4fv(glGetUniformLocation(program, "color2"), 1, color2);
196  glDrawArrays(GL_TRIANGLES, 0, 3);
197  }
198 #endif
199 
200 private:
201  NVGframebuffer* mFBO = nullptr;
202  int mInitialFBO = 0;
203  bool invalidateFBO = true;
204 };
205 
206 #else
207 
208 class TestGLControl : public IControl
209 {
210 public:
211  TestGLControl(const IRECT& rect)
212  : IControl(rect)
213  {
214  SetTooltip("TestGLControl");
215  }
216 
217  void Draw(IGraphics& g) override
218  {
219  g.DrawText(mText, "UNSUPPORTED", mRECT);
220  }
221 };
222 #endif
The lowest level base class of an IGraphics control.
Definition: IControl.h:42
Used to manage a rectangular area, independent of draw class/platform.
virtual void OnResize()
Called when IControl is constructed or resized using SetRect().
Definition: IControl.h:148
User-facing bitmap abstraction that you use to manage bitmap data, independant of draw class/platform...
virtual void DrawDottedRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f, float dashLen=2.f)
Draw a dotted rectangle to the graphics context.
Definition: IGraphics.cpp:2517
bool mMouseIsOver
if mGraphics::mHandleMouseOver = true, this will be true when the mouse is over control.
Definition: IControl.h:545
Used for choosing a drawing backend.
float GetScreenScale() const
Gets the screen/display scaling factor, e.g.
Definition: IGraphics.h:1068
int WindowHeight() const
Gets the height of the graphics context including draw scaling.
Definition: IGraphics.h:1056
void SetTooltip(const char *str)
Set a tooltip for the control.
Definition: IControl.h:210
void DrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend=0)
Draw some text to the graphics context in a specific rectangle.
Definition: IGraphics.cpp:631
This file contains the base IControl implementation, along with some base classes for specific types ...
float H() const
float W() const
float GetDrawScale() const
Gets the graphics context scaling factor.
Definition: IGraphics.h:1064
IControl(const IRECT &bounds, int paramIdx=kNoParameter, IActionFunction actionFunc=nullptr)
Constructor.
Definition: IControl.cpp:81
Control to test Drawing in using OpenGL in supporting backends.
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
virtual void FillRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill a rectangular region of the graphics context with a color.
Definition: IGraphics.cpp:2547
virtual void DrawFittedBitmap(const IBitmap &bitmap, const IRECT &bounds, const IBlend *pBlend=0)
Draw a bitmap (raster) image to the graphics context, scaling the image to fit the bounds...
Definition: IGraphics.cpp:2752
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
virtual void * GetDrawContext()=0
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
int WindowWidth() const
Gets the width of the graphics context including draw scaling.
Definition: IGraphics.h:1052
void Draw(IGraphics &g) override
Draw the control to the graphics context.
virtual void OnRescale()
Implement to do something when graphics is scaled globally (e.g.
Definition: IControl.h:145