Tuesday 31 December 2013

Initialization of variables

Initialization of variables:

                                              When declaring a variable, its value is by default undetermined. But you may want a variable to store a determined value when you declared it. In order to do that, you can initialize the variable. There are two ways to do this in C++:

 1-First way like c:

   Syntax:
                                 Data type variable name = initialize value;

 For example:

      Int a = 0;
     Float pi=3.4;

 2-other way initialize a variable called constructor initialization:

Syntax:
                   Data type variable name(initialize value);

For example:

                           Int a(0);
                           Float pi(3.4);


Scope of a variable

Scope of a variable:

A variable can has global scope or local scope.

Local scope:

         If we declare a variable within main function. Then its scope will be local. We can only use these variable only main function if we use it other  than main function then compiler will generate the error .So we can say that local variable are those variable which we declare within body of function  or block. And its will be within the block or function.

Example:

     Main()
{
Int a;
Char c, b ;
}
Here a,b and c are local variable.

Global variable:

                           Global variable are those variable which we will declare outside the function.  And its scope will be over the entire program. We can use it anywhere in the program or any function or Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration.

Example:

Int a;
Char b,c;
Main()
{
…………
………………..
………………….
}
Here a,b and c are the global variable.

Note:

         If we use same variable name as different data type then compiler will generate the error

  Example:

            Int a;
           Char a;





Monday 30 December 2013

Declaration of variables:

Declaration of variables:

                                         In order to use variable in c++ we must declare a variable . declaration mean specify the data  which store in the variable.
The syntax of declaration of variable is 

syntax:

Int a;
Char c;
Float number;

Explanation:

                                In the first statement variable name is ‘a’ and int is its data type . it mean variable a reserve the space in the memory which only can store integer type value. Every statement in c++ is end with semicolon . similarly in the second statement variable ‘c’ store the character value . variable ‘number’ store the float value .

Note:

     if we store the value 4 in number variable whose data type is float . whenever ‘4’  is integer value so the compiler take ‘4’ as ‘4.0000’ which is now become a floating value.

More example:

       Int a;
       Int b;
       Int c;
Here is three variable a, b and c which has same data type .so we can declare these three as single statement so we can write it as 
     Int a,b,c;
So now we can say that if the variable which has the data type can declare it single statement rather than declare it more than one statement.


Saturday 28 December 2013

Data Type in C++:

Data Type in C++:

we store variable in computer memory. but computer has to know what kind of data we store in a variable. for this purpose we use data type that tell the computer what kind of data we are store in a variable.

There are 4 basic data types  :



  • int
  • char
  • float
  • double


int:

used to declare numeric program variables of integer type whole numbers, positive and 
negative
keyword: int
Range :
 signed: -2147483648 to 2147483647 
unsigned: 0 to 4294967295
Size     : 1byte

char: 

equivalent to ‘letters’ in English language
Example of characters:
Numeric digits: 0 - 9
Lowercase/uppercase letters: a - z and A - Z
Space (blank)
Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc
single character
keyword: char
Range :
signed: -128 to 127
unsigned: 0 to 255
Size: 4 byte

Float:

fractional parts, positive and negative
keyword: float
Range    :+/- 3.4e +/- 38
Size        : 4 byte

Double:

used to declare floating point variable of higher precision or higher range of numbers
exponential numbers, positive and negative
keyword: double
Range    : +/- 1.7e +/- 308 
Size        :  8 byte


variable and keyword in C++.

what is variable in c++?

                           An entity that may vary during program execution is called variable. variable name in c++ programming is the name of location in memory . different type of variable use in c++ programming. e.g integer variable etc certain type can hold certain value. mean integer variable can hold integer value.

RULES FOR CONSTRUCTION VARIABLE NAME:

1) A variable is combination of alphabets, digits or underscores.
2)first character of variable must be alphabet or underscore.
3) No commas or blank space are allow in variable.
4)No special symbol and character can be use within variable name expect underscore,
5)C++ keyword can not be use as c++ variable 
 Example of variable name:
                                            s_name                //valid 
                                            _val                    //valid
                                            4name              //invalid because first character must be alphabet or underscore
                                            int                    //invalid because it is c++ keyword
                                        

keyword in c++:

                                 keyword are the words whose meaning already been explained in c++ compiler.
list of keyword in c++:



Tuesday 17 December 2013

