Je suis en train de dessiner un rectangle simple dans mon jeu, mais je continue à courir dans la pause suivante: Erreur lors de la tentative d'utiliser glDrawArrays en C++
Voici le code dans mon Sprite.cpp où je lie et rendre les choses.
void Sprite::init(float x, float y, float width, float height){
this->x = x;
this->y = y;
this->width = width;
this->height = height;
if(vbo == 0)
glGenBuffers(1, &vbo);
Vertex vertex[6];
vertex[0].setPosition(x, y);
vertex[0].setUV(0, 0);
vertex[1].setPosition(x, y + height);
vertex[1].setUV(0, 1);
vertex[2].setPosition(x + width, y + height);
vertex[2].setUV(1, 1);
vertex[3].setPosition(x, y);
vertex[3].setUV(0, 0);
vertex[4].setPosition(x + width, y);
vertex[4].setUV(1, 0);
vertex[5].setPosition(x + width, y + height);
vertex[5].setUV(1, 1);
for (int i = 0; i < 6; i++)
vertex[i].setColor(0.0, 0.0, 1.0, 1.0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(vbo, sizeof(vertex), vertex, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Sprite::render(){
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
Si je commente la ligne "glDrawArrays (GL_TRIANGLES, 0, 6)" Ensuite, le programme fonctionnera parfaitement bien. Voici ma structure Vertex juste pour des informations supplémentaires:
#pragma once
#include <GL\glew.h>
struct Position {
float x, y;
};
struct UV{
float u, v;
};
struct Color {
GLubyte r, g, b, a;
};
struct Vertex {
Position position;
Color color;
UV uv;
void setUV(float u, float v) {
uv.u = u;
uv.v = v;
}
void setColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a) {
color.r = r;
color.g = g;
color.b = b;
color.a = a;
}
void setPosition(float x, float y) {
position.x = x;
position.y = y;
}
};
Je ne sais absolument pas pourquoi cela se produit. J'ai entendu cette erreur a à voir avec de-référence quelque null pointeur ou quelque chose. Je ne sais pas comment résoudre ce problème. Toute aide est extrêmement appréciée.
Je me déteste en ce moment ... Merci beaucoup. Je suppose que depuis que je travaille là-dessus depuis si longtemps, j'ai juste besoin d'une nouvelle paire d'yeux. Je vous remercie! – Luke
Lol ikr, nous avons tous fait et faisons toujours la même erreur à un moment donné! À votre santé. –