13 December 2012

Static Linking OpenCV in Visual Studio 2010

A static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker or binder, producing an object file and a stand-alone executable. When you link OpenCV libraries statically, it produces a single large executable or binary which doesn't require any opencv dlls to run. The precompiled opencv binary comes with dynamic library as well as static library in the build folder. opencv\build\x86\vc10\staticlib contains the static library files and opencv\build\x86\vc10\lib contains dynamic library files.

Take the following steps to statically link OpenCV to your Visual Studio project -
Step 1: Go to your Project->Properties. Add the OpenCV include directory to your project's additional include directories.
Step 2: Now go the Linker->General Settings-> Additional Library Directories and add the OpenCV static library location.
Step 3: Go to the Linker->Input->Additional Dependencies and add the OpenCV static libraries here. Apart from the basic opencv libraries like opencv_core243.lib, opencv_highgui243.lib that you need, you need to add the libtiff.lib, libpng.lib, libjpeg.lib, libjasper.lib, IlmImf.lib, zlib.lib. These library files are required by other opencv libraries. Try to compile your project. If it doesn't compile, add Vfw32.Lib and comctl32.lib to the library list. These libraries are visual studio libraries which are also required.

Try to compile your project now, it should work.

29 November 2012

Correcting "Another version of this product is already installed" error in Windows

Sometimes the softwares are not uninstalled properly and when you try to reinstall them on windows, you get the error -
"Another version of this product is already installed.  Installation of this version cannot continue.  To configure or remove the existing version of this product, use Add/Remove Programs on the Control Panel"
 To correct this error, do the following -
Step 1: Execute the following command from the command prompt:
         msiexec /i <ApplicationName>.msi /lv logfile.log
where the application-name.msi file is the setup of new version.
/lv is log Verbose output.
msiexec - Provides the means to install, modify, and perform operations on Windows Installer from the command line.

Step 2: After executing this command, it will try to install your software and also generate a log file of the installation steps. If any error occurs, you can see it in the log file. The main aim of generating this log file is to get the GUID of the product you are installing. It will look something like this - X2X90X73-3D6A-4195-B3D5-8B570394FB5F}. Copy this code from the log file.

Step 3: Execute the following command from the command prompt/visual studio command prompt -
            msizap.exe TWP {copied GUID}

Msizap.exe is a command line utility that removes either all Windows Installer information for a product or all products installed on a computer.
T - This flag removes all the information for the specified product code. When using this option, enclose the Product Code in curly braces.
W - Removes windows installer information for all users.
P -   Removes the In-Progress key.

Now your product has been completely removed and you are ready to begin the fresh installation of the software.




15 November 2012

LibCurl in Visual Studio 2010

cURL is a computer software project providing a library and command-line tool for transferring data using various protocols. The libcurl library is free, thread-safe, IPv6 compatible, and fast. Bindings in more than 40 languages are available for libcurl, including C/C++, Java, PHP and Python.

You can directly download libCurl for windows built in Visual Studio 2010. You don't need to build it again. This library can be used in any visual studio 2010 project. Below are the steps to add it to your project.

  1. Download the library from the libCurl's site. If you want curl with SSL support, you can download it from here. If you want without SSL, you can download from here. These are the builds for 32 bit machines.
  2. Now create new VC++ project or open your old project.
  3. Go to Project Properties->Configuration Properties->VC++ Directories->Include Directories. Here add the libcurl's include folder of the extracted location. (E.g.- <extracted location>\libcurl-7.19.3-win32-ssl-msvc\include\curl\). Also add the directory above the curl folder i.e.  'libcurl-7.19.3-win32-ssl-msvc\include'as in some places, curl is included as include "curl/curlxxx.h".
  4. Now go to Linker->General->Additional Library Directories and add the location of curl library. If you have downloaded curl with SSL support, add the Debug directory containing curllib_static.lib, curllib.dll and curllib.lib. If you have downloaded curl without ssl support, just add the extracted location of curl containing the libcurl.lib file.
  5. Go to your Linker->Input->Additional Dependencies and add the library file to be linked i.e. libcurl.lib or curllib_static.lib, or curllib.lib.
  6.  You're done. Now add your code and compile it.

12 November 2012

OpenGL screenshot using OpenCV

OpenGL renders on the buffer which is then shown on the screen. It can render on the Back Buffer or the Front Buffer as specified. We can copy this buffer and save it as screenshot or continuous video. The basic opengl functions which are used for such purposes are glDrawBuffer, glReadBuffer and glReadPixels. glDrawBuffer to specify to which color buffer the image is rendered. By default it is front(GL_FRONT) for single-buffered and back(GL_BACK) for double-buffered applications. glReadBuffer is used to determine from whch color buffer the image is read. This function is generally called after the rendering function and before calling glReadPixels which is used to read the color buffer and to store the image in memory.

