예제 #1
0
void GLTexture::refresh() {
	Bitmap *bitmap = getBitmap();

	/* Bind to the texture */
	glBindTexture(m_glType, m_id);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	if (m_type == ETexture1D) {
		Assert((math::isPowerOfTwo(m_size.x) && m_size.y == 1)
			|| (math::isPowerOfTwo(m_size.y) && m_size.x == 1));

		if (isMipMapped()) {
			/* Let GLU generate mipmaps for us */
			gluBuild1DMipmaps(m_glType, m_internalFormat, m_size.x == 1 ? m_size.y : m_size.x,
				m_format, m_dataFormat, bitmap->getData());
		} else {
		}
		glTexImage1D(m_glType, 0, m_internalFormat, m_size.x == 1 ? m_size.y : m_size.x,
			0, m_format, m_dataFormat, bitmap->getData());
	} else if (m_type == ETexture2D) {
		/* Anisotropic texture filtering */
		float anisotropy = (float) getMaxAnisotropy();
		if (isMipMapped() && m_filterType == EMipMapLinear && anisotropy > 1.0f)
			glTexParameterf(m_glType, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);

		glTexImage2D(m_glType, 0, m_internalFormat, m_size.x, m_size.y,
			0, m_format, m_dataFormat, bitmap->getData());
	} else if (m_type == ETextureCubeMap) {
		Assert(bitmap != NULL);
		Assert(bitmap->getWidth() == bitmap->getHeight());
		Assert(math::isPowerOfTwo(bitmap->getWidth()));
		if (isMipMapped())
			glTexParameteri(m_glType, GL_GENERATE_MIPMAP, GL_TRUE);

		for (int i=0; i<6; i++) {
			Bitmap *bitmap = getBitmap(i);

			GLuint pos;
			switch (i) {
				case ECubeMapPositiveX: pos = GL_TEXTURE_CUBE_MAP_POSITIVE_X; break;
				case ECubeMapNegativeX: pos = GL_TEXTURE_CUBE_MAP_NEGATIVE_X; break;
				case ECubeMapPositiveY: pos = GL_TEXTURE_CUBE_MAP_POSITIVE_Y; break;
				case ECubeMapNegativeY: pos = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y; break;
				case ECubeMapPositiveZ: pos = GL_TEXTURE_CUBE_MAP_POSITIVE_Z; break;
				case ECubeMapNegativeZ: pos = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; break;
				default: Log(EError, "Unknown cube map index"); return;
			};

			glTexImage2D(pos, 0, m_internalFormat, bitmap->getWidth(), bitmap->getHeight(),
				0, m_format, m_dataFormat, bitmap->getData());
		}
	} else {
		Log(EError, "Unknown texture type!");
	}
	if (isMipMapped())
		glGenerateMipmapEXT(m_glType);
}
예제 #2
0
void Utilities::setSampler(TextureType target, const TextureSampler& sampler) {
    clearError();
    GLenum target_gl;
    switch (target) {
        case TextureType::_2D:
            target_gl = GL_TEXTURE_2D;
            break;
        case TextureType::_1D:
            target_gl = GL_TEXTURE_1D;
            break;
        default:
            throw UnhandledSwitchCaseException(__FILE__, __LINE__);
    }
    if (isError()) {
        return;
    }

    GLint addressModeS_gl = toOpenGL(sampler.getAddressModeS()),
          addressModeT_gl = toOpenGL(sampler.getAddressModeT());
    glTexParameteri(target_gl, GL_TEXTURE_WRAP_S, addressModeS_gl);
    glTexParameteri(target_gl, GL_TEXTURE_WRAP_T, addressModeT_gl);
    if (isError()) {
        return;
    }

    GLint minFilter_gl, magFilter_gl;
    toOpenGL(sampler.getMinFilter(), sampler.getMagFilter(), sampler.getMipMapFilter(),
             minFilter_gl, magFilter_gl);
    glTexParameteri(target_gl, GL_TEXTURE_MIN_FILTER, minFilter_gl);
    glTexParameteri(target_gl, GL_TEXTURE_MAG_FILTER, magFilter_gl);
    if (isError()) {
        return;
    }

    if (TextureType::_2D == target) {
        auto extensions = getExtensions();
        if (extensions.cend() != extensions.find(anisotropyExtension)) {
            float anisotropyLevel = Ego::Math::constrain(sampler.getAnisotropyLevel(), getMinAnisotropy(), getMaxAnisotropy());
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropyLevel);
            if (isError()) {
                return;
            }
        }
    }
}