第零步:下载glfw:http://www.glfw.org/download.html,下载解压好,暂且成目录名字为 glfw3-3.x.x
ubuntu下安装:sudo apt-get install libglew-dev glew-utils
第一步:编译glfw:
(1): 安装依赖库,sudo
apt-get build-dep glfw, sudo
apt-get install cmake xorg-dev libglu1-mesa-dev
(2):进入 glfw3-3.x.x 目录,建立build子目录, 命令行执行 cmake-gui, 源码目录选择glfw3-3.x.x, 目标目录选择build。 configure,generate
(3): 命令行模式,cd build,执行 make, sudo make install .
第二步:使用glfw(关键)
建立main.cpp, 敲入下面源码:
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using namespace std;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
const GLuint WIDTH = 800, HEIGHT = 600;
int main()
{
cout << "Starting GLFW context, OpenGL3.3" << endl;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
if(window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glViewport(0, 0, WIDTH, HEIGHT);
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
std::cout << key << std::endl;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
编译: g++ -c main.cpp
g++ mian.o -o main.exec -lGL - lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -lXcursor -lXinerama
编译也可用CmakeLists.txt:
project(OpenGL_1)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
LINK_LIBRARIES(glfw3)
LINK_LIBRARIES(X11)
LINK_LIBRARIES(Xxf86vm)
LINK_LIBRARIES(Xrandr)
LINK_LIBRARIES(pthread)
LINK_LIBRARIES(Xi)
LINK_LIBRARIES(Xcursor)
LINK_LIBRARIES(Xinerama)
LINK_LIBRARIES(GL)
LINK_LIBRARIES(GLU)
add_executable(${PROJECT_NAME} ${SRC_LIST})