Use glRecti() function

Use glRecti() function:

glRecti function is use in opengl for draw rectangle. it optimize our code. in our previous lecture we draw a rectangle by using GL_QUADS function. in which we use four vertices . but in glrecti() function we only use two vertices. so we simply say that abut glRecti() function

Defination:

opposite corners  filled with current color. by using glRecti() function we can also draw the aligned rectangles. it mean  later rectangles are drawn on top of previous ones.

Syntax:

                           glRecti (int x1,       int y1,     int x2,    int y2);
Note:
             Main function is glRect we use 'i' for integer value for float value we use 'f' .
here x1 and y1 are the vertex of  first corner and x2 and y2 are the vertex of  second corner.

code:

in glutDisplayFunc() we call a function draw_rect function for example glutDisplayFunc(draw_rect)
so that 


void draw_rect(void)
{
glColor3f( 1.0, 0.0, 0.0);
glRecti(100,100,250, 250); 
glColor3f( 0.0, 1.0, 0.0);
glRecti(240,240,350, 350); 
    glFlush();
}

screen short:

 source code:                                                               


                                                                 Download:


Saturday 14 December 2013

All in one (draw hut)

All in one GL_QUADS ,GL_TRIANGLES,rotation,scaling and traslation:

we have discuss these all function in our first lesson. now we will discuss a example in which we use all these function we will draw a hut 

Draw hut:

                   first of all we will create the window for drawing that we have discuss it before in our first lesson in glutDisplayFun() we will call a function draw_hut()

void Draw_hut() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
//first of all will daraw triangle
glBegin(GL_TRIANGLES);
glVertex2f(0.20,0.95);
glVertex2f(0.15, 0.85);
glVertex2f(0.25,0.85);
glEnd();
//left rectangle in which we draw the door
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(0.15,0.85);
glVertex2f(0.15,0.60);
glVertex2f(0.25,0.60);
glVertex2f(0.25,0.85);
        glEnd();
//roof code
glColor3f(0.3, 0.2, 0.0);       
glBegin(GL_QUADS);
glVertex2f(0.20,0.95);
glVertex2f(0.50,0.95);
glVertex2f(0.55,0.85);
glVertex2f(0.25,0.85);
        glEnd();
//main portion in which in which we the window
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(0.25,0.85);
glVertex2f(0.25,0.60);
glVertex2f(0.55,0.60);
glVertex2f(0.55,0.85);
       glEnd();
//start bricks code
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);             //Horizontal Line
glVertex2f(0.215,0.92);
glVertex2f(0.515,0.92);
glEnd();
       glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(0.28,0.95);
glVertex2f(0.28,0.92);
glEnd();
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(0.35,0.95);
glVertex2f(0.35,0.92);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.42,0.95);
glVertex2f(0.42,0.92);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.48,0.95);
glVertex2f(0.48,0.92);
glEnd();
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);             // 2nd Horizontal Line
glVertex2f(0.230,0.89);
glVertex2f(0.527,0.89);
glEnd();
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(0.26,0.92);
glVertex2f(0.26,0.89);
glEnd();
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(0.33,0.92);
glVertex2f(0.33,0.89);
glEnd();
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(0.40,0.92);
glVertex2f(0.40,0.89);
glEnd();
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(0.47,0.92);
glVertex2f(0.47,0.89);
glEnd();
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);             // 3rd Horizontal Line
glVertex2f(0.243,0.86);
glVertex2f(0.542,0.86);
glEnd();
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(0.30,0.89);
glVertex2f(0.30,0.86);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.37,0.89);
glVertex2f(0.37,0.86);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.44,0.89);
glVertex2f(0.44,0.86);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.51,0.89);
glVertex2f(0.51,0.86);
glEnd();
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(0.26,0.86);
glVertex2f(0.26,0.85);
glEnd();
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(0.33,0.86);
glVertex2f(0.33,0.85);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.40,0.86);
glVertex2f(0.40,0.85);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.47,0.86);
glVertex2f(0.47,0.85);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.53,0.86);
glVertex2f(0.53,0.85);
glEnd();
//end bricks code
//draw the door
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_QUADS);         //Door
glVertex2f(0.15,0.70);
glVertex2f(0.15,0.60);
glVertex2f(0.20,0.6);
glVertex2f(0.20,0.70);
glEnd();
//draw the window
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_QUADS);                  //window
glVertex2f(0.35,0.78);
glVertex2f(0.45,0.78);
glVertex2f(0.45,0.68);
glVertex2f(0.35,0.68);
        glEnd();
