Saturday, May 31, 2014

Radio Button Control

Properties of Radio Button Control
    Checked – This is Boolean property, that is used to check if the button is checked or not.
    Text – This is string property used to get or set the text associated with the radio button control.
    AutoPostBack – Set this property to true, if you want the web form to be posted immediately when the checked status of the radio button changes.
    Group Name – By default the individual radio button selections, are not mutually exclusive. If you have a group of radio buttons, and if you want the selections among the group to be mutually exclusive, then use the same group name for all the radio button controls.

Events
    CheckedChanged– This is fired when the checked status of the radio button control is changed.
Here an example to print text in the textbox
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_11_RadioButtonControl.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <fieldset style="width:300px">  
       <legend><b>Gender</b></legend>  
       <asp:RadioButton ID="RadioButton1" runat="server" GroupName="Gender"   
         Text="Male" />  
 &nbsp;&nbsp;&nbsp;&nbsp;  
       <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Gender"   
         Text="Female" />  
       <br />  
       <asp:RadioButton ID="RadioButton3" runat="server" GroupName="Gender"   
         oncheckedchanged="RadioButton3_CheckedChanged" Text="Unknown" />  
       <br />  
       <br />  
       <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
     </fieldset>  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _11_RadioButtonControl  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       if (RadioButton1.Checked)  
         Response.Write("Gender is "+RadioButton1.Text);  
       else if(RadioButton2.Checked)  
         Response.Write("Gender is " + RadioButton2.Text);  
       else  
         Response.Write("Gender is " + RadioButton3.Text);  
     }  
   }  
 }  

TextBox control

Properties of TextBox control
        TextMode property
        Text
        ReadOnly
        ToolTip
        AutoPostBack-Automatically postback when text is changed.
Events of TextBox
        TextChanged :- This event is fired, when the text is changed.
Methods of TextBox
        Focus :- set input focus onto the control.

Here an example to print text in the textbox
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_10_TextBoxControl.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
     <br />  
     <br />  
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _10_TextBoxControl  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       TextBox1.Focus();  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       Response.Write("Hello "+TextBox1.Text);  
     }  
   }  
 }  

IIS & ASP.NET

What is a web server?
It is a software, that is used to deliver web pages to clients using the HTTP. For example, IIS is a web server that can be used to run asp.net web applications. Visual Studio ships with a build-in web server. If you want to just build and test web application on your machine, you don’t need an IIS. Build-in web server will not serve requests to another computer. By default, visual studio uses the build-in web server.

How to check it is installed???
Click start-->Type INETMGR in the run window-->ok If you get IIS manager window, it is installed, otherwise not installed.

How to install?
Start-->control panel-->programs-->programs and features option-->Turn window feature on or off-->In the windows features, select internet information services and related services.

Monday, May 26, 2014

Opengl,C++ : Draw Polygon and Rotate Polygon Using Keyboard Characters

The following program Specify a point for the Polygon by the left mouse click, Rotate Polygon using keyboard char "R" and Translate Polygon by (100,150) coordinates using keyboard char "T".
 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
 #include <cmath>  
    
 int screenheight = 600;  
 int screenwidth = 800;  
 bool flag = true;  
 int first = 0;  
   
 double angle = 30;   
 //The angle for the rotation (in degrees)  
 typedef struct{  
   float x;  
   float y;  
 }Point2D;  
   
 Point2D p1,p2,p3,p4;  
   
 void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){  
     glPointSize(1.0);  
        glBegin(GL_POLYGON);  
             glVertex2i(pt1.x, pt1.y);  
             glVertex2i(pt2.x, pt2.y);  
             glVertex2i(pt3.x, pt3.y);  
             glVertex2i(pt4.x, pt4.y);  
     glEnd();  
   
     glPointSize(6.0);  
     glBegin(GL_POINTS);  
     glVertex2i(pt2.x, pt2.y);  
        glVertex2i(pt3.x, pt3.y);  
        glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glFlush();  
   
 }  
   
 Point2D translate(Point2D p, float tx, float ty){  
     p.x =p.x+tx; //.....wite the equations for translation   
     p.y = p.y+ty; //.....wite the equations for translation  
     return p;  
 }  
   
 Point2D rotate(Point2D p, float ang){  
   ang = ang * 3.14 / 180.0;                 //angle in radians  
   Point2D ptemp;  
   
   ptemp.x = p.x * cos(ang) - p.y * sin(ang);  
   ptemp.y = p.x * sin(ang) + p.y * cos(ang);  
   
      return ptemp;  
 }  
   
 void myMouse(int button, int state, int x, int y) {  
     if(state == GLUT_DOWN) {  
        if(button == GLUT_LEFT_BUTTON) {  
            glClear(GL_COLOR_BUFFER_BIT);  
   
                           switch(first)  
                               {  
                               case 0:   
                                     p1.x = x;  
                                     p1.y =screenheight - y;  
                                    first = 1;  
                                    break;  
                               case 1:  
                                    p2.x = x;  
                                    p2.y = screenheight - y;  
                                    first = 2;  
                                    break;       
                               case 2:  
                                    p3.x = x;  
                                    p3.y = screenheight - y;  
                                    first = 3;  
                                    break;  
                               case 3:  
                                    p4.x = x;  
                                    p4.y = screenheight - y;  
                                    DrawPolygonSegment(p1,p2,p3,p4);  
                                    first = 0;  
                                    break;       
                               }  
                       
        }  
     }  
 }  
   
 void translatepolygon_R()  
 {  
      Point2D Rotated_p1 = rotate(p1,angle);  
   Point2D Rotated_p2 = rotate(p2,angle);  
      Point2D Rotated_p3 = rotate(p3,angle);  
      Point2D Rotated_p4 = rotate(p4,angle);  
   DrawPolygonSegment(Rotated_p1,Rotated_p2,Rotated_p3,Rotated_p4);  
 }  
   
 void translatepolygon_T()  
 {  
      Point2D translate_p1 = translate(p1,100,150);  
   Point2D translate_p2 = translate(p2,100,150);  
      Point2D translate_p3 = translate(p3,100,150);  
      Point2D translate_p4 = translate(p4,100,150);  
   DrawPolygonSegment(translate_p1,translate_p2,translate_p3,translate_p4);  
 }  
   
 void key(unsigned char key, int x, int y)  
 {  
      if(key=='T') glutIdleFunc(translatepolygon_T);  
      if(key=='R') glutIdleFunc(translatepolygon_R);  
 }  
   
   
 void myDisplay(){  
     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);  
     glClear(GL_COLOR_BUFFER_BIT);  
     glColor3f(0.0f, 0.0f, 0.0f);  
 }  
    
 int main( int argc, char ** argv ) {  
     glutInit( &argc, argv );  
     glutInitWindowPosition( 0, 0 );  
     glutInitWindowSize( 800, 600 );  
     glutCreateWindow( "My Drawing Screen" );  
        glMatrixMode( GL_PROJECTION );  
     glLoadIdentity();  
     gluOrtho2D( 0, 800, 0, 600 );  
     glViewport(0, 0, 800, 600);  
   
        glutDisplayFunc( myDisplay );  
     glutMouseFunc( myMouse );  
        glutKeyboardFunc(key);  
       
         
   
     glutMainLoop();  
     return( 0 );  
 }  

