从我检查它应该工作,甚至我的顶点和片段着色器是从一个教程网站复制,其中有其他代码和其他代码,但我认为我的代码中有些东西,我可能不会&#39 ; t注意这使我的程序表现得那样。
这是drawable的初始化代码:
// before the draw function:
std::vector<glm::vec3> vertices;
vertices.push_back(glm::vec3(-1, -1, 0)); vertices.push_back(glm::vec3(1, 0, 0)); vertices.push_back(glm::vec3(1, -1, 0));
std::vector<glm::vec3> colors;
colors.push_back(glm::vec3(1, 1, 0)); colors.push_back(glm::vec3(1, 1, 0)); colors.push_back(glm::vec3(1, 1, 0));
test.Initialize(vertices, colors);
// The Initialize function in the 'Drawable' class
void Drawable::Initialize(std::vector< glm::vec3 > vertex, std::vector< glm::vec3 > color)
{
// Check if the drawables had been initialized, if not, Initialize it, else return an error
if(hasInit)
std::cerr << "[-] Already Initialized";
else
{
hasInit = true;
vertices = vertex;
colors = color;
//Generate buffers and bind the object to OpenGL then bind the data for opengl to read from
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), &colors[0], GL_STATIC_DRAW);
// Calculate the model-view-projection matrix for later use
MVP = settings->projection * settings->camera * model;
}
}
在绘制函数当然循环:
// Calling the draw function for the Drawable class
test.Draw();
// The draw function in the Drawable class
void Drawable::Draw()
{
glUseProgram(settings->simpleProgramID);
glUniformMatrix4fv(settings->mMVPLocation, 1, GL_FALSE, &MVP[0][0]);
glEnableVertexAttribArray(settings->mVertexPositionLocation);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(settings->mVertexPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(settings->mVertexColorLocation);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glVertexAttribPointer(settings->mVertexColorLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glDisableVertexAttribArray(settings->mVertexColorLocation);
glDisableVertexAttribArray(settings->mVertexPositionLocation);
}
在初始化函数之前,我得到了我需要的所有统一和属性位置,并将它们存储在一个名为settings的结构中(任何人都可以访问的结构,我甚至检查以确保每个其他文件中的值都是匹配结构)
这些是我的着色器:
// Vertex Shader
#version 330 core
layout(location = 0) in vec3 positionVertex;
layout(location = 1) in vec3 colorVertex;
uniform mat4 MVP;
out vec3 oColor;
void main(void)
{
gl_Position = MVP * vec4(positionVertex, 1);
oColor = colorVertex;
}
// Fragment Shader
#version 330 core
in vec3 oColor;
out vec3 color;
void main(void)
{
color = oColor;
}
但屏幕上没有画出任何内容,有人注意到一些异常吗? 我将所有矩阵初始化为glm :: mat4(1.0f),它是单位矩阵 我调试了代码,一切似乎运行顺利,但仍然没有结果。
编辑: 我发现我的编译器在尝试编译顶点着色器时实际上返回了一个错误:
GLuint loadShaderes(const char* vertex_shader_file, const char* fragment_shader_file)
{
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
std::string vertexShaderCode;
std::ifstream vertexShaderStream(vertex_shader_file, std::ios::in);
if(vertexShaderStream.is_open())
{
std::string Line = "";
while(getline(vertexShaderStream, Line))
vertexShaderCode += "\n" + Line;
vertexShaderStream.close();
}
else
return ERR_OPEN_VERTEX_FILE;
std::string fragmentShaderCode;
std::ifstream fragmentShaderStream(fragment_shader_file, std::ios::in);
if(fragmentShaderStream.is_open())
{
std::string Line = "";
while(getline(fragmentShaderStream, Line))
fragmentShaderCode += "\n" + Line;
fragmentShaderStream.close();
}
else
return ERR_OPEN_FRAGMENT_FILE;
GLint Result = GL_FALSE;
int InfoLogLength;
char const * VertexSourcePointer = vertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if ( InfoLogLength > 0 ){
std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
return ERR_INIT_VERTEX_CODE; // Here the code returns
}
char const * FragmentSourcePointer = fragmentShaderCode.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
glCompileShader(FragmentShaderID);
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if ( InfoLogLength > 0 ){
std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
return ERR_INIT_FRAGMENT_CODE;
}
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if ( InfoLogLength > 0 ){
std::vector<char> ProgramErrorMessage(InfoLogLength+1);
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
return ERR_LINK_PROGRAM;
}
glDetachShader(ProgramID, VertexShaderID);
glDetachShader(ProgramID, FragmentShaderID);
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
return ProgramID;
}
从glGetError()得到值1280。