glFlush();
}

screen short:

Hut
now we apply the rotation,translation and scaling in hut for this we use the glrotatef() , glTranslatef(), and glscalef() function respectivly
we will add this function in our code so now code will be like this see below

code:

void Draw_hut() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
       glPushMatrix(); //Save the transformations performed thus far
glRotatef(-40.0, 0.0f, 0.0f, 1.0f);
glTranslatef(-0.40f, 0.0f, 0.0f);
    .
   ............... all hout code that is written above
.....................all hout code that is written above
.....................all hout code that is written above
.............................all hout code that is written above
...................................all hout code that is written above
...........................all hout code that is written above
//all hout code that is written above
.....................
.................................
...................................
......................................

glPopMatrix();
glFlush();
}

screen short:

After scaling ,rotation and translation


Use glScalef function

glScalef function:

                                        glScalef() function is use for scaling in OpenGL so the question is what is scaling?

scaling:

Scaling is about the origin. If Sx = Sy the scaling is uniform; otherwise it distorts the image. If Sx or Sy < 0, the image is reflected across the x or y axis. in scaling we multiply the object position with the scaling factor.so if object position is p(x,y) and scaling factor is s(x,y) so we can say that
                                                                  P'=p(x,y).s(x,y)
In two dimension scaling :


(sx and sy )> 1 ---> Increases Object Size
•(sx and sy )< 1 ---> Decreases Object Size
•(sx and sy )= 1 ---> Unchanged Object Size
•sx = sy              ---> Uniform Scaling
•sx != sy             ---> Differential Scaling

types of scaling:

uniform scaling:                      

                                  if scaling factor are same then scaling is called is called uniform scaling
                                                                              Sx=Sy

Differential scaling:

                               if the scaling factor is not same then scaling is called differential scaling
                                                           Sx is not equal to Sy

scaling in openGl:

                                          glScalef() function is use for scaling in OpenGL

syntax:

                                glsaclef(x   , y  , z);
Example:
void Draw_scalling() {
glPushMatrix(); 
glScalef(0.2,0.2,0.0);
glBegin(GL_QUADS);        
    glColor3f(0.0f,1.0f,0.0f);    
    glVertex2f( 0.2, 0.49);    
    glVertex2f(0.4, 0.49);   
    glVertex2f(0.4, 0.76);    
    glVertex2f( 0.2, 0.76);    
       
glEnd();
glPopMatrix();
glFlush();
}

screen short:

Before scaling
After scaling
you can see the scaling effect in above fig after scaling you can see that your body is moved in left corner . so i am telling you before scaling is always about the origin so if u want that your image is not move toward the origin so you can use the gltranslatef() function here




Friday 13 December 2013

ROTATION IN OpenGL

ROTATION IN OpenGL:

first of all we will create a window for drawing here is the code for creation of window here and naw draw a rectangle or triangle by using GL_QUADS function you can see that how to draw rectangle here 
for rotation we use the builtian function glRotatef()
syntax:

glRotatef(angle  ,  float x   ,  float y  ,   float z)

f indicate the it will take the value in floating point. it will take 4 perameter

example:

void Draw_triangle() {


glPushMatrix(); //Save the transformations performed thus far

glRotatef(-40.0, 0.0, 0.9, 0.0);

glBegin(GL_QUADS);        
       glColor3f(0.0f,1.0f,0.0f);    
       glVertex2f( 0.2, 0.49);    
       glVertex2f(0.4, 0.49);   
       glVertex2f(0.4, 0.76);    
       glVertex2f( 0.2, 0.76);    
       
glEnd();
glPopMatrix();
glFlush();
}
screen short:
Rotation



use GL_QUADS function and Translation.

Use GL_QUADS function and Transformation:

Use this primitive to draw individual convex quadrilaterals. OpenGL renders a quadrilateral for each group of four vertices. . If n isn't a multiple of 4, OpenGL ignores the excess vertices. so we can say that GL_QUADS require vertices.
so we take a example that is clear how to use GL_QUADS function in openGL 
first of we create a window for drawing . how to create the window i have been explain it in my first tutorial here is the link how to create window here
so after initialize the window in glutDisplayFunc(Draw_rectangle);  we call a function Draw_rectangle() here we use the GL_QUADS function

