Skip to content

Instantly share code, notes, and snippets.

@orlp

orlp/load.cpp Secret

Created October 7, 2015 05:07
Show Gist options
  • Save orlp/4c48ea637ece3a5d28f2 to your computer and use it in GitHub Desktop.
Save orlp/4c48ea637ece3a5d28f2 to your computer and use it in GitHub Desktop.
GLuint load_texture(std::istream& in) {
std::string str;
in >> str;
if (str != "P3") throw std::runtime_error("Input file must be plain PPM.\n");
int width, height;
in >> width >> height;
if (width == 0 || height == 0) throw std::runtime_error("Input file empty or invalid.\n");
int pixel_depth;
in >> pixel_depth;
if (pixel_depth != 255) throw std::runtime_error("Input file must be plain PPM.\n");
std::cout << width << " " << height << "\n";
std::vector<GLubyte> data;
data.reserve(width * height * 3);
for (int i = 0; i < (width * height * 3); ++i) {
int pixel = 0;
in >> pixel;
data.push_back(pixel);
}
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, &data[0]);
return tex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment