код в основном виде:
vao, vbo и ebo будут связаны при создании и в дальнейшем будут связаны с атрибутами, я не уверен, что именно настройка шагов или смещений вызывает эту проблему
Код: Выделить всё
int main(int argc, char** argv)
{
if (argc>=2) chdir(argv[1]);
ObjLoader obj = ObjLoader();
Mesh mesh = obj.load_obj_from_file(geo_texture_img, false, false, true);
glfwInit();
// set glfw's OpenGL to VERSION=3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// set glfw's to CORE profile (modern functions ONLY)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(window_width, window_height, extract_version(argv[0]), NULL, NULL);
// event handling
glfwSetErrorCallback(glfw_error_callback);
glfwSetWindowCloseCallback(window, glfw_window_close_callback);
glfwMakeContextCurrent(window);
//load GLAD to configures OpenGL
gladLoadGL();
// adjust viewport
glViewport(0, 0, window_width, window_height);
// Generates Shader object using shaders defualt.vert and default.frag
Shader shader_program = Shader(vert, frag);
// Generates Vertex Array Object and binds it
VAO vao;
VBO vbo(mesh.vertices);
EBO ebo(mesh.indices);
vao.link_attribute(vbo, 0, 3, GL_FLOAT, sizeof(Vertex), (void*)0);
vao.link_attribute(vbo, 1, 3, GL_FLOAT, sizeof(Vertex), (void*)(3 * sizeof(float)));
vao.link_attribute(vbo, 2, 2, GL_FLOAT, sizeof(Vertex), (void*)(6 * sizeof(float)));
vao.unbind();
vbo.unbind();
ebo.unbind();
//enable depth buffer
glEnable(GL_DEPTH_TEST);
// camera
float fov = glm::radians(45.0f), near = 0.1f, far = 1000.0f;
glm::vec3 eye_position = glm::vec3(0.0f, 0.0f, 150.0f), eye_direction = glm::vec3(0.0f, 0.0f, -1.0f);
Camera cam = Camera(eye_position, eye_direction, window_width, window_height, fov, near, far);
// texture
Texture grass_texture = Texture(grass_texture_img, shader_program.ID, "grass_texture");
//////////////////////////////////////////////////////////////////////////////////////////////////////
float rotation =45.0f;
while(!glfwWindowShouldClose(window)){
// Specify the color of the background
glClearColor(0.0f, 0.13f, 0.17f, 1.0f);
// Clean the back buffer and assign the new color to it
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Tell OpenGL which Shader Program we want to use
shader_program.activate();
// every assigning stuff go after activate
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, glm::radians(rotation), glm::vec3(1.0f, 0.0f, 0.0f));
int modelLoc = glGetUniformLocation(shader_program.ID, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
cam.setup_matrix(shader_program.ID, "proj_view");
//uniforms
glBindTexture(GL_TEXTURE_2D, grass_texture.uni_texture);
// Bind the VAO so OpenGL knows to use it
vao.bind();
// Draw the triangle using the GL_TRIANGLES primitive
glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_INT, 0);
// Swap the back buffer with the front buffer
glfwSwapBuffers(window);
// Take care of all GLFW events
glfwPollEvents();
// rotation++;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// delete everything
vao.remove();
vbo.remove();
ebo.remove();
grass_texture.remove();
shader_program.remove();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Код: Выделить всё
VAO::VAO()
{
glGenVertexArrays(1, &ID);
bind();
}
// Links a VBO to the VAO using a certain layout
void VAO::link_attribute(VBO& VBO, GLuint layout, GLuint numComponents, GLenum type, GLsizeiptr stride, void* offset)
{
VBO.bind();
glVertexAttribPointer(layout, numComponents, type, GL_FALSE, stride, offset);
glEnableVertexAttribArray(layout);
VBO.unbind();
}
// Binds the VAO
void VAO::bind()
{
glBindVertexArray(ID);
}
Код: Выделить всё
VBO::VBO(std::vector vertices){
glGenBuffers(1, &ID);
glBindBuffer(GL_ARRAY_BUFFER, ID);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
}
// Binds the VBO
void VBO::bind(){
glBindBuffer(GL_ARRAY_BUFFER, ID);
}
Код: Выделить всё
EBO::EBO(std::vector indices){
glGenBuffers(1, &ID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW);
}
// Binds the EBO
void EBO::bind(){
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ID);
}
Код: Выделить всё
struct Vertex
{
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texUV;
};
class Mesh{
public:
std::vector vertices;
std::vector indices;
Mesh(std::vector vertices, std::vector indices);
};
[
[img]https: //i.sstatic.net/bZLai3OU.png[/img]
]
Подробнее здесь: https://stackoverflow.com/questions/784 ... m-obj-file