OpenGL同时渲染3d模型和yuv420p视频

问题描述 投票:0回答:0

本期在这里:

技术栈:glfw、opengl、ubuntu

我的目标是同时渲染两个东西,一个 3d 模型和一个 yuv420p 视频(使用两个不同的着色器程序)。

如果我只渲染其中一个,它们没问题,但是把它们放在一起,视频部分闪烁着奇怪的绿色(仍然可以看出视频的内容),3d 模型某种“丢失了深度测试”的东西。

我是不是做错了什么。有什么帮助吗?

结果视频的东西

代码如下:

1.main.cpp

void main()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,6);
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
    // glfwWindowHint(GLFW_VISIBLE,GLFW_FALSE);


    GLFWwindow *pWin = glfwCreateWindow(800,600,"glwin",NULL,NULL);
    if(pWin == NULL){
        std::cout << "Failed to create window"<<std::endl;
        glfwTerminate();
        return -1;
    }
    std::cout<< "Create window success"<< std::endl;
    glfwMakeContextCurrent(pWin);
    glfwSetFramebufferSizeCallback(pWin,frame_buffer_size_callback);
    glfwSetCursorPosCallback(pWin,mouse_callback);
    glfwSetScrollCallback(pWin,scroll_callback);

    if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
        std::cout << "Fail to init GLAD" << std::endl;
        return -1;
    }

    glEnable(GL_DEPTH_TEST);

    Shader *glShader = new     Shader("../shadersources/loadModel.vs","../shadersources/loadModel.fs");
    Model ourModel("../resources/nanosuit/nanosuit.obj");

while(!glfwWindowShouldClose(pWin))
    {
        float currentFrame = static_cast<float>(glfwGetTime());
        deltaTime = currentFrame - lastFrame;
        lastFrame = currentFrame;

        processInput(pWin);
        glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glm::mat4 projection = glm::perspective(glm::radians(45.0f),win_width * 1.0f / win_height ,0.1f,100.0f);
        glm::mat4 model = glm::mat4(1.0f);
        glm::mat4 view = glm::mat4(1.0f);
        view = camera.GetViewMatrix();

        
        glShader->use();
        glShader->setMat4("projection",projection);
        model = glm::rotate(model,(float)glfwGetTime(),glm::vec3(0.5f,1.0f,0.0f));
        model = glm::translate(model, glm::vec3(0.0f,0.0f,0.0f));
        model = glm::scale(model,glm::vec3(1.0f,1.0f,1.0f));
        glShader->setMat4("model",model);
        glShader->setMat4("view",view);
        ourModel.Draw(*glShader);
        vs.Draw(model,projection,view);
        glfwSwapBuffers(pWin);
        glfwPollEvents();
        glUseProgram(0);
        glBindVertexArray(0);

    }
    return 0;

}

2.videostuff.h

float vertices[] = {
    -15.0f, -15.0f, -5.0f,      0.0f, 0.0f,
     15.0f, -15.0f, -5.0f,      1.0f, 0.0f,
    -15.0f,  15.0f, -5.0f,      0.0f, 1.0f,
     15.0f,  15.0f, -5.0f,      1.0f, 1.0f 
};



unsigned int indeices[] = {
    0, 1, 2,
    1, 2, 3
};

class VideoStuff
{
private:
    /* data */
    const char *path, *vs, *fs;
    AVFormatContext *avFormatContext;
    AVCodecContext *avCodecContext;
    
    AVPacket *avPacket;
    AVFrame *avFrame;
    int video_stream_index = -1;

    unsigned int VVAO, VVBO, VEBO;
    unsigned int texs[3];
    unsigned int width, height;
    Shader *videoShader;

    unsigned char* dataY, dataU, dataV;

public:
    VideoStuff(const char* path, const char* vs, const char *fs);
    void Draw(glm::mat4 model,glm::mat4 projection, glm::mat4 view);
    ~VideoStuff();
private:
    void InitFFmpeg();



    void Release();
};

