Saturday, April 6, 2013

How to plot / draw sine curve in Open GL with step function.

Our theoretical graphics lecturer gave us an assignment of drawing sine curve in opengl with step function. After weeks of hunting in WWW I couldn't find a success solution. So I came up with my own solution given below.

First let me explain what is step function. For example if you want to draw a line in opengl there are existing functions. But what is happening inside them? Well if you just start draw like this basic method given below.

First calculate starting point. x0,y0. Then you increase x by one and you calculate respective y value using line equation, y=mx + c
Then you draw that pixel by (x+1,round(m(x+1)+c)) note that you have to round the value of y since pixels are in integer format.

But due to rounding this function is very inefficient. So there exists a simpler method. 

This is the alternative for that using step function.

void drawline2(int x0, int y0, int x1, int y1)
{
    int dx = x1-x0;
    int dy = y1-y0;
    int ddy = 2*dy;
    int ddx = 2*dx;
    int pk = ddy-dx;
    int yk = y0;
    int xk;
    glBegin(GL_POINTS);
    for(xk=x0;xk<=x1;xk++)
    {
        glVertex2i(xk,yk);
        if(pk<0 br="">        {
            pk += ddy;
        }
        else
        {
            pk += ddy-ddx;
            yk++;
        }

    }
    glEnd();
}


If you study the code carefully + you have a basic mathematics you can understand the mechanism of code.  Since this doesn't processor extensive floating point operations this is more faster.

Same manner I tried to draw the sine curve using step function. The algorithm is given below.





"
#ifdef __APPLE__
#include
#else
#include
#include
#endif

#include
#include
#define PI 3.14159265

void reshape(GLsizei w, GLsizei h)
{
    if (h==0)
        h = 1;

    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w<=h)
        glOrtho(0.0f,250.0f,0.0f,250.0f*h/w,1.0,-1.0);
    else
        glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0,-1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

void writepixel(int x, int y)
{
    glBegin(GL_POINTS);
    glVertex2i(x,y);
    glVertex2i((180-x),y);
    glVertex2i((180+x),(-y));
    glVertex2i((360-x),(-y));
    glEnd();
}

void drawsine2()
{
    int x = 0;
    int y = 0;
    float temp = 57.3 * sin(PI/180);
    float pk = 0;
    while(x<=85) //after reaching 85 the change of Y is significantly low and curve fails to get it.
    {
        if(pk>=0)
        {
            writepixel(x,y);
            pk -= temp*sqrt(1-((y)*(y)/3283.29));//cos((x)*PI/180.0);
            x++;
        }
        else
        {
            writepixel(x,y);
            pk += 1 - temp*sqrt(1-((y)*(y)/3283.29));//cos((x)*PI/180.0);
            x++;
            y++;
        }
    }
    while(x<=90)
    {
        writepixel(x,y);
        x++;
    }
}



void Sinecurve(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0,0.0,1.0);
    drawsine2();
    glLoadIdentity();
    glTranslatef(0.0,125.0,0.0);
    glFlush();
}

void initial(void)
{
    glClearColor(0.0,0.0,0.0,0.0);
    glShadeModel(GL_SMOOTH);
}

/* Program entry point */

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(800,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Sine Curve");
    initial();
    glutDisplayFunc(Sinecurve);
    glutReshapeFunc(reshape);
    glutMainLoop();

"

Hope this might help anyone. :)

Monday, December 10, 2012

Interactive way of doing presentations

You guys may already using this technique. But thought to share it with you guys.
There is an alternative method of doing presentation rather than typical way of using slides.
This method is effective for Marketing presentation or Marketing research presentations etc. In my personal view I don't think this is a good way for academic or any other professional presentations but I would like to hear your feed backs on what this is good for and what it isn't.

Ref:-
Open source tools that have more features.


Demo of Infact in action - http://apps.forskning.se/InfactPlaneten/index.html?lang=eng

Infact - http://infact.se/english/

