Solved

OpenGL / C -- motion function

Posted on 2008-09-30
5
1,364 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
scoresAverage challenge 8 131
gdb doesn't stop on breakpoint 2 139
What are programming codes and how do they work? 7 119
Why  my code (program) build with old compiler? 11 145
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

734 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