After copying the image data into a memory location, you can use any image loading/saving library to do rest of the work for you. Some of such libraries are FreeImage, Corona, Devil(Developer's Image Library). I'll be using OpenCV for this purpose as my project was already using OpenCV for some other tasks.

 My OpenGL viewport was of 640x480 so my image buffer was of same dimensions. In OpenGL frame, data is arranged from left to right and rows from bottom to top, while in OpenCV, data is from left to right and rows from top to bottom. So this formatting has to be kept in mind while using any other image library for copying data from OpenGL.

The code for capturing OpenGL frame and saving it using OpenCV -

    cv::Mat img(480, 640, CV_8UC3);
    glPixelStorei(GL_PACK_ALIGNMENT, (img.step & 3)?1:4);
    glPixelStorei(GL_PACK_ROW_LENGTH, img.step/img.elemSize());
    glReadPixels(0, 0, img.cols, img.rows, GL_BGR_EXT, GL_UNSIGNED_BYTE, img.data);
    cv::Mat flipped(img);
    cv::flip(img, flipped, 0);
    cv::imwrite("snapshot.png", img);

Explanation -
  • First we create an empty matrix 'img' for our data to be read into. 
  • Then you have to account for cv::Mat not neccessarily storing image rows contiguously. There  might be small padding value at the end of each row to make rows 4-byte aligned (or 8?). So you need to mess with the pixels storage modes. glPixelStorei sets pixel storage modes that affect the operation of subsequent glReadPixels. GL_PACK_ALIGNMENT - specifies the alignment requirements for the start of each pixel row in memory. The allowable values are 1, 2, 4, 8. GL_PACK_ROW_LENGTH defines the number of pixels in a row.
  • The type of matrix influences the format and type parameters of glReadPixels. If you want color images you have to keep in mind that OpenCV usually stores color values in BGR order while OpenGL stores in RGB. glReadPixels(x, y, width, height, format, type, data) - returns the pixel data from the frame bufer, starting witht eh pixel whose lower left corner is at location x, y into client memory starting at location data.
  • Finally OpenCV stores the images from top to bottom. So you may need to either flip them after getting them or render them flipped in OpenGL in the first place. To flip a cv::Mat vertically, you can use cv::flip.

 

2 November 2012

Browser plugin development using Firebreath

FireBreath is a framework that allows easy creation of powerful browser plugins. A plugin built on FireBreath works as an NPAPI plugin or as an ActiveX control (windows only) and support could be added for other plugin types built in C++ as well. Check out the complete firebreath features here.

First download firebreath either by downloading the stable version or by cloning recent project from git repository. Check out the firebreath download link for more details.

After downloading/extracting into a folder, you are ready to develop browser plugin.Firebreath provides a plugin generator python script called fbgen.py. This scripts requires Python 2.5, 2.6, 2.7 or 3.2.3. Now you can generate your new plugin project by executing this script by -
> python2.7 fbgen.py
This will ask for the details of your plugin and then generate a project based on that. The details asked are -
  • Plugin Name [] : This is the human readable name of your plugin that will be used in the strings.
  • Plugin Identifier [] : This is the code friendly identifier that will be used to name classes, project directory etc. 
  • Plugin Prefix [] : The plugin prefix is used to generate project names in visual studio. 
  • Plugin Mime Type [] : The mime type that you'll use with the object tag to embed your plugin into the web page. 
  • Plugin Description [] :  A human readable description of the plugin that will go in the strings resource that is compiled into the plugin; It will be visible in the plugin navigator on NPAPI based browsers. 
  • Plugin has no UI [false] : Set to true if you do not plan to do any drawing with your plugin.
  • Company Name [] : This is used for copyright notices in the resource strings. 
  • CompanyIdentifier [] : This is used to generate ActiveX PROGID and a few other things in the plugin config.
  • Company Domain [] : This is needed to create a unique id for the mozilla plugin, which is of the form "@[company domain]/[plugin id]". The default is generated from the company id.
 fbgen.py takes the provided information and generates a project in the <firebreath>/projects directory. You can copy this project folder to any other required place. The important files which were created are as follows:
  • PluginConfig.cmake - Configuration options from this file are used to generate other files that are critical to your plugin configuration.
  • CMakeLists.txt - It is the main config file for generating the project. 
  • Factory.cpp - This contains a subclass of FactoryBase for your plugin. The method that is called by Firebreath to create the main plugin object is in here, as are the global init and deinit functions. The default behaviour is to create a [YourProjectID] object and call plugins StaticInitialize() and StaticDeinitialize() for the global init and deinit.
  • <PluginIdentifier>(.cpp | .h) - This is essentially the main entry-point for the project. Any time someone puts an object tag on a page with your plugin, this object gets initialized. It has a window from which it gets events and which can draw for it and it has an API which handles javascript calls. createJSAPI method creates the JSAPI that will be seen by javascript when it accesses the object.
  • <PluginIdentifier>API(.cpp | .h) - This is the default "API" class. Think of it as a javascript object when you access your plugin from javascript. You can add methods and functions, deal with and return arrays and javascript objects, and even return other API objects with their own methods and properties.
  • Win/projectDef.cmake -  This is the other half of the project definition started in 'CMakeLists.txt', which deals specifically with windows. Similarly, there are files for Mac and X11.
  • Win/np_winmain.cpp - This provides the main entrypoints for NPAPI. 
  • Win/dllmain.cpp - This provides the dllmain entrypoint of your DLL.
After generating your project, move the project folder to a desired location and create a build folder for the project. Now you can use the platform specific project generation script, to generate the project in build folder.
Firebreath provides automatic project development scripts for most platforms. These are prep scripts and some them are as follows - 
  • prep20xx.cmd - Generates visual studio projects (prep2010.cmd for visual studio 2010)
  • prep2010x64.cmd - Generates visual studio projects for a 64 bit build.
  • prepmac.sh  - Generates XCode project on Mac.
  • prepcodeblocks.sh - Generates CodeBlocks Project.
  • prepeclipse.sh - Generates Eclipse project.
  • prepmake.sh - Make based build.
Usage: prepXXX <project dir> <build dir> [params]

This project will generate a project of desired type in specified build directory. You can build this project to generate a browser plugin. You can proceed plugin development on this project.

After compilation of the project, it will generate the plugin in "<Build Dir>/bin/<ProjectName>/<Debug/Release>". You can register this plugin in browser by copying it in default browser plugin folder. To test your plugin, you can use the htm file generated in "<Build-Dir>/projects/<ProjectName>/gen/FBControl.htm".

Firebreath comes with a few 3dparty libraries which can be attached to the project as when needed. Boost is used by default in the project. If you want to add logging to your project, you can use log4cplus library into your project. To add it to your project. Go to the PluginConfig.cmake file of your project and add this line  -
add_firebreath_library(log4cplus)

To add any boost library to the firebreath project, you can add following line to the PluginConfig.cmake file -
add_boost_library(<library-name>)
E.g. - add_boost_library(filesystem), add_boost_library(thread), add_boost_library(signals)

To use some other version of boost library apart from the one which comes with firebreath, you can build the boost library externally and add it with the prep command.
 C:\>prepxxx.cmd <project-location> <build-location> "-DWITH_SYSTEM_BOOST=ON" "-DBoost_USE_STATIC_LIBS=ON" "-DBOOST_ROOT='<Boost-Library-Location>'" "-DBoost_DEBUG=OFF" "-DCMAKE_BUILD_TYPE=Debug"

Note - Make sure that your build location doesn't contain any previous project build before executing the above command.

 After this, run the prepXXX <project dir> <build dir> command again.
Now add following lines to the Factory.cpp class -
void getLoggingMethods( FB::Log::LogMethodList& outMethods )
    {
        // The next line will enable logging to the console (think: printf).
        outMethods.push_back(std::make_pair(FB::Log::LogMethod_Console, std::string()));

        // The next line will enable logging to a logfile.
        outMethods.push_back(std::make_pair(FB::Log::LogMethod_File, "C:\\log.txt"));

        // Obviously, if you use both lines, you will get output on both sinks.
    }

This function specifies the log file location. Now you can log to your file by using macros FBLOG_INFO(src, msg), FBLOG_TRACE(src, msg), FBLOG_DEBUG(src, msg), FBLOG_WARN(src, msg), FBLOG_ERROR(src, msg), FBLOG_FATAL(src, msg)

The most difficult part of plugin development is debugging. Its not as simple as application development. As you can take a look at firebreath's link for debugging http://www.firebreath.org/display/documentation/Debugging+Plugins, it requires a bit of setup before you can start with full fledged debugging. For simple break point debugging, I use google chrome with plugin startup dialog. This helps me to attach process to the plugin while it waits for the plugin to start. Follow the given steps to add breakpoints and debug your plugin on windows using visual studio.
1. Build your plugin in visual studio.
2. Now go to the command promt in windows and start google chrome executable with the flag --plugin-startup-dialog. Google chrome executable is mostly present in your "<Home-Folder>/AppData/Local/Google/Chrome/Application/chrome.exe". With the command also give the url to open as your plugin testing html page e.g. FBControl.htm. So your command would look something like this -
C:\> <Home-Folder>/AppData/Local/Google/Chrome/Application/chrome.exe --plugin-startup-dialog <Html-Location>/FBControl.htm 
 3. This command will start chrome and popup a process id for your plugin(Don't press ok button in the process id dialog). Go to visual studio and click on Debug>Attach to process...
4. Now a dialog will appear with list of processes. Attach your process to the process id provided by chrome.
5. After attaching the process, go to chrome and click Ok button of the process id dialog. This will start the plugin process and your execution will stop at the breakpoint provided in visual studio.

This debugging process might not work directly on all browsers. You just have to configure them as specified in this link - http://www.firebreath.org/display/documentation/Debugging+Plugins.



22 September 2012

Getting started with CMake

 Working on so many projects, you might have noticed that compilation process of a project involves searching for required libraries and their paths on the system, building a bunch of source files and some of these source files depend on each other. Finally all the source files and libraries located in different folders are compiled to generate a binary file or a library file. A description of the build process is stored in a group of files.

CMake is a cross-platform build systems generator which makes it easier to build software in a unified manner on a broad set of platforms. http://www.scons.org/wiki/SconsVsOtherBuildTools provides a comparison of several software building tools available presently. Cmake is a generator: it generates native build system files like Makefile, Visual Studio or other IDE projects, etc. CMake scripting language is used to describe the build. The developer has to edit the CMakeLists.txt and invoke CMake but should not change any of the generated files directly.

CMake workflow -
1. CMake time: CMake is running and processing CMakeLists.txt
2. Build time: The build tool runs and invokes the compiler.
3. Install time: The compiled binaries are installed i.e. from the build area to an install location.
4. CPack time: CPack is running for building package.
5. Package install time: The time taken to install the package developed in previous step.

Installing CMake
For several platforms such as Windows, Linux, Mac, etc, CMake is available as a standard package or binaries which can be downloaded from its official website. You can type cmake --help in the terminal to check the version of cmake and usage information.Generally CMake path is added to the system path automatically by the installer. If its not added, then you can run it directly from the installed location.

Simple CMake
cmake_minimum_required (VERSION 2.8)
# This project uses C source code.
PROJECT (A Simple C Project)
ADD_LIBRARY (mylibrary STATIC libsource.c)
# Build executable using specified list of source files.
ADD_EXECUTABLE (simple main.c)
TARGET_LINK_LIBRARIES (simple mylibrary z m)
 
This generates makefile to compile main.c file and link it to mylibrary. To build this project in linux, just go the terminal, go the folder where this cmake is present and type
cmake .
make

CMake Commands
CMake is in essence a simple interpreter. CMake input files have an extremely simple but powerful syntax. It consists of commands, primitive flow control constructs, macros and variables. All commands have exactly the same syntax:
COMMAND_NAME (ARGUMENT1, ARGUMENT2)

You can check the cmake help on any of its command by typing
cmake --help-command add_executable
 Output: Add an executable to the project using the specified source files.

cmake --help-command project
Syntax: PROJECT(<projectName> [languageName1, languageName2])
Set a name for entire project. Additionally this sets the variables <projectName>_BINARY_DIR and <projectName>_SOURCE_DIR to the respective values. Optionally you can specify which languages your project supports. Eg - CXX for C++, C, Fortran, etc. By default C and CXX are enabled.

cmake --help-command add_library
Syntax:  ADD_LIBRARY(<name> [STATIC | SHARED | MODULE] [EXCLUDE FROM ALL] source1 source2 ... sourceN)
Adds a library target called <name> to be build from the source files listed in the command invocation. STATIC, SHARED or MODULE may be given to specify the type of library to be created. By default the library file will be created in the build tree directory corresponding to the source tree directory in which the command was invoked.

Flow Control Constructs
A few flow control constructs, such as IF and FOREACH are used. The IF construct uses one of several types of expressions, such as boolean(NOT, AND, OR), check if command exists(COMMAND) and check if file exists(EXISTS). Expressions however cannot contain other commands. An example of commonly used IF statement would be:
IF(UNIX)
    IF(APPLE)
         SET(GUI "Cocoa"
     ELSE(APPLE)
         SET(GUI "X11"
     ENDIF(APPLE)
ELSE(UNIX)
     IF(WIN32)
          SET(GUI "Win32"
     ELSE(WIN32)
          SET(GUI "Unknown"
     ENDIF(WIN32)
ENDIF(UNIX)
MESSAGE("GUI system is ${GUI}")

This example shows simple us of IF statement and variables. FOREACH is used int the same fashion. For example if a list of executables are to be created, where every executable is created from a source file with the same name, following FOREACH would be used:

SET(SOURCES source1 source2 source3)
FOREACH(source ${SOURCES})
       ADD_EXECUTABLE(${source} ${source}.c)
ENDFOREACH(source)

Macros use a syntax of both commands and flow control constructs. MACRO construct is used to define a macro. For example, lets create a macro which creates executable from the source by linking it to libraries.

MACRO(CREATE_EXECUTABLE NAME SOURCES LIBRARIES)
      ADD_EXCUTABLE(${NAME} ${SOURCES})
      TARGET_LINK_LIBRARIES(${NAME} ${LIBRARIES})
ENDMACRO(CREATE_EXECUTABLE)
ADD_LIBRARY(mylibrary libSource.c)
CREATE_EXECUTABLE(myprogram main.c mylibrary)

Macros are not equivalent to procedures or functions from the programming languages and do not allow recursion.

Conditional Compiling
The build process also should be able to find and set locations to the system resources your project needs. All these functions are achieved in CMake using conditional compiling. E.g. -  If the project has to compile in two different modes namely DEBUG and REGULAR, you can define sections to compile in different mode based on the enabled variable.
#ifdef DEBUG
      fprintf(stderr, "The value of i is: %d\n", i);
#endif

In order to tell CMake to add -DDEBUG to compile lines, you can use the SET_SOURCE_FILE_PROPERTIES with the COMPILE_FLAGS property. But you probably do not want to edit the CMakeLists.txt every time you switch between the debug and regular build. The OPTION command creates a boolean variable that can be set before building the project. The syntax for above example will be:

OPTION(MYPROJECT_DEBUG    "Build the project using debugging code" ON)
IF(MYPROJECT_DEBUG)
    SET_SOURCE_FILE_PROPERTIES( libsource.c main.c COMPILE_FLAGS -DDEBUG)
#ENDIF(MYPROJECT_DEBUG)

The flags can be set/unset using any cmake gui component installed on the system like CMake-Gui. After using cmake command, go to that folder and initiate the gui in that folder. This will open a GUI where you can select or deselect components.

Another sample of user controlled build option
CMAKE_MINIMUM_REQUIRED (VERSION 2.8)
# This project uses C source code
PROJECT(TotallyFree C)
# Build option with default value to ON
OPTION(WITH_ACRODICT "include/acronym/dictionary/support" ON)
SET(BUILD_SHARED_LIBS true)
#build executable using specified list of source files
ADD_EXECUTABLE(Acrolibre acrolibre.c)
IF(WITH_ACRODICT)
       SET(LIBSRC acrodict.h acrodict.c)
       ADD_LIBRARY(acrodict ${LIBSRC})
       ADD_EXECUTABLE(Acrodictlibre acrolibre.c)
       TARGET_LINK_LIBRARIES(Acrodictlibre acrodict)
       SET_TARGET_PROPERTIES(Acrodictlibre PROPERTIES COMPILE_FLAGS "-DUSE_ACRODICT")
ENDIF(WITH_ACRODICT)

Another common type of variable is a PATH which specifies the location of some file on the system. If the program relies on the the file Python.h, this would be put in cmake file like this -
FIND_PATH(PYTHON_INCLUDE_PATH  Python.h    /usr/include   /usr/local/include)

FIND_PATH command is used to find a directory containing the named file. A cache entry named by <VAR> is created to store the result of this command. If the file in a directory is found, the result is stored in the variable and the search will not be repeated unless the variable is cleared. If nothing is found, the result will be <VAR>-NOTFOUND and the search will be attempted again the next time find_path is invoked with the same variable.

But the above command looks for that file in only those folder for the current project. So you'll have to add the same line for every project. To avoid this, you can include other CMake files, called modules. CMake comes with several useful modules, from the type that searches for different packages to the type that actually performs some tasks or define MACROS. For the list of all modules, check the Modules subdirectory of CMake. E.g. - FindPythonLibs.cmake which finds python libraries and header files on almost every system.

Handling Subdirectories
As a software developer, you probably organize source code in subdirectories. Different subdirectories can represent libraries, executables, testing or even documentation. We can enable or disable subdirectories to build parts of project and skip other parts. To tell CMake to process a subdirectory, use SUBDIRS comand. This command tells CMake to go to the specified subdirectory and find the CMakeLists.txt file.
The toplevel project looks something like this -
PROJECT(MyProject C)
SUBDIRS(SomeLibrary)
INCLUDE_DIRECTORIES(SomeLibrary)
ADD_EXECUTABLE(MyProgram main.c)
TARGET_LINK_LIBRARY(MyProgram MyLibrary)

INCLUDE_DIRECTORIES command tells the compiler where to find the header files for main.c

Lets see an example to cover all the steps covered till now. The CMake file has to be written for project called Zlib.

PROJECT(ZLIB)
#source files for zlib
SET(ZLIB_SRC adler32.c   gzio.c   inftrees.c   uncompr.c   compress.c   infblock.c   infutil.c   zutil.c   crc32.c   infcodes.c   deflate.c   inffast.c   inflate.c   trees.c )
ADD_LIBRARY(zlib ${ZLIB_SRCS})
ADD_EXECUTABLE(example example.c)
TARGET_LINK_LIBRARIES(example zlib)

ZLib needs unistd.h on some platforms. So a test has to be added to the project.

INCLUDE( ${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
CHECK_INCLUDE_FILE( "unistd.h" HAVE_UNISTD_H)
IF(HAVE_UNISTD_H)
    ADD_DEFINITION(-DHAVE_UNISTD_H)
ENDIF(HAVE_UNISTD_H)

Also, something has to be done abou the shared libraries on Windows. ZLIB needs to be compiled with -DZLIB_DLL, for proper export macros. So the following options have to be added.

OPTION(ZLIB_BUILD_SHARED)
  "Build ZLIB shared" ON)
IF(WIN32)
    IF(ZLIB_BUILD_SHARED)
         SET(ZLIB_DLL 1)
    ENDIF(ZLIB_BUILD_SHARED)
ENDIF(WIN32)
IF(ZLIB_DLL)
      ADD_DEFINITION(-DZLIB_DLL)
ENDIF(ZLIB_DLL)

This is a very simple example and will get you started with cmake for your basic projects. CMake is capable of doing several other tasks. It can now do platform independent TRY_RUN and TRY_COMPILE builds, wich come in handy when you want to test the capabilities of the system. CMake natively supports C and C++ and with a little effort, it can generate builds for most available languages. 
 
 











8 August 2012

Build MySQL Plugin for Qt(4.7.3) on Windows

The QtSql module uses driver plugins to communicate with the different database APIs. Since Qt's SQL Module API is database-independent, all database-specific code is contained within these drivers. Several drivers are supplied with Qt and other drivers can be added. The source for building QMYSQL plugin is provided on the qt repository and also along with the qt-everywhere-opensource archive. Here we'll see how to build the QMYSQL plugin using installed MySQL libraries.

Follow the given steps to build the plugin -
1. Download the following softwares/sources.
      a) QtSDK (if not installed) from http://qt.nokia.com/downloads.
      b) My SQL Community Server (e.g. - mysql-5.5.20-win32.msi) from http://dev.mysql.com/downloads/mysql/. Make sure it is 32 bit version as 64 bit is not supported yet.
      c) Qt-Everywhere-Opensource zip file so that you may have the complete plugin source. This is optional as you can download selected source later using QtSDK package manager.

2. Mostly, Qt Sources are not installed with the QtSDK, so you'll have to install the sources before proceeding. To install the sources -
     a) Open 'Maintain Qt SDK' program from start menu.
     b) Select the Qt Sources you want to install and follow the wizard.

Remember to mark the correct version of Qt Sources.

3. Install the downloaded MySQL. Install it in C:\MySql or any other location without any space in between.  Providing library path creates problem when there is space in between so better avoid it.

4. After installation is complete you are ready to build plugin. There are two ways in which you may proceed.
   a)   Run the following commands in command prompt. Change them according to you environment.
         i)   set mysql=c:\\mysql\\MySQL_Server_55
         ii)  cd C:\QtSDK\QtSources\4.7.4\src\plugins\sqldrivers\mysql\
         iii) qmake "INCLUDEPATH+=%sql%\\include" "LIBS+=%mysql%\\lib\\libmysql.lib" -o Makefile        mysql.pro
         iv) mingw32-make
         v)  qmake "INCLUDEPATH+=%sql%\\include" "LIBS+=%mysql%\\lib\libmysql.lib" -o Makefile mysql.pro "CONFIG+=release"
         vi) mingw32-make

   b) If the above method doesn't work, you can compile the plugin using the QtCreator. Go to the project location (i.e. C:\qt-everywhere-opensource-src-4.8.2\qt-everywhere-opensource-src-4.8.2\src\plugins\sqldrivers\mysql, if you have extracted it from qt-everywhere-opensource or C:\QtSDK\QtSources\4.7.4\src\plugins\sqldrivers\mysql\, if you have installed sources from Maintain Qt SDK) and open the mysql.pro file in QtCreator.
      i) You have to add mysql installed library to your project. To do this right click on the project and click on Add Library.
      ii) Add the library as shown in the images below - Initially select the External library option.

