Friday, October 21, 2016

CUDA bit-reverse template demo

I have Nvidia GEFORCE with CUDA graphics card with the 410M processor. I suppose this configuration is the oldest processor in the NVidia family of graphics cards. I feel I am lucky as I had this processor shipped in my laptop by sheer serendipity. I happen to know a little bit of game programming so would like to try my luck at developing with CUDA. It is a parallel computing platform and programming model. When Nvidia introduced CUDA it stood for compute unified device architecture.

I researched a bit on installing Nvidia CUDA and found that first Nvidia drivers need to be installed(which were'nt there). Then I installed the CUDA 5.5 toolkit. I chose the 5.5 version because my Ubuntu 14.04 LTS version with Nvidia GEFORCE 410M processor would only support that version of toolkit.

After much helter-skelter I found that you could not do programming with CUDA in normal eclipse. You needed the Nsight Eclipse. I downloaded all of these and finally I was ready to run a CUDA program. Found that C programs in CUDA have .cu extension.
Went to file-> New Project-> CUDA C/C++ Project
Gave a name to the project.
In the next screen let the defaults as it is:
In the next screen clicked finish.
Then right clicked on the project name->New->Source File
Then in the interface give a name to the file with .cu extension. You may choose any template of your choice and as I am new to CUDA I just let the default as it is(Bit-reverse demo template). With this basic experience in running a bit reverse program whose output looks like
Input value: 0, device output: 0, host output: 0
Input value: 1, device output: 128, host output: 128
Input value: 2, device output: 64, host output: 64
Input value: 3, device output: 192, host output: 192
Input value: 4, device output: 32, host output: 32
Input value: 5, device output: 160, host output: 160
Input value: 6, device output: 96, host output: 96
Input value: 7, device output: 224, host output: 224
Input value: 8, device output: 16, host output: 16
Input value: 9, device output: 144, host output: 144
Input value: 10, device output: 80, host output: 80
Input value: 11, device output: 208, host output: 208
Input value: 12, device output: 48, host output: 48
Input value: 13, device output: 176, host output: 176
Input value: 14, device output: 112, host output: 112
(so on upto 255)
I was a bit encouraged to try and develop parallel fast games using CUDA.
Wondering what this device and host mean. To hazard a guess the device means the Graphics card and the host means the Main CPU. Feeling too tired to try and understand the code. I hope you understood what the program does.
If interested you can download the program from here. Thank You :-)

Sunday, October 16, 2016

Noisy texture to a polygon using procedural noise

It is very easy to generate noise using methods mentioned in this site. By using functions smoothNoise, turbulence etc mentioned in the site, It was quite straight--forward to generate the following noisy texture. I am doing this in Java- bindings for OpenGL.


Here is the source.

Further modification of the program to generate textures such as mixture of sin wave and noise as mentioned in the same site gave the following result.