Opengl,C++ : Draw and Rotate a Polygon using 'Mouse Clicks'

The following program rotates a given Polygon in 2D.
(Specify a point for the Polygon by the left mouse button, and click the right mouse button to rotate it)
 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
 #include <cmath>  
    
 int screenheight = 600;  
 int screenwidth = 800;  
 bool flag = true;  
 int first = 0;  
   
 double angle = 30;   
 //The angle for the rotation (in degrees)  
 typedef struct{  
   float x;  
   float y;  
 }Point2D;  
   
 Point2D p1,p2,p3,p4;  
   
 void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){  
     glPointSize(1.0);  
        glBegin(GL_POLYGON);  
             glVertex2i(pt1.x, pt1.y);  
             glVertex2i(pt2.x, pt2.y);  
             glVertex2i(pt3.x, pt3.y);  
             glVertex2i(pt4.x, pt4.y);  
     glEnd();  
   
     glPointSize(6.0);  
     glBegin(GL_POINTS);  
       glVertex2i(pt2.x, pt2.y);  
        glVertex2i(pt3.x, pt3.y);  
        glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glFlush();  
   
 }  
   
 Point2D rotate(Point2D p, float ang){  
   ang = ang * 3.14 / 180.0;                 //angle in radians  
   Point2D ptemp;  
   
   ptemp.x = p.x * cos(ang) - p.y * sin(ang);  
   ptemp.y = p.x * sin(ang) + p.y * cos(ang);  
   
      return ptemp;  
 }  
   
 void myMouse(int button, int state, int x, int y) {  
     if(state == GLUT_DOWN) {  
        if(button == GLUT_LEFT_BUTTON) {  
            glClear(GL_COLOR_BUFFER_BIT);  
   
                           switch(first)  
                               {  
                               case 0:   
                                     p1.x = x;  
                                     p1.y =screenheight - y;  
                                    first = 1;  
                                    break;  
                               case 1:  
                                    p2.x = x;  
                                    p2.y = screenheight - y;  
                                    first = 2;  
                                    break;       
                               case 2:  
                                    p3.x = x;  
                                    p3.y = screenheight - y;  
                                    first = 3;  
                                    break;  
                               case 3:  
                                    p4.x = x;  
                                    p4.y = screenheight - y;  
                                    DrawPolygonSegment(p1,p2,p3,p4);  
                                    first = 0;  
                                    break;       
                               }  
                       
        }  
        else if (button == GLUT_RIGHT_BUTTON) {  
            Point2D Rotated_p1 = rotate(p1,angle);  
          Point2D Rotated_p2 = rotate(p2,angle);  
            Point2D Rotated_p3 = rotate(p3,angle);  
            Point2D Rotated_p4 = rotate(p4,angle);  
          DrawPolygonSegment(Rotated_p1,Rotated_p2,Rotated_p3,Rotated_p4);  
        }  
     }  
 }  
   
   
 void myDisplay(){  
     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);  
     glClear(GL_COLOR_BUFFER_BIT);  
     glColor3f(0.0f, 0.0f, 0.0f);  
 }  
    
 int main( int argc, char ** argv ) {  
     glutInit( &argc, argv );  
     glutInitWindowPosition( 0, 0 );  
     glutInitWindowSize( 800, 600 );  
     glutCreateWindow( "Rotate Polygon" );  
        glMatrixMode( GL_PROJECTION );  
     glLoadIdentity();  
     gluOrtho2D( 0, 800, 0, 600 );  
     glViewport(0, 0, 800, 600);  
   
     glutDisplayFunc( myDisplay );  
     glutMouseFunc( myMouse );  
   
     glutMainLoop();  
     return( 0 );  
 }  

Opengl,C++ : Draw a Line and Rotate using 'Mouse Clicks'

