int main() { glfwInit(); glfwDefaultWindowHints(); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); GLFWwindow *window = glfwCreateWindow(800, 600, "nanovg Demo", nullptr, nullptr); NVGcontext *vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); while (!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); nvgBeginFrame(vg, 800, 600, 1); nvgBeginPath(vg); nvgRoundedRect(vg, 12, 12, 800-12*2, 600-12*2, 4); nvgFillColor(vg, nvgRGBA(255, 255, 255, 192)); nvgFill(vg); nvgEndFrame(vg); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); return 0; }
void Application::drawUI() { if (context->dirtyUI) { context->setCursor(nullptr); if (uiFrameBuffer->width() != context->screenSize.x || uiFrameBuffer->height() != context->screenSize.y) { uiFrameBuffer->initialize(context->screenSize.x, context->screenSize.y); } uiFrameBuffer->begin(); glViewport(0, 0, uiFrameBuffer->width(),uiFrameBuffer->height()); NVGcontext* nvg = context->nvgContext; nvgBeginFrame(nvg, uiFrameBuffer->width(), uiFrameBuffer->height(),1.0f);//(float) context->pixelRatio nvgScissor(nvg, 0, 0, (float)uiFrameBuffer->width(), (float)uiFrameBuffer->height()); rootRegion.draw(context.get()); nvgScissor(nvg, 0, 0, (float)uiFrameBuffer->width(), (float)uiFrameBuffer->height()); Region* onTop = context->getOnTopRegion(); if (onTop != nullptr) { if (onTop->isVisible()) onTop->draw(context.get()); } const Cursor* cursor = context->getCursor(); if (!cursor) { cursor = &Cursor::Normal; } nvgEndFrame(nvg); uiFrameBuffer->end(); context->dirtyUI = false; } imageShader->draw(uiFrameBuffer->getTexture(), pixel2(0, 0),pixel2(context->viewSize)); }
virtual void drawImplementation( osg::RenderInfo& renderInfo ) const { unsigned int contextID = renderInfo.getContextID(); if ( !_initialized ) { NanoVGDrawable* constMe = const_cast<NanoVGDrawable*>(this); glewInit(); constMe->_vg = nvgCreateGL2( NVG_ANTIALIAS|NVG_STENCIL_STROKES|NVG_DEBUG ); if ( !constMe->_vg ) { OSG_NOTICE << "[NanoVGDrawable] Failed to create VG context" << std::endl; return; } constMe->initializeGL( renderInfo.getState() ); constMe->_activeContextID = contextID; constMe->_initialized = true; } else if ( _vg && contextID==_activeContextID ) { osg::State* state = renderInfo.getState(); state->disableAllVertexArrays(); state->disableTexCoordPointer( 0 ); nvgBeginFrame( _vg, _width, _height, 1.0f ); updateGL( state ); nvgEndFrame( _vg ); } }
void end_frame() { #ifdef GRAPHICS draw_ui_scale(); //renderGraph(vg, 5, 5, &fps_graph); //renderGraph(vg, 5 + 200 + 5, 5, &cpu_graph); if (gpu_timer.supported) renderGraph(vg, 5 + 200 + 5 + 200 + 5, 5, &gpu_graph); draw_world_scale(); nvgEndFrame(vg); cpu_time = glfwGetTime() - render_start_time; updateGraph(&fps_graph, delta_time); updateGraph(&cpu_graph, cpu_time); float gpu_times[3]; int n = stopGPUTimer(&gpu_timer, gpu_times, 3); for (int i = 0; i < n; i++) updateGraph(&gpu_graph, gpu_times[i]); glfwSwapBuffers(window); glfwPollEvents(); #endif }
void Screen::drawWidgets() { if (!mVisible) return; glfwMakeContextCurrent(mGLFWWindow); glfwGetFramebufferSize(mGLFWWindow, &mFBSize[0], &mFBSize[1]); glfwGetWindowSize(mGLFWWindow, &mSize[0], &mSize[1]); glViewport(0, 0, mFBSize[0], mFBSize[1]); /* Calculate pixel ratio for hi-dpi devices. */ mPixelRatio = (float) mFBSize[0] / (float) mSize[0]; nvgBeginFrame(mNVGContext, mSize[0], mSize[1], mPixelRatio); draw(mNVGContext); double elapsed = glfwGetTime() - mLastInteraction; if (elapsed > 0.5f) { /* Draw tooltips */ const Widget *widget = findWidget(mMousePos); if (widget && !widget->tooltip().empty()) { int tooltipWidth = 150; float bounds[4]; nvgFontFace(mNVGContext, "sans"); nvgFontSize(mNVGContext, 15.0f); nvgTextAlign(mNVGContext, NVG_ALIGN_CENTER | NVG_ALIGN_TOP); nvgTextLineHeight(mNVGContext, 1.1f); Vector2i pos = widget->absolutePosition() + Vector2i(widget->width() / 2, widget->height() + 10); nvgTextBoxBounds(mNVGContext, pos.x(), pos.y(), tooltipWidth, widget->tooltip().c_str(), nullptr, bounds); nvgGlobalAlpha(mNVGContext, std::min(1.0, 2 * (elapsed - 0.5f)) * 0.8); nvgBeginPath(mNVGContext); nvgFillColor(mNVGContext, Color(0, 255)); int h = (bounds[2] - bounds[0]) / 2; nvgRoundedRect(mNVGContext, bounds[0] - 4 - h, bounds[1] - 4, (int) (bounds[2] - bounds[0]) + 8, (int) (bounds[3] - bounds[1]) + 8, 3); int px = (int) ((bounds[2] + bounds[0]) / 2) - h; nvgMoveTo(mNVGContext, px, bounds[1] - 10); nvgLineTo(mNVGContext, px + 7, bounds[1] + 1); nvgLineTo(mNVGContext, px - 7, bounds[1] + 1); nvgFill(mNVGContext); nvgFillColor(mNVGContext, Color(255, 255)); nvgFontBlur(mNVGContext, 0.0f); nvgTextBox(mNVGContext, pos.x() - h, pos.y(), tooltipWidth, widget->tooltip().c_str(), nullptr); } } nvgEndFrame(mNVGContext); }
void Application::Impl_::paintEvent(NVGcontext* context){ int winWidth, winHeight; glfwGetWindowSize(window_.get(), &winWidth, &winHeight); int fWidth, fHeight; glfwGetFramebufferSize(window_.get(), &fWidth, &fHeight); float pxRatio = (float)fWidth / (float)winWidth; nvgBeginFrame(context, winWidth, winHeight, pxRatio); Rect textRect(0.f,10.f,winWidth,20.f); Rect boardMaxRect(0.f,textRect.height + textRect.y,winWidth,winHeight - textRect.height); float boardSizeMin = std::min(boardMaxRect.width,boardMaxRect.height); Rect boardRect(20.f, 20.f + textRect.height,boardSizeMin-40.f, boardSizeMin-40.f); if (boardMaxRect.height > boardMaxRect.width){ boardRect.move(0,(boardMaxRect.height - boardMaxRect.width)/2.f); }else{ boardRect.move((boardMaxRect.width - boardMaxRect.height)/2.f,0); } // draw the text rect nvgBeginPath(context); nvgFillColor(context, nvgRGBA(0,0,0,50)); nvgRect(context,textRect); nvgFill(context); nvgClosePath(context); // draw the board boardView_->paint(context,boardRect); if (isEnd_){ // change the color of the board nvgBeginPath(context); nvgFillColor(context, nvgRGBA(0,0,0,30)); nvgRect(context,boardMaxRect); nvgFill(context); nvgClosePath(context); // & display the game over std::string text("GAME OVER"); nvgBeginPath(context); float x= 0; float y= 0; textRect.center(x,y); nvgFontSize(context, 20); nvgFontFace(context, "sans"); nvgTextAlign(context, NVG_ALIGN_MIDDLE|NVG_ALIGN_CENTER); nvgFill(context); nvgFillColor(context, nvgRGBA(0,0,0,255)); nvgText(context,x+1,y+1,text.c_str(),NULL); nvgFillColor(context, nvgRGBA(200,20,20,255)); nvgText(context,x,y,text.c_str(),NULL); } nvgEndFrame(context); }
void QNanoQuickItemPainter::postpaint() { #ifdef QNANO_DEBUG m_drawDebug = nvgDrawDebug(m_painter->nvgCtx()); paintDrawDebug(); #endif nvgEndFrame(m_painter->nvgCtx()); }
int main(int argc, char **argv) { glfwInit(); GLFWwindow *window = glfwCreateWindow(1440, 750, "Desert golfer", NULL, NULL); glfwMakeContextCurrent(window); glewInit(); Scene scene; ViewInfo viewInfo; int width, height, comp; stbi_uc *pixels = stbi_load("test5.png", &width, &height, &comp, 3); viewInfo.viewData = pixels; viewInfo.viewWidth = width; viewInfo.viewHeight = height; viewInfo.screenWidth = 1334; viewInfo.screenHeight = 750; VisionScene *vision = visionReadScene(&scene, &viewInfo); scene.ballPos.x = 473.0f; scene.ballPos.y = 505.0f; scene.ballRadius = 5.0f; scene.goalMin.x = 1220.0f; scene.goalMin.y = 526.0f; scene.goalMax.x = 1256.0f; scene.goalMax.y = 550.0f; PhysicsScene *physics = physicsCreateScene(&scene); NVGcontext *nvg = nvgCreateGL2(0); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); physicsUpdate(physics, 50); int windowWidth, windowHeight; glfwGetWindowSize(window, &windowWidth, &windowHeight); glClearColor(0x64/255.0f, 0x95/255.0f, 0xED/255.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); nvgBeginFrame(nvg, windowWidth, windowHeight, 1.0f); physicsRender(physics, nvg); nvgEndFrame(nvg); glfwSwapBuffers(window); } return 0; }
static void draw(NVGcontext *nvg, struct gui_command_queue *queue, int width, int height) { const struct gui_command *cmd; glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glEnable(GL_SCISSOR_TEST); glEnable(GL_TEXTURE_2D); nvgBeginFrame(nvg, width, height, ((float)width/(float)height)); gui_foreach_command(cmd, queue) { switch (cmd->type) { case GUI_COMMAND_NOP: break; case GUI_COMMAND_SCISSOR: { const struct gui_command_scissor *s = gui_command(scissor, cmd); nvgScissor(nvg, s->x, s->y, s->w, s->h); } break; case GUI_COMMAND_LINE: { const struct gui_command_line *l = gui_command(line, cmd); draw_line(nvg, l->begin.x, l->begin.y, l->end.x, l->end.y, l->color); } break; case GUI_COMMAND_RECT: { const struct gui_command_rect *r = gui_command(rect, cmd); draw_rect(nvg, r->x, r->y, r->w, r->h, r->rounding, r->color); } break; case GUI_COMMAND_CIRCLE: { const struct gui_command_circle *c = gui_command(circle, cmd); draw_circle(nvg, c->x, c->y, (float)c->w / 2.0f, c->color); } break; case GUI_COMMAND_TRIANGLE: { const struct gui_command_triangle *t = gui_command(triangle, cmd); draw_triangle(nvg, t->a.x, t->a.y, t->b.x, t->b.y, t->c.x, t->c.y, t->color); } break; case GUI_COMMAND_TEXT: { const struct gui_command_text *t = gui_command(text, cmd); draw_text(nvg, t->x, t->y, t->w, t->h, t->foreground, t->background, t->string, t->length); } break; case GUI_COMMAND_IMAGE: { const struct gui_command_image *i = gui_command(image, cmd); draw_image(nvg, i->img.handle, i->x, i->y, i->w, i->h, 1); } break; case GUI_COMMAND_MAX: default: break; } } gui_command_queue_clear(queue); nvgResetScissor(nvg); nvgEndFrame(nvg); glPopAttrib(); }
void NVGRenderer::endFrame() { gr_opengl_set_2d_matrix(); nvgEndFrame(m_context); gr_opengl_end_2d_matrix(); resetGLState(); m_inFrame = false; }
void NVGRenderer::endFrame() { GR_DEBUG_SCOPE("NanoVG flush"); glBindVertexArray(0); gr_opengl_set_2d_matrix(); nvgEndFrame(m_context); gr_opengl_end_2d_matrix(); resetGLState(); m_inFrame = false; }
void Material::update(NVGcontext* vg, double time) { if (m_framebufferObject == nullptr) return; if (m_initialized == true && m_animate == false) return; float circle_size = float((cos(time) + 1.0) * 128.0); nvgluBindFramebuffer(m_framebufferObject); glViewport(0, 0, m_width, m_height); // Any alpha other than zero will fail for some FBO reason glClearColor(m_color.r, m_color.g, m_color.b, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); nvgBeginFrame(vg, m_width, m_height, /*pixelRatio*/1.0f); nvgBeginPath(vg); if (m_animate) nvgCircle(vg, float(m_width) * 0.5f, float(m_height) * 0.5f, circle_size); if(m_type == 2) nvgFillColor(vg, nvgRGBA(220, 45, 0, 200)); else if(m_type == 1) nvgFillColor(vg, nvgRGBA(0, 220, 45, 200)); else nvgFillColor(vg, nvgRGBA(10, 145, 200, 200)); nvgFill(vg); if(m_type == 2) { nvgFontFace(vg, "sans"); nvgFontSize(vg, 80.0f); nvgTextAlign(vg, NVG_ALIGN_CENTER); nvgFillColor(vg, nvgRGBA(255, 255, 255, 255)); nvgText(vg, float(m_width) * 0.5f, (float(m_height) * 0.5f) + 20.0f, "Add Object", nullptr); } nvgEndFrame(vg); nvgluBindFramebuffer(NULL); m_initialized = true; }
// 四角形の描画 void caRender() { glViewport(0, 0, width, height); //glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); /* glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST);*/ nvgBeginFrame(vg, width, height, pixelRatio); renderDemo(vg, 200, 200, width, height, 5, 0, 0); nvgEndFrame(vg); }
void renderFrame() { currentRotation = currentRotation*0.9f + rotation * 0.1f; glStencilMask(0xff); checkGlError("glStencilMask"); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); checkGlError("glClear"); nvgBeginFrame(vg, w, h, w/h); nvgBeginPath(vg); nvgMoveTo(vg, verticeX(90.f), verticeY(90.f)); nvgLineTo(vg, verticeX(90.f + 120.0f), verticeY(90.0f + 120.0f)); nvgLineTo(vg, verticeX(90.f + 240.f), verticeY(90.0f + 240.0f) ); nvgClosePath(vg); nvgFillColor(vg, nvgRGBA(255,192,0,255)); nvgFill(vg); nvgFillColor(vg, nvgRGBA(255,192,0,255)); nvgEndFrame(vg); }
void caRender() { glViewport(0, 0, width, height); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); nvgBeginFrame(vg, width, height, pixelRatio); // GUI int x = 10; int y = 100; // Widgets caDrawWindow(vg, "Title", x, y, 300, 360); caDrawSearchBox(vg, "Search", x+10, y+40, 280, 25); caDrawDropDown(vg, "Effects", x+10, y+70, 280, 28); // Form caDrawLabel(vg, "Login", x+10, y+110, 280, 20); caDrawEditBox(vg, "Email", x+10, y+135, 280, 28); caDrawEditBox(vg, "Password", x+10, y+170, 280, 28); caDrawCheckBox(vg, "Remember me", x+10, y+200, 140, 28); caDrawButton(vg, CATGL_ICON_LOGIN, "Sign in", x+148, y+200, 140, 28, nvgRGBA(0,96,128,255), 0); // Slider caDrawLabel(vg, "Diameter", x+10, y+245, 280, 20); caDrawEditBoxNum(vg, "123.00", "px", x+190, y+270, 100, 28); caDrawSlider(vg, 0.4f, x+10, y+270, 170, 28); caDrawButton(vg, CATGL_ICON_TRASH, "Delete", x+10, y+325, 160, 28, nvgRGBA(128,16,8,255), 0); caDrawButton(vg, 0, "Cancel", x+180, y+325, 110, 28, nvgRGBA(0,0,0,0), 0); nvgEndFrame(vg); }
void renderPattern(NVGcontext* vg, NVGLUframebuffer* fb, float t, float pxRatio) { int winWidth, winHeight; int fboWidth, fboHeight; int pw, ph, x, y; float s = 20.0f; float sr = (cosf(t)+1)*0.5f; float r = s * 0.6f * (0.2f + 0.8f * sr); if (fb == NULL) return; nvgImageSize(vg, fb->image, &fboWidth, &fboHeight); winWidth = (int)(fboWidth / pxRatio); winHeight = (int)(fboHeight / pxRatio); // Draw some stuff to an FBO as a test nvgluBindFramebuffer(fb); glViewport(0, 0, fboWidth, fboHeight); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); nvgBeginFrame(vg, winWidth, winHeight, pxRatio); pw = (int)ceilf(winWidth / s); ph = (int)ceilf(winHeight / s); nvgBeginPath(vg); for (y = 0; y < ph; y++) { for (x = 0; x < pw; x++) { float cx = (x+0.5f) * s; float cy = (y+0.5f) * s; nvgCircle(vg, cx,cy, r); } } nvgFillColor(vg, nvgRGBA(220,160,0,200)); nvgFill(vg); nvgEndFrame(vg); nvgluBindFramebuffer(NULL); }
void Application::draw() { glfwSetInputMode(context->window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); glClearColor(0.0, 0.0, 0.0, 10); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glViewport(0, 0, context->getFrameBufferWidth(), context->getFrameBufferHeight()); draw(context.get()); glDisable(GL_DEPTH_TEST); glViewport(0, 0, context->getFrameBufferWidth(), context->getFrameBufferHeight()); drawUI(); if (context->isDebugEnabled()) { drawDebugUI(); } const Cursor* cursor = context->getCursor(); if (!cursor) { cursor = &Cursor::Normal; } nvgBeginFrame(context->nvgContext, context->getScreenWidth(), context->getScreenHeight(),1.0f); cursor->draw(context.get()); nvgEndFrame(context->nvgContext); }
// 描画 void caRender() { glViewport(0, 0, width, height); //glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); nvgBeginFrame(vg, width, height, pixelRatio); nvgBeginPath(vg); nvgFillColor(vg, nvgRGBA(255,192,0,255)); nvgText(vg, 100, 100, "Hello! こんにちは!", NULL); nvgFill(vg); nvgEndFrame(vg); }
static void draw() { glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // Update and render glViewport(0, 0, fbWidth, fbHeight); glClearColor(0.2f, 0.2f, 0.2f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); nvgBeginFrame(vg, winWidth, winHeight, pxRatio); double t1 = glfwGetTime(); drawClock(vg, winWidth, winHeight); double t2 = glfwGetTime(); usleep(fmax((1.0 / fps - (t2 - t1)) * 1000000, 0)); nvgEndFrame(vg); glfwSwapBuffers(window); }
int main() { GLFWwindow* window; UIcontext *uictx; uictx = uiCreateContext(4096, 1<<20); uiMakeCurrent(uictx); uiSetHandler(ui_handler); if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } glfwSetErrorCallback(errorcb); #ifndef _WIN32 // don't require this on win32, and works with more cards glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); window = glfwCreateWindow(650, 650, "OUI Blendish Demo", NULL, NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwSetCharCallback(window, charevent); glfwSetCursorPosCallback(window, cursorpos); glfwSetMouseButtonCallback(window, mousebutton); glfwSetScrollCallback(window, scrollevent); glfwMakeContextCurrent(window); #ifdef NANOVG_GLEW glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) { printf("Could not init glew.\n"); return -1; } // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here. glGetError(); #endif //_vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES); _vg = nvgCreateGL3(NVG_ANTIALIAS); if (_vg == NULL) { printf("Could not init nanovg.\n"); return -1; } init(_vg); printf("sizeof(UIitem)=%lu\n", sizeof(UIitem)); glfwSwapInterval(0); glfwSetTime(0); double c = 0.0; int total = 0; int peak_items = 0; unsigned int peak_alloc = 0; while (!glfwWindowShouldClose(window)) { double mx, my; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // Update and render glViewport(0, 0, fbWidth, fbHeight); glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); double t = glfwGetTime(); nvgBeginFrame(_vg, winWidth, winHeight, pxRatio); draw(_vg, winWidth, winHeight); peak_items = (peak_items > uiGetItemCount())?peak_items:uiGetItemCount(); peak_alloc = (peak_alloc > uiGetAllocSize())?peak_alloc:uiGetAllocSize(); nvgEndFrame(_vg); double t2 = glfwGetTime(); c += (t2 - t); total++; if (total > (1*60)) { printf("%fms\n", (c / (double)total)*1000.0); total = 0; c = 0.0; } glfwSwapBuffers(window); glfwPollEvents(); } printf("Peak item count: %i (%lu bytes)\nPeak allocated handles: %u bytes\n", peak_items, peak_items * sizeof(UIitem), peak_alloc); uiDestroyContext(uictx); nvgDeleteGL3(_vg); glfwTerminate(); return 0; }
void NanoVG::endFrame() { nvgEndFrame( m_context() ); }
JNIEXPORT void JNICALL Java_org_lwjgl_nanovg_NanoVG_nnvgEndFrame(JNIEnv *__env, jclass clazz, jlong ctxAddress) { NVGcontext *ctx = (NVGcontext *)(intptr_t)ctxAddress; UNUSED_PARAMS(__env, clazz) nvgEndFrame(ctx); }
int main() { GLFWwindow* window; struct DemoData data; struct NVGcontext* vg = NULL; struct GPUtimer gpuTimer; struct PerfGraph fps, cpuGraph, gpuGraph; double prevt = 0, cpuTime = 0; if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time"); initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time"); initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time"); glfwSetErrorCallback(errorcb); #ifndef _WIN32 // don't require this on win32, and works with more cards glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif #ifdef DEMO_MSAA glfwWindowHint(GLFW_SAMPLES, 4); #endif window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL); // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwMakeContextCurrent(window); #ifdef NANOVG_GLEW glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) { printf("Could not init glew.\n"); return -1; } #endif #ifdef DEMO_MSAA vg = nvgCreateGL3(512, 512, 0); #else vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS); #endif if (vg == NULL) { printf("Could not init nanovg.\n"); return -1; } if (loadDemoData(vg, &data) == -1) return -1; glfwSwapInterval(0); initGPUTimer(&gpuTimer); glfwSetTime(0); prevt = glfwGetTime(); while (!glfwWindowShouldClose(window)) { double mx, my, t, dt; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; float gpuTimes[3]; int i, n; t = glfwGetTime(); dt = t - prevt; prevt = t; startGPUTimer(&gpuTimer); glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // Update and render glViewport(0, 0, fbWidth, fbHeight); if (premult) glClearColor(0,0,0,0); else glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); nvgBeginFrame(vg, winWidth, winHeight, pxRatio, premult ? NVG_PREMULTIPLIED_ALPHA : NVG_STRAIGHT_ALPHA); renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data); renderGraph(vg, 5,5, &fps); renderGraph(vg, 5+200+5,5, &cpuGraph); if (gpuTimer.supported) renderGraph(vg, 5+200+5+200+5,5, &gpuGraph); nvgEndFrame(vg); glEnable(GL_DEPTH_TEST); // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU) cpuTime = glfwGetTime() - t; updateGraph(&fps, dt); updateGraph(&cpuGraph, cpuTime); // We may get multiple results. n = stopGPUTimer(&gpuTimer, gpuTimes, 3); for (i = 0; i < n; i++) updateGraph(&gpuGraph, gpuTimes[i]); if (screenshot) { screenshot = 0; saveScreenShot(fbWidth, fbHeight, premult, "dump.png"); } glfwSwapBuffers(window); glfwPollEvents(); } freeDemoData(vg, &data); nvgDeleteGL3(vg); printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f); printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f); printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f); glfwTerminate(); return 0; }
void Renderer2D::finishRender( void ) { nvgEndFrame( _context2D ); }
static void draw(NVGcontext *nvg, struct zr_command_queue *queue, int width, int height) { const struct zr_command *cmd; glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glEnable(GL_SCISSOR_TEST); glEnable(GL_TEXTURE_2D); nvgBeginFrame(nvg, width, height, ((float)width/(float)height)); zr_foreach_command(cmd, queue) { switch (cmd->type) { case ZR_COMMAND_NOP: break; case ZR_COMMAND_SCISSOR: { const struct zr_command_scissor *s = zr_command(scissor, cmd); nvgScissor(nvg, s->x, s->y, s->w, s->h); } break; case ZR_COMMAND_LINE: { const struct zr_command_line *l = zr_command(line, cmd); nvgBeginPath(nvg); nvgMoveTo(nvg, l->begin.x, l->begin.y); nvgLineTo(nvg, l->end.x, l->end.y); nvgFillColor(nvg, nvgRGBA(l->color.r, l->color.g, l->color.b, l->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_CURVE: { const struct zr_command_curve *q = zr_command(curve, cmd); nvgBeginPath(nvg); nvgMoveTo(nvg, q->begin.x, q->begin.y); nvgBezierTo(nvg, q->ctrl[0].x, q->ctrl[0].y, q->ctrl[1].x, q->ctrl[1].y, q->end.x, q->end.y); nvgStrokeColor(nvg, nvgRGBA(q->color.r, q->color.g, q->color.b, q->color.a)); nvgStroke(nvg); } break; case ZR_COMMAND_RECT: { const struct zr_command_rect *r = zr_command(rect, cmd); nvgBeginPath(nvg); nvgRoundedRect(nvg, r->x, r->y, r->w, r->h, r->rounding); nvgFillColor(nvg, nvgRGBA(r->color.r, r->color.g, r->color.b, r->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_CIRCLE: { const struct zr_command_circle *c = zr_command(circle, cmd); nvgBeginPath(nvg); nvgCircle(nvg, c->x + (c->w/2.0f), c->y + c->w/2.0f, c->w/2.0f); nvgFillColor(nvg, nvgRGBA(c->color.r, c->color.g, c->color.b, c->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_TRIANGLE: { const struct zr_command_triangle *t = zr_command(triangle, cmd); nvgBeginPath(nvg); nvgMoveTo(nvg, t->a.x, t->a.y); nvgLineTo(nvg, t->b.x, t->b.y); nvgLineTo(nvg, t->c.x, t->c.y); nvgLineTo(nvg, t->a.x, t->a.y); nvgFillColor(nvg, nvgRGBA(t->color.r, t->color.g, t->color.b, t->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_TEXT: { const struct zr_command_text *t = zr_command(text, cmd); nvgBeginPath(nvg); nvgRoundedRect(nvg, t->x, t->y, t->w, t->h, 0); nvgFillColor(nvg, nvgRGBA(t->background.r, t->background.g, t->background.b, t->background.a)); nvgFill(nvg); nvgBeginPath(nvg); nvgFillColor(nvg, nvgRGBA(t->foreground.r, t->foreground.g, t->foreground.b, t->foreground.a)); nvgTextAlign(nvg, NVG_ALIGN_MIDDLE); nvgText(nvg, t->x, t->y + t->h * 0.5f, t->string, &t->string[t->length]); nvgFill(nvg); } break; case ZR_COMMAND_IMAGE: { const struct zr_command_image *i = zr_command(image, cmd); NVGpaint imgpaint; imgpaint = nvgImagePattern(nvg, i->x, i->y, i->w, i->h, 0, i->img.handle.id, 1.0f); nvgBeginPath(nvg); nvgRoundedRect(nvg, i->x, i->y, i->w, i->h, 0); nvgFillPaint(nvg, imgpaint); nvgFill(nvg); } break; case ZR_COMMAND_ARC: default: break; } } zr_command_queue_clear(queue); nvgResetScissor(nvg); nvgEndFrame(nvg); glPopAttrib(); }
int _main_(int _argc, char** _argv) { Args args(_argc, _argv); uint32_t width = 1280; uint32_t height = 720; uint32_t debug = BGFX_DEBUG_TEXT; uint32_t reset = BGFX_RESET_VSYNC; bgfx::init(args.m_type, args.m_pciId); bgfx::reset(width, height, reset); // Enable debug text. bgfx::setDebug(debug); // Set view 0 clear state. bgfx::setViewClear(0 , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH , 0x303030ff , 1.0f , 0 ); imguiCreate(); NVGcontext* nvg = nvgCreate(1, 0); bgfx::setViewSeq(0, true); DemoData data; loadDemoData(nvg, &data); bndSetFont(nvgCreateFont(nvg, "droidsans", "font/droidsans.ttf") ); bndSetIconImage(nvgCreateImage(nvg, "images/blender_icons16.png", 0) ); int64_t timeOffset = bx::getHPCounter(); entry::MouseState mouseState; while (!entry::processEvents(width, height, debug, reset, &mouseState) ) { int64_t now = bx::getHPCounter(); const double freq = double(bx::getHPFrequency() ); float time = (float)( (now-timeOffset)/freq); // Set view 0 default viewport. bgfx::setViewRect(0, 0, 0, width, height); // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. bgfx::touch(0); // Use debug font to print information about this example. bgfx::dbgTextClear(); bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/20-nanovg"); bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: NanoVG is small antialiased vector graphics rendering library."); nvgBeginFrame(nvg, width, height, 1.0f); renderDemo(nvg, float(mouseState.m_mx), float(mouseState.m_my), float(width), float(height), time, 0, &data); nvgEndFrame(nvg); // Advance to next frame. Rendering thread will be kicked to // process submitted rendering primitives. bgfx::frame(); } freeDemoData(nvg, &data); nvgDelete(nvg); imguiDestroy(); // Shutdown bgfx. bgfx::shutdown(); return 0; }
int main() { GLFWwindow* window; struct DemoData data; struct NVGcontext* vg = NULL; struct PerfGraph fps; double prevt = 0; if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time"); glfwSetErrorCallback(errorcb); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); #ifdef DEMO_MSAA glfwWindowHint(GLFW_SAMPLES, 4); #endif window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL); // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwMakeContextCurrent(window); #ifdef NANOVG_GLEW if(glewInit() != GLEW_OK) { printf("Could not init glew.\n"); return -1; } #endif #ifdef DEMO_MSAA vg = nvgCreateGL2(512, 512, 0); #else vg = nvgCreateGL2(512, 512, NVG_ANTIALIAS); #endif if (vg == NULL) { printf("Could not init nanovg.\n"); return -1; } if (loadDemoData(vg, &data) == -1) return -1; glfwSwapInterval(0); glfwSetTime(0); prevt = glfwGetTime(); while (!glfwWindowShouldClose(window)) { double mx, my, t, dt; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; t = glfwGetTime(); dt = t - prevt; prevt = t; updateGraph(&fps, dt); glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // Update and render glViewport(0, 0, fbWidth, fbHeight); if (premult) glClearColor(0,0,0,0); else glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); nvgBeginFrame(vg, winWidth, winHeight, pxRatio, premult ? NVG_PREMULTIPLIED_ALPHA : NVG_STRAIGHT_ALPHA); renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data); renderGraph(vg, 5,5, &fps); nvgEndFrame(vg); if (screenshot) { screenshot = 0; saveScreenShot(fbWidth, fbHeight, premult, "dump.png"); } glfwSwapBuffers(window); glfwPollEvents(); } freeDemoData(vg, &data); nvgDeleteGL2(vg); glfwTerminate(); return 0; }
int main() { GLFWwindow* window; struct NVGcontext* vg = NULL; char search[64] = "Foob-foob"; double t = 0; /* int blending = 0; float opacity = 0.5f; float position[3] = {100.0f, 120.0f, 234.0f}; float color[4] = {255/255.0f,192/255.0f,0/255.0f,255/255.0f}; float iterations = 42; int cull = 1; char name[64] = "Mikko"; const char* choices[] = { "Normal", "Minimum Color", "Screen Door", "Maximum Velocity" }; float scroll = 30;*/ float iconScale = 21.0f/1000.0f; printf("start\n"); if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } glfwSetErrorCallback(errorcb); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); window = glfwCreateWindow(1000, 600, "mg", NULL, NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, keycb); glfwSetCharCallback(window, charcb); glfwSetMouseButtonCallback(window, buttoncb); glfwMakeContextCurrent(window); vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS); if (vg == NULL) { printf("Could not init nanovg.\n"); return -1; } miInit(vg); if (miCreateFont(MI_FONT_NORMAL, "../example/fonts/Roboto-Regular.ttf")) { printf("Could not add font italic.\n"); return -1; } if (miCreateFont(MI_FONT_ITALIC, "../example/fonts/Roboto-Italic.ttf")) { printf("Could not add font italic.\n"); return -1; } if (miCreateFont(MI_FONT_BOLD, "../example/fonts/Roboto-Bold.ttf")) { printf("Could not add font bold.\n"); return -1; } if (miCreateIconImage("check", "../example/icons/check.svg", iconScale)) { printf("Could not create icon 'check'.\n"); return -1; } if (miCreateIconImage("arrow-combo", "../example/icons/arrow-combo.svg", iconScale)) { printf("Could not create icon 'arrow-combo'.\n"); return -1; } if (miCreateIconImage("tools", "../example/icons/tools.svg", iconScale)) { printf("Could not create icon 'tool'.\n"); return -1; } if (miCreateIconImage("cancel-circled", "../example/icons/cancel-circled.svg", iconScale)) { printf("Could not create icon 'cancel-circled'.\n"); return -1; } if (miCreateIconImage("plus", "../example/icons/plus.svg", iconScale)) { printf("Could not create icon 'plus'.\n"); return -1; } if (miCreateIconImage("search", "../example/icons/search.svg", iconScale)) { printf("Could not create icon 'search'.\n"); return -1; } glfwSetTime(0); MIcanvasState canvas = {0}; float value = 0.15f; while (!glfwWindowShouldClose(window)) { double mx, my; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; double dt; dt = glfwGetTime() - t; t += dt; // float t = glfwGetTime(); // float x,y,popy; // struct MGhit* hit; glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // Update and render glViewport(0, 0, fbWidth, fbHeight); glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_DEPTH_TEST); nvgBeginFrame(vg, winWidth, winHeight, pxRatio, NVG_STRAIGHT_ALPHA); input.mx = mx; input.my = my; miFrameBegin(winWidth, winHeight, &input, (float)dt); // input.nkeys = 0; // input.mbut = 0; // miInput(panel, &input); /* float s = slider; if (miSync(panel, "slider", &s)) { slider = s; } if (miValue(panel, "slider", &s)) { slider = s; } if (miClicked(panel, "footer-add")) { } */ // miRender(panel, vg); miPanelBegin(50,50, 250,450); miDockBegin(MI_TOP_BOTTOM); miText("Materials"); float cols[3] = {25, -1, 25}; miDivsBegin(MI_LEFT_RIGHT, 3, cols); miRowHeight(25); miText("S"); miInput(search, sizeof(search)); miText("X"); miText("Q"); miDivsEnd(); miSliderValue(&value, -1.0f, 1.0f); /* miLayoutBegin(); miRowHeight(25); miPack(MI_LEFT_RIGHT); miText("S"); miPack(MI_RIGHT_LEFT); miText("X"); miPack(MI_FILLX); miInput(search, sizeof(search)); miLayoutEnd();*/ miDockEnd(); miDockBegin(MI_BOTTOM_TOP); float cols2[3] = {-1, 60, 40}; miDivsBegin(MI_LEFT_RIGHT, 3, cols2); miRowHeight(20); miSpacer(); miButton("Add"); miButton("Delete"); miDivsEnd(); miDockEnd(); // miLayoutBegin(); // miRowHeight(20); /* miPack(MI_LEFT_RIGHT); miText("Ins"); miPack(MI_RIGHT_LEFT); miButton("Delete"); miButton("Add");*/ // miLayoutEnd(); miDockBegin(MI_FILLY); float cols3[2] = {50, -1}; miDivsBegin(MI_LEFT_RIGHT, 2, cols3); miRowHeight(50); miText("IMG"); float rows[4] = {-1, 20, 15, -1}; miDivsBegin(MI_TOP_BOTTOM, 4, rows); miSpacer(); miText("Plastic"); miLayoutBegin(MI_LEFT_RIGHT); miPack(MI_LEFT_RIGHT); miText("very shiny"); miPack(MI_RIGHT_LEFT); miText("7kB"); miLayoutEnd(); miDivsEnd(); miDivsEnd(); miLayoutBegin(MI_LEFT_RIGHT); miRowHeight(50); miText("IMG"); miLayoutBegin(MI_TOP_BOTTOM); miText("Plastic"); miText("very shiny"); miLayoutEnd(); miLayoutEnd(); miDockEnd(); /* miText("Text 1"); float cols[3] = {25, -1, 25}; miDivsBegin(MI_LEFT_RIGHT, 3, cols); // miLayoutBegin(MI_LEFT_RIGHT); miPack(MI_LEFT_RIGHT); miText("Text 2.1"); miButton("Text 2.2"); miText("Text 2.3"); miDivsEnd(); // miLayoutEnd(); miText("Text 3");*/ /* miPack(MI_BOTTOM_TOP); miText("BOTTOM"); miPack(MI_LEFT_RIGHT); miText("LEFT"); miPack(MI_RIGHT_LEFT); miText("RIGHT");*/ /* MIhandle button = miButton("Popup"); MIhandle popup = miPopupBegin(button, MI_ONCLICK, MI_BELOW); miText("Popup..."); miCanvasBegin(&canvas, MI_FIT, 50); miCanvasEnd(); if (miClicked(miButton("Close"))) { printf("Close popup\n"); miPopupHide(popup); } MIhandle button2 = miButton("Popup 2"); miPopupBegin(button2, MI_ONHOVER, MI_RIGHT); miText("Popup 2"); miPopupEnd(); MIhandle button3 = miButton("Popup 3"); miPopupBegin(button3, MI_ONHOVER, MI_RIGHT); miText("Popup 3"); miPopupEnd(); miPopupEnd(); miSlider(&value, -1.0f, 1.0f); miSliderValue(&value, -1.0f, 1.0f); miText("Foobar"); float divs[] = {50,100}; miDivsBegin(MI_ROW, divs, 2, 30, 5); miButton("Tab 1"); miButton("Tab 2"); miDivsBegin(MI_COL, divs, 2, 30, 0); miButton("Tab 4.1"); miDivsBegin(MI_ROW, divs, 2, 30, 0); miButton("Tab 4.2.1"); miButton("Tab 4.2.2"); miButton("Tab 4.2.3"); miDivsEnd(); miDivsEnd(); miButton("Tab 3"); miButton("Tab 5"); miStackBegin(MI_COL, 30, 5); miText("Tab 6.1"); miStackBegin(MI_ROW, 30, 5); miText("Tab 6.2.1"); miText("Tab 6.2.2"); miText("Tab 6.2.3"); miStackEnd(); miText("Tab 6.3"); miStackEnd(); miDivsEnd(); miText("Foofoo"); miButtonRowBegin(4); miButton("A"); miButton("B"); miButton("C"); miButton("D"); miButtonRowEnd(); miInput(search, sizeof(search)); miPanelBegin(250,250, 250,40); miText("Another one..."); miPanelEnd();*/ miPanelEnd(); miFrameEnd(); nvgEndFrame(vg); glEnable(GL_DEPTH_TEST); glfwSwapBuffers(window); glfwPollEvents(); } nvgDeleteGL3(vg); miTerminate(); glfwTerminate(); return 0; }
int main() { GLFWwindow* window; NVGcontext* vg = NULL; GPUtimer gpuTimer; PerfGraph fps, cpuGraph, gpuGraph; double prevt = 0, cpuTime = 0; NVGLUframebuffer* fb = NULL; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time"); initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time"); initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time"); glfwSetErrorCallback(errorcb); #ifndef _WIN32 // don't require this on win32, and works with more cards glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); #ifdef DEMO_MSAA glfwWindowHint(GLFW_SAMPLES, 4); #endif window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL); // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwMakeContextCurrent(window); #ifdef NANOVG_GLEW glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) { printf("Could not init glew.\n"); return -1; } // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here. glGetError(); #endif #ifdef DEMO_MSAA vg = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_DEBUG); #else vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); #endif if (vg == NULL) { printf("Could not init nanovg.\n"); return -1; } // Create hi-dpi FBO for hi-dpi screens. glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // The image pattern is tiled, set repeat on x and y. fb = nvgluCreateFramebuffer(vg, (int)(100*pxRatio), (int)(100*pxRatio), NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY); if (fb == NULL) { printf("Could not create FBO.\n"); return -1; } if (loadFonts(vg) == -1) { printf("Could not load fonts\n"); return -1; } glfwSwapInterval(0); initGPUTimer(&gpuTimer); glfwSetTime(0); prevt = glfwGetTime(); while (!glfwWindowShouldClose(window)) { double mx, my, t, dt; float gpuTimes[3]; int i, n; t = glfwGetTime(); dt = t - prevt; prevt = t; startGPUTimer(&gpuTimer); glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; renderPattern(vg, fb, t, pxRatio); // Update and render glViewport(0, 0, fbWidth, fbHeight); glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); nvgBeginFrame(vg, winWidth, winHeight, pxRatio); // Use the FBO as image pattern. if (fb != NULL) { NVGpaint img = nvgImagePattern(vg, 0, 0, 100, 100, 0, fb->image, 1.0f); nvgSave(vg); for (i = 0; i < 20; i++) { nvgBeginPath(vg); nvgRect(vg, 10 + i*30,10, 10, winHeight-20); nvgFillColor(vg, nvgHSLA(i/19.0f, 0.5f, 0.5f, 255)); nvgFill(vg); } nvgBeginPath(vg); nvgRoundedRect(vg, 140 + sinf(t*1.3f)*100, 140 + cosf(t*1.71244f)*100, 250, 250, 20); nvgFillPaint(vg, img); nvgFill(vg); nvgStrokeColor(vg, nvgRGBA(220,160,0,255)); nvgStrokeWidth(vg, 3.0f); nvgStroke(vg); nvgRestore(vg); } renderGraph(vg, 5,5, &fps); renderGraph(vg, 5+200+5,5, &cpuGraph); if (gpuTimer.supported) renderGraph(vg, 5+200+5+200+5,5, &gpuGraph); nvgEndFrame(vg); // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU) cpuTime = glfwGetTime() - t; updateGraph(&fps, dt); updateGraph(&cpuGraph, cpuTime); // We may get multiple results. n = stopGPUTimer(&gpuTimer, gpuTimes, 3); for (i = 0; i < n; i++) updateGraph(&gpuGraph, gpuTimes[i]); glfwSwapBuffers(window); glfwPollEvents(); } nvgluDeleteFramebuffer(fb); nvgDeleteGL3(vg); printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f); printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f); printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f); glfwTerminate(); return 0; }
int main() { GLFWwindow* window; DemoData data; NVGcontext* vg = NULL; PerfGraph fps; double prevt = 0; if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time"); glfwSetErrorCallback(errorcb); glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL); // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwMakeContextCurrent(window); vg = nvgCreateGLES2(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); if (vg == NULL) { printf("Could not init nanovg.\n"); return -1; } if (loadDemoData(vg, &data) == -1) return -1; glfwSwapInterval(0); glfwSetTime(0); prevt = glfwGetTime(); while (!glfwWindowShouldClose(window)) { double mx, my, t, dt; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; t = glfwGetTime(); dt = t - prevt; prevt = t; updateGraph(&fps, dt); glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // Update and render glViewport(0, 0, fbWidth, fbHeight); if (premult) glClearColor(0,0,0,0); else glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); nvgBeginFrame(vg, winWidth, winHeight, pxRatio); renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data); renderGraph(vg, 5,5, &fps); nvgEndFrame(vg); if (screenshot) { screenshot = 0; saveScreenShot(fbWidth, fbHeight, premult, "dump.png"); } glEnable(GL_DEPTH_TEST); glfwSwapBuffers(window); glfwPollEvents(); } freeDemoData(vg, &data); nvgDeleteGLES2(vg); glfwTerminate(); return 0; }