PDA

View Full Version : Rotate distinct polygons using mouse or trackpad



roshanp96
08-27-2016, 01:29 AM
Hi, i am new to opengl, i have done a basic program for making two different polygons and now i would like to Giving the control of rotation to mouse ie if we click mouse button on the polygon and drag the mouse the model should rotate. And this should happen for each model separately.please help me out, i am attaching what i have done so far,

#include <GLUT/glut.h>
#include <cstdlib> /* for exit */

using namespace std;
static bool spinning = true;
static const int FPS = 60;
static GLfloat currentAngleOfRotation = 0.0;


static void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(0, 0, 0);
glColor3f(0, 1, 0);
glVertex3f(0.5, 0, 0);
glColor3f(0, 0, 1);
glVertex3f(0, 0.5, 0);
glEnd();
glShadeModel(GL_SMOOTH);
glBegin(GL_POLYGON);
glColor3f(1,0,0);
glVertex3f(-0.1, -0.1,0);
glColor3f(1,1,0);
glVertex3f(-0.6, -0.1, 0);
glColor3f(1,1,1);
glVertex3f(-0.6,-0.6,0);
glColor3f(0,0,0);
glVertex3f(-0.1, -0.6, 0);


glEnd();

glFlush();
}


static void init()
{
glClearColor(1.0, 0.9, 0.65, 0.0);
glColor3f(0.1,0.5, 0.1);
}

static void keyboard(unsigned char key, int x, int y)
{
switch(key) {
case 'q':
case 'Q':
case 27: // ESC key
exit(0);
break;
}
}
static void myMouse( int button, int state, int x, int y ){

int selected=0;

if (x<=102 && x>=95 && y<=452 && y>=446)
selected=1;

if((selected == 1) &&( button == GLUT_LEFT_BUTTON ))
{

glTranslatef(-12, 6, 0);
glRotatef(30,0,0,1.0);
glTranslatef(12, -6, 0);

}

}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0,0);
glutCreateWindow("two polygons");
glutDisplayFunc(display);
/*glutKeyboardFunc(keyboard);*/

init();
glutMainLoop();

return 0;
}