【发布时间】:2020-02-19 02:47:28
【问题描述】:
嘿,我有一个抽象所有与着色器相关的类,名为 Shader.cpp:
#include "Shader.h"
#include "Renderer.h"
#include <iostream>
#include <fstream>
Shader::Shader()
{
GLCALL(m_RenderedID = glCreateProgram());
}
Shader::~Shader()
{
GLCALL(glDeleteProgram(m_RenderedID));
}
void Shader::Bind() const
{
GLCALL(glUseProgram(m_RenderedID));
}
void Shader::Unbind() const
{
GLCALL(glUseProgram(0));
}
std::string ParseShader(const std::string& path)
{
std::ifstream ifs(path);
return std::string((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
}
static unsigned int CompileShader(unsigned int type, const std::string& source)
{
GLCALL(unsigned int id = glCreateShader(type));
const char* src = source.c_str();
GLCALL(glShaderSource(id, 1, &src, nullptr));
GLCALL(glCompileShader(id));
int result;
GLCALL(glGetShaderiv(id, GL_COMPILE_STATUS, &result));
if (result == GL_FALSE)
{
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*)alloca(length * sizeof(char));
GLCALL(glGetShaderInfoLog(id, length, &length, message));
std::cout << "Failed to compile shader!" << std::endl;
std::cout << message << std::endl;
glDeleteShader(id);
return 0;
}
return id;
}
void Shader::Attach(const unsigned int type, const std::string& path)
{
unsigned int id = CompileShader(type, ParseShader(path));
if (m_ShaderFiles.find(id) == m_ShaderFiles.end())
m_ShaderFiles[id] = ShaderFile({ type, path });
GLCALL(glAttachShader(m_RenderedID, id));
}
void Shader::Link()
{
int result;
GLCALL(glLinkProgram(m_RenderedID));
GLCALL(glGetProgramiv(m_RenderedID, GL_LINK_STATUS, &result));
if (result == GL_FALSE)
{
std::cout << "Failed to link shader!" << std::endl;
return;
}
GLCALL(glValidateProgram(m_RenderedID));
GLCALL(glGetProgramiv(m_RenderedID, GL_VALIDATE_STATUS, &result));
if (result == GL_FALSE)
{
std::cout << "Failed to validate shader!" << std::endl;
return;
}
for (const auto& shaderFile : m_ShaderFiles) {
GLCALL(glDeleteShader(shaderFile.first));
}
}
// this part is taken from Shader.h because it's templated
template<typename T, unsigned int S>
void SetUniform(Uniform<T, S>& uniform)
{
Bind();
uniform.Set(GetUniformLocation(uniform.GetName()));
}
int Shader::GetUniformLocation(const std::string& name)
{
if (m_UniformLocationCache.find(name) != m_UniformLocationCache.end())
return m_UniformLocationCache[name];
GLCALL(int location = glGetUniformLocation(m_RenderedID, name.c_str()));
m_UniformLocationCache[name] = location;
return location;
}
这是我使用 Shader 类的app.cpp:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <string>
#include "core/renderer/Shader.h"
#include "core/renderer/uniform/Uniform4f.cpp"
#include "core/renderer/VertexArray.h"
#include "core/renderer/VertexBufferLayout.h"
#include "core/renderer/VertexBuffer.h"
#include "core/renderer/IndexBuffer.h"
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
if (glewInit() != GLEW_OK)
std::cout << "Error!" << std::endl;
std::cout << glGetString(GL_VERSION) << std::endl;
float vertices[] = {
-0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f
};
unsigned int indices[] = {
0, 1, 2,
2, 3, 0
};
VertexArray va;
VertexBufferLayout layout;
layout.Push({ GL_FLOAT, 2, sizeof(float) * 2, GL_FALSE });
VertexBuffer vb(vertices, sizeof(vertices), layout);
IndexBuffer ib(indices, 6);
va.AddVertexBuffer(vb);
Shader shader;
shader.Attach(GL_VERTEX_SHADER, "res/shaders/basic_vs.shader");
shader.Attach(GL_FRAGMENT_SHADER, "res/shaders/basic_fs.shader");
shader.Link();
shader.Bind();
Uniform4f colorUniform("u_Color");
float r = 0.00f;
float increment = 0.05f;
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
colorUniform.SetValues({ r, 0.5f, 0.9f, 1.0f });
shader.SetUniform(colorUniform);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
if (r > 1.0f)
increment = -0.05f;
else if (r < 0.0f)
increment = 0.05f;
r += increment;
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
#include <iostream>
这是我要设置的Uniform4f.cpp:
#include "Uniform.h"
#include "../Renderer.h"
#include <iostream>
class Uniform4f : public Uniform<float, 4>
{
public:
Uniform4f(const std::string& name, const std::array<float, 4>& values = {})
: Uniform(name, values)
{
}
virtual void Set(int location)
{
GLCALL(glUniform4fv(location, m_Values.size(), reinterpret_cast<const GLfloat*>(m_Values.data())));
}
};
现在我在app.cpp 的渲染循环中调用shader.SetUniform(colorUniform); 中的glUniform4fv(location, m_Values.size(), reinterpret_cast<const GLfloat*>(m_Values.data())) 调用GL_INVALID_OPERATION。
docs 声明它可能是以下问题之一:
- 如果没有当前程序,则生成 GL_INVALID_OPERATION 对象。
- 如果统一变量的大小生成 GL_INVALID_OPERATION 在着色器中声明的大小与 glUniform 命令。
- GL_INVALID_OPERATION 如果有符号或无符号之一生成 此函数的整数变体用于加载统一变量 float、vec2、vec3、vec4 或这些类型的数组,或者如果其中之一 此函数的浮点变量用于加载 int、ivec2、ivec3、ivec4、unsigned int 类型的统一变量, uvec2、uvec3、uvec4 或这些数组。
- 如果有符号整数之一生成 GL_INVALID_OPERATION 此函数的变体用于加载类型的统一变量 unsigned int、uvec2、uvec3、uvec4 或这些数组。
- 如果无符号整数之一生成 GL_INVALID_OPERATION 此函数的变体用于加载类型的统一变量 int、ivec2、ivec3、ivec4 或这些数组。
- 如果位置是无效的制服,则会生成 GL_INVALID_OPERATION 当前程序对象的位置和位置不等于 -1。
- 如果计数大于 1 并且 指示的统一变量不是数组变量。
- 如果使用 glUniform1i 和 glUniform1iv 以外的命令。
我真的不知道哪个错误会导致它,以上似乎都不对。在调试时,我看到传递给glUniform4fv 的统一值、大小和位置都是正确的,我在Binded 中的着色器程序在Linked 在app.cpp 之后,所以我不是确定是什么原因造成的。
如果能得到这方面的任何帮助,我将不胜感激。
【问题讨论】:
-
下次提示:我在您问题的开头看到了所有代码并且感到无聊。我看到代码后面有文字,但我没有阅读它的动力。 (我猜How to Ask 中的建议这次会很有用。)