OpenGL基础(06)颜色渐变三角形 源码&显示效果

该博客通过在Mac平台的Xcode中使用OpenGL,详细介绍了如何实现颜色渐变的三角形,涉及着色器插值和光栅化效果。代码展示了从创建和编译顶点及片段着色器,到设置顶点数据并绘制颜色渐变三角形的全过程。同时,博客参考了OpenGL官网和LearnOpenGL资源进行知识整理。
摘要由CSDN通过智能技术生成

说明:本系列视频使用 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知识的理解和知识体系。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图王大胜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值