VideoStuff::VideoStuff(const char* path, const char* vs, const char *fs):path(path),vs(vs),fs(fs)
{
    InitFFmpeg();
}

void VideoStuff::InitFFmpeg()
{
    this->avFormatContext = avformat_alloc_context();
    avformat_open_input(&this->avFormatContext, this->path, NULL, NULL);
    int stream_info = avformat_find_stream_info(this->avFormatContext,NULL);
    for(int i = 0 ; i < avFormatContext->nb_streams; i++){
        if(avFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
            video_stream_index = i;
            break;
        }
    }
    const AVCodec *avCodec = avcodec_find_decoder(avFormatContext->streams[video_stream_index]->codecpar->codec_id);
    avCodecContext = avcodec_alloc_context3(avCodec);

    avcodec_parameters_to_context(avCodecContext, avFormatContext->streams[video_stream_index]->codecpar);
    avcodec_open2(avCodecContext,avCodec, NULL);
    
    avPacket = av_packet_alloc();
    avFrame = av_frame_alloc();

    width = avCodecContext->width;
    height = avCodecContext->height;



    videoShader = new Shader(vs, fs);
    
    glGenVertexArrays(1, &VVAO);
    glGenBuffers(1, &VVBO);
    glGenBuffers(1, &VEBO);
    glBindVertexArray(VVAO);
    glBindBuffer(GL_ARRAY_BUFFER, VVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VEBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indeices), indeices, GL_STATIC_DRAW);


    videoShader->use();
    glGenTextures(3, texs);
    glBindTexture(GL_TEXTURE_2D, texs[0]);
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
    glGenerateMipmap(GL_TEXTURE_2D);
    videoShader->setInt("tex_y",0);



    glBindTexture(GL_TEXTURE_2D, texs[1]);
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width / 2, height / 2, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
    glGenerateMipmap(GL_TEXTURE_2D);
    videoShader->setInt("tex_u",1);



    glBindTexture(GL_TEXTURE_2D, texs[2]);
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width / 2, height / 2, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
    glGenerateMipmap(GL_TEXTURE_2D);
    videoShader->setInt("tex_v",2);


    unsigned int y_stride = 512;



}

void VideoStuff::Draw(glm::mat4 model,glm::mat4 projection, glm::mat4 view)
{
    
    videoShader->use();


    int ret = av_read_frame(avFormatContext, avPacket);
    if(ret == 0)
    {
        // std::cout << "read frame success  "<<  std::endl;
        if(avPacket->stream_index == video_stream_index)
        {
            ret = avcodec_send_packet(avCodecContext, avPacket);
            if(ret < 0){
                // std::cout << "send packet error  "<<  std::endl;
                glBindVertexArray(VVAO);
                glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
                glBindVertexArray(0);
                return;
            }
            // std::cout << "send packet success  "<<  std::endl;
            av_frame_unref(avFrame);
            ret = avcodec_receive_frame(avCodecContext, avFrame);
            if(ret < 0 || ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            {
                // std::cout << "receive frame error  "<<  std::endl;
                glBindVertexArray(VVAO);
                glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
                glBindVertexArray(0);
                return;
            }
            // std::cout << "receive frame success  "<<  std::endl;
            switch (avCodecContext->pix_fmt)
            {
            case AV_PIX_FMT_YUV420P:
                /* code */
                // std::cout << "yuv 420p data frame = "<< avCodecContext->frame_num << std::endl;

                glActiveTexture(GL_TEXTURE0);
                glBindTexture(GL_TEXTURE_2D, texs[0]);
                glPixelStorei(GL_UNPACK_ROW_LENGTH, avFrame->linesize[0]);
                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,avFrame->width, avFrame->height, GL_RED, GL_UNSIGNED_BYTE, avFrame->data[0]);
                videoShader->setInt("tex_y",0);

                
                glActiveTexture(GL_TEXTURE1);
                glBindTexture(GL_TEXTURE_2D, texs[1]);
                glPixelStorei(GL_UNPACK_ROW_LENGTH, avFrame->linesize[1]);
                glTexSubImage2D(GL_TEXTURE_2D,  0, 0, 0,avFrame->width / 2, avFrame->height / 2, GL_RED, GL_UNSIGNED_BYTE, avFrame->data[1]);
                videoShader->setInt("tex_u",1);

               
                glActiveTexture(GL_TEXTURE2);
                glBindTexture(GL_TEXTURE_2D, texs[2]);
                glPixelStorei(GL_UNPACK_ROW_LENGTH, avFrame->linesize[2]);
                glTexSubImage2D(GL_TEXTURE_2D,  0, 0, 0, avFrame->width / 2, avFrame->height / 2, GL_RED, GL_UNSIGNED_BYTE, avFrame->data[2]);
                videoShader->setInt("tex_v",2);

                glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);

                videoShader->setMat4("projection",projection);
                videoShader->setMat4("model",model);
                videoShader->setMat4("view",view);

                glBindVertexArray(VVAO);
                glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
                glBindVertexArray(0);
                glActiveTexture(GL_TEXTURE0);
                break;
            // default:
            //     break;
                
            }

        }else{


            glBindVertexArray(VVAO);
            glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
            glBindVertexArray(0);
        }
    }else{
        std::cout << "read frame error  "<<  std::endl;
        glBindVertexArray(VVAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);
    }


    

}

