You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
7.1KB

  1. //
  2. // Copyright (c) 2013 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <stdio.h>
  19. #ifdef NANOVG_GLEW
  20. # include <GL/glew.h>
  21. #endif
  22. #ifdef __APPLE__
  23. # define GLFW_INCLUDE_GLCOREARB
  24. #endif
  25. #include <GLFW/glfw3.h>
  26. #include "nanovg.h"
  27. #define NANOVG_GL3_IMPLEMENTATION
  28. #include "nanovg_gl.h"
  29. #include "nanovg_gl_utils.h"
  30. #include "perf.h"
  31. void renderPattern(NVGcontext* vg, NVGLUframebuffer* fb, float t, float pxRatio)
  32. {
  33. int winWidth, winHeight;
  34. int fboWidth, fboHeight;
  35. int pw, ph, x, y;
  36. float s = 20.0f;
  37. float sr = (cosf(t)+1)*0.5f;
  38. float r = s * 0.6f * (0.2f + 0.8f * sr);
  39. if (fb == NULL) return;
  40. nvgImageSize(vg, fb->image, &fboWidth, &fboHeight);
  41. winWidth = (int)(fboWidth / pxRatio);
  42. winHeight = (int)(fboHeight / pxRatio);
  43. // Draw some stuff to an FBO as a test
  44. nvgluBindFramebuffer(fb);
  45. glViewport(0, 0, fboWidth, fboHeight);
  46. glClearColor(0, 0, 0, 0);
  47. glClear(GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  48. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  49. pw = (int)ceilf(winWidth / s);
  50. ph = (int)ceilf(winHeight / s);
  51. nvgBeginPath(vg);
  52. for (y = 0; y < ph; y++) {
  53. for (x = 0; x < pw; x++) {
  54. float cx = (x+0.5f) * s;
  55. float cy = (y+0.5f) * s;
  56. nvgCircle(vg, cx,cy, r);
  57. }
  58. }
  59. nvgFillColor(vg, nvgRGBA(220,160,0,200));
  60. nvgFill(vg);
  61. nvgEndFrame(vg);
  62. nvgluBindFramebuffer(NULL);
  63. }
  64. int loadFonts(NVGcontext* vg)
  65. {
  66. int font;
  67. font = nvgCreateFont(vg, "sans", "../example/Roboto-Regular.ttf");
  68. if (font == -1) {
  69. printf("Could not add font regular.\n");
  70. return -1;
  71. }
  72. font = nvgCreateFont(vg, "sans-bold", "../example/Roboto-Bold.ttf");
  73. if (font == -1) {
  74. printf("Could not add font bold.\n");
  75. return -1;
  76. }
  77. return 0;
  78. }
  79. void errorcb(int error, const char* desc)
  80. {
  81. printf("GLFW error %d: %s\n", error, desc);
  82. }
  83. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  84. {
  85. NVG_NOTUSED(scancode);
  86. NVG_NOTUSED(mods);
  87. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  88. glfwSetWindowShouldClose(window, GL_TRUE);
  89. }
  90. int main()
  91. {
  92. GLFWwindow* window;
  93. NVGcontext* vg = NULL;
  94. GPUtimer gpuTimer;
  95. PerfGraph fps, cpuGraph, gpuGraph;
  96. double prevt = 0, cpuTime = 0;
  97. NVGLUframebuffer* fb = NULL;
  98. int winWidth, winHeight;
  99. int fbWidth, fbHeight;
  100. float pxRatio;
  101. if (!glfwInit()) {
  102. printf("Failed to init GLFW.");
  103. return -1;
  104. }
  105. initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
  106. initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time");
  107. initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time");
  108. glfwSetErrorCallback(errorcb);
  109. #ifndef _WIN32 // don't require this on win32, and works with more cards
  110. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  111. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  112. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  113. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  114. #endif
  115. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
  116. #ifdef DEMO_MSAA
  117. glfwWindowHint(GLFW_SAMPLES, 4);
  118. #endif
  119. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  120. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  121. if (!window) {
  122. glfwTerminate();
  123. return -1;
  124. }
  125. glfwSetKeyCallback(window, key);
  126. glfwMakeContextCurrent(window);
  127. #ifdef NANOVG_GLEW
  128. glewExperimental = GL_TRUE;
  129. if(glewInit() != GLEW_OK) {
  130. printf("Could not init glew.\n");
  131. return -1;
  132. }
  133. // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
  134. glGetError();
  135. #endif
  136. #ifdef DEMO_MSAA
  137. vg = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_DEBUG);
  138. #else
  139. vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
  140. #endif
  141. if (vg == NULL) {
  142. printf("Could not init nanovg.\n");
  143. return -1;
  144. }
  145. // Create hi-dpi FBO for hi-dpi screens.
  146. glfwGetWindowSize(window, &winWidth, &winHeight);
  147. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  148. // Calculate pixel ration for hi-dpi devices.
  149. pxRatio = (float)fbWidth / (float)winWidth;
  150. // The image pattern is tiled, set repeat on x and y.
  151. fb = nvgluCreateFramebuffer(vg, (int)(100*pxRatio), (int)(100*pxRatio), NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY);
  152. if (fb == NULL) {
  153. printf("Could not create FBO.\n");
  154. return -1;
  155. }
  156. if (!loadFonts(vg) == -1) {
  157. printf("Could not load fonts\n");
  158. return -1;
  159. }
  160. glfwSwapInterval(0);
  161. initGPUTimer(&gpuTimer);
  162. glfwSetTime(0);
  163. prevt = glfwGetTime();
  164. while (!glfwWindowShouldClose(window))
  165. {
  166. double mx, my, t, dt;
  167. float gpuTimes[3];
  168. int i, n;
  169. t = glfwGetTime();
  170. dt = t - prevt;
  171. prevt = t;
  172. startGPUTimer(&gpuTimer);
  173. glfwGetCursorPos(window, &mx, &my);
  174. glfwGetWindowSize(window, &winWidth, &winHeight);
  175. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  176. // Calculate pixel ration for hi-dpi devices.
  177. pxRatio = (float)fbWidth / (float)winWidth;
  178. renderPattern(vg, fb, t, pxRatio);
  179. // Update and render
  180. glViewport(0, 0, fbWidth, fbHeight);
  181. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  182. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  183. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  184. // Use the FBO as image pattern.
  185. if (fb != NULL) {
  186. NVGpaint img = nvgImagePattern(vg, 0, 0, 100, 100, 0, fb->image, 1.0f);
  187. nvgSave(vg);
  188. for (i = 0; i < 20; i++) {
  189. nvgBeginPath(vg);
  190. nvgRect(vg, 10 + i*30,10, 10, winHeight-20);
  191. nvgFillColor(vg, nvgHSLA(i/19.0f, 0.5f, 0.5f, 255));
  192. nvgFill(vg);
  193. }
  194. nvgBeginPath(vg);
  195. nvgRoundedRect(vg, 140 + sinf(t*1.3f)*100, 140 + cosf(t*1.71244f)*100, 250, 250, 20);
  196. nvgFillPaint(vg, img);
  197. nvgFill(vg);
  198. nvgStrokeColor(vg, nvgRGBA(220,160,0,255));
  199. nvgStrokeWidth(vg, 3.0f);
  200. nvgStroke(vg);
  201. nvgRestore(vg);
  202. }
  203. renderGraph(vg, 5,5, &fps);
  204. renderGraph(vg, 5+200+5,5, &cpuGraph);
  205. if (gpuTimer.supported)
  206. renderGraph(vg, 5+200+5+200+5,5, &gpuGraph);
  207. nvgEndFrame(vg);
  208. // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
  209. cpuTime = glfwGetTime() - t;
  210. updateGraph(&fps, dt);
  211. updateGraph(&cpuGraph, cpuTime);
  212. // We may get multiple results.
  213. n = stopGPUTimer(&gpuTimer, gpuTimes, 3);
  214. for (i = 0; i < n; i++)
  215. updateGraph(&gpuGraph, gpuTimes[i]);
  216. glfwSwapBuffers(window);
  217. glfwPollEvents();
  218. }
  219. nvgluDeleteFramebuffer(fb);
  220. nvgDeleteGL3(vg);
  221. printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f);
  222. printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f);
  223. printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f);
  224. glfwTerminate();
  225. return 0;
  226. }