The following program rotates a given Line about the origin in 2D.
(Specify a point by the left mouse button, and click the right mouse button to rotate it)
 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
 #include <cmath>  
    
 int screenheight = 600;  
 int screenwidth = 800;  
 bool flag = true;  
   
 double angle = 30;        //The angle for the rotation (in degrees)  
   
 typedef struct{  
   float x;  
   float y;  
 }Point2D;  
   
 Point2D p1,p2;  
   
 void DrawLineSegment(Point2D pt1, Point2D pt2){  
     glPointSize(1.0);  
     glBegin(GL_LINES);  
     glVertex2i(pt1.x, pt1.y);  
     glVertex2i(pt2.x, pt2.y);  
     glEnd();  
   
     glPointSize(6.0);  
     glBegin(GL_POINTS);  
     glVertex2i(pt2.x, pt2.y);  
     glEnd();  
     glFlush();  
   
 }  
   
 Point2D rotate(Point2D p, float ang){  
   ang = ang * 3.14 / 180.0;                 //angle in radians  
   Point2D ptemp;  
   
   ptemp.x = p.x * cos(ang) - p.y * sin(ang);  
   ptemp.y = p.x * sin(ang) + p.y * cos(ang);  
   
      return ptemp;  
 }  
   
 void myMouse(int button, int state, int x, int y) {  
     if(state == GLUT_DOWN) {  
        if(button == GLUT_LEFT_BUTTON) {  
            glClear(GL_COLOR_BUFFER_BIT);  
            Point2D p1;  
            p1.x = 0;  
            p1.y = 0;  
   
            p2.x = x;  
            p2.y = screenheight - y;  
   
            DrawLineSegment(p1, p2);  
        }  
        else if (button == GLUT_RIGHT_BUTTON) {  
            Point2D Rotated_p2 = rotate(p2,angle);  
            DrawLineSegment(p1,Rotated_p2);  
        }  
     }  
 }  
   
 void myDisplay(){  
     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);  
     glClear(GL_COLOR_BUFFER_BIT);  
     glColor3f(0.0f, 0.0f, 0.0f);  
 }  
    
 int main( int argc, char ** argv ) {  
     glutInit( &argc, argv );  
     glutInitWindowPosition( 0, 0 );  
     glutInitWindowSize( 800, 600 );  
     glutCreateWindow( "Rotate Line" );  
        glMatrixMode( GL_PROJECTION );  
     glLoadIdentity();  
     gluOrtho2D( 0, 800, 0, 600 );  
     glViewport(0, 0, 800, 600);  
   
     glutMouseFunc( myMouse );  
     glutDisplayFunc( myDisplay );  
   
     glutMainLoop();  
     return( 0 );  
 }  

Tuesday, May 20, 2014

Use of IsPostBack property

Example :-
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="video8_IsPostBackProperty.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
   <style type="text/css">  
     .style1  
     {  
       width: 114px;  
     }  
     .style2  
     {  
       width: 114px;  
       height: 23px;  
     }  
     .style3  
     {  
       height: 23px;  
     }  
   </style>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <table style="width: 70%;">  
       <tr>  
         <td class="style1">  
           <asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label>  
         </td>  
         <td>  
           <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
         </td>  
       </tr>  
       <tr>  
         <td class="style1">  
           <asp:Label ID="Label2" runat="server" Text="Last Name"></asp:Label>  
         </td>  
         <td>  
           <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
         </td>  
       </tr>  
       <tr>  
         <td class="style2">  
           <asp:Label ID="Label3" runat="server" Text="City"></asp:Label>  
         </td>  
         <td class="style3">  
           <asp:DropDownList ID="DropDownList1" runat="server">  
           </asp:DropDownList>  
         </td>  
       </tr>  
       <tr>  
         <td class="style1" colspan="1" rowspan="1">  
         </td>  
         <td colspan="1" rowspan="1">  
           <asp:Button ID="Button1" runat="server"   
             Text="Register Employee" />  
         </td>  
       </tr>  
     </table>  
     <br />  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace video8_IsPostBackProperty  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {   
       if(!IsPostBack)          
       LoadCityDropDownList();  
       //Or disable the view state of the drop downlist as Enable View State  
       //Or DropDownList1.Items.Clear();LoadCityDropDownList();  
     }  
     public void LoadCityDropDownList()  
     {  
       ListItem li1 = new ListItem("London");  
       DropDownList1.Items.Add(li1);  
       ListItem li2 = new ListItem("Sydni");  
       DropDownList1.Items.Add(li2);  
       ListItem li3 = new ListItem("paris");  
       DropDownList1.Items.Add(li3);  
     }  
   }  
 }  

Every time you click the button dropdownlist items will duplicated. we can use the IsPostBack property to solve this problem.

ASP.NET Server control Events

ASP.NET server control, such as TextBox, Button, and DropDownList has their own events. And also have validation controls, that has validation events. The events that these controls expose, can be broadly divided into 3 categories.

  • PostBack events – These events submit the web page, immediately to the server for processing. Click event of a button control is an example for PostBack event.
  • Cached events – These events are saved in the page’s view state to be processed when a postback event occurs. TextChanged event of Textbox control, and SelectedIndexChanged event of a DropDownList control are examples of cached events.
  • Cached events can be converted into postback events, by setting the AutoPostBack property of the control to true.
  • Validation events – These events occur on the client, before the page is posted back to the server. All validation controls use these type of events.

Difference between ViewState , SessionState , Application State

Example :-
     WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationState1.aspx.cs" Inherits="ViewState_Session_ApplicationState.ApplicationState1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click"   
       style="height: 26px" Text="Button" />  
   </div>  
   </form>  
 </body>  
 </html>  
ViewState
   1.   ViewState of a web form is available only within that web form.
   2.   ViewState is stored on the page in a hidden field called _ViewState. Because of this, the ViewState, will be lost, if you navigate away from the page, or if the browser is closed.
   3.    ViewState is used by all asp.net controls to retain their state across postback.
     ViewState1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace ViewState_Session_ApplicationState  
 {  
   public partial class ViewState1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if (!IsPostBack)  
       {  
         if (ViewState["Clicks"] == null)  
           ViewState["Clicks"] = 0;  
         TextBox1.Text = ViewState["Clicks"].ToString();  
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       int clickcount =(int) ViewState["Clicks"] + 1;  
       TextBox1.Text = clickcount.ToString();  
       ViewState["Clicks"] = clickcount;  
     }  
   }  
 }  

SessionState
   1.   SessionState variables are available across all pages, but only for a given single session. Session variables are like single-user global data.
   2.   SessionState variables are stored on the webserver.
   3.   SessionState variables are cleared, when the user session times out. The default is 20 minutes. This is configurable in web.config file.

     SessionState1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace ViewState_Session_ApplicationState  
 {  
   public partial class SessionState1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if (!IsPostBack)  
       {  
         if (Session["Clicks"] == null)  
           Session["Clicks"] = 0;  
         TextBox1.Text = Session["Clicks"].ToString();  
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       int clickcount = (int)Session["Clicks"] + 1;  
       TextBox1.Text = clickcount.ToString();  
       Session["Clicks"] = clickcount;  
     }  
   }  
 }  

