void CubemapStore::Load(const std::string& name, const RawImage& img, GLuint level /*= 0*/)
{
    CubemapDescription cubemap;
    glGenTextures(1, &cubemap.id);
    glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap.id);

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    // Calc image params
    int stride = img.Width();
    int width = img.Width() / 4;
    int height = img.Height() / 3;
    int channels = img.Channels();

    // Set row read stride
    glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);

    // Upload image data
    for (int i = 0; i < 6; ++i)
    {
        int xoff, yoff;
        int target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
        crossFaceOffset(target, &xoff, &yoff, stride);
        glTexImage2D(
            target, level, channels == 3 ? GL_RGB : GL_RGBA,
            width, height,
            0, channels == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE,
            img.Data() + (yoff * stride + xoff) * channels);
    }

    // Reset row stride
    glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);

    glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

    mCubemaps.insert({name, cubemap});
}
Beispiel #2
0
bool SaveRawImageToFile(const RawImage& img, const char* filename)
{
	const int w = img.Width();
	const int h = img.Height();
	const int bpp = img.BytesPerPixel();

	const ScopedFilePointer fp(filename, "wb");
	if (!fp)
	{
		return false;
	}

	const void* p = img.Bits();
	if (!p)
	{
		return false;
	}

	fwrite(p, w * h * bpp, 1, fp);
	return true;

}