Почему в моей реализации текст Render with nvgText () не работает, но в Примере работает нормально? - PullRequest
1 голос
/ 31 марта 2020

Этот пример взят из примеров NanoVG.

    DemoData data;
    NVGcontext* vg = NULL;
    GPUtimer gpuTimer;
    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
    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);
    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();

    //vg = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_DEBUG);
    vg = nvgCreateGL3(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);

    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);

        nvgBeginFrame(vg, winWidth, winHeight, pxRatio);

        renderDemo(vg, mx, my, winWidth, winHeight, t, blowup, &data);

        //works here
        //char sample[] = "Sample";
        //nvgBeginPath(vg);
        //nvgFontSize(vg, 18.0f);
        //nvgFontFace(vg, "sans");
        //nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
        //nvgText(vg, 200, 200, sample, NULL);
        //nvgFill(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]);

        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;

Этот сегмент работает в приведенной выше реализации.

    nvgBeginPath(vg);
    nvgFontSize(vg, 18.0f);
    nvgFontFace(vg, "sans");
    nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
    nvgText(vg, 200, 200, sample, NULL);
    nvgFill(vg);

В приведенной ниже реализации он не отображается.

    GLFWwindow* window;
    NVGcontext* vg = NULL;

    //initializing GLFW
    if (!glfwInit()) {
        printf("Failed to init GLFW.");
        return -1;
    }

    glfwSetErrorCallback(errorcb);

    //creating a GL-Window
    window = glfwCreateWindow(1366, 768, "NanoVG", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }

    //glfwGetCursorPos(window, &xpos, &ypos);
    glfwSetKeyCallback(window, key);
    glfwSetCursorPosCallback(window, cursor_position_callback);
    glfwSetMouseButtonCallback(window, mouse_button_callback);

    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;

    if (glewInit() != GLEW_OK) {
        printf("Could not init glew.\n");
        return -1;
    }

    glGetError();

    //initialize nanovg
    vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
    if (vg == NULL) {
        printf("Could not init nanovg.\n");
        return -1;
    }
    glfwSwapInterval(0);
    defaultColor = nvgRGBA(255, 255, 255, 255);
    defaultWidth = 1.0f;

    /*glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 1366, 768, 0.0f, 0.0f, 1000.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(-1366, -768, 0.0f);
    glScalef(1.0, -1.0, 1.0f);*/

    while (!glfwWindowShouldClose(window))
    {
        int winWidth, winHeight, pxRatio;
        int fbWidth, fbHeight;

        glfwGetCursorPos(window, &mx, &my);
        glfwGetWindowSize(window, &winWidth, &winHeight);
        glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
        pxRatio = (float)fbWidth / (float)winWidth;
        glViewport(0, 0, winWidth, winHeight);

        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);

        //doesnt work here
        char sample[] = "Sample";
        nvgBeginPath(vg);
        nvgFontSize(vg, 18.0f);
        nvgFontFace(vg, "sans");
        nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
        nvgText(vg, 500, 500, sample, NULL);
        nvgFill(vg);

        nvgEndFrame(vg);

        glfwSwapBuffers(window);
        glfwPollEvents();
        iteration_Number++;
    }

    nvgDeleteGL3(vg);
    glfwTerminate();
    return 0;

Я что-то упустил? c? Извините, я новичок в Nanovg и OpenGL? У меня есть два из упомянутых фрагментов в двух разных функциях, и я выполняю только одну одновременно. Кто-нибудь?

1 Ответ

1 голос
/ 31 марта 2020

2 Проблемы: 1. Пришлось инициализировать шрифт с диска, прежде чем я смог использовать его в контексте рендеринга.

    nvgCreateFont(vg, "sans", ".\\example\\Roboto-Regular.ttf");

Включил эту строку и работал нормально.

Я плохо прочитал документацию по Nanovg и частично понял ее.
...