الأحد، 13 ديسمبر 2015

CAM

// T2.cpp : Defines the entry point for the console application.
//


 #include<math.h>
#include <glut.h>
#include <stdio.h>
#include <string.h>
float tx=0.0;
float ty=0.0;
float tz=0.0;
void draw()
{
glColor3f (1.0, 1.0, 0.0);
glutWireCube (3);
//glutSolidCube(.01);
}
void reshape(int w, int h)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity();

//glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
gluPerspective(45,1,0,50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}
void mySpecialKey(int key, int x, int y)
{
//GLfloat tx = 0.0, ty = 0.0,tz=0.0;
switch(key)
{
case GLUT_KEY_UP : ty += 0.05; break;
case GLUT_KEY_DOWN : ty -= 0.05; break;
case GLUT_KEY_RIGHT : tx += 0.05; break;
case GLUT_KEY_LEFT : tx -= 0.05; break;
case GLUT_KEY_F1   : tz -= 0.01;break;
case GLUT_KEY_F2   : tz += 0.01;break;
case GLUT_KEY_HOME : glLoadIdentity();
break;
}
//render_1(tx,ty,tz); //no need for this call the system will call it automatcly when calling the follwoing fucntion glutPostRedisplay
glutPostRedisplay();
}
void render_1()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt (0.0+tx,0.0+ty, 0.0+tz, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0);
gluLookAt (tx,ty, tz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
draw();
glutSwapBuffers();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
//window
glutInitWindowPosition(40,40);
glutInitWindowSize(160,160);
glutCreateWindow("lookat 1");
glutReshapeFunc(reshape);
glutDisplayFunc(render_1);
glutSpecialFunc(mySpecialKey);
glutMainLoop();
}

Draw circle



// Example_2_1.cpp : Defines the entry point for the console application.
//
// Author  : ALIA

// Simple circul OpenGL
//
// Last tested in Visual C++ 2010 Express

//#include "stdafx.h"
#include <gl/glut.h>
#include <math.h>



//======================================================
// DISPLAY CALL BACK ROUTINE
//======================================================
void DrawCircle(float cx, float cy, float r, int num_segments)
{
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle

float x = r * cosf(theta);//calculate the x component
float y = r * sinf(theta);//calculate the y component

glVertex2f(x + cx, y + cy);//output vertex

}
glEnd();
}

void displayCallBack()
{
    // Clear the window
glClear(GL_COLOR_BUFFER_BIT);


DrawCircle(0,0,1,100);

// Flush the buffer to force drawing of all objects thus far
glFlush();
}

//======================================================
// MAIN PROGRAM
//======================================================
void main( int argc, char **argv)
{
// Create the Window
glutCreateWindow("Example ");
   
// Assign displayCallBack() to be the function called whenever a display
// event occurs, generally after a resize or expose event
glutDisplayFunc(displayCallBack);

// Print information about the application in the command console
//printf("OpenGL Console Output: Hello World\nNo Interaction with this application possible.");

//Enter infinite loop calling 'displayCallBack'
glutMainLoop();

// return 0;
}

Look At



// T2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
 #include<math.h>
#include <GL/glut.h>
#include <stdio.h>
#include <string.h>


float tx=0.0;
float ty=0.0;
float tz=0.0;

void draw()
{
glColor3f (1.0, 1.0, 0.0);
glutWireCube (3);
}
void reshape(int w, int h)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mySpecialKey(int key, int x, int y)
{
GLfloat tx = 0.0, ty = 0.0,tz=0.0;
switch(key)
{
case GLUT_KEY_UP : ty += 0.05; break;
case GLUT_KEY_DOWN : ty -= 0.005; break;
case GLUT_KEY_RIGHT : tx += 0.005; break;
case GLUT_KEY_LEFT : tx -= 0.005; break;
case GLUT_KEY_HOME : glLoadIdentity();
break;
}
//render_1(tx,ty,tz); //no need for this call the system will call it automatcly when calling the follwoing fucntion glutPostRedisplay
glutPostRedisplay();
}
void render_1()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gluLookAt (0.0+tx,0.0+ty, 0.0+tz, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0);
draw();
glutSwapBuffers();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
//window
glutInitWindowPosition(40,40);
glutInitWindowSize(160,160);
glutCreateWindow("lookat 1");
glutReshapeFunc(reshape);
glutDisplayFunc(render_1);
glutSpecialFunc(mySpecialKey);
glutMainLoop();
}