Then add the mysql library path -
Click on the next after specifying the path as above. Finally this library will be added to the pro file.

Now compile the project. This will generate the output as libqsqlmysqld4.a and libqsqlmysqld4.dll.

c) Copy above two files to C:\QtSDK\Desktop\Qt\4.7.4\mingw\plugins\sqldrivers from both debug as well as release build.

d) Copy libmysql.dll from MySql installed folder to C:\Windows folder.

Now you are ready to code using sql plugin.

Compile this qt code to check if the MySql plugin has been detected successfully.

#include <QtCore/QCoreApplication>
#include <QtSQL>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug()<<QSqlDatabase::drivers();
    return a.exec();
}

Note - Don't forget to add QT += sql to your project file.




5 July 2012

Setting up Nvidia Apex SDK on Visual Studio 2010

APEX is a multi-platform, scalable dynamics framework. Rather than providing a low-level API that requires a physics programmer, APEX creates an environment where artists can create complex, high-level, dynamic systems without any programming. APEX is completely artist focused by providing the artist with familiar and easy to use authoring tools.
APEX consists of two major components:
  • the authoring component, provides high-level authoring of dynamic systems through DCC plug-ins (3dsMax/Maya), standalone tools, or game engine plug-ins.
  • the runtime component, consists of a modular SDK which includes the framework that supports all APEX modules and provides a render interface to allow minimal integration into game engines.
