我终于得到了一个处理纹理的quad,为了清理我的代码,我决定把它移到一个单独的类中,但现在它消失了,没有任何错误消息。
主文件:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stb/stb_image.h>
#include <texture.h>
#include <window.h>
#include <shader.h>
#include <model.h>
#include <iostream>
GLfloat vertexes[]
{
0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0,
-0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0,
0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0,
-0.5, 0.5, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0,
-0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0
};
const int width = 1900;
const int height = 1200;
int main()
{
glfwInit();
window window(width, height, "Voxel Engine");
gladLoadGL();
glViewport(0, 0, width, height);
// shader
shader_program shader("src/shaders/vertex_shader.shader", "src/shaders/fragment_shader.shader");
model model1(vertexes);
/*// vao and vbo
unsigned int vbo, vao;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);*/
texture test_img("resources/test_img.png", GL_TEXTURE0, GL_RGB, GL_UNSIGNED_BYTE);
test_img.uniform_shader(shader, "tex_0", 0);
while (!glfwWindowShouldClose(window.glfw_window))
{
glfwPollEvents();
glClearColor(0.08, 0.12, 0.24, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
shader.use();
//glBindTexture(GL_TEXTURE_2D, texture1);
test_img.bind();
glBindVertexArray(model1.vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
window.swap_buffers();
}
glDeleteVertexArrays(1, &model1.vao);
glDeleteBuffers(1, &model1.vbo);
//glDeleteTextures(1, &texture1);
test_img.destroy();
shader.destroy();
window.destroy();
glfwTerminate();
return 0;
}
型号标题:
#ifndef MODEL_CLASS_H
#define MODEL_CLASS_H
#include <glad/glad.h>
class model
{
public:
unsigned int vao, vbo;
model(GLfloat vertexes[]);
void render();
void destroy();
};
#endif
最后,模型c++文件:
#include <model.h>
model::model(GLfloat vertexes[])
{
// vao and vbo
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
我发现由于某种原因,vbo和vao没有正确创建,但让我困惑的是,我直接从main.cpp文件中复制了代码,但现在它不起作用。