-
-
Save orlp/4c48ea637ece3a5d28f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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