code:

draw_rectangle()
{
glBegin(GL_QUADS);        
    glColor3f(0.0f,1.0f,0.0f);    
    glVertex2f( 0.2, 0.49);    
    glVertex2f(0.4, 0.49);   
    glVertex2f(0.4, 0.76);    
    glVertex2f( 0.2, 0.76);    
   glEnd();
glFlush();
}
 so glBegin() indicate the start of quads vertices and glEnd() indicate the end of vertices of quads.
glFlush(); indicate the end of drawing and output will be display .so the output of the following code will be

screen short:

translation:

in above fig rectangle is showing in left hand side but now i want it to display it in right side of the window so one thing i change the value  of all vertices so it will be time consuming but openGl provide the builtian function rather we change the value of all vertices we just use function that move our body from one position to other the function that we use is glTranslatef(x,y,z); here f in indicate we will give the value in floating point you can also use i for integer. 
so now the following code

code: 

Draw_rentangle() {

glPushMatrix(); 
glTranslatef(0.50f, 0.0f, 0.0f);
glBegin(GL_QUADS);        
    glColor3f(0.0f,1.0f,0.0f);    
    glVertex2f( 0.2, 0.49);    
    glVertex2f(0.4, 0.49);   
    glVertex2f(0.4, 0.76);    
    glVertex2f( 0.2, 0.76);    
        glEnd();
glPopMatrix();
glFlush();
}
screen short:
Before
after using glTranslate function.
After
here we use two new function glPushMatrix(); and glPopMatrix(); these function are just like glBegin() and glEnd() function glPushMatrix() save where from translation start i.e it indicate start of translation and glPopMatrix(); end of translation


Saturday 7 December 2013

Use the GL_TRIANGLES function:

GL_TRIANGLES function:

This primitive takes three vertices at a time and draws a triangle using them. If your application specifies n vertices, OpenGL renders n/3 triangles. If n isn't a multiple of 3, OpenGL ignores the excess vertices.

Example:

include header file
#include "stdafx.h"
#include<glut.h>
Defination of draw_trinagle function:
void Draw_triangle() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex2f(0.20,0.95);
glVertex2f(0.15, 0.85);
glVertex2f(0.25,0.85);
glEnd();
glFlush();
}


int main(int iArgc, char** cppArgv) {
...
       ....
       /*basic initialization */
      ...
     ...
     ...
glutDisplayFunc(Draw_triangle);
glutMainLoop();
return 0;
}

output screen short:
 

Note:
        In /*basic initialization * area we create the window for drawing initialize the window and set the size of window u can see how to set and initialize the window click here
                                             


Use the GL_LINES function:

Use the GL_LINES function:

This primitive takes two vertices and draws a line segment between them. If the vertices are more than two, OpenGL draws a vertex for each pair of vertices of two vertices. If the application specifies n vertices, OpenGL renders n/2 line segments. If n is odd, OpenGL ignores the final vertex.

Example:

Main function:
the following code are write in main function


int main(int argc, char **argv)

{

glutInit(&argc, argv);                                                      // initializes the toolkit

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);     //set the display mode
glutInitWindowSize(640,480);                                      //set the window size
glutInitWindowPosition(100,100);                       //set the window position on screen
glutCreateWindow(“draw the line”);                   //open the screen window
glutDisplayFunc(Displayline); 
myInit();                                                             // additional initializations, if necessary
glutMainLoop();                                             //go into a perpetual loop
}
Define the the myInit();  function:

void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

Define Displayline() function:

void Displayline()   {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glEnd();
glFlush();

}

Note:
          Both myInit(); and Displayline() must be define above main()

glbegin():

                   Marks the beginning of a vertex-data list that describes a geometric primitive 

glEnd():

                  Marks the end of a vertex-data list.


Friday 6 December 2013

HOW TO OPEN THE WINDOW FOR DRAWING?? (OpenGL utility library)

Open the window for drawing:

Here is the sample code for opening window for drawing using OpenGL Toolkit. 

code for intializiation window for drawing :


int main(int argc, char **argv)
{
glutInit(&argc, argv);                                                      // initializes the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);     //set the display mode
glutInitWindowSize(640,480);                                      //set the window size
glutInitWindowPosition(100,100);                       //set the window position on screen
glutCreateWindow(“MyProgram”);                   //open the screen window
/* register callback functions */
myInit();                                                             // additional initializations, if necessary
glutMainLoop();                                             //go into a perpetual loop
}

