Witam, stworzyłem klasę która mam rysować prostokąt i teraz chcę dodać możliwość załadowania tekstury. VS studio 2022 wyrzuca mi taki błąd jak poniżej:
Zgłoszono wyjątek w lokalizacji 0x00007FFF6B4AB568 (nvoglv64.dll) w terrain generation.exe: 0xC0000005: Naruszenie zasad dostępu podczas odczytywania w lokalizacji 0x0000029D1B8E8000.
Próbowałem do folderu z projektem dodać ten plik dll ale to nic nie dało. Zmieniałem też kolejność bindowania VAO bo czytałem, że to może powodować problem ale też nic się nie zmieniło. Co tu robię nie tak?
Tile.cpp
Tile::Tile()
{
float vertices[] = {
0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
position = glm::vec3(0.0f);
model = glm::mat4(1.0f);
current_angle = 0.0f;
vao = new VAO();
vao->Bind();
vbo = new VBO(vertices, sizeof(vertices));
ebo = new EBO(indices, sizeof(indices));
vao->LinkAttrib(vbo, 0, 3, GL_FLOAT, 5 * sizeof(float), (void*)0);
vao->LinkAttrib(vbo, 1, 2, GL_FLOAT, 5 * sizeof(float), (void*)(3 * sizeof(float)));
texture = new Texture("D:/Textures for OpenGL/grass.jpg", GL_TEXTURE_2D, GL_TEXTURE0, GL_RGBA, GL_UNSIGNED_BYTE);
texture->texUnit(*shaderProgram, "texture0", 0);
shaderProgram = new Shader("vertexShader.vert", "fragmentShader.frag");
}
void Tile::draw(glm::mat4 projection, glm::mat4 view)
{
shaderProgram->use();
shaderProgram->setMat4("model", model);
shaderProgram->setMat4("projection", projection);
shaderProgram->setMat4("view", view);
texture->Bind();
vao->Bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
Texture.cpp
Texture::Texture(const char* image, GLenum texType, GLenum slot, GLenum format, GLenum pixelType)
{
type = texType;
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char* bytes = stbi_load(image, &width, &height, &nrChannels, 0);
glGenTextures(1, &ID);
glActiveTexture(slot);
glBindTexture(texType, ID);
glTexParameteri(texType, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(texType, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(texType, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(texType, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(texType, 0, GL_RGB, width, height, 0, format, pixelType, bytes);
glGenerateMipmap(texType);
stbi_image_free(bytes);
}
void Texture::texUnit(Shader& shader, const char* uniform, GLuint unit)
{
shader.use();
shader.setInt(uniform, unit);
}
VAO.cpp
void VAO::LinkAttrib(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();
}