void VideoStuff::Release()
{
    avformat_close_input(&avFormatContext);
    avformat_free_context(avFormatContext);
    av_frame_free(&avFrame);
    av_packet_free(&avPacket);
    avcodec_free_context(&avCodecContext);
}

VideoStuff::~VideoStuff()
{
    Release();
}


#endif

3.模型部分(代码来自learn opengl lessons)

mesh.h

#ifndef MESH_H
#define MESH_H

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <string>
#include <vector>
#include "Shader.h"
#include "../libs/glad/include/glad.h"

#define MAX_BONE_INFLUENCE 4

struct Vertex{
    glm::vec3 Position;
    glm::vec3 Normal;
    glm::vec2 TexCoords;
    glm::vec3 Tangent;
    glm::vec3 Bitangent;
    int m_BoneIDs[MAX_BONE_INFLUENCE];
    float m_Weight[MAX_BONE_INFLUENCE];
};

struct Texture{
    unsigned int id;
    std::string type;
    std::string path;
};


class Mesh
{
public:
    std::vector<Vertex> vertrices;
    std::vector<unsigned int> indices;
    std::vector<Texture> textures;
public:
    Mesh(std::vector<Vertex> vertrices,std::vector<unsigned int> indices,std::vector<Texture> textures);
    ~Mesh();
    void Draw(Shader glShader);
private:
    unsigned int VAO, VBO, EBO;
    void setupMesh();
};

Mesh::Mesh(std::vector<Vertex> vertrices,std::vector<unsigned int> indices,std::vector<Texture> textures)
{
    this->vertrices = vertrices;
    this->indices = indices;
    this->textures = textures;
    setupMesh();
}

Mesh::~Mesh()
{
}

void Mesh::setupMesh(){
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, vertrices.size() * sizeof(Vertex), &vertrices[0], GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex,Normal));
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
    glEnableVertexAttribArray(4);
    glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));
    glEnableVertexAttribArray(5);
    glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, m_BoneIDs));
    glEnableVertexAttribArray(6);
    glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, m_Weight));
    
    
    
    glBindVertexArray(0);

}

void Mesh::Draw(Shader glShader){
    unsigned int diffuseNr      = 1;
    unsigned int specularNr     = 1;
    unsigned int normalNr       = 1;
    unsigned int heightNr       = 1;
    for(unsigned int i = 0; i < textures.size(); i++){
        glActiveTexture(GL_TEXTURE0 + i);
        std::string number;
        std::string name = textures[i].type;
        if(name == "texture_diffuse"){
            number = std::to_string(diffuseNr++);
        }
        else if(name == "texture_specular"){
            number = std::to_string(specularNr++);
        }
        else if(name == "texture_normal"){
            number = std::to_string(normalNr++);
        }
        else if(name == "texture_height"){
            number = std::to_string(heightNr++);
        }
        glShader.setInt((name+number).c_str(),i);
        glBindTexture(GL_TEXTURE_2D, textures[i].id);
    }
    glBindVertexArray(VAO);
    glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(indices.size()),GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);

    glActiveTexture(GL_TEXTURE0);
}


