实验一:搭建OpenGL环境并绘制窗口
#include <glad/glad.h>
//glad用来管理opengl的函数指针
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
int main() {
glfwInit();
//将主版本号与次版本号都设置为3,并告诉GLFW我们使用的是核心模式
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/*
int major, minor, rev;
glfwGetVersion(&major, &minor, &rev);
cout << major << endl << minor << endl << rev;
getchar();
可以用以上代码了解到当前的版本
*/
//创建一个窗口,前两个参数分别是宽和高,第三个参数是窗口的名字,最后两个参数暂时忽略
GLFWwindow* window = glfwCreateWindow(800, 600, "window", NULL, NULL);
if (window == NULL) {
cout << "fail to create glfw window" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//初始化glad,给glad传入加载系统相关的opengl函数指针地址的函数
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
cout << "fail to initialize glad" << endl;
return -1;
}
//初始化视口,这个函数有四个参数,前两个参数是视口左下角的位置,后两个参数控制渲染窗口的宽度和高度,视口的维度可以比GLFW的维度小
glViewport(0, 0, 800, 600);
//当窗口大小被改变时视口大小也应该被改变,可以给窗口注册一个回调函数,它再每次窗口大小被调整时调用
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//渲染循环,不断绘制图像并接收用户输入
while (!glfwWindowShouldClose(window)) {//每次循环前检查是否应该退出
glClearColor(0.0f, 0.34f, 0.57f, 1.0f);//颜色被清空并替换为glClearColor参数里设置的颜色
glClear(GL_COLOR_BUFFER_BIT);//清空屏幕颜色缓冲
glfwSwapBuffers(window);//使用双缓冲,交换前后缓图案
glfwPollEvents();//检查是否触发了事件,并调用对应的回调函数
}
glfwTerminate();//释放之前分配的资源
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
实验2:在窗口绘制三角形
步骤:
1.初始化:GLFW和GLAD
2.数据处理:生成绑定VAO,VBO,设置属性指针
3.着色器:顶点着色器,片段着色器
4.渲染
5.善后工作
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
void init_glfw() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
int main() {
//1.初始化
init_glfw();
auto window = glfwCreateWindow(2000, 1500, "Triangle", nullptr, nullptr);
if (window == nullptr) {
cout << "fail to create opengl context" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//初始化glad,加载opengl函数指针地址的函数
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
cout << "fail to init GLAD" << endl;
return -1;
}
glViewport(0, 0, 2000, 1500);
//2.顶点输入
//给定三角形顶点数据
const float triangle[] = {
-0.5f,-0.5f,0.0f,//左下
0.5f,-0.5f,0.0f,//右上
0.0f,0.5f,0.0f //正上
};
//3.数据处理
//生成并绑定VBO
GLuint vertex_buffer_object;//生成
glGenBuffers(1, &vertex_buffer_object);//绑定
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object);
//将顶点数据绑定至当前默认缓冲中
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
//生成并绑定VAO
GLuint vertex_array_object;
glGenVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);
//设置顶点属性指针,用这个函数告诉opengl我们如何解释这些顶点数据
//第一个参数是顶点着色器的位置值,3表示顶点属性是一个三分量的向量,第三个参数代表顶点类型,
//第四个参数我们是否希望数据标准化(映射到0~1间),第五个参数为步长,最后一个是数据的偏移量
//Enable这个函数表明我们开启了0这个通道,其默认是关闭的
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
//顶点着色原码
const char* vertex_shader_source =
"#version 330 core\n"
"layout (location=0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position=vec4(aPos,1.0);\n"
"}\n\0";
//片段着色器
const char* fragment_shader_source =
"#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor=vec4(1.0f,0.5f,0.2f,1.0f);\n"
"}\n\0";
// 生成并编译着色器
// 顶点着色器
int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
glCompileShader(vertex_shader);
int success;
char info_log[512];
// 检查着色器是否成功编译,如果编译失败,打印错误信息
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertex_shader, 512, NULL, info_log);
cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << info_log << endl;
}
// 片段着色器
int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
glCompileShader(fragment_shader);
// 检查着色器是否成功编译,如果编译失败,打印错误信息
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragment_shader, 512, NULL, info_log);
cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << info_log << endl;
}
// 链接顶点和片段着色器至一个着色器程序
int shader_program = glCreateProgram();
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program);
// 检查着色器是否成功链接,如果链接失败,打印错误信息
glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shader_program, 512, NULL, info_log);
cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << info_log << endl;
}
// 删除着色器
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
// 线框模式
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// 渲染循环
while (!glfwWindowShouldClose(window)) {
// 清空颜色缓冲
glClearColor(0.0f, 0.34f, 0.57f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// 使用着色器程序
glUseProgram(shader_program);
// 绘制三角形
glBindVertexArray(vertex_array_object); // 绑定VAO
glDrawArrays(GL_TRIANGLES, 0, 3); // 绘制三角形
glBindVertexArray(0); // 解除绑定
// 交换缓冲并且检查是否有触发事件(比如键盘输入、鼠标移动等)
glfwSwapBuffers(window);
glfwPollEvents();
}
// 删除VAO和VBO
glDeleteVertexArrays(1, &vertex_array_object);
glDeleteBuffers(1, &vertex_buffer_object);
// 清理所有的资源并正确退出程序
glfwTerminate();
return 0;
}