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

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
}

ليست هناك تعليقات:

إرسال تعليق