SubMenue - sphere .. cone and colour



#include <stdio.h>
#include <math.h>
#include <iostream>
#include <glut.h>
using namespace std;

GLfloat angle = 0.0; //angle for cube1
GLfloat tangle = 0.0; //angle for cube2
char c='s';
float r=1.0,g=0.0,b=0.0;
//-------------------------------------------------------------------
void cube (void) {
glPushMatrix(); //set where to start the current object transformations
glTranslatef(0.5, 1, 0); //move cube1 to the right
glRotatef(angle, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(r, g, b); //change cube1 to red
//glutWireCube(0.5);
glutWireCone(0.5, 0.8, 16, 16);
glPopMatrix(); //end the current object transformations
}

void cube2 (void) {
glPushMatrix(); //set where to start the current object
glTranslatef(1.5, 1, 0); //move cube2 to the left
glRotatef(tangle, 1.0, 0.0, 0.0);
glRotatef(tangle, 0.0, 1.0, 0.0);
glRotatef(tangle, 0.0, 0.0, 1.0);
glColor3f(r, g, b); //change cube2 to green
//glutWireTeapot(0.5);
glutWireTeapot(0.5);
glPopMatrix(); //end the current object transformations
}
void cube3 (void) {
glPushMatrix(); //set where to start the current object
glTranslatef(2.5, 1, 0); //move cube2 to the left
glRotatef(tangle, 0.0, 1.0, 0.0);
glRotatef(tangle, 1.0, 0.0, 0.0);
glRotatef(tangle, 0.0, 0.0, 1.0);
glColor3f(r, g, b); //change cube2 to green
glutWireSphere(0.5,16,16);

glPopMatrix(); //end the current object transformations
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
void myDisplay(void)
{
 glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

if (c=='c')
cube();
if (c=='t')
cube2();
if(c=='s')
cube3();
glFlush();
glutSwapBuffers();
angle+= 0.5;
tangle+= 1.0;

}

void exitmenu(int m)
{
if (m == 1) exit(0);  // exit the program if exit is selected


}
void shapemenu(int s)
{

if (s==1)
c='c';
if(s==2)
c='t';
if(s==3)
c='s';
}
void colormenu(int c)
{

if (c==1)
{r=1.0;g=0.0; b=0.0;}
if(c==2)
{r=0.0;g=1.0; b=0.0;}
if(c==3)
{r=0.0;g=0.0; b=1.0;}
}
void myMouse(int button, int state, int x, int y)
{
  if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    glutIdleFunc(myDisplay); // starts animation
  else if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    glutIdleFunc(NULL); // stops animation
}
void set_up_menu(void)
{
int coloridm, shapeidm;
coloridm=glutCreateMenu(colormenu);
glutAddMenuEntry("RED",1);
glutAddMenuEntry("GREEN",2);
glutAddMenuEntry("BLUE",3);
shapeidm=glutCreateMenu(shapemenu);
    glutAddMenuEntry("Cone",1);
glutAddMenuEntry("Teapot",2);
 glutAddMenuEntry("Sphere",3);

 glutCreateMenu(exitmenu);

 glutAddSubMenu("3D Shape", shapeidm);
 glutAddSubMenu("Color", coloridm);
glutAddMenuEntry("Exit",1);  //this is not a submenu -> calls callback


 glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void myInit(void)
{
  glClearColor(0.0, 0.0, 0.0, 0.0); // set the background to black
  glColor3f(1.0f, 1.0f, 1.0f);      // set the drawing color to white
 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0.0, 3.0, 0.0, 3.0);
}

//-------------------------------------------------------------------
void main( int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
  glutInitWindowSize(550,450);
  glutInitWindowPosition(0,0);
  glutCreateWindow("One Triangle");
  glutMouseFunc(myMouse);
  glutDisplayFunc(myDisplay);
  //glutKeyboardFunc(myKeyboard);
  //glutIdleFunc(myDisplay);

  myInit();
  set_up_menu();
  glutMainLoop();
}

Sphere .. Open Gl

#include <glut.h>
#include <stdio.h>
#include <math.h>

GLfloat angle = 0.0; //angle for cube1
GLfloat tangle = 0.0; //angle for cube2

//-------------------------------------------------------------------

void DrawSphere (void) {
glPushMatrix(); //set where to start the current object
glTranslatef(1.5, 1.5, 0); //move cube2 to the left
glRotatef(tangle, 0.0, 1.0, 0.0);
glRotatef(tangle, 1.0, 0.0, 0.0);
glRotatef(tangle, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0); //change cube2 to green
glutWireSphere(0.5,16,16);

glPopMatrix(); //end the current object transformations
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
void myDisplay(void)
{
 glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();


DrawSphere();
glFlush();
glutSwapBuffers();
angle+= 0.5;
tangle+= 1.0;

}

/*void myMouse(int button, int state, int x, int y)
{
  if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    glutIdleFunc(myDisplay); // starts animation
  else if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    glutIdleFunc(NULL); // stops animation
}*/
void myKeyboard(unsigned char key, int x, int y)
{
if (key=='r')
 glutIdleFunc(myDisplay);
if (key=='s')
 glutIdleFunc(NULL);
}



void myInit(void)
{
  glClearColor(0.0, 0.0, 0.0, 0.0); // set the background to black
  glColor3f(1.0f, 1.0f, 1.0f);      // set the drawing color to white
 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0.0, 3.0, 0.0, 3.0);
}

//-------------------------------------------------------------------
void main( int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
  glutInitWindowSize(550,450);
  glutInitWindowPosition(0,0);
  glutCreateWindow("One Triangle");
  //glutMouseFunc(myMouse);
  glutDisplayFunc(myDisplay);
  glutKeyboardFunc(myKeyboard);

  myInit();
 
  glutMainLoop();
}

Transformation -- Rotate



#include <glut.h>

#include <math.h>


float midX = 0.5;

float midY = sqrt(3.0)/2.0;

float PI=3.1415;


//------------------------------------------------------------------- 

void drawTriangle(void) 


  glBegin(GL_POLYGON); 

    glColor3f ( 1.0, 0.0, 0.0); 

    glVertex2f( 0.0, 0.0); 


    glColor3f ( 0.0, 1.0, 0.0); 

    glVertex2f( midX, midY); 


    glColor3f ( 0.0, 0.0, 1.0); 

    glVertex2f( 1.0, 0.0); 

  glEnd();



//------------------------------------------------------------------- 

void Transform(GLfloat tx, GLfloat ty)

{

 glMatrixMode(GL_MODELVIEW);

 glClear(GL_COLOR_BUFFER_BIT);



 glTranslatef(tx, ty, 0.0);

 drawTriangle();




 glFlush();

 glutSwapBuffers();

}

void R(GLfloat tx, GLfloat ty , GLfloat theta )

{

 glMatrixMode(GL_MODELVIEW);

 glClear(GL_COLOR_BUFFER_BIT);



 glTranslatef(tx, ty, 0.0);
glRotatef(theta, 1, 0 ,2);
 glTranslatef(-tx, -ty, 0.0);

 drawTriangle();




 glFlush();

 glutSwapBuffers();

}


//------------------------------------------------------------------- 

void myDisplay(void)

{

  glClear( GL_COLOR_BUFFER_BIT );


  drawTriangle();

  glFlush();

  glutSwapBuffers();

}


//-------------------------------------------------------------------

void mySpecialKey(int key, int x, int y)

{

 GLfloat tx = 0.0, ty = 0.0;


 switch(key)

 {

 case GLUT_KEY_UP       : ty += 0.05; break;

 case GLUT_KEY_DOWN : ty -= 0.05; break;

 case GLUT_KEY_RIGHT : tx += 0.05; break;

 case GLUT_KEY_LEFT : tx -= 0.05; break;

 case GLUT_KEY_F1 : R( tx,ty , 30 ); break;
 case GLUT_KEY_F2  : R( tx,ty , -90 ); break;

 case GLUT_KEY_HOME : glLoadIdentity();

   drawTriangle();

   break;

 }


 Transform(tx, ty);


 glutPostRedisplay();

}


//-------------------------------------------------------------------

void myInit(void)

{

  glClearColor(0.0, 0.0, 0.0, 0.0); // set the background to black

  glColor3f(1.0f, 1.0f, 1.0f);      // set the drawing color to white

  

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  gluOrtho2D(0.0, 3.0, 0.0, 3.0);

}


//-------------------------------------------------------------------

void main( int argc, char **argv)

{

  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB ); 

  glutInitWindowSize(450,450);

  glutInitWindowPosition(0,0);

  glutCreateWindow("One Triangle");

  

  glutDisplayFunc(myDisplay);

  glutSpecialFunc(mySpecialKey);

  myInit();

  

  glutMainLoop();

}

Transformations



Objectives
To perform transformations on 2D objects using OpenGL programming

OpenGL transformation functions


OpenGL maintains a 4x4 transformation matrix that is multiplied by every (x,y,z) value sent to glVertex(). This is sometimes referred to as the CT (current transform). The CT can be modified by the following functions:
  • glLoadIdentity();
  • glTranslate{fd}( dx, dy, dz );
  • glScale{fd}( sx, sy, sz);
  • glRotate{fd}( angleInDegrees, axisXvalue, axisYvalue, axisZvalue);
How to use the OpenGL transformation functions


There are two different 4x4 matrices used by OpenGL to produce an image. They are:
  • Projection matrix - used to "project" a 3D image onto a 2D plane to create an image.
  • ModelView matrix - used to transform objects and the camera to create the desired scene and view.
Commands like glTranslate, glRotate, and glScale will modify the active matrix. It is the programmer's responsibility to make sure the appropriate matrix is "active." The active matrix is determined by calls to glMatrixMode(), such as:
  • glMatrixMode(GL_PROJECTION); // make the projection matrix active
  • glMatrixMode(GL_MODELVIEW); // make the modelview matrix active
2) The transformations caused by glRotate, glTranslate, and glScale are applied to the CT (current transform). They will accumulate continually unless you reset the matrix back to the Identity matrix. If you want to start a new set of transformations, then call glLoadIdentity(), as in the following example:
  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity();               // start a new sequence of transforms
  glTranslatef(-3.0, -4.0, 0.0);
  glRotatef(90.0, 1.0, 0.0, 0.0);
  drawObject1();

  glLoadIdentity();               // start a new sequence of transforms
  glTranslatef(4.0, 2.0, 1.0);
  glScalef(2.0, 1.0, 1.0);
  drawObject2();




