Using QImage with OpenGL

前端 未结 3 1515
一整个雨季
一整个雨季 2020-12-09 21:19

I\'ve very recently picked up Qt and am using it with OpenGL The thing though is that when moving my SDL code to Qt and changing the texture code to use QImage it stops work

3条回答
  •  难免孤独
    2020-12-09 21:26

    It looks like your problem is not here, as I have no problem with the following code. You should check your GL init and display setup.

    Have you a glEnable(GL_TEXTURE_2D) somewhere ?

    Also note glTexCoord2f must be before glVertex2f.

    #include 
    #include 
    #include 
    
    GLuint texture[1] ;
    
    void LoadGLTextures( const char * name )
    {
        QImage img;
        if( ! img.load( name ) )
        {
            std::cerr << "error loading " << name << std::endl ;
            exit( 1 );
        }
    
        QImage GL_formatted_image;
        GL_formatted_image = QGLWidget::convertToGLFormat(img);
        if( GL_formatted_image.isNull() )
        {
            std::cerr << "error GL_formatted_image" << std::endl ;
            exit( 1 );
        }
    
        glGenTextures(1, texture);
        glBindTexture(GL_TEXTURE_2D, texture[0]);
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
                GL_formatted_image.width(), GL_formatted_image.height(),
                0, GL_RGBA, GL_UNSIGNED_BYTE, GL_formatted_image.bits() );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 
    }
    
    void resize(int Width, int Height)
    {
        glViewport( 0 , 0 , Width , Height );
    }
    
    void draw()
    {
        glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
        glClear( GL_COLOR_BUFFER_BIT );
    
        gluOrtho2D( -1 , 1 , -1 , 1 );
    
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    
        glShadeModel( GL_FLAT );
    
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, texture[0]);
    
        glBegin(GL_TRIANGLES);
        glTexCoord2f( 1.0f, 0.0f);
        glVertex2f( 1.0f, 0.0f);
        glTexCoord2f( 0.0f, 1.0f);
        glVertex2f( 0.0f, 1.0f);
        glTexCoord2f( 0.0f, 0.0f);
        glVertex2f( 0.0f, 0.0f);
        glEnd();
    
        glutSwapBuffers();
    }
    
    int main(int argc, char **argv) 
    {  
        glutInit(&argc, argv);  
        glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA );  
        glutInitWindowSize(640, 480);
    
        glutCreateWindow("Texture"); 
    
        LoadGLTextures( "star.png" );
    
        glutDisplayFunc( & draw );  
        glutReshapeFunc( & resize );
        glutMainLoop();  
    
        return 1;
    }
    

提交回复
热议问题