#endif

模型.h

#ifndef MODEL_H
#define MODEL_H

#include "stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "Shader.h"
#include "Mesh.h"
#include <vector>
#include <string>
#include <fstream>
#include <map>
#include <sstream>
#include <iostream>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>


unsigned int TextureFromFile(const char *path, const std::string &directory, bool gamma = false);


class Model
{
public:
    std::vector<Texture> textures_loaded;
    std::vector<Mesh> meshes;
    std::string directory;
    bool gammaCorrection;

    Model(std::string const &path, bool gamma = false) : gammaCorrection(gamma){
        loadModel(path);
    };
    ~Model(){

    }
    void Draw(Shader glShader){
        for(unsigned int i = 0; i < meshes.size(); i++){
            meshes[i].Draw(glShader);
        }
    }

private:
    
    void loadModel(std::string const &path){
        Assimp::Importer importer;
        const aiScene* scene = importer.ReadFile(path,aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
        if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode){
            std::cout << "Error::Assimp:: " << importer.GetErrorString() << std::endl;
            return;
        }
        directory = path.substr(0, path.find_last_of('/'));
        processNode(scene->mRootNode, scene);
    }

    void processNode(aiNode *node, const aiScene *scene){
        for(unsigned int i = 0; i < node->mNumMeshes; i++){
            aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
            meshes.push_back(processMesh(mesh,scene));
        }
        for(unsigned int i = 0; i < node->mNumChildren; i++){
            processNode(node->mChildren[i],scene);
        }
    }

    Mesh processMesh(aiMesh *mesh, const aiScene *scene){
        std::vector<Vertex> vertices;
        std::vector<unsigned int> indices;
        std::vector<Texture> textures;

        for(unsigned int i = 0; i < mesh->mNumVertices;i++){
            Vertex vertex;
            glm::vec3 vector;
            vector.x = mesh->mVertices[i].x;
            vector.y = mesh->mVertices[i].y;
            vector.z = mesh->mVertices[i].z;
            vertex.Position = vector;

            if(mesh->HasNormals()){
                vector.x = mesh->mNormals[i].x;
                vector.y = mesh->mNormals[i].y;
                vector.z = mesh->mNormals[i].z;
                vertex.Normal = vector;
            }

            if(mesh->mTextureCoords[0]){
                glm::vec2 vec;
                vec.x = mesh->mTextureCoords[0][i].x;
                vec.y = mesh->mTextureCoords[0][i].y;
                vertex.TexCoords = vec;

                vector.x = mesh->mTangents[i].x;
                vector.y = mesh->mTangents[i].y;
                vector.z = mesh->mTangents[i].z;
                vertex.Tangent = vector;

                vector.x = mesh->mBitangents[i].x;
                vector.y = mesh->mBitangents[i].y;
                vector.z = mesh->mBitangents[i].z;
                vertex.Bitangent = vector;

            }else{
                vertex.TexCoords = glm::vec2(0.0f, 0.0f);
            }
            vertices.push_back(vertex);
        }

        for(unsigned int i = 0; i < mesh->mNumFaces;i++){
            aiFace face = mesh->mFaces[i];
            for(unsigned int j = 0; j < face.mNumIndices; j++){
                indices.push_back(face.mIndices[j]);
            }
        }

        aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
        
        std::vector<Texture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE,"texture_diffuse");
        textures.insert(textures.end(),diffuseMaps.begin(), diffuseMaps.end());
        