Here I'll show you how to setup the SDK so that you can start using Apex SDK in Visual Studio in your c++ programs.

Download following files from you Apex account -
1. Nvidia PhysX sdk installer (mostly named as PhysX_2.8.4.6_for_PC_Core.msi). If  you are using Apex  1.1 SDK, then you need to install PhysX 2.8.4 and not later version.
2. Then you need Apex SDK (mostly named as APEXSDK-1.1-Build112-PhysX_2.8.4-WIN-VC9.zip).
3. Latest Microsoft DirectX SDK which can be downloaded from here.

Install DirectX SDK, Nvidia PhysX and then Apex SDK. Now you need to add Nvidia PhysX installed location to your system path. To do this, go to System Properties->Advanced System Settings->Environment Variable. Click on New in the top list. Add the variable name NXPATH and variable value as <Location> of your physx sdk(For my case it was "C:\Program Files (x86)\NVIDIA Corporation\NVIDIA PhysX SDK\v2.8.4_win").


Now you are ready to open the visual studio sample projects given with ApexSDK.
  1. Go to your apex installed location/samples/compiler\vc9WIN32-PhysX_2.8.4 or /samples/compiler\vc9WIN64-PhysX_2.8.4 folder. Open the Apex_Samples project. This project consists of 8 sub projects. 
  2. Right click on the properties of SampleRenderer-MT. Go to C/C++->General->Additional Include Directories. Replace the directX include path with the one on your system. (For me it was C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include).
  3. Similarly replace the directx include path in the properties of project "SamplePlatform-MT".