The modification in the above program was replacing turbulence function by:

 for (int y = 0; y < 128; y++) {
            for (int x = 0; x < 128; x++) {
                // to do use a formula here to create a texture
                value=(1 + Math.sin( (x + turbulence((double)x ,(double) y,64 ) / 2 ) * 50) ) / 2;
                int red=(int)((0xff)*value);
                int green=(int)((0xff)*value);
                int blue=(int)((0xff)*value);//*(x^y);
                buffer.put((byte) ((0xFF)&red));     // Red component
                buffer.put((byte) ((0xFF)&green));      // Green component
                buffer.put((byte) ((0xFF)&blue));               // Blue component
                buffer.put((byte) (0xFF));    // Alpha component. Only for RGBA

in the init function.
The explanation in the site is much clearer and you can refer that as well.

Similarly the following formula(done in a different way) for value yields this pattern:

xyValue = x * xPeriod / noiseWidth + y * yPeriod / noiseHeight + turbPower * turbulence(x, y, turbSize);
value = (1+Math.sin(xyValue * 3.14159))/2;
Where the following variables have to be defined before hand:
           //xPeriod and yPeriod together define the angle of the lines
           //xPeriod and yPeriod both 0 ==> it becomes a normal clouds or turbulence pattern
           double xPeriod = 5.0; //defines repetition of marble lines in x direction
           double yPeriod = 10.0; //defines repetition of marble lines in y direction
           //turbPower = 0 ==> it becomes a normal sine pattern
           double turbPower = 5.0; //makes twists
           double turbSize = 32.0; //initial size of the turbulence

With these variables the following wooden texture was obtained:

double xyPeriod = 12.0; //number of rings
double turbPower = 0.1; //makes twists
double turbSize = 32.0; //initial size of the turbulence
And in the loop:
                double xValue = (x - noiseWidth / 2) / (double)(noiseWidth);
                double yValue = (y - noiseHeight / 2) / (double)(noiseHeight);
                double distValue = Math.sqrt(xValue * xValue + yValue * yValue) + turbPower * turbulence(x, y, turbSize);
                double sineValue = Math.sin(2 * xyPeriod * distValue * 3.14159);
                int red=(int)(0xff+(0xff)*sineValue);
                int green=(int)((0x20)*sineValue);
                int blue=(int)((0x20));//*(x^y);
                buffer.put((byte) ((0xFF)&red));     // Red component
                buffer.put((byte) ((0xFF)&green));      // Green component
                buffer.put((byte) ((0xFF)&blue));               // Blue component
                buffer.put((byte) (0xFF));    // Alpha component. Only for RGBA

Here is the texture output:

Tuesday, October 11, 2016

L-Systems- #JOGL progam to draw a tree using lindenmayer system

Happy vijayadashami to all my blog readers.
I have drawn a tree using java bindings for OpenGL. It took three hour sittings for three times to arrive at the code. Had a torrid time debugging. Phew! Still feeling giddy. For theory please refer to the wiki page here=>

It has some problems such as a line from last drawn point to first and no finishing in the recursion. Just thought will refine the program later.
Here is the code.

Wednesday, September 28, 2016

Play a video or video capture of webcam - texture on cube #OpenGL #OpenCV

Just visit this awesome site where there is this awesome code which needed a few modifications to play back any video or capture video from the webcam. Now you won't believe this but it was as simple as that. With OpenCV I feel I just turned a pro.


The mouse can be used as a trackball and you can rotate the scene which consists of a cube on both sides of which texture of video frames is mapped. That snapshot was not still image, it is me sitting in front of the computer poring over the monitor.

Monday, September 26, 2016

Add an image opened using #OpenCV as a texture in OpenGL

Image result for what is OpenCVI was toying with OpenCV since a long time. Here is a tutorial on how to download and configure eclipse for OpenCV. OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel's research center in Nizhny Novgorod (Russia), it was later supported by Willow Garage and is now maintained by Itseez.

In this C++ project in Eclipse an image is opened using imread method of OpenCV tool and the opened image is used as a texture to map to a polygon drawn across the screen. Actually this image in the window is itself a snapshot of another program which does texture mapping inside a 3D house scene program. I could have chosen any other image.


Here is the source.

Sunday, September 18, 2016

#JOGL Various Textures to Polygons

Some textures of size 10 x 10 have been created from scratch using formulas such as

//Use this formula to create texture in the program- Tex1
int red=(0xff)*(x%2);
int green=(0xff)*(y%2);
int blue=(0xff);//*(x^y);

Resulting Snapshot:

//Use this formula to create texture in the program- Tex2
int red=(int)((0xff)*Math.sin(10*x*Math.PI/180));
int green=(0xff);
int blue=(0xff);
Resulting Snapshot:                       
Going any further I feel would kill your drive to do something yourself and get exhilarating textures. So I hope I have shown the way to create beautiful patterns with sin,cos,modulus and other mathematical operations.

If you are knowledgeable about MipMaps and filtering in jogl then you may note that I have used following Magnification filter.


Here is the source which you can modify and tinker.

Next goal is to  use procedural methods to generate textures as mentioned in the site here.

#JOGL Lighting - An Animation

In this project the light source is moving in a circular path around the eight spheres. The position of GL_LIGHT0 is changed by a call to the method glLightfv().


Here is the source.