change shaderSercve so it can take both of char* and std::filesystem::path

This commit is contained in:
Jie 2024-02-20 13:51:41 +08:00
parent a68ef011ae
commit 64347a1ecd
5 changed files with 89 additions and 28 deletions

46
include/shader.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef SHADER_H
#define SHADER_H
#include <string_view>
constexpr std::string_view vSource = R"(
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}
)";
constexpr std::string_view fSource = R"(
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D textureY;
uniform sampler2D textureU;
uniform sampler2D textureV;
void main(){
vec3 yuv, rgb;
vec3 yuv2r = vec3(1.164, 0.0, 1.596);
vec3 yuv2g = vec3(1.164, -0.391, -0.813);
vec3 yuv2b = vec3(1.164, 2.018, 0.0);
yuv.x = texture(textureY, TexCoord).r - 0.0625;
yuv.y = texture(textureU, TexCoord).r - 0.5;
yuv.z = texture(textureV, TexCoord).r - 0.5;
rgb.x = dot(yuv, yuv2r);
rgb.y = dot(yuv, yuv2g);
rgb.z = dot(yuv, yuv2b);
FragColor = vec4(rgb, 1.0);
}
)";
#endif

View File

@ -16,7 +16,9 @@ private:
unsigned int programId; unsigned int programId;
public: public:
ShaderService(const std::filesystem::path &vertexShaderPath, const std::filesystem::path &fragShaderPath); explicit ShaderService(const char* vSource, const char* fSource);
explicit ShaderService(const std::filesystem::path &vertexShaderPath, const std::filesystem::path &fragShaderPath);
void InitShader(const char* vSource, const char* fSource);
bool CheckShader(unsigned int shaderIndex, bool isProgram = false); bool CheckShader(unsigned int shaderIndex, bool isProgram = false);
void Use(); void Use();
inline unsigned int GetId() { return this->programId; } inline unsigned int GetId() { return this->programId; }

20
main.cc
View File

@ -4,7 +4,7 @@
#include <filesystem> #include <filesystem>
#include "decoder.h" #include "decoder.h"
#include "shaderService.h" #include "shaderService.h"
using namespace std::filesystem; #include "shader.h"
using std::cout, std::endl; using std::cout, std::endl;
int main(int argc, char **const argv) int main(int argc, char **const argv)
@ -14,8 +14,15 @@ int main(int argc, char **const argv)
cout << SDL_GetError() << "\n"; cout << SDL_GetError() << "\n";
return -1; return -1;
} }
const char* targetFilepath = argv[1];
if(targetFilepath == nullptr || !std::filesystem::exists(targetFilepath)){
cout<<"File Not Exist\n";
return 0;
}
DecoderParam param{}; DecoderParam param{};
InitDecoder("/home/jie/code/cc/sdl2/videoPlayer/video/ocean.mp4", param); InitDecoder(targetFilepath, param);
int client_width = param.width / 2; int client_width = param.width / 2;
int client_height = param.height / 2; int client_height = param.height / 2;
SDL_Window *window = SDL_CreateWindow( SDL_Window *window = SDL_CreateWindow(
@ -92,9 +99,12 @@ int main(int argc, char **const argv)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} }
ShaderService shaderService{ /* ShaderService shaderService{
"/home/jie/code/cc/sdl2/videoPlayer/shaders/vertexShader.vert", std::filesystem::path("/home/jie/project/mp/shaders/vertexShader.vert"),
"/home/jie/code/cc/sdl2/videoPlayer/shaders/fragShader.frag"}; std::filesystem::path("/home/jie/project/mp/shaders/fragShader.frag")};
*/
ShaderService shaderService{vSource.data(), fSource.data()};
shaderService.Use(); shaderService.Use();
shaderService.SetUniform<int>("textureY", 0); shaderService.SetUniform<int>("textureY", 0);
shaderService.SetUniform<int>("textureU", 1); shaderService.SetUniform<int>("textureU", 1);

View File

@ -1,12 +1,9 @@
#include "decoder.h" #include "decoder.h"
#include <filesystem>
void InitDecoder(const char* filepath, DecoderParam& param){ void InitDecoder(const char* filepath, DecoderParam& param){
if(!std::filesystem::exists(filepath)) return;
AVFormatContext* fmtCtx = nullptr; AVFormatContext* fmtCtx = nullptr;
AVCodecContext* codecFmt = nullptr; AVCodecContext* codecFmt = nullptr;
auto ret = avformat_open_input(&fmtCtx, filepath, NULL, NULL); auto ret = avformat_open_input(&fmtCtx, filepath, NULL, NULL);
avformat_find_stream_info(fmtCtx, nullptr); avformat_find_stream_info(fmtCtx, nullptr);
for(int i=0; i<fmtCtx->nb_streams; i++){ for(int i=0; i<fmtCtx->nb_streams; i++){

View File

@ -5,6 +5,30 @@
#include "shaderService.h" #include "shaderService.h"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
ShaderService::ShaderService(const char* vSource, const char* fSource){
InitShader(vSource, fSource);
}
void ShaderService::InitShader(const char* vSource, const char* fSource){
unsigned int frag, vertex;
frag = glCreateShader(GL_FRAGMENT_SHADER);
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vSource, nullptr);
glShaderSource(frag, 1, &fSource, nullptr);
glCompileShader(vertex);
glCompileShader(frag);
if(const auto res = (CheckShader(vertex) && CheckShader(frag));!res){
return;
}
programId = glCreateProgram();
glAttachShader(programId, vertex);
glAttachShader(programId, frag);
glLinkProgram(programId);
CheckShader(programId, true);
glDeleteShader(vertex);
glDeleteShader(frag);
}
ShaderService::ShaderService(const std::filesystem::path &vertexShaderPath, ShaderService::ShaderService(const std::filesystem::path &vertexShaderPath,
const std::filesystem::path &fragShaderPath) { const std::filesystem::path &fragShaderPath) {
@ -27,25 +51,7 @@ ShaderService::ShaderService(const std::filesystem::path &vertexShaderPath,
const char* vSource = vertexSource.c_str(); const char* vSource = vertexSource.c_str();
const char* fSource = fragmentSource.c_str(); const char* fSource = fragmentSource.c_str();
InitShader(vSource, fSource);
unsigned int frag, vertex;
frag = glCreateShader(GL_FRAGMENT_SHADER);
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vSource, nullptr);
glShaderSource(frag, 1, &fSource, nullptr);
glCompileShader(vertex);
glCompileShader(frag);
if(const auto res = (CheckShader(vertex) && CheckShader(frag));!res){
return;
}
programId = glCreateProgram();
glAttachShader(programId, vertex);
glAttachShader(programId, frag);
glLinkProgram(programId);
CheckShader(programId, true);
glDeleteShader(vertex);
glDeleteShader(frag);
} }
void ShaderService::Use() { void ShaderService::Use() {