Application State
   1.   Application State variables are available across all pages and across all sessions. Application State variables are like muli-user global data.
   2.   Application State variables are stored on the web server.
   3.   Application State variables are cleared, when the process hosting the application is restarted.

     ApplicationState1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace ViewState_Session_ApplicationState  
 {  
   public partial class ApplicationState1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if (!IsPostBack)  
       {  
         if (Application["Clicks"] == null)  
           Application["Clicks"] = 0;  
         TextBox1.Text = Application["Clicks"].ToString();  
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       int clickcount = (int)Application["Clicks"] + 1;  
       TextBox1.Text = clickcount.ToString();  
       Application["Clicks"] = clickcount;  
     }  
   }  
 }  

Events

In a web application, events can occur at 3 levels
   1.   At the application level(application start)
   2.   At the page level (page load)
   3.   At the control level(Button click)

Techniques to send data from one web form to another
   1.   Query string
   2.   Cookies
   3.   Session state
   4.   Application state

  • Session state variables are available across all pages, but only for a given single session.
  • Session variable are like single-user global data. Only current session has access to its session state.
  • Application state variables are available across all pages and across all sessions.
  • Application state variables are like multi-user global data. All sessions can read and write application state
  • variables.
Here application to find the number of application running and the number of users in online :-
Global.asax.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.Security;  
 using System.Web.SessionState;  
 namespace video4_session  
 {  
   public class Global : System.Web.HttpApplication  
   {  
     void Application_Start(object sender, EventArgs e)  
     {  
       // Code that runs on application startup  
       Application["TotalApplications"] = 0;  
       Application["TotalUserSessions"]=0;  
       Application["TotalApplications"] = (int)Application["TotalApplications"]+1;  
     }  
     void Session_Start(object sender, EventArgs e)  
     {  
       // Code that runs when a new session is started  
       Application["TotalUserSessions"] = (int)Application["TotalUserSessions"] + 1;  
     }  
     void Session_End(object sender, EventArgs e)  
     {  
       // Code that runs when a session ends.   
       // Note: The Session_End event is raised only when the sessionstate mode  
       // is set to InProc in the Web.config file. If session mode is set to StateServer   
       // or SQLServer, the event is not raised.  
       Application["TotalUserSessions"] = (int)Application["TotalUserSessions"] - 1;  
     }  
   }  
 }  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace video4_session  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       Response.Write("Number of Applications : "+Application["TotalApplications"]);  
       Response.Write("<br/>");  
       Response.Write("Number of Total users online : " + Application["TotalUserSessions"]);  
     }  
   }  
 }  
How to get a new session-id and force the Session_Start() event to execute??????
1.   Close the existing browser window and then open new instance or the browser window.
2.   Open new instance of a different browser
3.   Use Cookie-less Sessions
< sessionState mode="InProc" cookieless="true" ></sessionState> in Web.config file

Different between the ASP.NET Server control & HTML controls

ASP.NET server control retains state.
HTML controls do not retain state across post backs.
Example  :-
WebForm1.aspx  :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="FirstApplication.WebForm4" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     ASP.NET Server Control :<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
     <br />  
     <br />  
     HTML Control :&nbsp;&nbsp;&nbsp;  
     <input id="Text1" type="text"/><br />  
     <br />  
     <br />  
     <asp:Button ID="Button1" runat="server" Text="Button" />  
   </div>  
   </form>  
 </body>  
 </html>  

An HTML control can be converted in ASP.NET server control by adding runat=”server” attribute in the HTML source.

What is ViewState ?

Web Applications work on HTTP protocol. HTTP protocol is a stateless protocol, meaning it does not retain state between user requests.

Here an example to understand the stateless protocol :-
WebForm1.aspx  :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FirstApplication.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
   </div>  
   </form>  
 </body>  
 </html>  
WebForm1.aspx.cs  :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace FirstApplication  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     int clickcount = 0;  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if(!IsPostBack){  
         TextBox1.Text = "0";  //If it is an initial get request.
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       clickcount = clickcount + 1;  
       TextBox1.Text = clickcount.ToString();  
     }  
   }  
 }  

Web forms live for barely a moment. When a request is received
1. An instance of the requested webform is created.
2. Events Processed.
3. Generates the HTML & posted to the client.
4. The web form is immediately destroyed.

               In the above example every time you click the button new instance of the web form will be created. For the first time it will go into “if condition statements” because it is an initial get request. After the first click it won’t go. For the second click clickcount will be 1. Beyond the second click the clickcount value will not change stays at 1 because every time you click the button new instance of the web form will be created. Every time you post this web form web server doesn’t remember the state of the between request. To make the server to remember the state in ASP.NET has the ViewState field.
Using ViewState field Correction of above code  :-
WebForm2.aspx.cs  :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace FirstApplication  
 {  
   public partial class WebForm2 : System.Web.UI.Page  
   {  
     int clickcount = 1;  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if(!IsPostBack){  
         TextBox1.Text = "0";  
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       if(ViewState["Clicks"]!=null){  
         clickcount = (int)ViewState["Clicks"] + 1;  
       }  
       TextBox1.Text = clickcount.ToString();  
       ViewState["Clicks"] = clickcount;  
     }  
   }  
 }  

        Here “Clicks” is a ViewState variable to store the clickcounts. The ViewState data travels with every request and response between the client and the web server in hidden field data formate.

Another way to solve this problem  :-
WebForm3.aspx.cs  :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace FirstApplication  
 {  
   public partial class WebForm3 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if (!IsPostBack)  
       {  
         TextBox1.Text = "0";  
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       int clickcount = Convert.ToInt32(TextBox1.Text) + 1;  
       TextBox1.Text = clickcount.ToString();  
     }  
   }  
 }  

Introduction to Web forms

  • WebForms has the extension of .aspx.
  • A web form also has a code behind and designer files. Code behind files has the extention of .aspx.cs or aspx.vb.
  • Designer files contains the extension of .aspx.designer.cs or .aspx.designer.vb.
  • Code behind files contain the code that user writes, the designer file contain the auto generated code. A webform’s Html can be edited either in source or design mode or split mode.

What is ASP.NET?

ASP.NET is web application framework to build dynamic data driven web applications and web services. ASP.NET is a subset of .NET framework. A framework is a collection of classes. ASP.NET is the successor to classic ASP (Active Server Pages).

