QImage get data... transfer from basic C++



  • Hello, i need help.
    I'm switching from basic C++ opengl to Qt framework opengl. What i am doing? Rendering terrain from heightmap png.

    This is basic c++ source code: http://pastebin.com/Hhg2ph5w
    vec3f is just xyz... bitmap = ImageLoader.h = http://www.gamedev.net/page/resources/_/technical/game-programming/how-to-load-a-bitmap-r1966

    In basic c++ i render from bitmap = .bmp, but for better heightmaps details i using grayscale png 16 bit and its little difficult in basic c++ so i switch on qt. And here im trying to render terrain but when i open program i just see background :(

    Qt sources:
    Terrain.h http://pastebin.com/r1xhHneN
    Terrain.cpp http://pastebin.com/6eQhPu7i
    widget.h http://pastebin.com/xxR9mXf3
    widget.cpp http://pastebin.com/m945uhMG

    All is same until: loadTerrain

    basic:
    @Terrain* loadTerrain(const char* fileName, float height)
    {
    Bitmap* image = new Bitmap();
    image->loadBMP(fileName);

    Terrain* t = new Terrain(image->width, image->height);

    for(int y = 0; y < image->height; y++)
    {
    for(int x = 0; x < image->width; x++)
    {
    unsigned char color = (unsigned char)image->data[3 * (y * image->width + x)];
    float h = height * ((color / 255.0f) - 0.5f);

    t->setHeight(x, y, h);
    }
    }

    delete image;
    t->computeNormals();

    return t;
    }@

    qt:
    @Terrain* loadTerrain(QString fileName, float height)
    {
    QImage* image = new QImage(fileName);

    Terrain* t = new Terrain(image->width(), image->height());
    
    char* data;
    data = new char[image->width() * image->height() * 3];
    
    for(int y = 0; y < image->height(); y++)
    {
        for(int x = 0; x < image->width(); x++)
        {
            unsigned char color = data[3 * (y * image->width() + x)];
            float h = height * ((color / 255.0f) - 0.5f);
    
            t->setHeight(x, y, h);
        }
    }
    
    delete image;
    t->computeNormals();
    
    return t;
    

    }@

    It must be problem here. I think its problem about char* data ...
    And what is format type for grayscale 16 bit?



  • To access the bitmap data within QImage, take a look at QImage::bits(). In your code youre just allocating a new buffer and accessing it.

    To load image data, do something such as:

    @QImage image(imageName);
    if ( !image.isNull() )
    {
    const char* data = (const char*)image.bits();
    // TODO your stuff here :)
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.