static void R_LoadHeightmap(const char *name, const SDL_Surface *surf) { char heightmap[MAX_QPATH]; g_strlcpy(heightmap, name, sizeof(heightmap)); char *c = strrchr(heightmap, '_'); if (c) { *c = '\0'; } SDL_Surface *hsurf; if (Img_LoadImage(va("%s_h", heightmap), &hsurf)) { if (hsurf->w == surf->w && hsurf->h == surf->h) { Com_Debug("Merging heightmap %s\n", heightmap); byte *in = hsurf->pixels; byte *out = surf->pixels; const size_t len = surf->w * surf->h; for (size_t i = 0; i < len; i++, in += 4, out += 4) { out[3] = (in[0] + in[1] + in[2]) / 3.0; } } else { Com_Warn("Incorrect heightmap resolution for %s\n", name); } SDL_FreeSurface(hsurf); } }
/** * @brief */ static void R_SetWindowIcon(void) { SDL_Surface *surf; if (!Img_LoadImage("icons/quetoo", &surf)) { return; } SDL_SetWindowIcon(r_context.window, surf); SDL_FreeSurface(surf); }
/** * @brief */ void CalcTextureReflectivity(void) { char path[MAX_OS_PATH]; int32_t i, j, texels; uint32_t color[3]; SDL_Surface *surf; // always set index 0 even if no textures VectorSet(texture_reflectivity[0], 0.5, 0.5, 0.5); for (i = 0; i < bsp_file.num_texinfo; i++) { // see if an earlier texinfo already got the value for (j = 0; j < i; j++) { if (!g_strcmp0(bsp_file.texinfo[i].texture, bsp_file.texinfo[j].texture)) { VectorCopy(texture_reflectivity[j], texture_reflectivity[i]); break; } } if (j != i) { // earlier texinfo found, continue continue; } // load the image to calculate reflectivity g_snprintf(path, sizeof(path), "textures/%s", bsp_file.texinfo[i].texture); if (!Img_LoadImage(path, &surf)) { Com_Warn("Couldn't load %s\n", path); VectorSet(texture_reflectivity[i], 0.5, 0.5, 0.5); continue; } // calculate average color texels = surf->w * surf->h; color[0] = color[1] = color[2] = 0; for (j = 0; j < texels; j++) { const byte *pos = (byte *) surf->pixels + j * 4; color[0] += *pos++; // r color[1] += *pos++; // g color[2] += *pos++; // b } Com_Verbose("Loaded %s (%dx%d)\n", bsp_file.texinfo[i].texture, surf->w, surf->h); SDL_FreeSurface(surf); for (j = 0; j < 3; j++) { const vec_t r = color[j] / texels / 255.0; texture_reflectivity[i][j] = r; } } }
/** * @brief Calculates the texture color that is used for light emitting surfaces */ void CalcTextureReflectivity (void) { int i, j, texels = 0; char path[MAX_QPATH]; int color[3]; SDL_Surface* surf; /* always set index 0 even if no textures */ VectorSet(texture_reflectivity[0], 0.5, 0.5, 0.5); VectorSet(color, 0, 0, 0); for (i = 0; i < curTile->numtexinfo; i++) { /* see if an earlier texinfo already got the value */ for (j = 0; j < i; j++) { if (Q_streq(curTile->texinfo[i].texture, curTile->texinfo[j].texture)) { VectorCopy(texture_reflectivity[j], texture_reflectivity[i]); break; } } if (j != i) /* earlier texinfo found, continue */ continue; /* load the image file */ Com_sprintf(path, sizeof(path), "textures/%s", curTile->texinfo[i].texture); if (!(surf = Img_LoadImage(path))) { Verb_Printf(VERB_NORMAL, "Couldn't load %s\n", path); VectorSet(texture_reflectivity[i], 0.5, 0.5, 0.5); continue; } /* calculate average color */ texels = surf->w * surf->h; color[0] = color[1] = color[2] = 0; for(j = 0; j < texels; j++){ const byte* pos = (byte*)surf->pixels + j * 4; color[0] += *pos++; /* r */ color[1] += *pos++; /* g */ color[2] += *pos++; /* b */ } Verb_Printf(VERB_EXTRA, "Loaded %s (%dx%d)\n", curTile->texinfo[i].texture, surf->w, surf->h); SDL_FreeSurface(surf); for(j = 0; j < 3; j++){ const float r = color[j] / texels / 255.0; texture_reflectivity[i][j] = r; } } }
/* * @brief Loads the image by the specified name. */ r_image_t *R_LoadImage(const char *name, r_image_type_t type) { r_image_t *image; char key[MAX_QPATH]; if (!name || !name[0]) { Com_Error(ERR_DROP, "NULL name\n"); } StripExtension(name, key); if (!(image = (r_image_t *) R_FindMedia(key))) { SDL_Surface *surf; if (Img_LoadImage(key, &surf)) { // attempt to load the image image = (r_image_t *) R_AllocMedia(key, sizeof(r_image_t)); image->media.Retain = R_RetainImage; image->media.Free = R_FreeImage; image->width = surf->w; image->height = surf->h; image->type = type; if (image->type == IT_NORMALMAP) { R_LoadHeightmap(name, surf); } if (image->type & IT_MASK_FILTER) { R_FilterImage(image, GL_RGBA, surf->pixels); } R_UploadImage(image, GL_RGBA, surf->pixels); SDL_FreeSurface(surf); } else { Com_Debug("Couldn't load %s\n", key); image = r_image_state.null; } } return image; }