OpenGL配置(Mac + CLion)

受人指导配置了OpenGL开发环境,记下备忘

安装库

1
brew install glew glfw

CLion cmake配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cmake_minimum_required(VERSION 3.8)
project(graph)
set(CMAKE_CXX_STANDARD 11)
# find library
find_library(OPENGL OpenGL)
find_library(GLFW glfw)
find_library(GLEW glew)
# link them
link_libraries(${GLEW} ${GLFW} ${OPENGL})
set(SOURCE_FILES main.cpp)
add_executable(graph ${SOURCE_FILES})

写代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const int SCR_WIDTH = 800;
const 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);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
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);
// glad: load all OpenGL function pointers
GLenum err = glewInit();
if(err != GLEW_OK) {
std::cout << "glewInit failed: " << glewGetErrorString(err) << std::endl;
exit(1);
}
// render loop
while (!glfwWindowShouldClose(window))
{
// input
// ......
// 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;
}

学习资源

https://learnopengl-cn.github.io/