下载
内容来自 https://blog.csdn.net/weixin_41685373/article/details/104576808
个人针对自己电脑情况做了详细介绍
GLAD 下载地址
GLFW下载地址
! 此处需要特殊说明:个人在GLFW下载时,选择了32位已预编译好的包,并使用了lib-mingw文件夹下的内容,因为本机的Clion默认使用了32位的环境,我想大多数Clion默认安装后都是这样的
下载两者并解压后,我在C盘根目录下创建了个文件夹,用于存放以后需要存放的环境:Environment
- include文件夹下存放glad和glfw的include内容
- lib下存放glfw文件夹下lib-mingw中的三个文件(不知道只存放dll是否可以)
- 在工程目录下新建src文件夹,存放glad src中的glad.c文件,并像引用文章中描述的那样改写CMakeLists.txt文件
cmake_minimum_required(VERSION 3.16)
project(learn_opengl)
set(CMAKE_CXX_STANDARD 14)
link_directories("C:/Environments/OpenGL/lib")
include_directories("C:/Environments/OpenGL/include")
add_executable(learn_opengl main.cpp src/glad.c)
target_link_libraries(learn_opengl libglfw3.a)
测试代码:
# 源码来源https://learnopengl.com/
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}