Creating your first ASP.NET web application :-
        Select File --> NewProject --> Visual C# tab --> Web
To view the solution explorer window:-
        view --> Solution Explorer

Thursday, May 15, 2014

Opengl,C++ : Flood-Fill Algorithm Using Recursion

  #include <GL/glut.h>   
  int ww = 600, wh = 500;   
  float bgCol[3] = {0.2, 0.4,0.0};   
  float intCol[3] = {1.0,0.0,0.0};   
  float fillCol[3] = {0.4,0.0,0.0};   
      void setPixel(int pointx, int pointy, float f[3])   
      {   
             glBegin(GL_POINTS);   
                  glColor3fv(f);   
                  glVertex2i(pointx,pointy);   
             glEnd();   
             glFlush();   
      }   
      void getPixel(int x, int y, float pixels[3])   
      {   
           glReadPixels(x,y,1.0,1.0,GL_RGB,GL_FLOAT,pixels);   
      }   
      void drawPolygon(int x1, int y1, int x2, int y2)   
       {      
             glColor3f(1.0, 0.0, 0.0);   
             glBegin(GL_POLYGON);   
                     glVertex2i(x1, y1);    
                  glVertex2i(x1, y2);   
                  glVertex2i(x2, y2);   
                     glVertex2i(x2, y1);   
             glEnd();   
             glFlush();   
       }   
       void display()   
       {   
             glClearColor(0.2, 0.4,0.0, 1.0);   
             glClear(GL_COLOR_BUFFER_BIT);   
             drawPolygon(150,250,200,300);   
             glFlush();   
       }   
       void floodfill4(int x,int y,float oldcolor[3],float newcolor[3])   
       {   
            float color[3];   
            getPixel(x,y,color);   
            if(color[0]==oldcolor[0] && (color[1])==oldcolor[1] && (color[2])==oldcolor[2])   
            {   
                     setPixel(x,y,newcolor);   
                     floodfill4(x+1,y,oldcolor,newcolor);   
                     floodfill4(x-1,y,oldcolor,newcolor);   
                  floodfill4(x,y+1,oldcolor,newcolor);   
                  floodfill4(x,y-1,oldcolor,newcolor);   
             }   
       }   
       void mouse(int btn, int state, int x, int y)   
       {   
             if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)    
            {   
                     int xi = x;   
                     int yi = (wh-y);   
                     floodfill4(xi,yi,intCol,fillCol);   
             }   
       }   
       void myinit()   
       {      
            glViewport(0,0,ww,wh);   
            glMatrixMode(GL_PROJECTION);   
            glLoadIdentity();   
            gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);   
            glMatrixMode(GL_MODELVIEW);   
       }   
       int main(int argc, char** argv)   
       {   
             glutInit(&argc,argv);   
             glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);   
             glutInitWindowSize(ww,wh);   
             glutCreateWindow("Flood-Fill-Recursive");   
             glutDisplayFunc(display);   
             myinit();   
             glutMouseFunc(mouse);   
             glutMainLoop();   
             return 0;   
       }   

Opengl,C++ : Boundary-Fill Algorithm Using Recursion

1:  #include <GL/glut.h>  
2:  int ww = 600, wh = 500;  
3:  float fillCol[3] = {0.4,0.0,0.0};  
4:  float borderCol[3] = {0.0,0.0,0.0};  
5:  void setPixel(int pointx, int pointy, float f[3])  
6:  {  
7:       glBegin(GL_POINTS);  
8:            glColor3fv(f);  
9:            glVertex2i(pointx,pointy);  
10:       glEnd();  
11:       glFlush();  
12:  }  
13:  void getPixel(int x, int y, float pixels[3])  
14:  {  
15:       glReadPixels(x,y,1.0,1.0,GL_RGB,GL_FLOAT,pixels);  
16:  }  
17:  void drawPolygon(int x1, int y1, int x2, int y2)  
18:  {       
19:       glColor3f(0.0,0.0,0.0);  
20:       glBegin(GL_LINES);  
21:            glVertex2i(x1, y1);  
22:            glVertex2i(x1, y2);   
23:       glEnd();  
24:       glBegin(GL_LINES);  
25:            glVertex2i(x2, y1);  
26:            glVertex2i(x2, y2);  
27:       glEnd();  
28:       glBegin(GL_LINES);  
29:            glVertex2i(x1, y1);  
30:            glVertex2i(x2, y1);  
31:       glEnd();  
32:       glBegin(GL_LINES);  
33:            glVertex2i(x1, y2);  
34:            glVertex2i(x2, y2);  
35:       glEnd();  
36:       glFlush();  
37:  }  
38:  void display()  
39:  {  
40:       glClearColor(0.6,0.8,0.1, 1.0);  
41:       glClear(GL_COLOR_BUFFER_BIT);  
42:       drawPolygon(150,250,200,300);  
43:       glFlush();  
44:  }  
45:  void boundaryFill4(int x,int y,float fillColor[3],float borderColor[3])  
46:  {  
47:       float interiorColor[3];  
48:       getPixel(x,y,interiorColor);  
49:       if((interiorColor[0]!=borderColor[0] && (interiorColor[1])!=borderColor[1] && (interiorColor[2])!=borderColor[2]) && (interiorColor[0]!=fillColor[0] && (interiorColor[1])!=fillColor[1] && (interiorColor[2])!=fillColor[2]))  
50:       {  
51:            setPixel(x,y,fillColor);  
52:            boundaryFill4(x+1,y,fillColor,borderColor);  
53:            boundaryFill4(x-1,y,fillColor,borderColor);  
54:            boundaryFill4(x,y+1,fillColor,borderColor);  
55:            boundaryFill4(x,y-1,fillColor,borderColor);  
56:       }  
57:  }  
58:  void mouse(int btn, int state, int x, int y)  
59:  {  
60:       if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)   
61:       {  
62:            int xi = x;  
63:            int yi = (wh-y);  
64:            boundaryFill4(xi,yi,fillCol,borderCol);  
65:       }  
66:  }  
67:  void myinit()  
68:  {        
69:       glViewport(0,0,ww,wh);  
70:       glMatrixMode(GL_PROJECTION);  
71:       glLoadIdentity();  
72:       gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);  
73:       glMatrixMode(GL_MODELVIEW);  
74:  }  
75:  int main(int argc, char** argv)  
76:  {  
77:       glutInit(&argc,argv);  
78:       glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  
79:       glutInitWindowSize(ww,wh);  
80:       glutCreateWindow("Bountry-Fill-Recursive");  
81:       glutDisplayFunc(display);  
82:       myinit();  
83:       glutMouseFunc(mouse);  
84:       glutMainLoop();  
85:       return 0;  
86:  }  