Now compile your projects and you are ready to go. Mostly the project names in the properties are different from the one which are built e.g. ApexHelloWorld sample builds with the name ApexHelloWorldDebug.exe So you'll have to change this name in properties so that you may debug them directly from your visual studio editor. The build location is in ApexSDK home location/bin.


1 July 2012

Creating animated splash screen in Qt

The QSplashScreen widget in Qt provides a splash screen that can be shown during application startup. A splash screen is a widget that is usually displayed when an application is being started. Splash screens are often used for applications that have long start up times to provide the user with feedback that the application is loading. The splash screen appears in the center of the screen. It may be useful to add the Qt::WindowStaysOnTopHint to the splash widget's window flags if you want to keep it above all the other windows on the desktop.

Many times you might feel to add animated splash screens for better look of the splash screen. This can be achieved in multiple ways. One way is to make your own splash screen class by inheriting from QSplashScreen and painting the screen as you wish. I would show a relatively simple approach just by changing the series of images with changing time.

To get a series of images, use a video making tool like After Effect or Sony Vegas. Design your complete splash screen as a video using these tools. When you are ready, publish this video and in publish settings, change the output to png or jpg mode. This will render your video as series of image files. Now you just need to copy these images in your project folder so that they can be easily available while loading splash.

For my case, I copied all my images in icons/splash/ folder. The image names follow the following pattern
SplashScreen_#####. Where ##### is the sequence of image.


