#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glut32.lib")
#include <ctime>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glut.h>
#include <gl\GLU.h>
#include <math.h>
/**
Sets the logical coordinate system we will use to specify
our drawings.
*/
//////////////////////////////////////////////////////////
int xPositionDownMallet = 0 , yPositionDownMallet = -70;
void processMouse( int x , int y)
{
xPositionDownMallet = x % 85 ;
yPositionDownMallet = -1 * y % 85;
glutPostRedisplay();
}
void SetTransformations() {
//set up the logical coordinate system of the window: [-100, 100] x [-100, 100]
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
}
/**
Handles the paint event. This event is triggered whenever
our displayed graphics are lost or out-of-date.
ALL rendering code should be written here.
*/
void OnDisplay() {
//set the background color to white
glClearColor( 0.6 , 1 , 1 , 0);
//fill the whole color buffer with the clear color
glClear(GL_COLOR_BUFFER_BIT);
SetTransformations();
//////////////////////////////////////////////////////////////
// playground
glColor3d(1 , 1 , 1);
glBegin(GL_QUADS);
glVertex3d(85 , 90 ,0);
glVertex3d(-85 , 90 ,0);
glVertex3d(-85 , -90 ,0);
glVertex3d(85 , -90 ,0);
glEnd();
glRectd(-85 , -90 , 85 , 90);
/////////////////////////////////////////////////////////
// outer frame of the playground
glColor3d(0 , 0 , 0);
glBegin(GL_LINE_LOOP);
glVertex3f( 0 , -90 , 0);
glVertex3f( 85 , -90 , 0);
glVertex3f( 85 , 90 , 0);
glVertex3f( 0 , 90 , 0);
glVertex3f(-85 , 90 , 0);
glVertex3f( -85 , -90 , 0);
glEnd();
///////////////////////////////////////////////////////
// line at the middle
glBegin(GL_LINES);
glColor3f(1, 0, 0);
glVertex3f(-85,0, 0);
glVertex3f(85, 0, 0);
glEnd( );
////////////////////////////////////////////////////////
// Goals
glColor3d( 0 , 0 , 0);
glRectd(-20 , 90 , 20 , 100);
glRectd(-20 , -90 , 20 , -100);
///////////////////////////////////////////////////////
// circle at the center of the playground
glColor3d( 0.2 , 0 , 0.6);
glBegin(GL_LINE_LOOP);
for( double i = 45; i <= 360 ; i += 1)
{
glVertex3f(20*sin(i) , 20* cos(i) , 0);
}
glEnd();
/////////////////////////////////////////////////////////////////
glBegin(GL_LINES);
glColor3f(1, 0, 0);
glVertex3f(-85,70, 0);
glVertex3f(85, 70, 0);
glEnd( );
//////////////////////////////////////////////////////////////////
// the first position of the mallets
glColor3d( 1 , 0 , 0.2);
glBegin(GL_LINE_LOOP);
for( int i = 45; i <= 360 ; i += 1)
{
glVertex3f(12*sin(double(i)) , 12* cos(double(i))+70 ,0);
}
glEnd();
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////`
glBegin(GL_LINES);
glColor3f(1, 0, 0);
glVertex3f(-85,-70, 0);
glVertex3f(85, -70, 0);
glEnd( );
//////////////////////////////////////////////////////////////////
// the second position of the mallets
glBegin(GL_LINE_LOOP);
for( int i = 45 ; i <= 360 ; i += 1)
{
glVertex3f(12*sin(double(i)) , 12* cos(double(i))-70 ,0);
}
glEnd();
////////////////////////////////////////////////////////////////////
// the two mallets
// the down mallet
glColor3d( 0 , 0.6 , 0.6);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(xPositionDownMallet , yPositionDownMallet , 0 );
for( int i = 0; i <= 360 ; i += 1)
{
glVertex3f(9*sin(double(i))+xPositionDownMallet , 9* cos(double(i))+yPositionDownMallet , 0);
}
glEnd();
glColor3d( 0.1 , 0 , 0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(xPositionDownMallet , yPositionDownMallet , 0);
for( int i = 0; i <= 360 ; i += 1)
{
glVertex3f(4*sin(double(i))+xPositionDownMallet , 4* cos(double(i))+yPositionDownMallet , 0);
}
glEnd();
///////////////////////////////////////////////
// the upper mallet
glColor3d( 0 , 0.6 , 0.6);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0 , 70 ,-1);
for( int i = 0; i <= 360 ; i += 1)
{
glVertex3f(9*sin(double(i)) , 9* cos(double(i))+70 , 0 );
}
glEnd();
glColor3d( 0.1 , 0 , 0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0 , 70 ,-1);
for( int i = 0; i <= 360 ; i += 1)
{
glVertex3f(4*sin(double(i)) , 4* cos(double(i))+70 , 0 );
}
glEnd();
////////////////////////////////////////////////////////////////////
// the puck
glColor3d( 0.5 , 0 , 0);
for( int i = 0; i <= 360; i++)
{
glRotatef(i, 0.0, 0.0, 1.0);
}
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0 , 0 ,-1);
for( int i = 0; i <= 360 ; i += 1)
{
glVertex3f(7*sin(double(i)) , 7* cos(double(i)) ,0);
}
glEnd();
//////////////////////////////////////////////////////////////////
//force previously issued OpenGL commands to begin
//execution
glFlush();
}
/**
Creates the main window, registers event handlers, and
initializes OpenGL stuff.
*/
void InitGraphics(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
//Create an 800x600 window with its top-left corner at pixel (100, 100)
glutInitWindowPosition(0, 0); //pass (-1, -1) for Window-Manager defaults
glutInitWindowSize(600, 800);
glutCreateWindow("OpenGL Lab");
//OnDisplay will handle the paint event
glutDisplayFunc(OnDisplay);
glutMotionFunc(processMouse);
glutPassiveMotionFunc(processMouse);
glutMainLoop();
}
int main(int argc, char* argv[])
{
//glutInit(&argc, argv); // Initialize graphics
InitGraphics(argc, argv);
return 0;
}