Programming Exercises


1.      Implement an Opengl interactive graphics program that draws a triangle which can be translated by pressing 'l', 'r', 'u', or 'd' keys.

You can draw a triangle with equal side 1 unit:

//-------------------------------------------------------------------
float midX = 0.5;
float midY = sqrt(3)/2.0;
void drawTriangle(void)
{
  glBegin(GL_POLYGON);
    glColor3f ( 1.0, 0.0, 0.0);
    glVertex2f( 0.0, 0.0);


    glColor3f ( 0.0, 1.0, 0.0);
    glVertex2f( midX, midY);


    glColor3f ( 0.0, 0.0, 1.0);
    glVertex2f( 1.0, 0.0);
  glEnd();
}
//-------------------------------------------------------------------



2.      Modify your programs from the previous questions so that the triangle now will translate and rotate when the user presses the 'l', 'r', 'u', or 'd' keys (for translation) and 'c' (for clockwise rotation),'a' (for anti-clockwise rotation),





.................


#include <glut.h>

#include <math.h>


float midX = 0.5;

float midY = sqrt(3.0)/2.0;

float PI=3.1415;


//------------------------------------------------------------------- 

void drawTriangle(void) 


  glBegin(GL_POLYGON); 

    glColor3f ( 1.0, 0.0, 0.0); 

    glVertex2f( 0.0, 0.0); 


    glColor3f ( 0.0, 1.0, 0.0); 

    glVertex2f( midX, midY); 


    glColor3f ( 0.0, 0.0, 1.0); 

    glVertex2f( 1.0, 0.0); 

  glEnd();