QString name = "./icons/splash/SplashScreen_000";
QStringList fileNames;
for(int i=0; i<100; i++)
{
    if(i<10)
       fileNames << (name+QString("0%1").arg(i));
    else
       fileNames << (name+QString("%1").arg(i));
}


Here I read all the image file names into a list. Make sure you don't add the splash images to your resources file as they will be packaged into your build and would consume a lot of useless space. Moreover, if the count of your splash images is too much, then if would fail to compile. So its better to read it from the folder itself.


QSplashScreen *splashScreen = new QSplashScreen(QPixmap(":/icons/splashscreen.png"));
splashScreen->show();
QTime finishingTime = QTime::currentTime().addSecs(5);
int count = 0;
while(QTime::currentTime()<=finishingTime)
{
    if(count>299)
      splashScreen->setPixmap(QPixmap(":/icons/splashscreen.png"));
    else
      splashScreen->setPixmap(QPixmap(fileNames[count/3]));
    count++;
}
splashScreen->hide();



I have taken a timer of 5 seconds to show how the splash images would change to give a feel of animation. Moreover you can ignore the count/3 part in the code. I rendered my splash images at 20fps, so I had to adjust the timing with number of images. You can adjust this based on your design. This method seems simple but I believe its better and simple to overload the QSplashScreen class and design everthing in modular form.

25 June 2012

Common Qt related queries

While using Qt, programmers tend to forget lot of small things, which they need to search again and again while coding. So I compiled some of these issues which I came across in this document.

->Creating modal dialogs in Qt
       CustomMessageBox customMessageBox(..., parent, Qt::Dialog);
       customMessageBox.setWindowModality(Qt::ApplicationModal);
       customMessageBox.exec();

-> Regular expression to check whether MAC address exists in a string
   <string>.contains(QRegExp("(([0-9A-Fa-f]{2}[-:]){5}[0-9A-Fa-f]{2})|(([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})"))

-> Saving QPixmap as file. -

    QPixmap myImage(":/test.jpg");
     myImage.save("newTest.jpg");

Or
   QByteArray bytes;
   QBuffer buffer(&bytes);
   buffer.open(QIODevice::WriteOnly);
   pixmap.save(&buffer, "PNG");


-> Creating simple thread in Qt -
   Create a class inheriting QThread with a function void run().  The content of this function runs on a thread.
   class HelloThread: public QThread
    {
      Q_OBJECT
      private:
            void run();
    }

     void HelloThread::run()
    {
              qDebug()<<"Hello from worker thread" <<thread()->currentThreadId();
    }

    int main(int argc, char *argv[])
   {
         QCoreApplication app(argc, argv);
         HelloThread thread;
         thread.start();
         qDebug()<<"hello from GUI thread"<<app.thread()->currentThreadId();
         thread.wait();
         return 0;
    }

-> Difference between two QDateTime values
       use    QDateTime::daysTo() or secsTo().

-> Disabling a window
       mainWindow->setEnabled(false);
    If there are multiple windows, its better to disable them in pieces.

->  Redirection operator(<<, >>) overloading in a class
      Add a operator overloading friend function to the class as -
     friend std::ostream& operator<<(std::ostream &out, const MyClass &rhs);

     After defining this function, as -
     std::ostream& operator<<(std::ostream &out, const MyClass &rhs)
     {
           out<<rhs.name;
     }

   The class's objects can be used with << operator.

-> Deploying Qt application on windows
    Compile your project in release mode.
    Add resources and settings to your application as shown here.
    Then use dependency walker to check for the dll dependencies of your application. Copy these dlls into the folder of the executable.
    Finally use setup creating softwares like Inno Setup Compiler to package everything in a single executable.

->  View QTreeWidget as read only 
  treeWidget->editTriggers(QAbstractItemView::NoEditTriggers);

-> Change column width of QTreeWidget
   header()->resizeSection(int logicalIndex, int size);

-> Getting QGraphicsTextItem length 
   int length = my_graphics_text_item.toPlainText().size()


-> Find index of a value in QComboBox 
  int QComboBox::findText(const QString &text, Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly | Qt::MatchCaseSensitive) is the function used to get index of the string value in QComboBox.

-> Implementing an Alarm functionality in Qt
// alarm.h
#include <QThread>
#include <QDateTime>
#include <QTime>
#include <QMutex>
#include <QDebug>

class Alarm : public QThread
{
    Q_OBJECT
public:
    Alarm(QTime time, QObject *parent = 0);
    void setEndTime(QTime time);
public slots:
    void stopAlarm();
signals:
    void alarmRing();
protected:
    void run();
private:
    QTime endTime;
    bool stopAlarmFlag;
    QMutex mutex;
};




 // alarm.cpp

#include "alarm.h"

Alarm::Alarm(QTime time, QObject *parent) : QThread(parent)
{
    endTime = time;
    stopAlarmFlag = false;
}

void Alarm::setEndTime(QTime time)
{
    mutex.lock();
    endTime = time;
    mutex.unlock();
}

void Alarm::run()
{
    while(1)
    {
        qDebug()<<"Run";
        if(QTime::currentTime()>=endTime)
        {
            emit alarmRing();
            return;
        }
        sleep(1);
    }

}

void Alarm::stopAlarm()
{
    this->exit();
}




-> Implementing a drag drop functionality between Qt widgets
   When the drag starts in a Qt widget, a new drag is created where mime data is fed into the event. The drop event in another widget reads these mime data. The code below show how to create a drag event with some mime data - 

