int GenNormalizationCubeMap(unsigned int size, GLuint &texid)
{
glGenTextures(1, &texid);
glBindTexture(GL_TEXTURE_CUBE_MAP, texid);
unsigned char* data = new unsigned char[size*size*3];
float offset = 0.5f;
float halfSize = size * 0.5f;
vec3 temp;
unsigned int bytePtr = 0;
for(unsigned int j=0; j<size; j++)
{
for(unsigned int i=0; i<size; i++)
{
temp[0] = halfSize;
temp[1] = (j+offset-halfSize);
temp[2] = -(i+offset-halfSize);
scale_to_01(temp);
data[bytePtr] = (unsigned char)(temp[0] * 255.0f);
data[bytePtr+1] = (unsigned char)(temp[1] * 255.0f);
data[bytePtr+2] = (unsigned char)(temp[2] * 255.0f);
bytePtr+=3;
}
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X,
0, GL_RGB8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
bytePtr = 0;
for(j=0; j<size; j++)
{
for(unsigned int i=0; i<size; i++)
{
temp[0] = -halfSize;
temp[1] = (j+offset-halfSize);
temp[2] = (i+offset-halfSize);
scale_to_01(temp);
data[bytePtr] = (unsigned char)(temp[0] * 255.0f);
data[bytePtr+1] = (unsigned char)(temp[1] * 255.0f);
data[bytePtr+2] = (unsigned char)(temp[2] * 255.0f);
bytePtr+=3;
}
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
0, GL_RGB8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
bytePtr = 0;
for(j=0; j<size; j++)
{
for(unsigned int i=0; i<size; i++)
{
temp[0] = i+offset-halfSize;
temp[1] = -halfSize;
temp[2] = j+offset-halfSize;
scale_to_01(temp);
data[bytePtr] = (unsigned char)(temp[0] * 255.0f);
data[bytePtr+1] = (unsigned char)(temp[1] * 255.0f);
data[bytePtr+2] = (unsigned char)(temp[2] * 255.0f);
bytePtr+=3;
}
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
0, GL_RGB8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
bytePtr = 0;
for(j=0; j<size; j++)
{
for(unsigned int i=0; i<size; i++)
{
temp[0] = i+offset-halfSize;
temp[1] = halfSize;
temp[2] = -(j+offset-halfSize);
scale_to_01(temp);
data[bytePtr] = (unsigned char)(temp[0] * 255.0f);
data[bytePtr+1] = (unsigned char)(temp[1] * 255.0f);
data[bytePtr+2] = (unsigned char)(temp[2] * 255.0f);
bytePtr+=3;
}
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
0, GL_RGB8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
bytePtr = 0;
for(j=0; j<size; j++)
{
for(unsigned int i=0; i<size; i++)
{
temp[0] = i+offset-halfSize;
temp[1] = (j+offset-halfSize);
temp[2] = halfSize;
scale_to_01(temp);
data[bytePtr] = (unsigned char)(temp[0] * 255.0f);
data[bytePtr+1] = (unsigned char)(temp[1] * 255.0f);
data[bytePtr+2] = (unsigned char)(temp[2] * 255.0f);
bytePtr+=3;
}
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
0, GL_RGB8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
bytePtr = 0;
for(j=0; j<size; j++)
{
for(unsigned int i=0; i<size; i++)
{
temp[0] = -(i+offset-halfSize);
temp[1] = (j+offset-halfSize);
temp[2] = -halfSize;
scale_to_01(temp);
data[bytePtr] = (unsigned char)(temp[0] * 255.0f);
data[bytePtr+1] = (unsigned char)(temp[1] * 255.0f);
data[bytePtr+2] = (unsigned char)(temp[2] * 255.0f);
bytePtr+=3;
}
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
0, GL_RGB8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
delete [] data;
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
return true;
} |