//------------------------------------------------------------------- 

void Transform(GLfloat tx, GLfloat ty)

{

 glMatrixMode(GL_MODELVIEW);

 glClear(GL_COLOR_BUFFER_BIT);



 glTranslatef(tx, ty, 0.0);

 drawTriangle();




 glFlush();

 glutSwapBuffers();

}


//------------------------------------------------------------------- 

void myDisplay(void)

{

  glClear( GL_COLOR_BUFFER_BIT );


  drawTriangle();

  glFlush();

  glutSwapBuffers();

}


//-------------------------------------------------------------------

void mySpecialKey(int key, int x, int y)

{

 GLfloat tx = 0.0, ty = 0.0;


 switch(key)

 {

 case GLUT_KEY_UP       : ty += 0.05; break;

 case GLUT_KEY_DOWN : ty -= 0.05; break;

 case GLUT_KEY_RIGHT : tx += 0.05; break;

 case GLUT_KEY_LEFT : tx -= 0.05; break;

 case GLUT_KEY_HOME : glLoadIdentity();

  // drawTriangle();

   break;

 }


 Transform(tx, ty);


 glutPostRedisplay();

}


//-------------------------------------------------------------------

void myInit(void)

{

  glClearColor(0.0, 0.0, 0.0, 0.0); // set the background to black

  glColor3f(1.0f, 1.0f, 1.0f);      // set the drawing color to white

  

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  gluOrtho2D(0.0, 3.0, 0.0, 3.0);

}


