Код: Выделить всё
#pragma once
#include"shader/_Shader.h"
#include"includes.h"
namespace SHADER
{
enum class ShaderID
{
SH_00 = 0,
SH_01 = 1, SH_02,..., SH_50,
};
#define MAX_SHADER_ID 50
class Shader_Binder
{
private:
struct Data
{
ShaderID id;
Shader m_shader;
std::vector m_VAOs;
std::vector m_VBOs;
};
ShaderID m_CurrentShader = ShaderID::SH_00; // Track current shader
unsigned int m_CurrentVAO = 0; // Track current VAO
unsigned int m_CurrentVBO = 0; // Track current VBO
std::unordered_mapm_Data;
public:
// Static method to get the single instance of the class
static Shader_Binder& getInstance()
{
static Shader_Binder instance; // Guaranteed to be destroyed, instantiated on first use
return instance;
}
// Deleted copy constructor and assignment operator to prevent copying
Shader_Binder(const Shader_Binder&) = delete;
Shader_Binder& operator=(const Shader_Binder&) = delete;
/* Creates a new shader, binds it and returns a ID */
ShaderID CreateNewShader(const std::string vShader = "", const std::string fShader = "", const std::string gShader = "");
/* Binds the shader passed in and returns a ID */
ShaderID CreateNewShader(Shader& shader);
/* Binds the shader, VAO and VBO*/
void bind(ShaderID id = ShaderID::SH_00, int vaoid = -1, int vboid = -1);
/* Unbinds the shader, VAO and VBO*/
void unbind(ShaderID id = ShaderID::SH_00, int vaoid = -1, int vboid = -1);
/* Creates a new VAO on the given shader id, binds it and returns the value of the created VAO. */
unsigned int CreateNewVAO(ShaderID id);
/* Creates a new VBO on the given shader id, binds it and returns the value of the created VBO. */
unsigned int CreateNewVBO(ShaderID id);
/* Returns a pointer to the shader object relative to the id passed in*/
Shader* getShader(ShaderID id);
/* Sets the vertex attributes based on the id of the shader passed
* vaoIndex is set to 0 by default ,
* considering there are no multiple vao's created for the same shader */
void setVertexAttribs(ShaderID id, unsigned int Gl_location_index, int size,
unsigned int type, GLboolean normalized, int stride, const void* pointer,
unsigned int vaoIndex = 0);
/*
* Assumes GL_DYNAMIC_DRAW as the default data usage.
* Requires the size of the buffer to be sent, even if the usage is GL_DYNAMIC_DRAW.
* Assumes the required shaders and others are bound before using.
* Set the usage to GL_STATIC_DRAW if the data is to be static and other params are passed.
* Assumes GL_ARRAY_BUFFER as the default target to be used
*/
void setBufferData(GLenum usage = GL_DYNAMIC_DRAW, GLsizeiptr size = 0,
const void* data = nullptr, GLenum target = GL_ARRAY_BUFFER);
/*
* Updates the buffer data if the usage was GL_DYNAMIC_DRAW.
* Assumes the required shaders and others are bound before using.
* Uses glBufferSubData() for the task
* Considers GL_ARRAY_BUFFER as the default target
*/
void UpdateBufferData(GLintptr offset, GLsizeiptr size, const void* data, GLenum target = GL_ARRAY_BUFFER);
/*unbinds the last used (or whichever currently bound) shader and vao and vbo */
void reset();
private:
bool isIdUsed(ShaderID id);
Shader_Binder() {}
~Shader_Binder() {}
};
}
typedef SHADER::ShaderID ShaderID;
static SHADER::Shader_Binder& Binder = SHADER::Shader_Binder::getInstance();
чье определение
Код: Выделить всё
Shader* Shader_Binder::getShader(ShaderID id)
{
return &m_Data.at(id).m_shader;
}
Я попробовал вызвать функцию с именем Setuniform3f() это была одна из функций-членов внутри объекта шейдера, и, видимо, она не работает, и я получаю GL_INVALID_OPERATION.
Я попробовал сделать это вне функции, и это работает:
Код: Выделить всё
Binder.getShader(shaderID)->Setuniform3f("uColor", color.r, color.g, color.b);//this doesnt work
Код: Выделить всё
auto x=glGetUniformLocation(Binder.getShader(shaderID)->Program_id, "uColor");
glUniform3f(x, color.r, color.g, color.b);//this works
Я искал в Интернете и даже спросил в чате, и они упомянули что-то о времени жизни объекта шейдера или висячих указателях, но они мне совсем не помогли.
Не стесняйтесь задавать мне любые вопросы, связанные с к этому, и я был бы признателен за любую помощь, даже если она небольшая. меня это уже давно беспокоит.
Подробнее здесь: https://stackoverflow.com/questions/790 ... engl-class