        std::vector<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE,"texture_specular");
        textures.insert(textures.end(),specularMaps.begin(), specularMaps.end());

        std::vector<Texture> narmalMap = loadMaterialTextures(material, aiTextureType_DIFFUSE,"texture_normal");
        textures.insert(textures.end(),narmalMap.begin(), narmalMap.end());

        std::vector<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE,"texture_height");
        textures.insert(textures.end(),heightMaps.begin(), heightMaps.end());
        return Mesh(vertices, indices, textures);
    }

    std::vector<Texture> loadMaterialTextures(aiMaterial *mat, aiTextureType type, std::string typeName){
        std::vector<Texture> textures;
        for(unsigned int i = 0; i < mat->GetTextureCount(type); i++){
            aiString str;
            mat->GetTexture(type, i, &str);
            bool skip = false;
            for(unsigned int j = 0; j < textures_loaded.size(); j++){
                if(std::strcmp(textures_loaded[j].path.data(),str.C_Str()) == 0){
                    textures.push_back(textures_loaded[j]);
                    skip = true;
                    break;
                }
            }
            if(!skip){
                Texture texture;
                texture.id = TextureFromFile(str.C_Str(),this->directory);
                texture.type = typeName;
                texture.path = str.C_Str();
                textures.push_back(texture);
                textures_loaded.push_back(texture);
            }
        }
        return textures;
    }

};

unsigned int TextureFromFile(const char* path, const std::string &directory, bool gamma){
    std::string filename = path;
    filename = directory + "/" + filename;
    unsigned int textureId;
    glGenTextures(1, &textureId);
    int width ,height, nrComponents;
    unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents,0);
    if(data)
    {
        GLenum format;
        if(nrComponents == 1){
            format = GL_RED;
        }
        else if(nrComponents == 3){
            format = GL_RGB;
        }
        else if(nrComponents == 4){
            format = GL_RGBA;
        }
        glBindTexture(GL_TEXTURE_2D, textureId);
        glTexImage2D(GL_TEXTURE_2D, 0, format,width,height,0, format,GL_UNSIGNED_BYTE,data);
        glGenerateMipmap(GL_TEXTURE_2D);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        stbi_image_free(data);

    }
    else
    {
        std::cout << "Texture failed toload path : " << path << std::endl;
        stbi_image_free(data);
    }

    return textureId;
}

#endif

和着色器 3d模型的顶点和片段着色器

#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;

out vec2 Texcoords;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    Texcoords = aTexCoords;
    gl_Position = projection * view * model * vec4(aPos,1.0f);
}



#version 330 core

out vec4 FragColor;

in vec2 Texcoords;
uniform sampler2D texture_diffuse1;

void main()
{
    FragColor = vec4(1.0f,1.0f,1.0f,1.0f) * texture(texture_diffuse1, Texcoords);
}

视频的顶点和片段着色器

#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;

out vec2 Texcoord;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform float transX;
uniform float transY;

void main()
{
    gl_Position = projection * view * model * vec4(aPos.x + transX,aPos.y + transY,aPos.z, 1.0f);
    // gl_Position = vec4(aPos, 1.0f);
    Texcoord = vec2(aTexCoord.x, 1.0f - aTexCoord.y);
}


#version 330 core

in  vec2 Texcoord;
uniform sampler2D tex_y;
uniform sampler2D tex_u;
uniform sampler2D tex_v;

void main()
{
    
    float y = texture2D(tex_y, Texcoord).r;
    float u = texture2D(tex_u, Texcoord).r - 0.5f;
    float v = texture2D(tex_v, Texcoord).r - 0.5f;
    
    float r = y + 1.403f * v;
    float g = y - 0.344f * u - 0.714f * v;
    float b = y + 1.770f * u;

    gl_FragColor = vec4(r, g, b, 1.0f);
}

一切都在这里。

  1. 3D模型部分基本上是使用learn opengl lessons的代码
  2. 视频部分来自ffmpeg解码yuv420p视频+

很多视频游戏都有这样的原因: 在 3D 环境中,人们正在看电视或视频,就像那样

c++ opengl glsl video-processing glfw
© www.soinside.com 2019 - 2024. All rights reserved.