Dizzy (with demo) - http://dizzy.metafnord.org/#intro

Impressi.js - https://github.com/bartaz/impress.js

Reveal.js (with demo) - http://lab.hakim.se/reveal-js/#/

Impressive - http://impressive.sourceforge.net/

Sozi (an addon to Inkscape) - http://sozi.baierouge.fr/wiki/doku.php

Friday, December 7, 2012

Mulithreading in 'Quickly', python RAD tool by canonical

I was doing my WiMixer application through Quickly tool by Canonical for developing application for Ubuntu/Linux. But when I had to do socket programming phase I can't start a threading inside the Quickly.

I was so frustrated and tried everything. After a while a help from Michael Hall in IRC chat of Quickly channel + thanks to this this post in askubuntu. I found out how to do it. I thought it might be valubale to some if I shared it in here with you.

What you have to do is simple. At the very beginning of the python file which your thread will fire, include these lines.

import threading
from gi.repository import GObject
GObject.threads_init()

and then as usually done in python just start the thready as

listener_thread = threading.Thread(target=function_name)
listener_thread.start()

We want to initialize the GObject as quickly uses gi repository to import GUI from the glade file. So it initially starts in a separate thread at beginning of the loading of GUI. So the next threads we start won't be started until GUI (gobject thread) is closed.

I'll conclude this post as my usual statement. If you are using open source, you will always have a way to solve your problems or you might find a friend that will help you. :-)

Tuesday, November 13, 2012

How to zip a folder in google drive without having repeated files in it

Using google appscript we can zip a google drive folder. But one problem arises. In google drive you can keep repeating named (same named) files in single folder. But zip file can't do it. So gives a fatal error without even giving a hint. By using this code you can get rid of repeated files and zip folder.
function zipfolder(e) {
  var foldername = e.parameter.emailAddressbx;
      try{
      var folder = DocsList.getFolder("CV/"+foldername);
        }
  catch (e) {
    Browser.msgBox(e);
    Browser.msgBox("Given folder doesn't exist!");
    }
  var cvlist=[];
  var j=0;
  var found = false;

  for(i=0;i    for(k=0;k    if(folder.getFiles()[i].getName() == cvlist[k].getName())
    { found = true;
    break;}
      }
    if (found == false){
   cvlist.push(folder.getFiles()[i].getBlob());
      j++;
    }
    found = false;  
  }
  if(cvlist.length>0){
  var zip = Utilities.zip(cvlist, 'For_download.zip');
  folder.createFile(zip);
  }
}

Saturday, October 27, 2012

OpenGL and freeglut on Linux + Codeblocks

Hey everyone,
After long time I'm writing on this blog. Hope to keep things up from here onwards but cant promise.

This post is about doing OpenGL programming in codeblocks.

Codeblocks is a C/CPP IDE. Usually opengl c programming is done in this. Our lecturer, Ms. G. S. Makalanda ( Senior Lecturer) B.Sc.(Math)(SL), M.Sc.(Stat)(SL), M.Sc. (Comp. Sci.)(UK),  has asked us to install code-blocks in order to do C programming on opengl. Well everyone except me is working on windows pc's. This is my story, problems, solutions I found to overcome to run code successfully on codeblocks.

Codeblocks was in Ubuntu Software center so no problem in installing it. I guess there was opengl installed in default. When I create new opengl project, the sample code runs smoothly. But when I create a new glut project it says,  
CG/gluttest.o:gluttest.c:function display: error||undefined reference to 'glClear'|
CG/gluttest.o:gluttest.c:function display: error||undefined reference to 'glColor3f'|
CG/gluttest.o:gluttest.c:function display: error||undefined reference to 'glBegin'|
CG/gluttest.o:gluttest.c:function display: error||undefined reference to 'glVertex3f'|
CG/gluttest.o:gluttest.c:function display: error||undefined reference to 'glVertex3f'|
CG/gluttest.o:gluttest.c:function display: error||undefined reference to 'glVertex3f'|
CG/gluttest.o:gluttest.c:function display: error||undefined reference to 'glVertex3f'|
*some more references like same*
||=== Build finished: 33 errors, 0 warnings ===|
 like wise gives build error for each gl function. Other guys on windows pc had the same problem but they seems to fix it when they changed glut library. So I did some search and found that I have to install freeglut (linux version of the glut).