//-------------------------------------------------------------------

void main( int argc, char **argv)

{

  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB ); 

  glutInitWindowSize(450,450);

  glutInitWindowPosition(0,0);

  glutCreateWindow("One Triangle");

  

  glutDisplayFunc(myDisplay);

  glutSpecialFunc(mySpecialKey);

  myInit();

  

  glutMainLoop();

}


Menu - Open Gl

#include <glut.h>

float r=1.0, g=0.0, b=0.0;
void init(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0.0,500.0,0.0,400.0);
}
void myLine(void)
{
 
 glClear( GL_COLOR_BUFFER_BIT );
  glColor3f(r, g, b);    
  glBegin(GL_LINES);
    glVertex2i(0, 0);
    glVertex2i( 200, 200);

  glEnd();

  glFlush();
}
void myTriangle(void)
{
 
glClear( GL_COLOR_BUFFER_BIT );
 glColor3f(r, g, b);    
  glBegin(GL_TRIANGLES);
    glVertex2i( 200, 200);
glVertex2i( 200, 300);
glVertex2i( 100, 250);
   
  glEnd();

  glFlush();
}
void mySquare(void)
{
 
glClear( GL_COLOR_BUFFER_BIT );
  glColor3f(r, g, b);    
  glBegin(GL_POLYGON);
    glVertex2i( 200, 200);
glVertex2i( 200, 400);
glVertex2i( 400, 400);
    glVertex2i( 400, 200);
  glEnd();

  glFlush();
}
void myDisplay(void)
{
  glClear( GL_COLOR_BUFFER_BIT );

}
//-------------------------------------------------------------------
void menu(int ms)
{
if (ms==1)
myLine();
if(ms==2)
myTriangle();
if(ms==3)
mySquare();
if(ms==4)
{
r=1.0;
g=0.0;
b=0.0;
}
if(ms==5)
{
r=0.0;
g=1.0;
b=0.0;
}
if(ms==6)
{
r=0.0;
g=0.0;
b=1.0;
}
if(ms==7)
exit(0);
}
void set_up_menu(void)
{
glutCreateMenu(menu);
    glutAddMenuEntry("Line",1);
glutAddMenuEntry("Triangle",2);
 glutAddMenuEntry("Square",3);
 glutAddMenuEntry("Red",4);
glutAddMenuEntry("Green",5);
 glutAddMenuEntry("Blue",6);
 glutAddMenuEntry("Exit",7);
 glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void main( int argc, char **argv)
{
  glutInit(&argc, argv); // Initialize GLUT
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); // Set display mode
  glutInitWindowPosition(100,200); // Set top-left display window position
  glutInitWindowSize(320,320); // Set display window width and height
  glutCreateWindow("An Example");// Create display window

  init();   // Execute initialization procedure
  glutDisplayFunc(myDisplay);
  set_up_menu();// Send graphics to display window
  glutMainLoop(); // Display everything and wait
}


Mouse & keyboard .. switch

#include <iostream>
#include <glut.h>

GLint  WindowHeight = 400;
GLint  WindowWidth  = 400;

//-------------------------------------------------------------------
void myInit(void)
{
glClearColor(0.0, 0.0, 1.0, 0.0); // set the background to blue
glColor3f(1.0f, 1.0f, 1.0f);      // set the drawing color to white
glPointSize(4.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(float) WindowWidth,0.0, (float) WindowHeight);
}
//-------------------------------------------------------------------
void myDisplay(void)
{
  glClear( GL_COLOR_BUFFER_BIT );
  glFlush();
}
//-------------------------------------------------------------------
void draw(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
 
glFlush();
}
//-------------------------------------------------------------------
void myMouse(int button, int state, int x, int y)
{

if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
draw(x, WindowHeight-y);
}

}
void processNormalKeys(unsigned char key, int x, int y){

if (key=='q')
exit(0);

}
//-------------------------------------------------------------------
void main( int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(WindowWidth,WindowHeight);
glutInitWindowPosition(0,0);
glutCreateWindow("Mouse Input");

glutDisplayFunc(myDisplay);
glutMouseFunc(myMouse);
glutKeyboardFunc(processNormalKeys);
myInit();
glutMainLoop();
}


...............................................................................

#include <iostream> 
#include <glut.h> 

GLint  WindowHeight = 400; 
GLint  WindowWidth  = 400; 
int count=0;
int a1,b1;

//------------------------------------------------------------------- 
void myInit(void) 
glClearColor(0.0, 0.0, 1.0, 0.0); // set the background to blue 
glColor3f(1.0f, 1.0f, 1.0f);      // set the drawing color to white 
glPointSize(4.0); 

glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
gluOrtho2D(0.0,(float) WindowWidth,0.0, (float) WindowHeight); 
//------------------------------------------------------------------- 
void myDisplay(void) 
  glClear( GL_COLOR_BUFFER_BIT ); 
  glFlush(); 
//------------------------------------------------------------------- 
void draw(int x, int y)  
{
if (count<2)
{
a1=x;
b1=y;
glBegin(GL_POINTS); 
glVertex2i(x, y); 
glEnd(); 
}
else
{
glColor3f(1.0f, 0.0f, 0.0f);      // set the drawing color to red
glBegin(GL_LINES); // start drawing in 'line' mode
    glVertex2i( x, y); // specify the lines geometry
    glVertex2i( a1, b1);
glEnd();
count=0;
}

  
glFlush();
}
//------------------------------------------------------------------- 
void myMouse(int button, int state, int x, int y) 
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) 
count++;
draw(x, WindowHeight-y);

void processNormalKeys(unsigned char key, int x, int y){

if (key=='q')
exit(0);
}
//------------------------------------------------------------------- 
void main( int argc, char **argv) 
glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );  
glutInitWindowSize(WindowWidth,WindowHeight); 
glutInitWindowPosition(0,0); 
glutCreateWindow("Mouse Input"); 

glutDisplayFunc(myDisplay); 
glutMouseFunc(myMouse); 
glutKeyboardFunc(processNormalKeys);
myInit(); 
glutMainLoop(); 
}


...............................................


#include <glut.h>  // Note: this automatically includes other 
                      // required header files, including: 
                      //    glu.h      (GL utility functions) 
                      //    gl.h       (GL functions) 
                      //    windows.h  (Microsoft windows stuff) 

int red=0.0,green=0.0,blue=0.0;

void init(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0); // set the background to white

  glMatrixMode(GL_PROJECTION); // set projection parameters
  glLoadIdentity();
  gluOrtho2D(0.0,640.0,0.0,480.0);
}

void lineSegment(void)
{
  glClear( GL_COLOR_BUFFER_BIT ); // Clear display window

  glColor3f(red,green, blue);      // set the drawing color to red
   glBegin(GL_TRIANGLES); // start drawing in 'line' mode
    glVertex2i( 50, 200); // specify the lines geometry
    glVertex2i( 150, 350);
glVertex2i(250, 200);
// end drawing 
  glEnd();


  glFlush(); // send any buffered output to  
                                    // the OpenGL rendering process 
}
void processNormalKeys(unsigned char key, int x, int y){

switch(key) {
case 'r' :
red = 1.0;
green = 0.0;
blue = 0.0; break;
case 'g' :
red = 0.0;
green = 1.0;
blue = 0.0; break;
case 'b' :
red = 0.0;
green = 0.0;
blue = 1.0; break;
default : exit(0);
}
lineSegment();
}
//-------------------------------------------------------------------

void main( int argc, char **argv)
{
  glutInit(&argc, argv); // Initialize GLUT
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); // Set display mode
  glutInitWindowPosition(100,200); // Set top-left display window position
  glutInitWindowSize(320,320); // Set display window width and height
  glutCreateWindow("An Example");// Create display window
  
  init();   // Execute initialization procedure
  glutDisplayFunc(lineSegment); // Send graphics to display window
  glutKeyboardFunc(processNormalKeys);
  glutMainLoop(); // Display everything and wait
}

Simple Drawing


  1. Open the attached source file: Lab01.cpp
Drawing lines
·         Modify parameters in glVertex2i() and change the line width.
·         Add glVertex2i() to draw another 2 lines. Note that a line needs 2 vertices as the starting and ending positions.  Thus, the number of vertices within the glBegin() and glEnd() is of multiple of 2. 
·         Set different colors for the lines.
Drawing triangles
·         Change the GL_LINE_LOOP to GL_TRIANGLES. 
·         As a triangle will need 3 vertices, make sure the number of vertices within the glBegin() and glEnd() is of multiple of 3. 
·         Change the GL_TRIANGLES to GL_TRIANGLE_FAN.  What happen to your drawing?
Drawing rectangles
·         Change the GL_TRIANGLE_FAN to GL_QUADS.  
·         As a rectangle will need 4 vertices, make sure the number of vertices within the glBegin() and glEnd() is of multiple of 4.  What will happen if it is not?
·         Change the GL_QUADS to GL_QUAD_STRIP.  What happen to your drawing?
Drawing polygons
·         Change the GL_QUAD_STRIP to GL_POLYGONS.  Try drawing an irregular shape.
Screen Coordinate System:
·         Modify the parameters in the function calls to glutInitWindowSize() and glutInitWindowPosition() until you have a good understanding of what each parameter means.
·         Note that these values are in pixels.

Window Coordinate System:
·         Modify the parameters in the function call to gluOrtho2D(); until you have a good understanding of what each parameter means
·         Note that these values are in world units.

2. Create a program to draw the 2D objects as shown in the following diagram using GL_TRIANGLE, GL_QUAD and GL_POLYGON.


>>..................................................................................


#include <glut.h>  // Note: this automatically includes other
                      // required header files, including:
                      //    glu.h      (GL utility functions)
                      //    gl.h       (GL functions)
                      //    windows.h  (Microsoft windows stuff)


void init(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0); // set the background to white

  glMatrixMode(GL_PROJECTION);           // set projection parameters
  glLoadIdentity();
  gluOrtho2D(0.0,640.0,0.0,480.0);
}

void lineSegment(void)
{
  glClear( GL_COLOR_BUFFER_BIT ); // Clear display window

  glColor3f(1.0f, 0.0f, 0.0f);      // set the drawing color to red
  glBegin(GL_TRIANGLES);                        // start drawing in 'line' mode
    glVertex2i( 50, 200);                // specify the lines geometry
    glVertex2i( 150, 350);
       glVertex2i(250, 200);
                           // end drawing
  glEnd();
   glColor3f(0.0f, 1.0f, 0.0f);      // set the drawing color to red
  glBegin(GL_QUADS);                     // start drawing in 'line' mode
    glVertex2i( 300, 200);               // specify the lines geometry
    glVertex2i( 300, 350);
       glVertex2i(400, 350);
       glVertex2i(400, 200);
                           // end drawing
  glEnd();
  glColor3f(0.0f, 0.0f, 1.0f);      // set the drawing color to red
  glBegin(GL_POLYGON);                          // start drawing in 'line' mode
    glVertex2i(450, 200); 
       glVertex2i(550, 200);// specify the lines geometry
    glVertex2i(550, 300);
       glVertex2i(500, 400);
       glVertex2i(450, 300);
       glVertex2i(450, 200);
                           // end drawing
  glEnd();
  glFlush();                                    // send any buffered output to 
                                    // the OpenGL rendering process
}
//-------------------------------------------------------------------

