重点:使用VBO(顶点缓存对象),VAO(顶点数组对象),创建顶点着色器,片段着色器,绘制三角形。
#include
#include
#include
const char* cpVertexShader ="#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);"//赋值给预定义变量 gl_Position
"}\n;";//顶点着色器
const char* cpFragmentShader = "#version 330 core\n"//设置主版本号 启用核心模式
"out vec4 FragColor;"//输出的颜色
"void main()"
"{\n"
" FragColor = vec4(1.0f,0.5f,0.2f,1.0f);"
"}\n";//片段着色器
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
void framebuffer_size_callback(GLFWwindow* window, int width, int height);//回调函数,窗口改变大小,视口也调整
int main()
{
glfwInit();//初始化GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//设置主版本号 3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//设置次版本号 3
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//设置使用核心模式
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);//创建窗口对象
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);//设置为当前线程主上下文
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))//初始化GLAD(GLAD用来管理OpenGL的函数指针)
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);//设置渲染窗口大小
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);//注册回调函数,刷新渲染窗口
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);//创建顶点数组对象
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);//启用顶点属性,设置起始位置
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);//创建顶点着色器
glShaderSource(vertexShader, 1, &cpVertexShader, NULL);//绑定源代码到着色器程序
glCompileShader(vertexShader);//编译着色器程序
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);//检测是否创建成功
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);//创建片段着色器
glShaderSource(fragmentShader, 1, &cpFragmentShader, NULL);//设置片段着色器源代码
glCompileShader(fragmentShader);//编译着色器
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);//检测创建十分成功
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
unsigned int shaderProgram;
shaderProgram = glCreateProgram();//创建着色器程序对象
glAttachShader(shaderProgram, vertexShader);//附加顶点着色器
glAttachShader(shaderProgram, fragmentShader);//附件片段着色器
glLinkProgram(shaderProgram);//连接着色器程序
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);//判断链接是否成功
if (!success)
{
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::COMPILATION_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);//创建完成 删除不需要的缓存
glDeleteShader(fragmentShader);
while (!glfwWindowShouldClose(window))//渲染循环,检查GLFW是否被要求退出。
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);//清理屏幕
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);//激活着色器程序
glBindVertexArray(VAO);//绑定VAO
glDrawArrays(GL_TRIANGLES, 0, 3);//绘制模型
glfwSwapBuffers(window);//交换颜色缓存
glfwPollEvents();//检测触发事件
}
glfwTerminate();//释放所有资源
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
结果:
本文是根据LearnOpenGL教程自己整理的笔记,详细的教程内容请到LearnOpenGL官网查看。地址:https://learnopengl-cn.github.io/