-1

while doing a opengl project i go to compile my program and it stops and produces a breakpoint in my code

CSkybox::CSkybox()
{
    for (int idx = 0; idx < 6; idx++)
        m_textures[idx] = 0;
}

CSkybox::~CSkybox()
{
    Release();
}


void CSkybox::Initialize(float size)
{
    m_size = size;
}


bool CSkybox::LoadTextures(char* top, char* bottom, char* front, char* back, char* left, char* right)
{
    CTargaImage image;

    image.Load(top);
    glGenTextures(1, &m_textures[SKY_TOP]);
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_TOP]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
    image.Release();

    image.Load(bottom);
    glGenTextures(1, &m_textures[SKY_BOTTOM]);
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_BOTTOM]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
    image.Release();

    image.Load(front);
    glGenTextures(1, &m_textures[SKY_FRONT]);
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_FRONT]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
    image.Release();

    image.Load(back);
    glGenTextures(1, &m_textures[SKY_BACK]);
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_BACK]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
    image.Release();

    image.Load(left);
    glGenTextures(1, &m_textures[SKY_LEFT]);
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_LEFT]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
    image.Release();

    image.Load(right);
    glGenTextures(1, &m_textures[SKY_RIGHT]);
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_RIGHT]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
    image.Release();

    return true;
}


void CSkybox::Render(float cameraX, float cameraY, float cameraZ)
{
    glPushMatrix();

    // Move the skybox so that it's centered on the camera.
    glTranslatef(cameraX, cameraY, cameraZ);

    glPushAttrib(GL_FOG_BIT | GL_DEPTH_BUFFER_BIT | GL_LIGHTING_BIT);
    glDisable(GL_DEPTH_TEST);

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    // Top
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_TOP]);
    glBegin(GL_QUADS);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-m_size, m_size, -m_size);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(m_size, m_size, -m_size);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(m_size, m_size, m_size);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-m_size, m_size, m_size);
    glEnd();

    // Bottom
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_BOTTOM]);
    glBegin(GL_QUADS);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(m_size, -m_size, -m_size);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-m_size, -m_size, -m_size);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-m_size, -m_size, m_size);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(m_size, -m_size, m_size);
    glEnd();

    // Front
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_FRONT]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-m_size, -m_size, -m_size);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(m_size, -m_size, -m_size);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(m_size, m_size, -m_size);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-m_size, m_size, -m_size);
    glEnd();

    // Back
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_BACK]);
    glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(m_size, -m_size, m_size);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-m_size, -m_size, m_size);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-m_size, m_size, m_size);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(m_size, m_size, m_size);
    glEnd();

    // Right
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_RIGHT]);
    glBegin(GL_QUADS);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(m_size, -m_size, m_size);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(m_size, m_size, m_size);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(m_size, m_size, -m_size);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(m_size, -m_size, -m_size);
    glEnd();

    // Left
    glBindTexture(GL_TEXTURE_2D, m_textures[SKY_LEFT]);
    glBegin(GL_QUADS);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-m_size, -m_size, -m_size);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-m_size, m_size, -m_size); 
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-m_size, m_size, m_size);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-m_size, -m_size, m_size);
    glEnd();

    glPopAttrib();
    glEndList();
    glPopMatrix();
}


void CSkybox::Release()
{
    for (int i = 0; i < 6; ++i)
        glDeleteTextures(6, &m_textures[0]);
}
GManNickG
  • 494,350
  • 52
  • 494
  • 543
rich
  • 33
  • 1
  • 1
  • 4
  • This is a debugger error. What's the expression you're trying? What's the actual problem? This question is unanswerable. – Cat Plus Plus Mar 30 '11 at 00:08
  • m_textures is used to call the sides of my skybox – rich Mar 30 '11 at 00:14
  • @rich: Show us the entire `CSkybox` class definition. – GManNickG Mar 30 '11 at 00:14
  • You're still missing the definition of `m_textures`... Also, your destructor logic is wrong, you probably mean to use `i` instead of `0`. Also, you're breaking the [rule of three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – GManNickG Mar 30 '11 at 00:26
  • Aside disable depth test you should also disable depth writes, otherwise you mess up the rendering of other parts of the scene. Now a small trick to properly align the skybox with the scene without that camera depending translation: GLfloat mvm[16]; glMatrixMode(GL_MODELVIEW); glGetFloatv(GL_MODELVIEW_MATRIX, mvm); mvm[3] = mvm[7] = mvm[11] = mvm[12] = mvm[13] = mvm[14] = 0.; glPushMatrix(); glLoadMatrixfv(mvm); /*draw skybox*/ glPopMatrix(). What this does is, it removes all but the rotational part from the matrix; for the skybox you only want the rotation. – datenwolf Mar 30 '11 at 07:26
  • @rich: How are SKY_TOP, SKY_LEFT, SKY_... defined? Did you use a enum, #define, const int or something else? Which values are they? – datenwolf Mar 30 '11 at 07:29

2 Answers2

3

The Release() method is doing the delete of all six textures six times:

  for (int i = 0; i < 6; ++i)
        glDeleteTextures(6, &m_textures[0]);

Should be either:

 for (int i = 0; i < 6; ++i)
        glDeleteTextures(1, &m_textures[i]);

or

  glDeleteTextures(6, &m_textures[0]);

Also, you do not show it, but SKY_ need to be in the range [0..5], not e.g. [1..6].

I'd also be looking at whether the images load correctly and have error handling for that.

Keith
  • 6,756
  • 19
  • 23
1

Looks like Array Bounds Overflow. m_textures i suppose is an array in which the generated texture names are to be stored.

Sadique
  • 22,572
  • 7
  • 65
  • 91