Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. QImage get data... transfer from basic C++

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

Scheduled Pinned Locked Moved Game Development
2 Posts 2 Posters 3.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    glararan
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      matti-
      wrote on last edited by
      #2

      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 :)
      }
      @

      Author of <a href="http://mmark.777-team.org/">MMark13</a>

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved