我试图制作我自己的opengl引擎用于学习目的,我使用glfw3,glew和assimp。到目前为止,我可以从文件加载模型并在屏幕上显示它们。它还可以从文件中读取和编译着色器。当我尝试使用着色器绘制网格时,它会消失,但如果我不使用着色器,它就会很好地显示出来。我认为我的问题可能是当我将网格数据绑定到VBO和VAO时。它也可能是我加载它的方式
这是我的Shader加载函数:
<script src="http://code.jquery.com/jquery-1.12.1.min.js"></script>
<input type="text" id="textarea" placeholder="Write here..."/>
<button id="add">Add</button>
<div id="list"></div>
这是我的Vertex Shader程序:
Shader::Shader(string vertex_path, string fragment_path) {
Shader::success = false;
string vertex, fragment;
vertex = file_read_all(vertex_path);
fragment = file_read_all(fragment_path);
const GLchar *verCode = vertex.c_str();
const GLchar *fraCode = fragment.c_str();
GLuint verShader, fraShader;
GLint success;
GLchar log[512];
verShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(verShader, 1 , &verCode, NULL);
glCompileShader(verShader);
glGetShaderiv(verShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(verShader, 512, NULL, log);
cout << "Failed to Compile Vertex Shader " << vertex_path << "!" << endl << log;
return;
}
fraShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fraShader, 1, &fraCode, NULL);
glCompileShader(fraShader);
glGetShaderiv(fraShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fraShader, 512, NULL, log);
cout << "Failed to Compile Vertex Shader " << fragment_path << "!" << endl << log;
return;
}
Shader::shader_id = glCreateProgram();
glAttachShader(Shader::shader_id, verShader);
glAttachShader(Shader::shader_id, fraShader);
glLinkProgram(Shader::shader_id);
glGetShaderiv(Shader::shader_id, GL_LINK_STATUS, &success);
glDeleteShader(verShader);
glDeleteShader(fraShader);
if (!success) {
glGetShaderInfoLog(Shader::shader_id, 512, NULL, log);
cout << "Failed to Link Shaders " << vertex_path << " and " << fragment_path << "!" << endl << log;
return;
}
Shader::success = true;
}
这是我的功能,它将网格数据绑定到VBO&#39; s / VAO&#39>:
#version 330 core
layout (location = 0) in vec3 position;
uniform mat4 view;
void main()
{
gl_Position = view * vec4(position, 1.0f);
}
以下是绘制网格的代码:
void Mesh::bind(vector<float> vertex, vector<float> normal, vector<GLuint> indices) {
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glGenBuffers(1, &this->EBO);
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, vertex.size() * sizeof(float), &vertex[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertex.size() * sizeof(float), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, normal.size() * sizeof(float), 0);
glBindVertexArray(0);
}
这是我加载和处理网格的函数:
void Mesh::Draw() {
glBindVertexArray(this->VAO);
glDrawArrays(GL_TRIANGLES, 0, this->count);
}