I finally got a quad working with textures, and to clean up my code, I decided to move it into a separate class, but now it disappeared without any error message.
Main file:
#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;
}
Model header:
#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
and finally, Model c++ file:
#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);
}
I found that for some reason the vbo and vao werent being created correctly, but what confused me was that I directly copied the code from my main.cpp file, yet now it doesnt work.
The error is here
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
In your new code vertexes
is a pointer. It is not possible to have an array parameter in C++, it is always converted to a pointer instead. Therefore sizeof(vertexes)
returns the size of the pointer, not the size of the original array. Pass the size in as a separate parameter.
model::model(GLfloat* vertexes, size_t vertex_size)
{
...
glBufferData(GL_ARRAY_BUFFER, vertex_size, vertexes, GL_STATIC_DRAW);
...
}