Opengl,C++ : Draw Circle With Midpoint Circle Algorithm

1:  #include<stdio.h>  
2:  #include<GL/glut.h>  
3:  #include<math.h>  
4:  int ww=400,wh=400;  
5:  int first=0;  
6:  int xi,yi,xf,yf;  
7:  void putpixel(int x,int y)  
8:  {  
9:       glBegin(GL_POINTS);  
10:            glVertex2i(x,y);  
11:       glEnd();  
12:       glFlush();  
13:  }  
14:  int round(double n)  
15:  {  
16:       return (n>=0)?(int)(n+0.5):(int)(n-0.5);  
17:  }  
18:  void MidPoint_circle(int r)  
19:  {  
20:       int x=0,y=r,p=1-r;  
21:       while(x<=y){  
22:             //Here Transform each x,y coordinates by 250 pixels   
23:            putpixel(250+x, 250+y);  
24:            putpixel(250+y, 250+x);  
25:            putpixel(250-x, 250+y);  
26:            putpixel(250-x, 250-y);  
27:            putpixel(250-y, 250+x);  
28:            putpixel(250-y, 250-x);  
29:            putpixel(250+y, 250-x);  
30:            putpixel(250+x, 250-y);  
31:            if(p<0)  
32:                 p=p+2*x+3;  
33:            else{  
34:                 p=p+2*(x-y)+5;  
35:                 y--;  
36:            }  
37:            x++;  
38:       }  
39:  }  
40:  void display()  
41:  {  
42:       glClearColor(0.4,0.7,0.2,1.0);  
43:       glColor3f(0.5,0.3,0.0);  
44:       glClear(GL_COLOR_BUFFER_BIT);  
45:       glutSwapBuffers();  
46:       glFlush();  
47:  }  
48:  void mouse(int btn,int state,int y,int x)  
49:  {  
50:       if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)  
51:       {  
52:            xi=0;  
53:            yi=y;  
54:            MidPoint_circle(yi);  
55:       }  
56:  }  
57:  void myinit()  
58:  {  
59:       glViewport(0,0,ww,wh);  
60:       glMatrixMode(GL_PROJECTION);  
61:       glLoadIdentity();  
62:       gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);  
63:       glMatrixMode(GL_MODELVIEW);  
64:  }  
65:  int main(int argc,char** argv)  
66:  {  
67:       glutInit(&argc,argv);  
68:       glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  
69:       glutInitWindowSize(ww,wh);  
70:       glutCreateWindow("MidPoint-Circle");  
71:       glutDisplayFunc(display);  
72:       myinit();  
73:       glutMouseFunc(mouse);  
74:       glutMainLoop();  
75:       return 0;  
76:  }  

Opengl,C++ : Draw Circle With Bresenham’s Circle Algorithm

1:  #include<stdio.h>  
2:  #include<GL/glut.h>  
3:  #include<math.h>  
4:  int ww=400,wh=400;  
5:  int first=0;  
6:  int xi,yi,xf,yf;  
7:  void putpixel(int x,int y)  
8:  {  
9:       glBegin(GL_POINTS);  
10:            glVertex2i(x,y);  
11:       glEnd();  
12:       glFlush();  
13:  }  
14:  int round(double n)  
15:  {  
16:       return (n>=0)?(int)(n+0.5):(int)(n-0.5);  
17:  }  
18:  void Bresenham_circle(int r)  
19:  {  
20:       int x=0,y=r,d=3-2*r;  
21:       while(x<=y){  
22:            //Here Transform each x,y coordinates by 250 pixels   
23:            putpixel(250+x, 250+y);  
24:            putpixel(250+y, 250+x);  
25:            putpixel(250-x, 250+y);  
26:            putpixel(250-x, 250-y);  
27:            putpixel(250-y, 250+x);  
28:            putpixel(250-y, 250-x);  
29:            putpixel(250+y, 250-x);  
30:            putpixel(250+x, 250-y);  
31:            if(d<0)  
32:                 d=d+(4*x)+6;  
33:            else{  
34:                 d=d+(4*(x-y))+10;  
35:                 y--;  
36:            }  
37:            x++;  
38:       }  
39:  }  
40:  void display()  
41:  {  
42:       glClearColor(0.4,0.7,0.2,1.0);  
43:       glColor3f(0.5,0.3,0.0);  
44:       glClear(GL_COLOR_BUFFER_BIT);  
45:       glutSwapBuffers();  
46:       glFlush();  
47:  }  
48:  void mouse(int btn,int state,int y,int x)  
49:  {  
50:       if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)  
51:       {  
52:            xi=0;  
53:            yi=y;  
54:            Bresenham_circle(yi);  
55:       }  
56:  }  
57:  void myinit()  
58:  {  
59:       glViewport(0,0,ww,wh);  
60:       glMatrixMode(GL_PROJECTION);  
61:       glLoadIdentity();  
62:       gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);  
63:       glMatrixMode(GL_MODELVIEW);  
64:  }  
65:  int main(int argc,char** argv)  
66:  {  
67:       glutInit(&argc,argv);  
68:       glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  
69:       glutInitWindowSize(ww,wh);  
70:       glutCreateWindow("Bresenham-Circle");  
71:       glutDisplayFunc(display);  
72:       myinit();  
73:       glutMouseFunc(mouse);  
74:       glutMainLoop();  
75:       return 0;  
76:  }  

Opengl,C++ : Draw Line With Bresenham Line Algorithm

