CreateImage doesn't work, createScreenshot is OK (trunk)

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
lvgeng
Posts: 38
Joined: Fri Aug 14, 2015 4:35 pm

CreateImage doesn't work, createScreenshot is OK (trunk)

Post by lvgeng »

First of all, I am using trunk version.
A little bit strange that the function createScreenshot works fine, but the CreateImage or CreateImageFromFata will all leads to same error. (It is hard to say it is an error, the program just crashed)
I tried a lot of times and now I am pretty sure that the program will crash when the functions mentioned before are called.
Did I miss anything or is that a problem in trunk version?

Code: Select all

#include <irrlicht.h>
//#include <SIrrCreationParameters.h>
 
#include <iostream>
#include <chrono>
#include <stdio.h>
#include <stdlib.h>
 
using namespace std;
using namespace chrono;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
 
 
int main(int argc, char* argv[])
{
    int widthOfDisplayZone = 800;
    int heightOfDisplayZone = 600;
    int widthOfSubimage = 20;
    int heightOfSubimage = 20;
    int widthOfHole = 2;
    int heightOfHole = 2;
    video::SColor backgroundColor;
    video::SColor dotColor;
    std::string filename;
 
    bool isTesting = true;
 
    try
    {
        if (argc == 3 && std::string(argv[1])=="t")
        {
            isTesting = true;
            widthOfDisplayZone = atoi(argv[2]);
        }
        else if (argc == 2 && atoi(argv[1]) != 0)
        {
            isTesting = false;
            widthOfDisplayZone = atoi(argv[1]);
        }
        else
        {
            throw 0;
        }
    }
    catch(int error)
    {
        cout<<"Usage:\n"<<"'./pattern-irrlicht t HEIGHT' for testing images\n"<<"'./pattern-irrlicht HEIGHT' for images for using\n";
        return 0;
    }
 
    // Load the width and height.
    heightOfDisplayZone = widthOfDisplayZone * 210 / 297;
 
  heightOfDisplayZone = 1024;
  widthOfDisplayZone = 1024;
 
    backgroundColor = SColor(255,255,255,255);
    dotColor = SColor(255,0,0,0);
 
    IrrlichtDevice *device =
        createDevice(
            video::EDT_OPENGL,
            dimension2d<u32>(widthOfDisplayZone, heightOfDisplayZone),
            16,false,true,true,0);
 
 
    device->setWindowCaption(L"Light field display");
 
    IVideoDriver* videoDriver = device->getVideoDriver();
    ISceneManager* sceneManager = device->getSceneManager();
    IGUIEnvironment* guiEnvironment = device->getGUIEnvironment();
 
  video::IRenderTarget* simulatorRenderTarget = 0;
  video::ITexture* renderTargetTex = 0;
  scene::ICameraSceneNode* fixedCam = 0;
  renderTargetTex = videoDriver->addRenderTargetTexture(core::dimension2d<u32>(widthOfDisplayZone,heightOfDisplayZone), "RTT1", video::ECF_A8R8G8B8);
  video::ITexture* renderTargetDepth = videoDriver->addRenderTargetTexture(core::dimension2d<u32>(widthOfDisplayZone,heightOfDisplayZone), "DepthStencil", video::ECF_D24S8);
  simulatorRenderTarget = videoDriver->addRenderTarget();
  simulatorRenderTarget->setTexture(renderTargetTex, renderTargetDepth);
 
    videoDriver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, backgroundColor);
  videoDriver->setRenderTargetEx(simulatorRenderTarget, video::ECBF_COLOR | video::ECBF_DEPTH, SColor(0));
 
            videoDriver->draw2DRectangle(
                dotColor,
                core::rect<s32>(0,0,400,400)
                );
 
    guiEnvironment->drawAll();
    videoDriver->endScene();
 
    filename = "Img-testing-" + to_string(widthOfDisplayZone) + "-" + to_string(heightOfDisplayZone) + ".png";
 
  void* imageData = renderTargetTex->lock();
  IImage * image = videoDriver->createImageFromData(renderTargetTex->getColorFormat(), renderTargetTex->getSize(), imageData);
  videoDriver->writeImageToFile(image, path(filename.c_str()));
 
  while(device->run());
}
My log...

Code: Select all

Irrlicht Engine version 1.9.0
Linux 3.19.0-49-generic #55~14.04.1-Ubuntu SMP Fri Jan 22 11:24:31 UTC 2016 x86_64
Creating X window...
GLX >= 1.3
Visual chosen: : 33
Using renderer: OpenGL 4.5.0
GeForce GTX TITAN/PCIe/SSE2: NVIDIA Corporation
OpenGL driver version is 1.2 or better.
Dedicated video memory (kB): 6291456
Total video memory (kB): 6291456
Available video memory (kB): 5118500
GLSL version: 4.5
[1]    12468 segmentation fault (core dumped)  ./pattern-irrlicht t 1000
 
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: CreateImage doesn't work, createScreenshot is OK (trunk)

Post by Foaly »

First of all, always show the line, where the crash happens, so we don't have to guess.

Also read the docu http://irrlicht.sourceforge.net/docu/cl ... 1ed859df38
Looks like you want to read from the texture. Think about that.
And you forgot to unlock.
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: CreateImage doesn't work, createScreenshot is OK (trunk)

Post by Foaly »

Oh, sorry, I had assumed lock() without parameter is for write only, but I guess that's not true.
lvgeng
Posts: 38
Joined: Fri Aug 14, 2015 4:35 pm

Re: CreateImage doesn't work, createScreenshot is OK (trunk)

Post by lvgeng »

Foaly wrote:Oh, sorry, I had assumed lock() without parameter is for write only, but I guess that's not true.
Sorry but I am a little bit confused... what is exactly you suggested?
And to be honest I haven't found a tutorial for saving files yet...
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: CreateImage doesn't work, createScreenshot is OK (trunk)

Post by Foaly »

I thought you have to use lock like this: lock(ETLM_READ_ONLY)
But I guess it should work even though.

Are you sure it crashes in createImage? Run it in a debugger to find out where it crashes exactly.
lvgeng
Posts: 38
Joined: Fri Aug 14, 2015 4:35 pm

Re: CreateImage doesn't work, createScreenshot is OK (trunk)

Post by lvgeng »

Foaly wrote:I thought you have to use lock like this: lock(ETLM_READ_ONLY)
But I guess it should work even though.

Are you sure it crashes in createImage? Run it in a debugger to find out where it crashes exactly.
Pretty sure.
And it turned out to be a problem in trunk... they are fixing it.
Post Reply