为什么在我的实现中,使用nvgText()渲染的文本在示例中效果良好



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 msn", getGraphAverage(&fps) * 1000.0f);
printf("          CPU Time: %.2f msn", getGraphAverage(&cpuGraph) * 1000.0f);
printf("          GPU Time: %.2f msn", 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;

我是不是错过了一些基本的东西?对不起,我是nanovg和opengl的初学者?我在两个不同的函数中有两个提到的片段,并且一次只执行一个。任何人

2问题:1.必须先从磁盘初始化字体,然后才能在渲染上下文中使用它。

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

包括这一行,工作良好。

  1. 我没有很好地阅读Nanovg文档,并且部分理解它

相关内容

  • 没有找到相关文章

最新更新