I'm making a simple program with C++, SDL and GLEW. So far it is going great but I ran into a weird problem. One of my four textures would not show up on screen even though it used the same code as the other three. EXAMPLE: https://www.dropbox.com/s/4038p0yv041mhdy/Sk%C3%A4rmdump%202014-09-10%2021.37.48.png?dl=0 HOW IT SHOULD BE: https://www.dropbox.com/s/huhln17zjip1pjb/Sk%C3%A4rmdump%202014-09-10%2021.39.04.png?dl=0
But the weird thing is how I fixed it. NOT WORKING:
_sprite[0].init(0.0f, 0.0f, 1.0f, 1.0f, "kitten.png");
_sprite[1].init(-1.0f, -1.0f, 1.0f, 1.0f, "kitten1.png");
_sprite[2].init(0.0f, -1.0f, 1.0f, 1.0f, "kitten1.png");
_sprite[3].init(-1.0f, 0.0f, 1.0f, 1.0f, "kitten.png");
WORKING:
_sprite[1].init(0.0f, 0.0f, 1.0f, 1.0f, "kitten.png");//Switched around the 0 and the 1
_sprite[0].init(-1.0f, -1.0f, 1.0f, 1.0f, "kitten1.png");
_sprite[2].init(0.0f, -1.0f, 1.0f, 1.0f, "kitten1.png");
_sprite[3].init(-1.0f, 0.0f, 1.0f, 1.0f, "kitten.png");
I only changed the index when the init function was called and all of a sudden it started to work again. So my question is: Why does the texture not display when I'm using index 1 but working when I'm using index 0.
Nothing happened when I only changed the draw function.
INIT FUNCTION:
void Sprite::init(float x, float y, float w, float h, std::string image) {
//Loads image, initilaze the varibles and generate the Vertex Buffer Object(VBO)
_texture = ResourceManager::getTexture(image);
_x = x;
_y = y;
_w = w;
_h = h;
if(_vboid == 0) {
glGenBuffers(1, &_vboid);
}
if(_eboid == 0) {
glGenBuffers(1, &_eboid);
}
//The vertex data
Vertex vertexData[4];
//Specify which vertices to reuse
GLuint elementdata[6] = {
0, 1, 2,
2, 3, 0
};
//Top right corner position
vertexData[0].setPosition(_x + _w, _y + _h);
vertexData[0].setTexCoord(1, 0);
//Top left corner position
vertexData[1].setPosition(_x, _y + _h);
vertexData[1].setTexCoord(0, 0);
//Bottom left corner position
vertexData[2].setPosition(_x, _y);
vertexData[2].setTexCoord(0, 1);
//Bottom right corner position
vertexData[3].setPosition(_x + _w, _y);
vertexData[3].setTexCoord(1, 1);
for(int i = 0; i < 4; i++) {
vertexData[i].setColor(255, 255, 255, 255);
}
//Let's set some special color
//Binds the buffer, then upload data to the GPU and the last thing it does is unbind the buffer
glBindBuffer(GL_ARRAY_BUFFER, _vboid);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _eboid);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementdata), elementdata, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}