boost::optional< PowerOf2 > makePowerOf2(uint64_t n) { uint64_t x = n; if (x == 0) return boost::optional< PowerOf2 >(); while (x % 2 == 0) x /= 2; if (x > 1) return boost::optional< PowerOf2 >(); else return boost::optional< PowerOf2 >(PowerOf2(n)); }
T NearestPowerOf2(T val) { if (val < 1) { return 1; } // Find position of the most significant bit T k = BinaryLogarithm(val); // Return result depending on the next bit if (!Algo::Bits::IsBitSet(val, T(k - 1))) { return PowerOf2(k); } // Check overflow if (k + 1 == (sizeof(T) << 3)) { throw OverflowException(SAF_SOURCE_LOCATION, Text::String("Overflow.")); } return PowerOf2(k + 1); }
void initTextures() { gliGenericImage *image[num_textures]; int n=0; image[n++] = readImage("/Users/nate/school/3600/Maze/Maze/hedge.tga"); image[n++] = readImage("/Users/nate/school/3600/Maze/Maze/snow.tga"); image[n++] = readImage("/Users/nate/school/3600/Maze/Maze/sky.tga"); image[n++] = readImage("/Users/nate/school/3600/Maze/Maze/johnny.tga"); if(n!=num_textures) { printf("Error: Wrong number of textures\n"); exit(1);; } glGenTextures(num_textures, texName); for(int i=0; i<num_textures; i++) { glBindTexture(GL_TEXTURE_2D, texName[i]); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); int repeats = i == 1 || i == 3; int needs_border = false; // i == 3; // Needed if clamping and not filling the whole polygon. if(repeats) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); } else { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); } if(needs_border) { // set a border. SetBorder(image[i]); } bool mipmaps = false; if(!PowerOf2(image[i]->height) || !PowerOf2(image[i]->width)) { // WARNING: Images that do not have width and height as // powers of 2 MUST use mipmaps. mipmaps = true; } if (mipmaps) { gluBuild2DMipmaps(GL_TEXTURE_2D, image[i]->components, image[i]->width, image[i]->height, image[i]->format, GL_UNSIGNED_BYTE, image[i]->pixels); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, //GL_LINEAR_MIPMAP_LINEAR); GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, //GL_LINEAR); GL_NEAREST); } else { glTexImage2D(GL_TEXTURE_2D, 0, image[i]->components, image[i]->width, image[i]->height, 0, image[i]->format, GL_UNSIGNED_BYTE, image[i]->pixels); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } } }