Sunday, May 19, 2013

Modern OpenGL 3.x: Perspective Projection using GLM (OpenGL Mathematics)

In my previous article you may notice that in vertex data  for drawing a triangle, I have fixed the co-ordinate of z-axis to 1. If you change the value of z-axis greater than 1 or less than -1 then Image will not render on the screen because we haven't define the perspective projection matrix. In this project we are using GLM ,  in previous article there is no especial use of it so I haven't talk about it before but here we use GLM function for perspective projection .GLM is header only library made by heavy use of C++ templates due to which performance can be reduce but make programming easier. GLM can be use with GLSL easily so we are using GLM.

Saturday, May 18, 2013

Modern OpenGL 3.x Drawing the basic shapes:Tutorial02

This article is almost similar to my previous tutoirals series. In previous article we draw only triangle but here we are drawing other shapes.Vertex data for drawing is given below:

// Put the 18 triangle verticies into the VBO
    GLfloat vertexData[] = {
        //  X     Y     Z

        //Pentagon
        0.25f, 0.25f, 1.0f,
        0.75f, 0.25f, 1.0f,
        0.25f, 0.5f, 1.0f,

        0.25f, 0.5f, 1.0f,
        0.75f, 0.25f, 1.0f,
        0.75f, 0.5f, 1.0f,

        0.25f, 0.5f, 1.0f,
        0.75f, 0.5f, 1.0f,
        0.5f, .75f, 1.0f,

        //Triangle
        -0.25f, 0.25f, 1.0f,
        -0.5f, 0.75f, 1.0f,
        -0.75f, 0.25f, 1.0f,
        //Trapezoid
        -0.2f, -0.25f, 1.0f,
        -0.35f, -0.75f, 1.0f,
        0.35f, -0.75f, 1.0f,

        0.2f, -0.25f, 1.0f,
        -0.2f, -0.25f, 1.0f,
        0.35f, -0.75f, 1.0f,
    };

Introduction to Modern OpenGL 3.x with shader GLSL (OpenGL shader language) :Tutorial01

Shader are compulsory on Modern OpenGL and most important. Shader are a little program they run in GPU i.e for OpenGL, shader run in rendering hardware. There are many shader language available but we use GLSL (OpenGL Shader Language) which is very similar to C but not C. For beginner, shader can be new things so just think it is a little program written in different language that run in  your graphics card. Without shader nothing can be displayed on the screen. Shader takes vertex data as an input, process it and return output value to display on the screen it is vertex processing shader which is called vertex shader. In this program we also use another shader called fragment shader which is used to output the color pixel of fragment. How shader takes input and vertex are processed you will understand later.
Lets start digging the code and I will explain related theory side by side in short so that you can learn quickly and start to write you own program.You can also download the complete source code of this tutorials.
In this whole tutorials series we are using four things  GLFW for creating window, GLEW to acess OpenGL API function at run time , GLM for doing some math for us like rotation, translation etc and GLSL as shader language.

Tuesday, May 7, 2013

Beginner learning materials for Modern OpenGL API version 3.2

Modern OpenGL may be difficult for especially beginner because of vertex shader and fragment shader, VAO (vertex array objects) and VBO (vertex buffer objects) . You also have to understand GLSL (OpenGL shader language). So, I am trying to prepare the tutorials from scratch for beginner. Follow my blog I will prepare further article for learning Modern OpenGL where I will explain about vertex and fragment shader,VAO , VBO and other many things in details. My tutorials will focus on game programming. I use OpenGL API version 3.2 . Your GPU must have support for OpenGL API version 3.3. Before starting you have to setup the libraries. I have choose Codeblocks IDE because it is open source and cross platform. Setup instruction Codeblocks is available here(setup Modern OpenGL in codeblocks) . After library setup is completed just compiled and run the code below to be insure that all libraries are successfully setup  and your GPU graphics hardward have support for OpenGL API version 3.2.
You can download source code form here.

Setup Modern OpenGL ( 3 and 4) with GLFW, GLEW and GLM in Codeblocks

Modern OpenGL 3 and above version may be difficult especially for beginner because of all the new shaders   and buffer object concepts. OpenGL 1.x is simple to understand and to setup the project also which you can find in my previous articles here. By setting the GLUT or just by including gl.h and glu.h we can acess the OpenGL function because all the API function are determined in compiled time but in modern OpenGL concept is different that all the API function are determined in run time not in compiled time. 
Introduction to GLFW,GLEW and GLM. 
Lets discusses about all this open source library why they are necessary.
GLEW (OpenGL Extension Wrangler ) will handle the run time loading of the OpenGL API. This will give access to the OpenGL 3.2 and above API functions. It is important because accessing OpenGL functions isn't as simple as #include <GL/gl.h> unless we want to use an ancient version of OpenGL. In modern OpenGL , the API functions are determined at run time,not compile time.

GLFW (OpenGL Frame Work) is used to create a window, and receive mouse and keyboard input in a cross-platform way. We can use freeglut as a alternative of GLFW . GLFW is especially design for game. OpenGL does not handle window creation or input, so this must be done with the help of GLFW or other alternatives. 
GLM(OpenGL Mathematics) is a mathematics libray that handles vectors and metrices. Older versions of OpenGL provided functions like glRotate, glTranslate and glScale which would so some of the mathematics but in modern OpenGL previously mentioned functions do not exist and we have to do by ourselves. GLM will help us to do math.

Saturday, December 29, 2012

Using OpenCV in GTK sample program "Inverting an image on Image Processing"

In this article I want to show you How we can use the OpenCV on GTK (GIMP toolkit) on fedora.Although GTK+ is a GNU project like the GMP, it is released under the terms of the more liberal LGPL(lesser general public license) that permits software (including closed source proprietary software) to be written using GTK+ without payment of fees, royalties, or other restirictions. The freedom offered by the GTK+ license is in contrast to its competitor QT(you must instead purchase a commercial Qt license in that case).
This program is compile using gcc compiler on fedora. At first you need to install the gtk and gnome on your machine.
In fedra that use RPM packages, you should have at least install the following RPM package.
$  su

$  yum install gtk2
$  yum install gtk2-devel
$  yum install gtk2-engines
$  yum install libgnome-
$  yum install libgnome-devel
$  yum install libgnomeui
$  yum install libgnomeui-devel

Now its time to install OpenCV package.

Using semaphore to synchronize the multiple thread using c on linux/fedora

Semaphore is a new type of variable introduced by E. W. Dijkstra. You may learn about semaphores in university. It is used to solve the Producer-Consumer Problem in Operation system design which I have learn in books "Modern Operating Systems " (second edition) by Andrew S. Tanenbaum. In that books the algorithm for semaphore is clearly defined and easy to understand. But I was always curious about that "Does this type of problem occur on our program?" rather than operating system design and "Where can I use it?". When I begin to learn linux programming I found the semaphore function under <semaphore.h> header and example program where semaphore is used to synchronize the thread.
In this article we look at the simplest type of semaphore, a binary semaphore that takes only values 0 or 1. There is also a more general semaphore that takes a wider range of values. Normally, semaphores are used to protect a piece of code so that only one thread of execution can run it at any one time.The semaphore functions do not start with pthread_,as most thread-specific functions do,but with sem_. Four basic semaphore functions are used in threads. They are all quite simple.