1 Star 0 Fork 1

波波/Gut

forked from 2144/Gut 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Shader.h 4.04 KB
一键复制 编辑 原始数据 按行查看 历史
2144 提交于 2023-08-31 15:46 . s
#pragma once
#include "Core.h"
struct ShaderCode
{
unsigned int id = 0;
bool CreateFrom(const char* code, int type);
void Release();
};
struct ShaderFile :public ShaderCode
{
bool CreateFromFile(std::string path, int type);
};
struct Shader
{
public:
bool CreateFromCode(const char* vcode, const char* fCode);
bool CreateFromFile(ShaderFile vFile, ShaderFile fFile);
bool CreateFromFilePath(std::string vPath, std::string fPath);
#ifdef WIN32
bool CreateFromFilePath(std::string vPath, std::string gPath, std::string fPath);
bool CreateFormFile(ShaderFile vFile, ShaderFile gFile, ShaderFile fFile);
#endif // DEBUG
public:
unsigned int ID = 0;
void Use();
void Create();
void Release();
void Link();
void UnUse();
~Shader()
{
glDeleteShader(ID);
}
void Bind(const std::string& name, unsigned int binding)
{
glUniformBlockBinding(ID , GetUniformBlockIndex(name), binding);
}
unsigned int GetUniformBlockIndex(const std::string& name)
{
return glGetUniformBlockIndex(ID, name.c_str());
}
unsigned int GetLocation(const std::string& name) const
{
unsigned int res= glGetUniformLocation(ID, name.c_str());
#ifdef SHADER_DEBUG
if (res == -1)
{
LOGE("index of location %s is -1", name.c_str());
}
#endif // SHADER_DEBUG
return res;
}
void setBool(const std::string& name, bool value) const
{
glUniform1i(GetLocation(name), (int)value);
}
// ------------------------------------------------------------------------
void setInt(const std::string& name, int value) const
{
glUniform1i(GetLocation(name), value);
}
// ------------------------------------------------------------------------
void setFloat(const std::string& name, float value) const
{
glUniform1f(GetLocation(name), value);
}
void SetFloatArray(const std::string& name, float* val, int num)
{
for (int i = 0; i < num; i++)
{
std::string label = name + std::string("[") + std::to_string(i) + "]";
setFloat(label, val[i]);
}
}
// ------------------------------------------------------------------------
void setVec2(const std::string& name, const float* value) const
{
glUniform2fv(GetLocation(name), 1, &value[0]);
}
void setVec2(const std::string& name, float x, float y) const
{
glUniform2f(GetLocation(name), x, y);
}
// ------------------------------------------------------------------------
void setVec3(const std::string& name, const float* value) const
{
glUniform3fv(GetLocation(name), 1, &value[0]);
}
void setVec3(const std::string& name, float x, float y, float z) const
{
glUniform3f(GetLocation(name), x, y, z);
}
// ------------------------------------------------------------------------
void setVec4(const std::string& name, const float* value, const int num = 1) const
{
glUniform4fv(GetLocation(name), num, &value[0]);
}
void setVec4(const std::string& name, float x, float y, float z, float w)
{
glUniform4f(GetLocation(name), x, y, z, w);
}
// ------------------------------------------------------------------------
void setMat2(const std::string& name, const float* mat) const
{
glUniformMatrix2fv(GetLocation(name), 1, GL_FALSE, &mat[0]);
}
// ------------------------------------------------------------------------
void setMat3(const std::string& name, const float* mat) const
{
glUniformMatrix3fv(GetLocation(name), 1, GL_FALSE, &mat[0]);
}
// ------------------------------------------------------------------------
void setMat4(const std::string& name, const float* mat) const
{
glUniformMatrix4fv(GetLocation(name), 1, GL_FALSE, &mat[0]);
}
};
struct ShaderVertexFramentDesc
{
const char* vcode;
const char* fcode;
};
struct ShaderVertexFragment : public Shader
{
};
struct ShaderVertexGeometryFragment :public Shader
{
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bobo110/gut.git
git@gitee.com:bobo110/gut.git
bobo110
gut
Gut
master

搜索帮助