说明:本系列视频使用 mac平台的Xcode来实现,windows平台和Linux平台与之类似。
1 程序功能简介
主要是通过实战 理解着色器插值的使用方法和光栅化效果,效果为颜色渐变。
2 程序代码-颜色渐变的三角形
源码如下所示:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cmath>
#include <stb_image.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
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;
//===================vertex shader programme defination start===================
const char *vertexShaderBlendSource ="#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"out vec3 ourColor;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 1.0);\n"
" ourColor = aColor;\n"
"}\0";
const char *fragmentShaderBlendSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec3 ourColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(ourColor, 1.0f);\n"
"}\n\0";
//===================vertex shader programme defination end===================
int main()
{
// @1.1 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
// @1.2 glfw window creation---------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "DaoshengOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// @1.3 glad: load all OpenGL function pointers---------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// @2 build and compile our shader program---------------------
int success;
char infoLog[512];
// @2.1 vertex shader
// vertex blend shader
int vertexBlendShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexBlendShader, 1, &vertexShaderBlendSource, NULL);
glCompileShader(vertexBlendShader);
// check for shader compile errors
glGetShaderiv(vertexBlendShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexBlendShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// @2.2 fragment shader
// fragment blend shader
int fragmentBlendShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentBlendShader, 1, &fragmentShaderBlendSource, NULL);
glCompileShader(fragmentBlendShader);
// check for shader compile errors
glGetShaderiv(fragmentBlendShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentBlendShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
//@2.3 create & attach & link shaders
// link Blend shaders
int shaderBLendProgram = glCreateProgram();
glAttachShader(shaderBLendProgram, vertexBlendShader);
glAttachShader(shaderBLendProgram, fragmentBlendShader);
glLinkProgram(shaderBLendProgram);
// check for linking errors
glGetProgramiv(shaderBLendProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderBLendProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexBlendShader);
glDeleteShader(fragmentBlendShader);
// set up vertex data (and buffer(s)) and configure vertex attributes
float blend_vertices[] = {
// 位置 // 颜色
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // 右下
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // 左下
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // 顶部
};
//uniform vertices
unsigned int VBO_blend, VAO_blend;
glGenVertexArrays(1,&VAO_blend);
glGenBuffers(1,&VBO_blend);
glBindVertexArray(VAO_blend);
glBindBuffer(GL_ARRAY_BUFFER, VBO_blend);
glBufferData(GL_ARRAY_BUFFER, sizeof(blend_vertices), blend_vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// render loop
while (!glfwWindowShouldClose(window))
{
// input process
processInput(window);
// render process
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//@2.4 use shaderprogram
//draw blend triangle
glUseProgram(shaderBLendProgram);
glBindVertexArray(VAO_blend);
glDrawArrays(GL_TRIANGLES, 0, 3);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
glDeleteVertexArrays(1,&VAO_blend);
glDeleteBuffers(1,&VBO_blend);
glfwTerminate(); // glfw: terminate, clearing all previously allocated GLFW resources.
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);
}
该系列文章主要参考openGL官网 和学习 learnopenGL官网 进行知识体系的梳理和重构,重在形成自己对openGL知识的理解和知识体系。
- 参考相关链接总站:LearnOpenGL-CN
- 参考OpenGL官网:About The Khronos Group - The Khronos Group Inc
- 同时也参考了网上其他内容知识 进而 进行整合。