In the views mouse event create a new drag object to contain the data you want moved,
QDrag* drag = new QDrag( this );
QByteArray ba;
QDataStream* data = new QDataStream(&ba, QIODevice::WriteOnly);
*data << m_slideIndex;
QMimeData* myMimeData = new QMimeData;
    myMimeData->setData("application/x-thumbnaildatastream", ba);
drag->setMimeData( myMimeData );
drag->setPixmap( thumb );
drag->setHotSpot( thumb.rect().center() );
if ( drag->exec() == Qt::IgnoreAction )
{
    qDebug() << "DRAG CANCELLED";
    m_dragging = false;
}
drag->deleteLater();
delete data;


-> Implementing a drag drop functionality between Qt widgets

  A simple display message
   QMessageBox msgBox;
   msgBox.setText("The document has been modified.");
   msgBox.exec();

  A document modified message - 
  QMessageBox msgBox;
  msgBox.setText("The document has been modified.");
  msgBox.setInformativeText("Do you want to save your changes?");
  msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  msgBox.setDefaultButton(QMessageBox::Save);
  int ret = msgBox.exec();
  switch (ret) 
  {
   case QMessageBox::Save:
       // Save was clicked
       break;
   case QMessageBox::Discard:
       // Don't Save was clicked
       break;
   case QMessageBox::Cancel:
       // Cancel was clicked
       break;
   default:
       // should never be reached
       break;
  }

  A Warning message box -
int ret = QMessageBox::warning(this, tr("My Application"),
                                tr("The document has been modified.\n"
                                   "Do you want to save your changes?"),
                                QMessageBox::Save | QMessageBox::Discard
                                | QMessageBox::Cancel,
                                QMessageBox::Save);

  An error message box -
  QErrorMessage errorMessage;
  errorMessage.showMessage("Same resource can't be imported twice");
  errorMessage.exec();
  return;

Adding Signals and Slots to QGraphicsItem

Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal. 


The QGraphicsItem class is the base class for all graphical items in a QGraphicsSceneIt provides a light-weight foundation for writing your own custom items. This includes defining the item's geometry, collision detection, its painting implementation and item interaction through its event handlers.


When we want to create a graphics item of our own, we can inherit from QGraphicsItem. QGraphicsItem is an abstract class. So two functions namely, 

virtual QRectF boundingRect() const;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

These two functions have to be defined in the class which inherits from QGraphicsItem. But QGraphicsItem is not inherited from QObject, so we can't add signals or slots to this. So we need to inherit our class from QObject as well to access the signal slot functionality. QGraphicsObject is a predefined class which does the same thing but it is relatively slow as it has lot of other signals which you might not require. So, I prefer going for my own class which derives from both QGraphicsItem and QObject. The class below is a sample class to do so. 
 
class MyGraphicsObject : public QGraphicsItem, public QObject
{
     Q_OBJECT
     Q_INTERFACES(QGraphicsItem)
public:
     MyGraphicsObject(QGraphicsItem *parent = 0);

private:
     virtual QRectF boundingRect() const;
     virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

}; 



After defining this class, you can add signals/slots of your choices.


21 March 2012

Installing FLANN on Ubuntu 11.10

FLANN is a library for performing fast approximate nearest neighbor searches in high dimensional spaces. It contains a collection of algorithms we found to work best for nearest neighbor search and a system for automatically choosing the best algorithm and optimum parameters depending on the dataset. FLANN is written in C++ and contains bindings for the following languages: C, MATLAB and Python. 

I noticed two ways of installing FLANN on ubuntu namely the easy way and the hard way. Lets start with the hard way.

1. Install the basic dependencies
  • cmake
  • libhdf5-serial-dev
  • python-numpy (for python bindings)
  • python-h5py
2. Now download the latest version of flann from http://www.cs.ubc.ca/~mariusm/index.php/FLANN/FLANN. 
3. Extract it in a folder and run 'cmake' command in that folder.
    If all the dependencies and PATH are defined in your system, it will install neatly.
4. If it shows errors, open the Makefile and  check if you have defined all the paths. 'cmake' command will give you errors on which you may need to install other dependencies. Keep repeating until you make through. :-P

Easy way is to install FLANN from the PCL repository.
1. Open the terminal and type -
    sudo add-apt-repository ppa:v-launchpad-jochen-sprickerhof-de/pcl
2. Now update your repository by typing -
    sudo apt-get update
3. Now go to your synaptic package manager, and search for flann. Check it and install it.


8 February 2012

Setting up DirectX SDK in Visual Studio 2010

Microsoft DirectX is a collection of application programming interfaces (APIs) for handling tasks related to multimedia, especially game programming and video, on Microsoft platforms. The DirectX software development kit (SDK) consists of runtime libraries in redistributable binary form, along with accompanying documentation and headers for use in coding. Originally, the runtimes were only installed by games or explicitly by the user. Direct3D 9Ex, Direct3D 10, and Direct3D 11 are only available for Windows Vista and Windows 7 because each of these new versions was built to depend upon the new Windows Display Driver Model that was introduced for Windows Vista. The new Vista/WDDM graphics architecture includes a new video memory manager supporting virtualization of graphics hardware for various applications and services like the Desktop Window Manager.

Download and install the latest DirectX SDK binary from microsoft's website - http://msdn.microsoft.com/en-us/directx/aa937788.

After installation of SDK, you can run DxDiag.exe to check your current DirectX settings or for any existing problems.

Setting up the include and library directories on Visual Studio -
1. You can add the include settings and library settings of the directx sdk present in your program files folder individually for every project. This is advisable if you are working on a serious project. But if you are lazy to do so, you can add it to the global settings so that it is incorporated in all your projects.
2. Open the "Property Manager" from the View menu.

3. Expand the project/configuration tree and select the "Microsoft.Cpp.X.user" user file. Right click and select "Properties".

4. Add the DirectX SDK include directory to the "VC++ Directories">"Include Directories" -
   $(DXSDK_DIR)Include
   Add the DirectX SDK library directory to the "VC++ Directories">Library Directories" -
   $(DXSDK_DIR)Lib\x86

5. Now, open your DirectX project and add the DirectX linker settings to it.
   Expand the "Linker" section and select "Input" and under "Additional Dependencies" add the necessary lib files -
  d3d11.lib
  d3dx11.lib
  dxerr.lib

