[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

OpenGL / C -- motion function

Posted on 2008-09-30
5
Medium Priority
?
1,392 Views
Last Modified: 2013-12-06
I'd like to modify the program below so that only my left mouse button can start/stop the spinning of the  cube.   At this time, both LEFT and RIGHT mouse button can start/stop the spinning of the cube.

I tried the code below but the RIGHT mouse button still has functionality.   How do I need to modify the code that only the LEFT button can activate the function?

EEH

My attempt to disable the right mouse button:
====================================
void mouse(int button, int state, int x, int y)
{
         if (button== GLUT_LEFT_BUTTON && state == GLUT_DOWN)
            {
            switch(state)
            {
            case GLUT_DOWN:
                  startMotion(x, y);
                  break;
            case GLUT_UP:
                  stopMotion(x, y);
                  break;
            }
      }

    if (button == GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
    {
            // do nothing
    }
}
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/glu.h>
 
#define bool int
#define false 0
#define true 1
#define M_PI 3.14159
 
/*
** Global settings.
*/
 
float 	nnear = 3.0;    	/* near clipping plane in eye coords */
float 	nfar = 7.0;    	/* far clipping plane in eye coords */
float 	viewxform_z = -5.0;
 
int 	winWidth, winHeight;
 
float 	angle = 0.0, axis[3];
bool 	trackballMove = false;
bool 	redrawContinue = false;
 
GLfloat objectXform[4][4] = {
	{1.0, 0.0, 0.0, 0.0},
	{0.0, 1.0, 0.0, 0.0},
	{0.0, 0.0, 1.0, 0.0},
	{0.0, 0.0, 0.0, 1.0}
};
 
void display(void);
void spinCube(void);
 
/*----------------------------------------------------------------------*/
/*
** Set state according to user interaction.
*/
void userSettings(void) 
{
	glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-2.0, 2.0, -2.0, 2.0, nnear, nfar);
	viewxform_z = -5.0;
	glMatrixMode(GL_MODELVIEW);
}
 
/*----------------------------------------------------------------------*/
/*
** Draw the cube.
*/
GLfloat vertices[][3] = {
	{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, 
	{-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}
};
 
GLfloat colors[][3] = {
	{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, 
	{0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}
};
 
 
void polygon(int a, int b, int c , int d, int face)
{
	glBegin(GL_POLYGON);
		glColor3fv(colors[a]);
		glVertex3fv(vertices[a]);
		glColor3fv(colors[b]);
		glVertex3fv(vertices[b]);
		glColor3fv(colors[c]);
		glVertex3fv(vertices[c]);
		glColor3fv(colors[d]);
		glVertex3fv(vertices[d]);
	glEnd();
}
 
void colorcube(void)
{
	/* map vertices to faces */
	polygon(1,0,3,2,0);
	polygon(3,7,6,2,1);
	polygon(7,3,0,4,2);
	polygon(2,6,5,1,3);
	polygon(4,5,6,7,4);
	polygon(5,4,0,1,5);
}
 
 
float lastPos[3] = {0.0F, 0.0F, 0.0F};
int startX, startY;
 
void trackball_ptov(int x, int y, int width, int height, float v[3])
{
	float d, a;
 
	/* project x, y onto a hemi-sphere centered within width, height */
	v[0] = (2.0F * x - width) / width;
	v[1] = (height - 2.0F * y) / height;
	d = (float) (v[0] * v[0] + v[1] * v[1]);
	//v[2] = (float) cos((M_PI/2.0F) * ((d < 1.0F) ? d : 1.0F));
	v[2] = (float) sqrt(1 - ((d < 1.0F) ? d : 1.0F));
	a = 1.0F / (float) sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
	v[0] *= a;
	v[1] *= a;
	v[2] *= a;
}
 
 
void mouseMotion(int x, int y)
{
	float curPos[3], dx, dy, dz;
 
	trackball_ptov(x, y, winWidth, winHeight, curPos);
 
	dx = curPos[0] - lastPos[0];
	dy = curPos[1] - lastPos[1];
	dz = curPos[2] - lastPos[2];
 
	if (dx || dy || dz) {
		angle = 90.0F * sqrt(dx * dx + dy * dy + dz * dz) / 3;
 
		axis[0] = lastPos[1] * curPos[2] - lastPos[2] * curPos[1];
		axis[1] = lastPos[2] * curPos[0] - lastPos[0] * curPos[2];
		axis[2] = lastPos[0] * curPos[1] - lastPos[1] * curPos[0];
 
		lastPos[0] = curPos[0];
		lastPos[1] = curPos[1];
		lastPos[2] = curPos[2];
	}
	glutPostRedisplay();
}
 
void startMotion(int x, int y)
{
	redrawContinue = false;
	startX = x; startY = y;
	trackball_ptov(x, y, winWidth, winHeight, lastPos);
	trackballMove = true;
}
 
void stopMotion(int x, int y)
{
	if (startX != x || startY != y) 
		redrawContinue = true;
	else 
	{
		//angle = 0.0F;
		redrawContinue = false;
		trackballMove = false;
	}
}
 
 
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 
	/* view transform */
	glLoadIdentity();
	glTranslatef(0.0, 0.0, viewxform_z);
 
	if (trackballMove) {
		glPushMatrix();
		glLoadIdentity();
		glRotatef(angle, axis[0], axis[1], axis[2]);
		glMultMatrixf((GLfloat *) objectXform);
		glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *) objectXform);
		glPopMatrix();
	}
 
	glMultMatrixf((GLfloat *) objectXform);
	colorcube();
 
	glutSwapBuffers();
	glFlush();
}
 
 
void mouseButton(int button, int state, int x, int y)
{
	switch(state) 
	{
	case GLUT_DOWN:
		startMotion(x, y);
		break;
	case GLUT_UP:
		stopMotion(x, y);
		break;
	} 
}
 
 
void myReshape(int w, int h)
{
	glViewport(0, 0, w, h);
	winWidth = w;
	winHeight = h;
}
 
