// initialize the textures void init_textures(Scene* scene, ShadeState* state) { // YOUR CODE GOES HERE --------------------- // grab textures from scene auto texture_vector = get_textures(scene); GLuint tex_id; // foreach texture for (auto tex_ : texture_vector) { // if already in the state->gl_texture_id map, skip auto txt = state->gl_texture_id.find(tex_); if (txt->first == tex_) continue; // gen texture id glGenTextures(1, &tex_id); // set id to the state->gl_texture_id map for later use // If the texture is not in the map, we have to add it: so we insert the // tuple made by <image3f, id> of the current texture state->gl_texture_id.insert(pair<image3f*, int>(tex_, tex_id)); // bind texture glBindTexture(GL_TEXTURE_2D, tex_id); // set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // load texture data glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_->width(), tex_->height(), 0, GL_RGB, GL_FLOAT, tex_->data()); glGenerateMipmap(GL_TEXTURE_2D); } }
// initialize the textures void init_textures(Scene* scene, ShadeState* state) { // grab textures from scene auto textures = get_textures(scene); // foreach texture for(auto texture : textures) { // if already in the state->gl_texture_id map, skip if(state->gl_texture_id.find(texture) != state->gl_texture_id.end()) continue; // gen texture id unsigned int id = 0; glGenTextures(1, &id); // set id to the state->gl_texture_id map for later use state->gl_texture_id[texture] = id; // bind texture glBindTexture(GL_TEXTURE_2D, id); // set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // load texture data glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->width(), texture->height(), 0, GL_RGB, GL_FLOAT, texture->data()); } }
u64 load_pixel_frame(u64 texture, int x, int y, int x2, int y2){ texture_info texinfo; if(!try_get_texture_info(texture, &texinfo)){ char namebuf[100]; get_textures(texture, namebuf, sizeof(namebuf)); stbi_info(namebuf, &texinfo.width, &texinfo.height, &texinfo.comp); set_texture_info(texture, texinfo); } vec2 size = vec2_new(texinfo.width, texinfo.height); texture_section sec = {.uv_offset = vec2_div(vec2_new(x, y), size), .uv_size = vec2_div(vec2_new(x2 - x, y2 - y), size), .render_offset = vec2_new(0,0), .pixel_size = vec2_new(x2 - x, y2 - y)}; //sec.uv_offset.y = 1.0 - sec.uv_offset.y; add_texture_sections(texture, sec); return 0; } u64 load_pixel_frame_center_of_mass(u64 texture, int x, int y, int x2, int y2, int cx, int cy){ texture_info texinfo; if(!try_get_texture_info(texture, &texinfo)){ char namebuf[100]; get_textures(texture, namebuf, sizeof(namebuf)); stbi_info(namebuf, &texinfo.width, &texinfo.height, &texinfo.comp); set_texture_info(texture, texinfo); } vec2 size = vec2_new(texinfo.width, texinfo.height); int center_x = (x2 + x) / 2, center_y = (y2 + y) / 2; texture_section sec = {.uv_offset = vec2_div(vec2_new(x, y), size), .uv_size = vec2_div(vec2_new(x2 - x, y2 - y), size), .render_offset = vec2_new(center_x - cx, center_y - cy), .pixel_size = vec2_new(x2 - x, y2 - y)}; add_texture_sections(texture, sec); return 0; } CREATE_TABLE2(animation, u64, animation_state); struct{ u32 count; i32 * texture; u64 * texid; }loaded_textures; i32 get_animation_gltexture(u64 tex){ u64 * id = memmem(loaded_textures.texid, loaded_textures.count * sizeof(u64), &tex, sizeof(u64)); if(id != NULL){ ASSERT(*id == tex); return loaded_textures.texture[id - loaded_textures.texid]; } char buffer[100]; get_textures(tex, buffer, sizeof(buffer)); i32 newtex = load_gl_texture(buffer); list_push(loaded_textures.texture, loaded_textures.count, newtex); list_push2(loaded_textures.texid, loaded_textures.count, tex); return newtex; }