14#if defined IGRAPHICS_NANOVG
21#include "IGraphicsNanoVG.h"
24using namespace igraphics;
41 nvgDeleteFramebuffer(mFBO);
53 nvgDeleteFramebuffer(mFBO);
55 mFBO = nvgCreateFramebuffer(vg, w, h, 0);
57 invalidateFBO =
false;
64 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &mInitialFBO);
66 nvgBindFramebuffer(mFBO);
67 nvgBeginFrame(vg,
static_cast<float>(w),
static_cast<float>(h),
static_cast<float>(g.
GetScreenScale()));
69 glGetIntegerv(GL_VIEWPORT, vp);
71 glViewport(0, 0, w, h);
73 glScissor(0, 0, w, h);
74 glClearColor(0.f, 0.f, 0.f, 0.f);
75 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
79 auto compileShader = [](GLenum shaderType,
const char *src) {
80 GLuint shader = glCreateShader(shaderType);
81 glShaderSource(shader, 1, &src, NULL);
82 glCompileShader(shader);
85 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
89 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
90 char *buf = (
char*)malloc(maxLength+1);
91 glGetShaderInfoLog(shader, maxLength, &maxLength, buf);
100 auto createProgram = [](GLuint vertexShader, GLuint fragmentShader) {
101 GLuint program = glCreateProgram();
102 glAttachShader(program, vertexShader);
103 glAttachShader(program, fragmentShader);
104 glBindAttribLocation(program, 0,
"apos");
105 glBindAttribLocation(program, 1,
"acolor");
106 glLinkProgram(program);
110 static bool sLoggedGLSLVersion =
false;
111 if (!sLoggedGLSLVersion) {
112 DBGMSG(
"Supported GLSL version is %s.\n", (
char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
113 sLoggedGLSLVersion =
true;
117 static const char vs_str[] =
118 "attribute vec4 apos;\n"
119 "attribute vec4 acolor;\n"
120 "varying vec4 color;\n"
123 " gl_Position = apos;\n"
126 static const char vs_str[] =
127#ifdef IGRAPHICS_GLES3
129 "precision highp float;\n"
133#elif defined(IGRAPHICS_GLES2)
134 "precision highp float;\n"
135 "attribute vec4 apos;\n"
136 "attribute vec4 acolor;\n"
137 "varying vec4 color;\n"
139 "#version 330 core\n"
146 " gl_Position = apos;\n"
149 GLuint vs = compileShader(GL_VERTEX_SHADER, vs_str);
152 static const char fs_str[] =
153 "varying vec4 color;\n"
154 "uniform vec4 color2;\n"
156 " gl_FragColor = color;\n"
159 static const char fs_str[] =
160#ifdef IGRAPHICS_GLES3
162 "precision mediump float;\n"
164 "out vec4 FragColor;\n"
165 "uniform vec4 color2;\n"
167 " FragColor = color;\n"
169#elif defined(IGRAPHICS_GLES2)
170 "precision mediump float;\n"
171 "varying vec4 color;\n"
172 "uniform vec4 color2;\n"
174 " gl_FragColor = color;\n"
177 "#version 330 core\n"
179 "out vec4 FragColor;\n"
180 "uniform vec4 color2;\n"
182 " FragColor = color;\n"
187 GLuint fs = compileShader(GL_FRAGMENT_SHADER, fs_str);
189 GLuint program = createProgram(vs, fs);
190 glUseProgram(program);
192 static const float posAndColor[] = {
194 -0.6f, -0.6f, 1.0f, 0.0f, 0.0f,
195 0.6f, -0.6f, 0.0f, 1.0f, 0.0f,
196 0.f, 0.6f, 0.0f, 0.0f, 1.0f,
199 #if !defined(IGRAPHICS_GL2) && !defined(IGRAPHICS_GLES2)
201 glGenVertexArrays(1, &vao);
202 glBindVertexArray(vao);
206 glGenBuffers(1, &vbo);
207 glBindBuffer(GL_ARRAY_BUFFER, vbo);
208 glBufferData(GL_ARRAY_BUFFER,
sizeof(posAndColor), posAndColor, GL_STATIC_DRAW);
211 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 *
sizeof(
float), 0);
212 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 *
sizeof(
float), (
void*)(2 *
sizeof(
float)));
213 glEnableVertexAttribArray(0);
214 glEnableVertexAttribArray(1);
216 GLint posAttrib = glGetAttribLocation(program,
"apos");
217 GLint colorAttrib = glGetAttribLocation(program,
"acolor");
218 glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 5 *
sizeof(
float), 0);
219 glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 5 *
sizeof(
float), (
void*)(2 *
sizeof(
float)));
220 glEnableVertexAttribArray(posAttrib);
221 glEnableVertexAttribArray(colorAttrib);
224 glDrawArrays(GL_TRIANGLES, 0, 3);
226 glViewport(vp[0], vp[1], vp[2], vp[3]);
229 glBindFramebuffer(GL_FRAMEBUFFER, mInitialFBO);
230 nvgBeginFrame(vg,
static_cast<float>(g.
WindowWidth()),
234 APIBitmap apibmp {mFBO->image, w, h, 1, 1.};
235 IBitmap bmp {&apibmp, 1,
false};
242 invalidateFBO =
true;
247 invalidateFBO =
true;
249#elif defined IGRAPHICS_METAL
258 NVGframebuffer* mFBO =
nullptr;
260#ifdef IGRAPHICS_METAL
261 void* mRenderPassDescriptor =
nullptr;
262 void* mRenderPipeline =
nullptr;
266 bool invalidateFBO =
true;
282 g.
DrawText(mText,
"UNSUPPORTED", mRECT);
This file contains the base IControl implementation, along with some base classes for specific types ...
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
User-facing bitmap abstraction that you use to manage bitmap data, independant of draw class/platform...
The lowest level base class of an IGraphics control.
virtual void OnResize()
Called when IControl is constructed or resized using SetRect().
bool mMouseIsOver
if mGraphics::mHandleMouseOver = true, this will be true when the mouse is over control.
virtual void OnRescale()
Implement to do something when graphics is scaled globally (e.g.
IControl * SetTooltip(const char *str)
Set a tooltip for the control.
The lowest level base class of an IGraphics context.
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.
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.
virtual void * GetDrawContext()=0
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
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.
int WindowWidth() const
Gets the width of the graphics context including draw scaling.
virtual void FillRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill a rectangular region of the graphics context with a color.
float GetScreenScale() const
Gets the screen/display scaling factor, e.g.
float GetDrawScale() const
Gets the graphics context scaling factor.
int WindowHeight() const
Gets the height of the graphics context including draw scaling.
A base class for knob/dial controls, to handle mouse action and Sender.
Control to test IGraphicsNanoVG with Metal Shaders.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
Used to manage a rectangular area, independent of draw class/platform.