xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
shaderprogram.cc
浏览该文件的文档.
2
3//
6
7#define ROLE GL
8#define LABEL ShaderProgram
10
12 const Shader &shader) {
13 if (program_ == 0) {
14 program_ = _gl glCreateProgram();
15 }
16 _I("Attaching shader: " << shader.shader_);
17 _gl glAttachShader(program_, shader.shader_);
18}
20 if (program_ == 0) {
21 throw std::runtime_error("program not created");
22 }
23 _gl glLinkProgram(program_);
24 _gl GLint ok;
25 _gl glGetProgramiv(program_, _gl GL_LINK_STATUS, &ok);
26 if (!ok) {
27 _gl GLint length;
28 _gl glGetProgramiv(program_, _gl GL_INFO_LOG_LENGTH, &length);
29 std::string log(length, '\0');
30 _gl glGetProgramInfoLog(program_, length, nullptr, log.data());
31 _E("Failed to link program: " << program_ << " " << log);
32 throw std::runtime_error(log);
33 }
34 _I("Program linked: " << program_);
35};
37 _gl glUseProgram(program_);
38};
39;
41 if (program_ != 0) {
42 _gl glDeleteProgram(program_);
43 _I("Program deleted: " << program_);
44 }
45 program_ = 0;
46};
47
50 const char *name, const xcmath::mat<gl::GLfloat, 4, 4> &mat) const {
51 _gl glUseProgram(program_);
52 _gl glUniformMatrix4fv(_gl glGetUniformLocation(program_, name), 1,
53 _gl GL_TRUE, &mat[0][0]);
54}
56 const char *name, const xcmath::mat<gl::GLfloat, 3, 3> &mat) const {
57 _gl glUseProgram(program_);
58 _gl glUniformMatrix3fv(_gl glGetUniformLocation(program_, name), 1,
59 _gl GL_TRUE, &mat[0][0]);
60}
62 const char *name, const xcmath::vec<gl::GLfloat, 3> &vec) const {
63 _gl glUseProgram(program_);
64 _gl glUniform3fv(_gl glGetUniformLocation(program_, name), 1, &vec[0]);
65}
67 const char *name, const xcmath::vec<gl::GLfloat, 4> &vec) const {
68 _gl glUseProgram(program_);
69 _gl glUniform4fv(_gl glGetUniformLocation(program_, name), 1, &vec[0]);
70};
71std::shared_ptr<xcal::render::opengl::GL::ShaderProgram>
73 std::string_view vertex_file, std::string_view fragment_file) {
74 auto tmp = std::make_shared<xcal::render::opengl::GL::ShaderProgram>();
76 _gl GL_VERTEX_SHADER, vertex_file));
78 _gl GL_FRAGMENT_SHADER, fragment_file));
79 tmp->link();
80 tmp->use();
81 _D("Shader program created: " << tmp.get());
82 return tmp;
83}
static Shader from_file(gl::GLenum type, std::string_view file_path)
Definition shader.cc:28
void uniform(const char *name, const xcmath::mat< gl::GLfloat, 4, 4 > &mat) const
static std::shared_ptr< ShaderProgram > from_file(std::string_view vertex_file, std::string_view fragment_file)
void atttach_shader(const Shader &shader)
Matrix class template
Definition mat.hpp:27
Vector class template
Definition vec.hpp:206