在C++中获取OpenGL使用的总内存(以字节为单位),可以通过以下步骤实现:
- 首先,确保已经安装了OpenGL库并正确配置了开发环境。
- 在代码中包含必要的头文件:#include <GL/glew.h>
#include <GLFW/glfw3.h>
- 初始化GLFW窗口和OpenGL上下文:if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL Memory Usage", NULL, NULL);
if (!window) {
fprintf(stderr, "Failed to create GLFW window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
glfwTerminate();
return -1;
}
- 使用
glGetIntegerv
函数获取OpenGL使用的总内存:GLint totalMemory;
glGetIntegerv(GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &totalMemory); - 将获取到的内存转换为字节:long long totalMemoryBytes = static_cast<long long>(totalMemory) * 1024;
- 输出内存信息:std::cout << "OpenGL total memory: "<< totalMemoryBytes << " bytes"<< std::endl;
- 最后,释放资源并关闭窗口:glfwTerminate();
这样,就可以在C++中获取OpenGL使用的总内存(以字节为单位)。需要注意的是,这里使用的是NVIDIA特定的扩展GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX
,其他显卡厂商可能有不同的扩展。