#include<GLFW/glfw3.h>#include<iostream>intmain(){
GLFWwindow* window;/* Initialize the library */if(!glfwInit())return-1;/* Create a windowed mode window and its OpenGL context */
window =glfwCreateWindow(640,480,"Hello World",NULL,NULL);if(!window){glfwTerminate();return-1;}/* Make the window's context current */glfwMakeContextCurrent(window);/* Loop until the user closes the window */while(!glfwWindowShouldClose(window)){/* Render here */glClear(GL_COLOR_BUFFER_BIT);/* Swap front and back buffers */glfwSwapBuffers(window);/* Poll for and process events */glfwPollEvents();}glfwTerminate();return0;}
Test code(with glad.h)
#include<glad/glad.h>#include<GLFW/glfw3.h>#include<iostream>voidframebuffer_size_callback(GLFWwindow* window,int width,int height);voidprocessInput(GLFWwindow *window);// settingsconstunsignedint SCR_WIDTH =800;constunsignedint SCR_HEIGHT =600;intmain(){// 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();return0;}// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly// ---------------------------------------------------------------------------------------------------------voidprocessInput(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// ---------------------------------------------------------------------------------------------voidframebuffer_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);}
corresponding cmake file
cmake_minimum_required(VERSION 3.0.0)
project(opengl_test VERSION 0.1.0 LANGUAGES C CXX)
# opengl
find_package(OpenGL REQUIRED)
# glfw
find_package(glfw3 CONFIG REQUIRED)
include_directories(${PROJECT_SOURCE_DIR}/include)
FILE(GLOB_RECURSE cpps ${PROJECT_SOURCE_DIR}/src/*.cpp ${PROJECT_SOURCE_DIR}/src/*.c)
FILE(GLOB_RECURSE headers ${PROJECT_SOURCE_DIR}/include/glad/*.h ${PROJECT_SOURCE_DIR}/include/*.h)
set(SRC ${cpps} ${headers})
# execute
add_executable(${PROJECT_NAME} ${SRC})
# include
target_include_directories(${PROJECT_NAME} PUBLIC ${OPENGL_INCLUDE_DIR})
target_include_directories(${PROJECT_NAME} PUBLIC ${GLFW_INCLUDE_DIR})
# libs
target_link_libraries(${PROJECT_NAME} PRIVATE ${OPENGL_LIBRARIES})
target_link_libraries(${PROJECT_NAME} PRIVATE glfw)