(目录)
一、Windows下的OpenGL环境搭建(VSC++环境)
1. 工具及库
- GLFW OpenGL的跨平台函数库,可以屏蔽操作系统间的差异,提供统一的接口。这里我们使用OpenGL函数库,而OpenGL ES是适用于嵌入式设备——对OpenGL做了剪裁。 官网 https://www.glfw.org/ 下载zip源码包。
- CMake 编译工具。 官网 https://cmake.org/
- GLAD GLAD用来管理OpenGL的函数指针。所以在调用任何OpenGL函数之前都需要初始化GLAD:
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
cout << "Failed to initialize GLAD" << endl;
return -1;
}
官网 https://glad.dav1d.de/
将下载的zip文件解压,其中src/glad.c可以直接包含进我们的项目。
2. 测试工程
通过CMake引入GLFW及GLAD库
CMakeLists.txt的内容
# CMakeList.txt: HeadFirst 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.8)
#引入外部库的路径
include_directories(./lib/glfw/include)
include_directories(./lib/glad/include)
link_directories(./lib/glfw/lib)
# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()
project ("HeadFirst")
# 将源代码添加到此项目的可执行文件。
add_executable (HeadFirst "HeadFirst.cpp" "HeadFirst.h" ./lib/glad/src/glad.c)
# 这里引入GLFW静态库
target_link_libraries(HeadFirst glfw3)
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET HeadFirst PROPERTY CXX_STANDARD 20)
endif()
# TODO: 如有需要,请添加测试并安装目标。
HeadFirst.h的内容
#pragma once
#include <iostream>
HeadFirst.cpp的内容
#include "HeadFirst.h"
#include <glad/glad.h>
#include <glfw/glfw3.h>
using namespace std;
int main()
{
glfwInit();
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
cout << "Hello OpenGL." << endl;
return 0;
}
如果正常运行不报错,表示环境搭建正常。
二、第一个案例
HeadFirst.cpp的内容
#include "HeadFirst.h"
#include <glad/glad.h>
#include <glfw/glfw3.h>
using namespace std;
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window,GLFW_KEY_ESCAPE)==GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
}
int main()
{
#pragma region
//初始化GLFW
glfwInit();
//glfwWindowHint配置GLFW
//参数一 代表选项名称以GLFW_开头,是一系列枚举的值
//参数二 接收一个整型,来设置这个选项的值
//主版本号是3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
//次版本号是3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//核心模式(Core-profile),告诉GLFW我们使用的渲染模式是核心渲染模式
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//如果使用的是Mac OS X系统 使用这一段代码
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
//创建一个窗口对象
GLFWwindow* window = glfwCreateWindow(800, 600, "Hello OpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
#pragma endregion
//初始化GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
cout << "Failed to initialize GLAD" << endl;
return -1;
}
//定义定点
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
//创建VAO
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
//创建VBO
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
//顶点着色器
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
//片段着色器
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\0";
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
//链接VS-PS
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
//渲染循环
while (!glfwWindowShouldClose(window))
{
//处理用户的输入
processInput(window);
//改变窗口颜色
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//绘制三角形
glUseProgram(shaderProgram);
glDrawArrays(GL_TRIANGLES, 0, 3);
//交换前后缓冲
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
可参考:https://learnopengl-cn.github.io/