Sunday, August 22, 2010

Gmail Video chat and voice calls on linux!!!!

සුභ අරන්චියක් කියන්න මෙ හදන්නෙ! දැන් google voice  and video chatting පාවිච්චි කරන්න debian linux users අවස්තාව උදාවෙලා. http://www.google.com/chat/video මේ stie එකෙන් එකට ඔන firefox plugin download කරන්න පුලුවන් .එතකොට inbox එකෙ ඉදන් ගන්න video calls ගන්න පුලුවන්check the demo අවුරුදු 2ක් පහුවෙලා හරි google team එක ගත්තු මෙ පියවරට Linux users ලගෙන් උනුසුම් සුභ පැතුම් !

Monday, January 25, 2010

What is all about shell????

Hell NO command line??????

Linux shell is more user firendly than normal text mode OS before. Why the hell I'm telling that? Its because it make the passion inside you to use keyboard than mouse and it can automaticaly complete a long command or filename, retrieve a command recently executed , or edit a command recently used. (Magic isn't it?)

If you are login to linux using command line interface (default login for slackware, which lolipro is using :-) ) the chances are that you are default in shell, but if you are in graphical user interface, go to a program called terminal also known as xterm, Konsole etc. with respective to the OS.



                                            xterm


konsole

Most of KDE desktop systems use Konsole. OS like Ubuntu uses terminal. This is like windows prompt but have more powers built in ;-) It enables you to be contacted with the shell it self. If you cant find it hit for run and type xter or konsole. That should do the trick.

Mostly used internal and external shell commands
No matter you are using bash or anyother shell program most of commands offered are similar. Feel free to use the help menu by using 'man' command. I have mentioned mostly used internal commands below.

1) Changing the working directory :- Whenever you're runnning a shell, you're working inside a specific directory(or shall we say a folder). If you type "cd /home/lolipro" changes your current directory to /(root folder)->home(much like my documents in window)->lolipro (my user account) If my user home directory is /home/lolipro then if I type cd~ takes me to my default home directory without wasting time to type the whole path.

2)Ever wonder how to know in which folder I'm currently working on? If so the command you are looking for is "pwd" which prints the path you are currently working on to the screen.

3) Display the line of text :- typing "echo Lolipro" causes the system to display the string Lolipro in screen. It is very useful when you want to put something happening inside the kernal to the screen. You'll learn more when you study further.

4)Eecute a program :- The exec command runs an external program that you specify. For instance If I type "exec lolipro" it runs the lolipro program. You can also do it by just typing the name of program. For instance if I want to open 'vlc player' I would just type vlc and hit enter. But this exec command have a special feature, when you just runing the program it makes a parallel thread to process alongside with the shell. When you type exec it makes the process inside the shell and when the new process terminates the shell is also terminates.

5) Time and operation :- This is like a stopwatch . For example, typing time cd~ will tells you how much time taken by the system to execute the cd command. The time is displayed after the full command terminates. Three times are printed as : total execution time (real time), user CPU time, and system CPU time. The last two tells how much time CPU has used to process. Its more likely to be less than total execution time.

6) Set option :- In its most basic form, set displays a wide variety of options relating to bash operation. These options are formatted much like environment variables, but they aren't the same things. You can pass various options to set to have it affect a wide range of shell operations.

7) Terminate the shell :- The exit and logout commands both terminates the shell. The exit command terminates any shell, but the logout command termiantes only login shells-that is, those that are launched automatically when you initiate a text-mode login as opposed to those that run in konsole windows or the like.

Note: This is not a complete list but a short introduction. with the help of  LPIC-1 study guide.