安装GLFW:
从官网拷贝源代码,用cmake编译然后安装
git clone https://github.com/glfw/glfw
cd glfw/
cmake .
make .
sudo make install
安装的主要文件在:
/usr/local/lib/libglfw3.a
/usr/local/include/GLFW
/usr/local/include/GLFW/glfw3.h
/usr/local/include/GLFW/glfw3native.h
写代码时:
#include <GLFW\glfw3.h>
g++编译时:
g++ test.cpp -lglfw3 -lGL
安装GLEW:
来到 这个网址 https://glad.dav1d.de/
使用以下配置:
Profile需改成Core,API->gl选择3.3或以上版本(建议选择最新版本,我目前选择的是4.6)。记得勾选Options->Generate a loader。最后点击Generate按钮。
点击glad.zip将文件下载下来
在终端界面解压zip文件:
unzip glad.zip
会生成两个文件夹:include和src
将 include/ 中的文件复制到本地的 include/ 中:
sudo cp -r include/* /usr/local/include/
src/ 中的glad.c需放到项目中
测试代码:
(该代码原于:opengl官方中文教程)
#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);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// 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);
}
使用g++编译:
g++ -o RUN test.cpp -lglfw3 -lGL -lGLU
./RUN
程序运行结果是创建一个窗口
编译失败问题
如果遇到编译失败,报错undefine referance,试试将:
#include <glad/glad.h>
改为:
#include "glad.c"
注意glad.c要和源文件放在同一目录