Skip to content

Instantly share code, notes, and snippets.

@wadhwasahil
Created August 21, 2015 05:00
Show Gist options
  • Save wadhwasahil/3be368d3e10a77878a12 to your computer and use it in GitHub Desktop.
Save wadhwasahil/3be368d3e10a77878a12 to your computer and use it in GitHub Desktop.
#define GLUT_DISABLE_ATEXIT_HACK
#include <windows.h>
#include<iostream>
#include <iostream>
#include <gl\glut.h>
using namespace std;
// Initial Position of the Ball
float x_position = 0.0;
float y_position = 0.0;
// Radius of ball
float radius = 0.2;
// Speed with which the ball tranlates
float speed = 0.05;
// Limits of line
float line_begin = -1.25 ;
float line_end = 1.25 ;
void resize_handler(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, w/h, 1, 200);
}
void key_handler( int key, int x, int y )
{
if (key == GLUT_KEY_LEFT)
x_position -= ( x_position > line_begin ? speed : 0 );
else if (key == GLUT_KEY_RIGHT)
x_position += ( x_position < line_end ? speed : 0 );
}
void drawBall(float r, float g, float b, float xpos, float ypos, float rad)
{
glColor3f(r, g, b);
glPushMatrix();
glTranslatef(xpos, ypos, -5.0);
glutSolidSphere(rad, 100, 100);
glPopMatrix();
}
void drawLine( float x1, float y1, float x2, float y2)
{
glLineWidth(1.0);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x1, y1, -5.0);
glVertex3f(x2, y2, -5.0);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
drawBall(1.0,1.0,0.0, x_position, y_position, radius);
glutSwapBuffers();
}
void motionPassive(int x, int y)
{
x_position=(double)x/600.0,y_position=-1.0*(double)y/600.0;
//glutSetCursor(GLUT_CURSOR_FULL_CROSSHAIR);
cout << "Mouse moved at "
<< "(" << x_position << "," << y_position << ")" << endl;
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutCreateWindow("Collision Window");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutIdleFunc(display);
// Resize Handler
glutReshapeFunc(resize_handler);
// Key Handler
glutSpecialFunc(key_handler);
POINT mpos;
glutPassiveMotionFunc(motionPassive);
glutMainLoop();
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment