الأحد، 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();

}