Algorithm to rasterize lines that go from left to right with slope between 0 and 90 degree ,right to left with slope between 0 and 90 degree.

1:  #include <GL/glut.h>  
2:  #include <math.h>  
3:  int ww = 600, wh = 400;  
4:  int first = 0;  
5:  int xi, yi, xf, yf;  
6:  void putPixel (int x, int y)  
7:  {  
8:   glColor3f (0.3, 0.2, 0.0); // activate the pixel by setting the point color to white  
9:   glBegin (GL_POINTS);  
10:   glVertex2i (x, y); // set the point  
11:   glEnd ();  
12:   glFlush (); // process all openGL routines as quickly as possible  
13:  }  
14:  void display()  
15:  {  
16:   glClearColor(0.4, 0.7, 0.5, 1.0);  
17:   glColor3f(0.2, 0.3, 0.3);  
18:   glClear(GL_COLOR_BUFFER_BIT);  
19:   glFlush();  
20:  }  
21:  void bresenhamAlg (int x0, int y0, int x1, int y1)  
22:  {  
23:  int dx = abs (x1 - x0);  
24:  int dy = abs (y1 - y0);  
25:  int x, y;  
26:  if (dx >= dy)  
27:  {  
28:   int d = 2*dy-dx;  
29:   int ds = 2*dy;  
30:   int dt = 2*(dy-dx);  
31:       if (x0 < x1)   
32:       {  
33:            x = x0;  
34:            y = y0;  
35:       }  
36:        else  
37:        {   
38:             x = x1;  
39:             y = y1;  
40:             x1 = x0;  
41:             y1 = y0;  
42:        }  
43:       putPixel (x, y);  
44:   while (x < x1)  
45:   {  
46:        if (d < 0)  
47:        d += ds;  
48:        else {  
49:             if (y < y1) {  
50:              y++;  
51:              d += dt;  
52:             }   
53:             else {  
54:              y--;  
55:              d += dt;  
56:             }  
57:            }  
58:   x++;  
59:        putPixel (x, y);  
60:       }  
61:       }  
62:       else {   
63:             int d = 2*dx-dy;  
64:             int ds = 2*dx;  
65:             int dt = 2*(dx-dy);  
66:             if (y0 < y1) {  
67:             x = x0;  
68:             y = y0;  
69:             }  
70:             else {   
71:             x = x1;  
72:             y = y1;  
73:             y1 = y0;  
74:             x1 = x0;  
75:             }  
76:            putPixel (x, y);   
77:        while (y < y1)  
78:        {  
79:              if (d < 0)  
80:                 d += ds;  
81:              else {  
82:                      if (x > x1){  
83:                      x--;  
84:                      d += dt;  
85:                   } else {  
86:                      x++;  
87:                      d += dt;  
88:                   }  
89:              }  
90:              y++;  
91:              putPixel (x, y);  
92:        }  
93:       }  
94:  }  
95:       void mouse(int btn, int state, int x, int y)  
96:       {  
97:        if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)  
98:        {  
99:        switch(first)  
100:        {  
101:        case 0:  
102:         xi = x;  
103:         yi = (wh-y);  
104:         first = 1;  
105:         break;  
106:        case 1:  
107:         xf = x;  
108:         yf = (wh-y);  
109:         bresenhamAlg ( xi, yi, xf, yf);  
110:         first = 0;  
111:         break;   
112:        }  
113:        }  
114:       }  
115:  void myinit()  
116:  {        
117:     glViewport(0,0,ww,wh);  
118:     glMatrixMode(GL_PROJECTION);  
119:     glLoadIdentity();  
120:     gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);  
121:     glMatrixMode(GL_MODELVIEW);  
122:  }  
123:  int main(int argc, char** argv)  
124:  {  
125:     glutInit(&argc,argv);  
126:     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  
127:     glutInitWindowSize(ww,wh);  
128:     glutCreateWindow("Bresenham Line Algorithm");  
129:     glutDisplayFunc(display);  
130:     myinit();  
131:     glutMouseFunc(mouse);  
132:     glutMainLoop();  
133:     return 0;  
134:  }  

Opengl,C++ : Draw Line With Mouse Click

1:  #include <GL/glut.h>  
2:  int ww=600,wh=400;  
3:  int first=0;  
4:  int xi,yi,xf,yf;  
5:  void drawLine(int x1,int y1,int x2,int y2)  
6:  {  
7:       glClear(GL_COLOR_BUFFER_BIT);  
8:       glLineWidth(5.0);  
9:       glBegin(GL_LINES);  
10:            glVertex2i(x1,y1);  
11:            glVertex2i(x2,y2);  
12:       glEnd();  
13:       glFlush();  
14:  }  
15:  void display()  
16:  {  
17:       glClearColor(0.2, 0.4, 0.0, 1.0);  
18:       glColor3f(0.7, 0.4, 0.0);  
19:       glClear(GL_COLOR_BUFFER_BIT);  
20:       glFlush();  
21:  }  
22:  void mouse(int btn,int state,int x,int y)  
23:  {  
24:       if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)  
25:       {  
26:            switch(first)  
27:            {  
28:            case 0:  
29:                 xi=x;  
30:                 yi=wh-y;  
31:                 first=1;  
32:                 break;  
33:            case 1:  
34:                 xf=x;  
35:                 yf=wh-y;  
36:                 drawLine(xi,yi,xf,yf);  
37:                 first=0;  
38:                 break;  
39:            }  
40:       }  
41:  }  
42:  void myinit()  
43:  {  
44:       glViewport(0,0,ww,wh);  
45:       glMatrixMode(GL_PROJECTION);  
46:       glLoadIdentity();  
47:       gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);  
48:       glMatrixMode(GL_MODELVIEW);  
49:  }  
50:  int main(int argc,char** argv)  
51:  {  
52:       glutInit(&argc,argv);  
53:       glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  
54:       glutInitWindowSize(ww,wh);  
55:       glutCreateWindow("Draw Line With Mouse Click");  
56:       glutDisplayFunc(display);  
57:       myinit();  
58:       glutMouseFunc(mouse);  
59:       glutMainLoop();  
60:       return 0;  
61:  }  

