change shaderSercve so it can take both of char* and std::filesystem::path
This commit is contained in:
parent
a68ef011ae
commit
64347a1ecd
46
include/shader.h
Normal file
46
include/shader.h
Normal 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
|
@ -16,7 +16,9 @@ private:
|
||||
unsigned int programId;
|
||||
|
||||
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);
|
||||
void Use();
|
||||
inline unsigned int GetId() { return this->programId; }
|
||||
|
20
main.cc
20
main.cc
@ -4,7 +4,7 @@
|
||||
#include <filesystem>
|
||||
#include "decoder.h"
|
||||
#include "shaderService.h"
|
||||
using namespace std::filesystem;
|
||||
#include "shader.h"
|
||||
using std::cout, std::endl;
|
||||
|
||||
int main(int argc, char **const argv)
|
||||
@ -14,8 +14,15 @@ int main(int argc, char **const argv)
|
||||
cout << SDL_GetError() << "\n";
|
||||
return -1;
|
||||
}
|
||||
const char* targetFilepath = argv[1];
|
||||
|
||||
if(targetFilepath == nullptr || !std::filesystem::exists(targetFilepath)){
|
||||
cout<<"File Not Exist\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
DecoderParam param{};
|
||||
InitDecoder("/home/jie/code/cc/sdl2/videoPlayer/video/ocean.mp4", param);
|
||||
InitDecoder(targetFilepath, param);
|
||||
int client_width = param.width / 2;
|
||||
int client_height = param.height / 2;
|
||||
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_MAG_FILTER, GL_LINEAR);
|
||||
}
|
||||
ShaderService shaderService{
|
||||
"/home/jie/code/cc/sdl2/videoPlayer/shaders/vertexShader.vert",
|
||||
"/home/jie/code/cc/sdl2/videoPlayer/shaders/fragShader.frag"};
|
||||
/* ShaderService shaderService{
|
||||
std::filesystem::path("/home/jie/project/mp/shaders/vertexShader.vert"),
|
||||
std::filesystem::path("/home/jie/project/mp/shaders/fragShader.frag")};
|
||||
|
||||
*/
|
||||
ShaderService shaderService{vSource.data(), fSource.data()};
|
||||
shaderService.Use();
|
||||
shaderService.SetUniform<int>("textureY", 0);
|
||||
shaderService.SetUniform<int>("textureU", 1);
|
||||
|
@ -1,12 +1,9 @@
|
||||
#include "decoder.h"
|
||||
#include <filesystem>
|
||||
|
||||
void InitDecoder(const char* filepath, DecoderParam& param){
|
||||
if(!std::filesystem::exists(filepath)) return;
|
||||
AVFormatContext* fmtCtx = nullptr;
|
||||
AVCodecContext* codecFmt = nullptr;
|
||||
auto ret = avformat_open_input(&fmtCtx, filepath, NULL, NULL);
|
||||
|
||||
avformat_find_stream_info(fmtCtx, nullptr);
|
||||
|
||||
for(int i=0; i<fmtCtx->nb_streams; i++){
|
||||
|
@ -5,6 +5,30 @@
|
||||
#include "shaderService.h"
|
||||
#include <fstream>
|
||||
#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,
|
||||
const std::filesystem::path &fragShaderPath) {
|
||||
@ -27,25 +51,7 @@ ShaderService::ShaderService(const std::filesystem::path &vertexShaderPath,
|
||||
|
||||
const char* vSource = vertexSource.c_str();
|
||||
const char* fSource = fragmentSource.c_str();
|
||||
|
||||
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);
|
||||
InitShader(vSource, fSource);
|
||||
}
|
||||
|
||||
void ShaderService::Use() {
|
||||
|
Loading…
Reference in New Issue
Block a user