Now you are ready to start coding with DirectX.

You can download sample files from here and include them in an empty project to see how a DirectX code looks like. This is a simple code which just displays a blank DirectX screen.


14 January 2012

Unity3d - Removing Render Texture alpha

 If you want to apply the Render Texture to a GUITexture object so it will be sized to the screen with other GUITexture objects. But GUITextures don't have indpendent materials or shaders so you won't be able to remove the alpha which is added to the render texture. Just setting the color of the GUITexture and the Render Texture camera to white and 100% still leaves the Render Texture transparent. So you can remove the alpha values from that render texture using the following script.




var alpha = 1.0;
private var mat : Material;

function Start ()
{
    mat = new Material(
        "Shader \"Hidden/Clear Alpha\" {" +
        "Properties { _Alpha(\"Alpha\", Float)=1.0 } " +
        "SubShader {" +
        "    Pass {" +
        "        ZTest Always Cull Off ZWrite Off" +
        "        ColorMask A" +
        "        SetTexture [_Dummy] {" +
        "            constantColor(0,0,0,[_Alpha]) combine constant }" +
        "    }" +
        "}" +
        "}"
    );
}

function OnPostRender()
{
    GL.PushMatrix();
    GL.LoadOrtho();
    mat.SetFloat( "_Alpha", alpha );
    mat.SetPass(0);
    GL.Begin( GL.QUADS );
    GL.Vertex3( 0, 0, 0.1 );
    GL.Vertex3( 1, 0, 0.1 );
    GL.Vertex3( 1, 1, 0.1 );
    GL.Vertex3( 0, 1, 0.1 );
    GL.End();
    GL.PopMatrix();

Apply this script to camera of whose render texture is to be taken.

Installing Kinect on Windows(OpenNI) with Unity Wrapper

Real application of Kinect starts when you integrate it with any gaming engine. For a head start, OpenNI provides a wrapper in C# to be used in Unity3D gaming engine. They have also given a sample code for Ogre. You can start working on them after few simple steps of installation.

The fastest way to get started with this is to install the Zigfu bundle and Wrapper for OpenNI in Unity.
1. Download and install the latest zigfu development bundle from http://www.zigfu.com/devtools.html. This bundle install OpenNI, NITE and sensor drivers for kinect.
2. Now download the Zigfu Unity3D wrapper and extract it in your project. It contains sample scenes for help.
Now you can start developing your own motion sensing applications and games.

If you want to go with the latest OpenNI drivers and wrappers, you'll have to install them individually.
1. Download OpenNI module from http://75.98.78.94/Downloads/OpenNIModules.aspx. Select "OpenNI Binaries" in the first drop down box, then select the stable/unstable release and then download the specific module according to your system. If you want to work with PCL also, download this module from http://pointclouds.org/downloads/windows.html in the OpenNI column.
2. Now select the "OpenNI Compliant Middleware Binaries"  and download the middleware binary for your system.
3. Select the "OpenNI Compliant Hardware Binaries" and download stable/unstable release according to your system. If you want to work with PCL, you'll have to download it separately from http://pointclouds.org/downloads/windows.html under the sensor column.
4. Install all these downloaded binaries in the order in which they were downloaded.
5. Now you'll have to download the Unity3d wrapper from https://github.com/OpenNI/UnityWrapper according to your version of binaries.
6. Extract this and include in your project to get started with using kinect for application development.
7. Or you can download the Sinbad sample in Ogre from https://github.com/OpenNI/SampleAppSinbad to get started in Ogre gaming engine.

12 January 2012

Unity3d - Displaying output of one camera as background of other camera

Unity is an integrated authoring tool for creating 3D video games or other interactive content such as architectural visualizations or real-time 3D animations. Unity's development environment runs on Microsoft Windows and Mac OS X, and the games it produces can be run on Windows, Mac, Xbox 360, PlayStation 3, Wii, iPad, iPhone, as well as the Android platform. It can also produce browser games that use the Unity web player plugin, supported on mac and Windows but not Linux. The web player is also used for deployment as Mac widgets.

I was working with a problem where I needed to display the output of one camera as the background image of the other camera. The concepts used in this method are applicable at lot of places during the game development like showing the mirror effect in car races or the score board. 

I would suggest to go through the following links before using this method. The following links will give a rough idea about the Camera, GUI Texture, Render Texture and Layers in Unity.

Setup your scene with the Main Camera showing your game objects. After this follow these steps to create the background using a different Camera  -

1. Create a new camera (GameObject->Create Other-> Camera), and name it "Background Camera".

2. Create a new GUI Texture (GameObject->Create Other->GUI Texture), and name it "Background Image".

3. Click the "Layer" dropdown menu in the Background Image's inspector pane, and select "Add Layer".

4. In the next free "User Layer" slot, create a new layer name called "Background Image". This will be directly under the layer named "Terrain" if you haven't yet added any others.

5. Select your Background Image in the hierarchy, and give it the desired texture, and set the x, y, width and height under "Pixel Inset" so that it fills the screen appropriately.

6. Near the top of the inspector window, Use the layer dropdown menu to assign your "Background Image" layer that you defined earlier to this game object.

7. Now select your Background Camera in the hierarchy, and adjust these settings in the inspector:
   * Un-check Flare Layer and Audio Listener (but leave GUILayer enabled)
   * Set Clear Flags to Solid Color
   * Set Depth to -1
   * Set Culling Mask, first to "Nothing", and then to "Background Image"

8. Now Select your other (Main) camera and in the inspector:
   * Set its Clear Flags to "Depth Only"
   * Click its culling mask setting, and un-check "Background Image". This should result in the culling mask displaying as "Mixed..."

9. Now your system should display the Background GUI Texture as the background of the main camera. Since, this GUI Texture is the background for the present camera, you can change it dynamically from the script to display any other video texture or Render Texture from other camera. Render Texture is  used to render the camera view to a texture which can be displayed somewhere else.

10. Create a new Render Texture by Assets->Create->Render Texture. Give a proper name to this render texture and edit the properties according to your needs.

11. Now setup the scene and a camera whose output you want to display as the background in the Main Camera.

12. Go to the 'Target Texture'  option of this new camera and assign the render texture you created to it.

13. Now make a script which assigns this render texture as GUI Texture to the Background Image you created earlier.