void main( int argc, char **argv)
{
  glutInit(&argc, argv);                                      // Initialize GLUT
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); // Set display mode
  glutInitWindowPosition(100,200);       // Set top-left display window position
  glutInitWindowSize(400,200);           // Set display window width and height
  glutCreateWindow("An Example");// Create display window
 
  init();                                       // Execute initialization procedure
  glutDisplayFunc(lineSegment);          // Send graphics to display window
  glutMainLoop();                               // Display everything and wait
}




  
Exercise
1.

Vertices specified, the interpretations are as follows:
GL_POINTS
Treats each vertex as a single point. Vertex n defines point n. N points are drawn.
GL_LINES
Treates each pair of vertices as an independent line segment. Vertices 2n-1 and 2n define line n. N/2 lines are drawn.
GL_LINE_STRIP
Draws a connected group of line segments from the first vertex to the last. Vertices n and n+1 define line n. N-1 lines drawn.
GL_LINE_LOOP
Draws a connected group of line segments from the first vertex to the last, then back to the first. Vertices n and n+1 define line n. The last line, however, is defined by vertices N and 1. N lines are drawn.
GL_TRIANGLES
Treates each triplet of vertices as an independent triangle. Vertices 3n-2, 3n-1, and 3n define triangle n. N/3 triangles are drawn.
GL_TRIANGLE_STRIP
Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. For odd n, vertices n, n+1, and n+2 define triangle n. For even n, vertices n+1, n, and n+2 define triangle n. N-2 triangles are drawn.
GL_TRIANGLE_FAN
Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. Vertices 1, n+1, and n+2 define triangle n. N-2 triangles are drawn.
GL_QUADS
Treats each group of four vertices as an independent quadrilateral. Vertices 4n-3, 4n-2, 4n-1, and 4n define quadrilateral n. N/4 quadrilaterals are drawn.
GL_QUAD_STRIP
Draws a connected group of quadrilaterals. One quadrilateral is defined for each pair of vertices presented after the first pair. Vertices 2n-1, 2n, 2n+2, and 2n+1 define quadrilateral n. N/2-1 quadrilaterals are drawn. Note that the order in which vertices are used to construct a quadrilateral from strip data is different from that used with independent data.
GL_POLYGON
Draws a single, convex polygon. Vertices 1 through N define this polygon.



Window Coordinate System

glutInitWindowPosition, glutInitWindowSize

  glutInitWindowPosition and   glutInitWindowSize set the initial window position and size respectively.
Usage
void glutInitWindowSize(int width, int height);
void glutInitWindowPosition(int x, int y);
width
Width in pixels.
height
Height in pixels.
x
Window X location in pixels.
y
Window Y location in pixels.
Description
Windows created by glutCreateWindow will be requested to be created with the current initial window position and size.
The initial value of the initial window position GLUT state is -1 and -1. If either the X or Y component to the initial window position is negative, the actual window position is left to the window system to determine. The initial value of the initial window size GLUT state is 300 by 300. The initial window size components must be greater than zero.
The intent of the initial window position and size values is to provide a suggestion to the window system for a window's initial size and position. The window system is not obligated to use this information. Therefore, GLUT programs should not assume the window was created at the specified size or position. A GLUT program should use the window's reshape callback to determine the true size of the window.


World Coordinate System

gluOrtho2D

NAME

gluOrtho2D - define a 2-D orthographic projection matrix

C SPECIFICATION

void gluOrtho2D( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top )

PARAMETERS

left, right

Specify the coordinates for the left and right vertical clipping planes.
bottom, top

Specify the coordinates for the bottom and top horizontal clipping planes.

DESCRIPTION

gluOrtho2D sets up a two-dimensional orthographic viewing region. This is equivalent to calling glOrtho with near = -1 and far = 1 .
2.