void StelQGLTextureBackend::loadFromPVR()
{
	invariant();
	//NOTE: We ignore bind options (mipmapping and filtering type) from params
	//      at the moment.
	//
	//      We also ignore maximum texture size (QGLContext::bindTexture should 
	//      fail if the texture is too large.
	//
	//      QGLContext::bindTexture handles gl texture format for us.
	QGLContext* context = prepareContextForLoading();
	glTextureID = context->bindTexture(path);
	if(glTextureID == 0)
	{
		errorOccured("loadFromPVR() failed to load a PVR file to a GL texture - "
		             "Maybe the file \"" + path + "\" is missing?");
		invariant();
		return;
	}
	// For some reason only LINEAR seems to work.
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	setTextureWrapping();

	// This is an assumption; PVRTC can be either 4 or 2 bits per pixel - we assume the worse case.
	pixelBytes = 0.5f;

	completeLoading();
	invariant();
}
示例#2
0
文件: texture.cpp 项目: cgaebel/gluon
bool Texture::load( const QUrl& url )
{
#ifdef __GNUC__
#warning Todo: Add support for non-2D textures and non-RGBA colour formats. Also, find a way \
around the nasty const_cast .
#endif

    if( d->glTexture )
        return true;

    if( !QGLContext::currentContext() || !QGLContext::currentContext()->isValid() )
        return false;

    QImage image;
    image.load( url.toLocalFile() );

    QGLContext* context = const_cast<QGLContext*>( QGLContext::currentContext() );
    d->glTexture = context->bindTexture( image );

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
#ifndef GLUON_GRAPHICS_GLES
    glTexParameterf( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE );
#endif

    if( d->glTexture != 0 )
        return true;

    return false;
}
示例#3
0
void CButton::loadTextures(const char *normalTex, const char *selectedTex, const char *pressedTex)
{
    QGLContext *context = const_cast<QGLContext*>(QGLContext::currentContext());

    QImage img;

    img.load(normalTex);
    normalId=context->bindTexture(img);

    img.load(selectedTex);
    selectedId=context->bindTexture(img);

    img.load(pressedTex);
    pressedId=context->bindTexture(img);

    setWindowSize( img.width(), img.height() );
}
示例#4
0
void CTexture::loadFromFile(const char *fileName)
{
    QGLContext *context = const_cast<QGLContext*>(QGLContext::currentContext());
    QImage img;
    img.load(fileName);
    id=context->bindTexture(img);

    width = img.width();
    height = img.height();
}
void HostWindowDataOpenGL::initializePixmap(QPixmap& screenPixmap)
{
	QGLContext* gc = (QGLContext*) QGLContext::currentContext();
	if (gc) {
		if (m_textureId)
			gc->deleteTexture(m_textureId);

		screenPixmap = QPixmap(m_width, m_height);
        screenPixmap.fill(QColor(255,255,255,0));
		m_textureId = gc->bindTexture(screenPixmap, GL_TEXTURE_2D, kGLInternalFormat,
									  QGLContext::PremultipliedAlphaBindOption);
	}
	m_dirty = true;
}
//Image struct is copied (data is not - implicit sharing), so that
//ensureTextureSizeWithinLimits won't affect the original image.
void StelQGLTextureBackend::loadFromImage(QImage image)
{
	invariant();

	if(image.isNull())
	{
		errorOccured("loadFromImage(): Image data to load from is null. "
		             "Maybe the image failed to load from file?");
		invariant();
		return;
	}

	// Shrink if needed.
	glEnsureTextureSizeWithinLimits(image);

	if(!renderer->areNonPowerOfTwoTexturesSupported() && 
		(!StelUtils::isPowerOfTwo(image.width()) ||
		 !StelUtils::isPowerOfTwo(image.height())))
	{
		errorOccured("loadFromImage(): Failed to load because the image has "
		             "non-power-of-two width and/or height while the renderer "
		             "backend only supports power-of-two dimensions");
		invariant();
		return;
	}

	GLint internalFormat = glGetTextureInternalFormat(image);
	switch(internalFormat)
	{
		case GL_RGBA8:             pixelBytes = 4.0f; break;
		case GL_RGB8:              pixelBytes = 3.0f; break;
		case GL_LUMINANCE8_ALPHA8: pixelBytes = 2.0f; break;
		case GL_LUMINANCE8:        pixelBytes = 1.0f; break;
		default: Q_ASSERT_X(false, Q_FUNC_INFO, "Unknown GL internal format for QImage");
	}

	QGLContext* context = prepareContextForLoading();
	glTextureID = context->bindTexture(image, GL_TEXTURE_2D, internalFormat,
	                                   getTextureBindOptions(textureParams));
	if(glTextureID == 0)
	{
		errorOccured("loadFromImage(): Failed to load an image to a GL texture");
		invariant();
		return;
	}
	setTextureWrapping();

	completeLoading();                       
	invariant();
}
示例#7
0
文件: cwater.cpp 项目: Tiikara/RTS-3D
void CWater::initializeGL()
{
    noise.gen(time(NULL));

    water_shader.addShaderFromSourceFile(QGLShader::Vertex,":/shaders/water.vert");
    water_shader.addShaderFromSourceFile(QGLShader::Fragment,":/shaders/water.frag");
    water_shader.link();
    water_shader.bind();
    water_shader.setUniformValue("sky_map",0);
    water_shader.setUniformValue("noise_table",1);
    water_shader.release();

    QImage img("tex/sky.jpg");
    QGLContext *context = const_cast<QGLContext*>(QGLContext::currentContext());
    sky_tex=context->bindTexture(img);
}
QPixmap* HostWindowDataOpenGL::acquirePixmap(QPixmap& screenPixmap)
{
	if (m_dirty) {
		m_dirty = false;
    
		m_ipcBuffer->lock();

		QGLContext* gc = (QGLContext*) QGLContext::currentContext();
		if (gc) {
            QImage data((uchar*) m_ipcBuffer->data(), m_width, m_height, QImage::Format_ARGB32_Premultiplied);
            screenPixmap = QPixmap::fromImage(data);
            // FIXME: is it really necessary to bind it here? Qt will bind it through
            // QGL2PaintEngineEx::drawPixmap -> QGLContextPrivate::bindTexture anyways
            m_textureId = gc->bindTexture(screenPixmap, GL_TEXTURE_2D, kGLInternalFormat,
                                          QGLContext::PremultipliedAlphaBindOption);
		}

		m_ipcBuffer->unlock();
	}

	return &screenPixmap;
}
static PyObject *meth_QGLContext_bindTexture(PyObject *sipSelf, PyObject *sipArgs, PyObject *sipKwds)
{
    PyObject *sipParseErr = NULL;

    {
        const QImage* a0;
        GLenum a1 = GL_TEXTURE_2D;
        GLint a2 = GL_RGBA;
        QGLContext *sipCpp;

        static const char *sipKwdList[] = {
            NULL,
            sipName_target,
            sipName_format,
        };

        if (sipParseKwdArgs(&sipParseErr, sipArgs, sipKwds, sipKwdList, NULL, "BJ9|ui", &sipSelf, sipType_QGLContext, &sipCpp, sipType_QImage, &a0, &a1, &a2))
        {
            GLuint sipRes;

            sipRes = sipCpp->bindTexture(*a0,a1,a2);

            return PyLong_FromUnsignedLong(sipRes);
        }
    }

    {
        const QPixmap* a0;
        GLenum a1 = GL_TEXTURE_2D;
        GLint a2 = GL_RGBA;
        QGLContext *sipCpp;

        static const char *sipKwdList[] = {
            NULL,
            sipName_target,
            sipName_format,
        };

        if (sipParseKwdArgs(&sipParseErr, sipArgs, sipKwds, sipKwdList, NULL, "BJ9|ui", &sipSelf, sipType_QGLContext, &sipCpp, sipType_QPixmap, &a0, &a1, &a2))
        {
            GLuint sipRes;

            sipRes = sipCpp->bindTexture(*a0,a1,a2);

            return PyLong_FromUnsignedLong(sipRes);
        }
    }

    {
        const QString* a0;
        int a0State = 0;
        QGLContext *sipCpp;

        if (sipParseKwdArgs(&sipParseErr, sipArgs, sipKwds, NULL, NULL, "BJ1", &sipSelf, sipType_QGLContext, &sipCpp, sipType_QString,&a0, &a0State))
        {
            GLuint sipRes;

            sipRes = sipCpp->bindTexture(*a0);
            sipReleaseType(const_cast<QString *>(a0),sipType_QString,a0State);

            return PyLong_FromUnsignedLong(sipRes);
        }
    }

    {
        const QImage* a0;
        GLenum a1;
        GLint a2;
        QGLContext::BindOptions* a3;
        int a3State = 0;
        QGLContext *sipCpp;

        if (sipParseKwdArgs(&sipParseErr, sipArgs, sipKwds, NULL, NULL, "BJ9uiJ1", &sipSelf, sipType_QGLContext, &sipCpp, sipType_QImage, &a0, &a1, &a2, sipType_QGLContext_BindOptions, &a3, &a3State))
        {
            GLuint sipRes;

            sipRes = sipCpp->bindTexture(*a0,a1,a2,*a3);
            sipReleaseType(a3,sipType_QGLContext_BindOptions,a3State);

            return PyLong_FromUnsignedLong(sipRes);
        }
    }

    {
        const QPixmap* a0;
        GLenum a1;
        GLint a2;
        QGLContext::BindOptions* a3;
        int a3State = 0;
        QGLContext *sipCpp;

        if (sipParseKwdArgs(&sipParseErr, sipArgs, sipKwds, NULL, NULL, "BJ9uiJ1", &sipSelf, sipType_QGLContext, &sipCpp, sipType_QPixmap, &a0, &a1, &a2, sipType_QGLContext_BindOptions, &a3, &a3State))
        {
            GLuint sipRes;

            sipRes = sipCpp->bindTexture(*a0,a1,a2,*a3);
            sipReleaseType(a3,sipType_QGLContext_BindOptions,a3State);

            return PyLong_FromUnsignedLong(sipRes);
        }
    }

    /* Raise an exception if the arguments couldn't be parsed. */
    sipNoMethod(sipParseErr, sipName_QGLContext, sipName_bindTexture, doc_QGLContext_bindTexture);

    return NULL;
}
StelQGLTextureBackend* StelQGLTextureBackend::fromViewport
	(StelQGLRenderer* renderer, const QSize viewportSize, const QGLFormat& viewportFormat)
{
	// This function should only be used as a fallback for when FBOs aren't supported.


	const int r = viewportFormat.redBufferSize();
	const int g = viewportFormat.greenBufferSize();
	const int b = viewportFormat.blueBufferSize();
	const int a = viewportFormat.alpha() ? viewportFormat.alphaBufferSize() : 0;

	// Creating a texture from a dummy image.
	QImage dummyImage(64, 64, QImage::Format_Mono);
	const TextureParams params = TextureParams();

	StelQGLTextureBackend* result = 
		new StelQGLTextureBackend(renderer, QString(), params);

	// Get image and GL pixel format matching viewport format.
	int glFormat;

	if(r == 8 && g == 8 && b == 8 && a == 8)     
	{
		glFormat           = GL_RGBA8;
		result->pixelBytes = 4.0f;
	}
	else if(r == 8 && g == 8 && b == 8 && a == 0)
	{
		glFormat           = GL_RGB8;
		result->pixelBytes = 3.0f;
	}
	else if(r == 5 && g == 6 && b == 5 && a == 0)
	{
		glFormat           = GL_RGB5;
		result->pixelBytes = 2.0f;
	}
	else
	{
		// This is extremely unlikely, but we can't rule it out.
		Q_ASSERT_X(false, Q_FUNC_INFO,
		           "Unknown screen vertex format when getting texture from viewport. "
		           "Switching to OpenGL2, disabling viewport effects or "
		           "chaning video mode bit depth might help");
		// Invalid value to avoid warnings.
		glFormat = -1;
	}

	QGLContext* context = result->prepareContextForLoading();
	const GLuint glTextureID = 
	    context->bindTexture(dummyImage, GL_TEXTURE_2D, glFormat,
	                         getTextureBindOptions(params));

	// Need a power-of-two texture (as this is used mainly with GL1)
	const QSize size = 
		StelUtils::smallestPowerOfTwoSizeGreaterOrEqualTo(viewportSize);

	// Set viewport so it matches the size of the texture
	glViewport(0, 0, size.width(), size.height());
	// Copy viewport to texture (this overrides texture size - we must use POT for GL1)
	glCopyTexImage2D(GL_TEXTURE_2D, 0, glFormat, 0, 0, size.width(), size.height(), 0);
	// Restore viewport
	glViewport(0, 0, viewportSize.width(), viewportSize.height());

	// Will need change if different screen bit depths are ever supported
	renderer->getStatistics()[ESTIMATED_TEXTURE_MEMORY]
		+= size.width() * size.height() * result->pixelBytes;

	result->startedLoading();
	result->glTextureID = glTextureID;
	result->finishedLoading(size);

	return result;
}