我的電腦是windows,在電腦上學(xué)習(xí)Opengl,推薦開發(fā)工具 Clion
推薦一個(gè)很好的opengl學(xué)習(xí)網(wǎng)站 https://learnopengl-cn.readthedocs.io/zh/latest/01%20Getting%20started/03%20Hello%20Window/
話不多說(shuō)摄咆,先創(chuàng)建一個(gè)Hello word 工程
1.先下載幾個(gè)庫(kù)
- freeglut 這個(gè)庫(kù)最初由MarkKilgard編寫,從OpenGL Redbook(紅寶書)第二版起就用來(lái)作為示例程序的支持環(huán)境 GLUT因?yàn)槠浜?jiǎn)單晌缘、可用性廣长捧、可移植性強(qiáng),被廣泛應(yīng)用于各種OpenGL實(shí)際應(yīng)用中
https://www.transmissionzero.co.uk/software/freeglut-devel/- glad 也是用來(lái)訪問(wèn)OpenGL規(guī)范接口的第三方庫(kù)
https://glad.dav1d.de/- glfw 是一個(gè)專門針對(duì)OpenGL的C語(yǔ)言庫(kù)放祟,它提供了一些渲染物體所需的最低限度的接口鳍怨。它允許用戶創(chuàng)建OpenGL上下文,定義窗口參數(shù)以及處理用戶輸入
http://www.glfw.org/download.html- glew (OpenGL Extension Wrangler Library) 也是opengl額外的庫(kù)跪妥,這個(gè)庫(kù)封裝了opengl復(fù)雜的操作京景,簡(jiǎn)化的我們opengl的API調(diào)用
http://glew.sourceforge.net/
2.Clion 創(chuàng)建工程
- 本工程采用glad glew glfw 三個(gè)庫(kù)
- 分別創(chuàng)建inclue lib src三個(gè)目錄
- inclue主要是頭文件,lib主要是需要鏈接的庫(kù)
- src下面就一個(gè)glad源文件
3.CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(OpenglTest2)
# 指定支持的c++版本
set(CMAKE_CXX_STANDARD 14)
# 指定編譯后的輸出目錄
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
# 指定include 和 鏈接lib
include_directories(include)
link_directories(lib)
add_executable(OpenglTest2 main.cpp src/glad.c)
# 把glfw 和 glew鏈接進(jìn)來(lái)
target_link_libraries(OpenglTest2 libglfw3.a glew32s.lib)
4.測(cè)試文件
#include <iostream>
#include "glad/glad.h"
#include "GLFW/glfw3.h"
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); // uncomment this statement to fix compilation on OS X
#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);
}
5.運(yùn)行結(jié)果如下
1.有用VS開發(fā)的配置工程會(huì)更簡(jiǎn)單一些,不過(guò)安裝VS要電腦要牛逼點(diǎn)
2.Clion 開發(fā)工具學(xué)習(xí)OpenGL足夠了