Note: 
          the argument of glutinit() and main() must be same.

Define myInit();  function:

void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

OpenGL Events:

There are four main types of event functions that we work with in OpenGL.
int main(int argc, char **argv)
{
/* initialize basic things */
/* register callback functions */
glutDisplayFunc(myDisplay);                     //register the redraw function
glutReshapeFunc(myReshape);                //register the reshape function
glutMouseFunc(myMouse);                       //register the mouse action function
glutKeyboardFunc(myKeyboard);           //register the keyboard action function
/* end register callback functions */
/* may be more initializations */
glutMainLoop();                                            //enter the unending main loop
}

glutDisplayFunc(myDisplay):

 Windows issues a “redraw” event when it determines that a window should be redrawn on the screen. This happens when a window is first opened. Here myDisplay() is a registered callback function for the redraw event.

glutReshapeFunc(myReshape):

 Screen windows can be reshaped by a user, by resizing the windows. So myReshape() is registered with the reshape event.

glutMouseFunc(myMouse):

 This command registers the myMouse() function. It is called whenever a mouse event occurs like click. myMouse() is automatically passed arguments telling the exact position of mouse and the nature of action initiated.

glutKeyboardFunc(myKeyboard):

 This command registers the myKeyboard() function with the event of pressing or releasing any key on the keyboard.

Color in OpenGL:

Initialize color buffer before you use colors. You need to set the color value. You can
use the function glColor3f(red, green, blue) for this purpose. For example

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                                                               // Clear The Screen And The Depth Buffer
glLoadIdentity();                                  // Reset The Current Modelview Matrix
glBegin(GL_TRIANGLES);                   // Begin Drawing Triangles
glColor3f(1.0,1.0,1.0)                           //set white color
….
.....
....
...
...


Saturday 30 November 2013

HOW TO SET OpenGL ENVIRONMENT IN VISUAL STDIO 2012 ULTIMATE?

Set OpenGL Environment in visual stdio 2012: 

for set the opengl environment in visual stdio 2012 need thee files
1) glut32.h
2) glut32.dll
3)glut32.lib
download all three file from glut-3.7.6-bin
1=Now go to c:\program files
2= there will be folder Microsoft visual stdio 11.0 open this folder and now go to VC folder open this folder
3= Now extract glut-3.7.6-bin that you download now copy the glut32.h and paste it in c:\program files\Microsoft visual stdio 2011.0\vc\include\
4=now copy the glut32.dll file and paste it in c:\program files\Microsoft visual stdio 2011.0\vc\bin\
5=now copy the glut32.lib file and paste in c:\program files\Microsoft visual stdio 2011.0\vc\lib\
6=now open the visual stdio 2012 open c++ console application when your project will be start at right side there will be show a solution explorer window if solution explorer window is not appear at right side then go to view-->solution explorer then solution explorer window will be appear. 
7=now right click on your project name in solution explorer. show in figure below
Set OpenGL Environment in visual stdio 2012
now go to properties then new window will be open
8=now click linker then input as show in figure
Set OpenGL Environment in visual stdio 2012
9=now go to additional dependencies which is drop down menu and click edit as show in fig below
Set OpenGL Environment in visual stdio 2012

10= now write 
opengl32.lib
glu32.lib
glut32.lib
Set OpenGL Environment in visual stdio 2012

and now click ok now your visual stdio is ready for openGL environment enjoy and remember me in your prayer....:)


what is openGL(introduction)?

what is openGL?

opengl is not a programming language and not a window application and not a data structure container. so opengl is a software interface to the graphics hardware.it does depend upon the operating system. it gives the user tools to create the high quality rendering. for opengl we include
#include"glut.h"
opengl is device independent. opengl is an API. it controls whatever hardware you are using.and use its function instead of controlling hardware directly. opengl is open source.
what is opengl?
openGL

library to include: 

1) GLUT: it initialize toolkit, open windows, develop menus, and manage events.
2) GLU : it provide high level routne to handle the complex mathematical and drawing function.
3) GLUI : the user interface library which completely integrate with GLUT library.