Opengl,C++ : Display Various glu Objects

1:  /* displays various glu objects */  
2:  #include <stdlib.h>  
3:  #include <GL/glut.h>  
4:  GLUquadricObj *obj;  
5:  static GLfloat theta[] = {0.0,0.0,0.0};  
6:  static GLint axis = 2;  
7:  void init()  
8:  {  
9:       glClearColor(1.0, 1.0, 1.0, 1.0);  
10:       glColor3f(1.0, 0.0, 0.0);  
11:       obj = gluNewQuadric(); //Create quadrics to draw simple geometric shapes  
12:       gluQuadricDrawStyle(obj, GLU_LINE); //Draw the shapes using different OpenGL primitives  
13:       //GLU_LINE     Quadrics are drawn “wireframe,” using line primitives.  
14:  }  
15:  void display()  
16:  {  
17:       /* display callback, clear frame buffer and z buffer,  
18:       rotate object and draw, swap buffers */  
19:       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
20:       glLoadIdentity();//Reset the drawing perspective  
21:       glRotatef(theta[0], 1.0, 0.0, 0.0);  
22:       glRotatef(theta[1], 0.0, 1.0, 0.0);  
23:       glRotatef(theta[2], 0.0, 0.0, 1.0);  
24:       gluDisk(obj, 0.5, 1.0, 10, 10);  
25:    // GLUquadric*      quad, GLdouble      inner,GLdouble      outer,GLint slices, GLint loops)  
26:  //quad== Specifies the quadrics object (created with gluNewQuadric).  
27:  //inner== Specifies the inner radius of the disk (may be 0).  
28:  //outer== Specifies the outer radius of the disk.  
29:  //slices== Specifies the number of subdivisions around the z axis.  
30:  //loops== Specifies the number of concentric rings about the origin into which the disk is subdivided.  
31:       //glutWirelcosahedron();  
32:       //glutWireDodecahedron();  
33:       //gluSphere(obj, 1.0, 12, 12);  
34:       //gluCylinder(obj,1.0,0.5,1.0,12,12);  
35:       //gluPartialDisk(obj,0.5,1.0,10,10,0.0,45.0);  
36:       //glutWireTeapot(1.0);  
37:       //glutWireTorus(0.5,1.0,10,10);  
38:       //glutWireCone(1.0,1.0,10,10);  
39:       glutSwapBuffers();  
40:  }  
41:  void spinObject()  
42:  {  
43:       /* Idle callback, spin cube 2 degrees about selected axis */  
44:       theta[axis] += 0.01;  
45:       if( theta[axis] > 360.0 ) theta[axis] -= 360.0;  
46:       glutPostRedisplay();  
47:  }  
48:  void mouse(int btn, int state, int x, int y)  
49:  {  
50:       /* mouse callback, selects an axis about which to rotate */  
51:       if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;  
52:       if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;  
53:       if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;  
54:  }  
55:  void myReshape(int w, int h)  
56:  {  
57:    glViewport(0, 0, w, h);  
58:    glMatrixMode(GL_PROJECTION);  
59:    glLoadIdentity();  
60:    if (w <= h)  
61:      glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,  
62:        2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);  
63:    else  
64:      glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,  
65:        2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);  
66:    glMatrixMode(GL_MODELVIEW);  
67:  }  
68:  void key(unsigned char key, int x, int y)  
69:  {  
70:       if(key=='x') glutIdleFunc(NULL);  
71:       if(key=='s') glutIdleFunc(spinObject);  
72:  }  
73:  void main(int argc, char **argv)  
74:  {  
75:    glutInit(&argc, argv);  
76:    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);  
77:    glutInitWindowSize(500, 500);  
78:    glutCreateWindow("object");  
79:       init();  
80:    glutReshapeFunc(myReshape);  
81:    glutDisplayFunc(display);  
82:       glutIdleFunc(NULL);  
83:       glutMouseFunc(mouse);  
84:       glutKeyboardFunc(key);  
85:       glutMainLoop();  
86:  }  

Opengl,C++ : Drawing Polygon

1:  #include<GL/glut.h>  
2:  void init(){  
3:  }  
4:  void display(void)  
5:  {  
6:       glClear(GL_COLOR_BUFFER_BIT);  
7:       glPointSize(5.0);  
8:       //glBegin(GL_POINTS);  
9:       //glBegin(GL_TRIANGLES);  
10:       glBegin(GL_POLYGON);  
11:            glColor3f(1.0,1.0,0.0);  
12:            glVertex2f(-0.5,-0.5);  
13:            glColor3f(0.5,1.0,0.8);  
14:            glVertex2f(0.5,-0.5);  
15:            glColor3f(1.0,0.2,0.0);  
16:            glVertex2f(0.5,0.5);  
17:            glColor3f(1.0,0.2,0.0);  
18:            glVertex2f(0.1,0.9);  
19:       glEnd();  
20:       glFlush();  
21:  }  
22:  int main(int argc,char** argv)  
23:  {  
24:       glutCreateWindow("Draw Polygon");  
25:       glutDisplayFunc(display);  
26:       init();  
27:       glutMainLoop();  
28:  }  

Opengl,C++ : Drawing Points

1:  #include<GL/glut.h>  
2:  void init(){  
3:  }  
4:  void display(void)  
5:  {  
6:       glClear(GL_COLOR_BUFFER_BIT);  
7:       glPointSize(5.0);  
8:       glBegin(GL_POINTS);       
9:            glColor3f(1.0,1.0,0.0);  
10:            glVertex2f(-0.5,-0.5);  
11:            glColor3f(0.5,1.0,0.8);  
12:            glVertex2f(0.5,-0.5);  
13:            glColor3f(1.0,0.2,0.0);  
14:            glVertex2f(0.5,0.5);  
15:            glColor3f(1.0,0.2,0.0);  
16:            glVertex2f(0.1,0.9);  
17:       glEnd();  
18:       glFlush();  
19:  }  
20:  int main(int argc,char** argv)  
21:  {  
22:       glutCreateWindow("Draw Points");  
23:       glutDisplayFunc(display);  
24:       init();  
25:       glutMainLoop();  
26:  }