1
\$\begingroup\$

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);
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Could be a memory issue when declaring / using _sprite. Make sure you allocate memory correctly for its class. Perhaps you could also post the relevant code for allocating memory. \$\endgroup\$ – Dan Sep 11 '14 at 2:52
  • \$\begingroup\$ @Dan My code for allocating the sprite is very simple. In the maingame class I simply put this line of code: Sprite _sprite[4]; \$\endgroup\$ – Anti Sep 11 '14 at 11:34
  • \$\begingroup\$ guess without seeing the Sprite class your constructor is not setting it up correctly. \$\endgroup\$ – Surt Sep 17 '14 at 14:59
  • \$\begingroup\$ Looks like an initialization error in one of the support functions or classes, could you show the constructor for Sprite and where vboid and eboid are initialized. \$\endgroup\$ – Surt Sep 18 '14 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.