void spinCube()
{
	if (redrawContinue) glutPostRedisplay();
}
 
 
void main(int argc, char **argv)
{
	glutInit(&argc, argv);
 
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(500, 500);
	glutCreateWindow("colorcube");
	glutReshapeFunc(myReshape);
	glutDisplayFunc(display);
	glutIdleFunc(spinCube);
	glutMouseFunc(mouseButton);
	glutMotionFunc(mouseMotion);
	userSettings();
	glutMainLoop();
}

Open in new window

0
Comment
Question by:ExpExchHelp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 11

Expert Comment

by:jgordos
ID: 22617200
you'll need to modify the routine

void mouseButton(int button, int state, int x, int y)

too...


-john
0
 

Author Comment

by:ExpExchHelp
ID: 22618181
-john,

that's where I've put the additional IF statement.    It doesn't work for me.

What am I missing?   Did the code work for you so that only the LEFT mouse button works?   If yes, could you pls provide the full code?

EEH
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 22620416
add this as the first line of the function mouseButton,
if( !(button == GLUT_LEFT_BUTTON) ) return;

that way you cannot stop it with any other button than the left button, however you can move it with another button
0
 
LVL 20

Accepted Solution

by:
ikework earned 2000 total points
ID: 22622342
hey ExpExchHelp,

actually with the code you showed in your mouse-function only the left-mouse-button is catched. however the button-up-event for the left mouse button is never invoked. try this:

void mouse(int button, int state, int x, int y)
{
         if (GLUT_LEFT_BUTTON == button)
            {
            switch(state)
            {
            case GLUT_DOWN:
                  startMotion(x, y);
                  break;
            case GLUT_UP:
                  stopMotion(x, y);
                  break;
            }
      }

    if (button == GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
    {
            // do nothing
    }
}


i think your problem is the mouseMotion-function, since you dont check for the pressed buttons there. you should save the pressed button in the mouse-function in a global variable and then check that variable in mouseMotion, something like this:

int g_mouseButton = -1;

void mouse(int button, int state, int x, int y)
{
    if( GLUT_DOWN == state )
    {
        // save the pressed button in global variable
        g_mouseButton = button;        
    }
    else
    {
        // state is button up, so unset the global variable, -1 means no button pressed
        g_mouseButton = -1;
    }
    // ...
}

void mouseMotion(int x, int y)
{
    // only do the mouse-move stuff if left button is pressed
    if(GLUT_LEFT_BUTTON == g_mouseButton)
    {
        // do the mouse-move stuff here
    }
}


hope it helps,
ike
0
 

Author Comment

by:ExpExchHelp
ID: 22624124
Mark_FreeSoftware:

"that way you cannot stop it with any other button than the left button, however you can move it with another button:
.. thanks -- I need to completely disable the right mouse button.   So, only the left mouse button can start and stop it.

**********************************

ikework:

thanks -- I'll give it a try tonight.  

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question