【发布时间】:2020-12-11 15:25:39
【问题描述】:
我按照 LearnOpenGL 教程创建了一个简单的 OpenGL 程序。它只是创建一个窗口并用颜色填充它。我正在使用 Visual Studio 2019。
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
int main() {
// Initialize GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Create GLFW Window
GLFWwindow* window = glfwCreateWindow(800, 600, "Golden Retriever", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// Initialize GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// Render Loop
while (!glfwWindowShouldClose(window)) {
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
该程序运行良好,但它使用了过多的 CPU(大约 50%)。是 Visual Studio 属性中的问题,还是我只是在我的代码中做错了什么?
【问题讨论】:
-
为什么这“太多”了?为什么它应该不使用那么多 CPU 时间?
-
会不会是你关闭了垂直同步?
-
@derhass 对于这个例子,如果开启了垂直同步,并且显示器有 60Hz(甚至 144Hz),那么 CPU 使用率应该非常低(如果它不是一个非常旧的 CPU) ,但是如果关闭它,那么 CPU 使用率会变高,直到驱动调用,或者 GPU 成为瓶颈。
标签: c++ visual-studio opengl