PDA

View Full Version : Texture artifacts and std::map crash



Zylzyl
01-02-2014, 06:07 PM
I iterate an std::map to load textures into OGL, but it does crash after the end of an iteration and before the start of the next. It occurs after the same iteration every time for specific contents of the map, regardless of what element would come next. The problem does not occur without the code inside the loop, so the error likely happens within the loop.


// ...

typedef std::list<RenderObject*> ObjectList;

template <typename T> class Scene
{
private:
typedef MipMap<T> Tex;
typedef std::map<std::string, Tex*, StringCompareCaseInsensitive> TexMap;
typedef std::map<std::string, GLuint, StringCompareCaseInsensitive> GLTexMap;
typedef std::pair<std::string, GLuint> GLTexMapPair;

public:

// ...

void initToGL() {
std::cout << "Soft iteration:" << std::endl; // This iteration works fine
for (TexMap::iterator it = textures.begin(); it != textures.end(); it++) {
std::cout << "Texture: " << it->first << std::endl;
}

system("@pause>nul");

for (TexMap::iterator it = textures.begin(); it != textures.end(); it++) { // Crash right here
std::cout << "Making texture " << std::flush;
std::cout << it->first << "..." << std::endl;

MipMap<T>* mipMap = it->second;
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

std::cout << "Generated and bound GL textures." << std::endl;

// Make pure RGB texture from texture with palette
RGBTexture tex(mipMap->getWidth(0), mipMap->getHeight(0));
T* topMap = mipMap->getMipMap(0);
for (UINT x = 0; x < mipMap->getWidth(0); x++) {
for (UINT y = 0; y < mipMap->getHeight(0); y++) {
tex.setColorAt(x, y, topMap->getColorAt(x, y));
}
}

std::cout << "Built RGBTexture with dimensions " << tex.getWidth() << "x" << tex.getHeight() << std::endl;

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, tex.getWidth(), tex.getHeight(),
GL_RGB, GL_UNSIGNED_BYTE, (void*) tex.getRawData());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

std::cout << "Generated mipmaps." << std::endl;

glBindTexture(GL_TEXTURE_2D, 0);

std::cout << "Unbound texture." << std::endl;

glTextureList.emplace(GLTexMapPair(it->first, texture));

std::cout << "Emplaced texture." << std::endl;
}
std::cout << "Made textures." << std::endl;
for (ObjectList::iterator it = objectList.begin(); it != objectList.end(); it++) {
(*it)->initToGL();
}
}

// ...

private:
TexMap textures;
GLTexMap glTextureList;
ObjectList objectList;
UINT frameCount;
Camera* cam;
};

The code works with certain (little) std::map contents, but will sometimes give me rendering artifacts (rainbow stripes):

http://img30.imageshack.us/img30/4610/jyp3.png

Any ideas, or do I have to show more code?

Zylzyl
01-02-2014, 08:05 PM
Fixed this. Was at some point multiplying with height instead of width. :whistle: