/*
* main.cpp
* CompFormApp
*
*/
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float mouseX = 0;
float mouseY = 0;
int windowW = 800;
int windowH = 600;
#define PI 3.14159265358979
void drawPortrait( void )
{
glColor3f(0,0,0);
glBegin(GL_TRIANGLE_STRIP);
glVertex2f(100,100);
glVertex2f(200,200);
glEnd();
glutPostRedisplay(); //with glutDisplayFunc();
glutSwapBuffers(); //swaps the buffers of the current window
}
void displayFunc ( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); //clears the previous image buffer(color and depth).
glColor3f( 1,1,1 ); //sets the color value of rectangle.
glRectf( mouseX-35, mouseY-25, mouseX+45, mouseY+25 ); //draws rectangle
//glColor3f( 0,1,1 );
//glRectf( windowW/2,windowH/2,windowW/2+10,windowH/2+10 ); //draws rectangle
// place your drawing code here
drawPortrait();
glutPostRedisplay(); //with glutDisplayFunc();
glutSwapBuffers(); //swaps the buffers of the current window
}
void reshapeFunc ( int w, int h )
{
windowW = w;
windowH = h;
glViewport( 0, 0, w, h ); //sets up the left corner x,y coordinates and width/length of viewport
glMatrixMode( GL_PROJECTION ); //sets the current matrix mode to GL_PROJECTION mode
glLoadIdentity(); //replace the current matrix with the identity matrix
gluOrtho2D( 0,w,0,h); //sets up a two-dimensional orthographic viewing region
glMatrixMode( GL_MODELVIEW ); //sets the current matrix mode to GL_MODELVIEW mode
glLoadIdentity(); //replace the current matrix with the identity matrix
}
void mouseDownFunc ( int button, int state, int x, int y )
{
mouseX = x; //when mouse is pressed, set mouseX to x
mouseY = windowH - y; //when mouse is pressed, set mouseY to windowH - y
//mouseY = 0;
}
void mouseMoveFunc ( int x, int y )
{
mouseX = x; //when mouse is in action in window area, set mouseX to x
mouseY = windowH - y; //when mouse is in action in window area, set mouseY to windowH - y
}
void mouseDragFunc ( int x, int y )
{
mouseX = x; //when mouse is dragged, set mouseX to x
mouseY = windowH - y;//when mouse is dragged, set mouseY to windowH - y
}
void keyboardFunc ( unsigned char key, int x, int y )
{
}
void arrowKeyFunc ( int a_keys, int x, int y )
{
}
void init ( GLvoid )
{
glShadeModel( GL_SMOOTH ); //sets glShadeModel to GL_SMOOTH
//glClearColor( 1.0, 1.0, 1.0, 1.0 );
glClearColor( 1.0, 1.0, 1.0, 1.0 ); // sets cleared background color to (1,1,1)
glEnable ( GL_COLOR_MATERIAL ); //enables one or more material parameters to track the current color
glEnable( GL_BLEND ); //blends the incoming RGBA color values with the values in the color buffers
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //specifies the blending mode
}
int main ( int argc, char** argv )
{
glutInit( &argc, argv ); //initialize the GLUT library
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE ); //sets the initial display mode to RGBA mode and double buffered window
glutInitWindowSize( windowW, windowH ); //sets window size to windowW * windowH
glutCreateWindow( "CompFormApp" ); //creates top-level window named "CompFormApp"
glutDisplayFunc( displayFunc ); //sets the display callback for the current window (with glutPostRedisplay();)
glutReshapeFunc( reshapeFunc ); //sets the reshape callback for the current window.
glutMouseFunc( mouseDownFunc ); //When a user presses and releases mouse buttons in the window, sets the mouse callback
glutMotionFunc( mouseDragFunc ); //when the mouse moves within the window while one or more mouse buttons are pressed
glutPassiveMotionFunc( mouseMoveFunc ); //When the mouse moves within the window while no mouse buttons are pressed
glutKeyboardFunc( keyboardFunc ); // When a user types into the window
glutSpecialFunc( arrowKeyFunc ); //The special keyboard callback is triggered when keyboard function or directional keys are pressed.
init();
glutMainLoop( ); // enters the GLUT event processing loop.
return 0;
}