PDA

View Full Version : How to control cube rotation using Keyboard keys

Sindhu
02-08-2010, 03:10 AM
Hi,

How to control cube rotation using Keyboard keys(Left,Right,Up, Bottom arrow keys) ?

burrows111
02-11-2010, 04:48 AM
Hi Sindhu - If you using windows, it is a good idea to start looking how to use a switch statement to check what key is pressed.

i.e

LRESULT CALLBACK MainWndProc( HWND hwnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
{
switch( wMsg )
{
case WM_KEYDOWN:
switch( LOWORD( wParam ) )
{
case VK_LEFT: // left key press
// do this rotation
break;
case VK_RIGHT: // right key press
//do this rotation
break;

case VK_UP: // up key press
// do this rotation
break;

case VK_DOWN: // down key press
//do this rotation
break;
}

MaxH
02-11-2010, 01:04 PM
Here's a simple OpenGL program that rotates objects in a scene using the arrow keys. As burrows111 recommends, I use the 'switch' statement to handle my keyboard inputs.

//************************************************** ****************************
//************************ Rotate Box With Arrow Keys **********************
//************************ MaxH - Feb. 11, 2010 **********************

#include <glut.h>
#include <stdlib.h>
#include <math.h>

float XUP[3] = {1,0,0}, XUN[3] = {-1, 0, 0},
YUP[3] = {0,1,0}, YUN[3] = { 0,-1, 0},
ZUP[3] = {0,0,1}, ZUN[3] = { 0, 0,-1},
ORG[3] = {0,0,0};

GLfloat viewangle = 0, tippangle = 0, traj[120][3];

GLfloat d[3] = {0.1, 0.1, 0.1};

GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

// Use arrow keys to rotate entire scene !!!

void Special_Keys (int key, int x, int y)
{
switch (key) {

case GLUT_KEY_LEFT : viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_UP : tippangle -= 5; break;
case GLUT_KEY_DOWN : tippangle += 5; break;

default: printf (" Special key %c == %d\n", key, key);
}

glutPostRedisplay();
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

void Keyboard (unsigned char key, int x, int y)
{
switch (key) {

case 'j' : d[0] += 0.1; break;
case 'k' : d[1] += 0.1; break;
case 'l' : d[2] += 0.1; break;

case 'x' : xAngle += 5; break;
case 'y' : yAngle += 5; break;
case 'z' : zAngle += 5; break;

default: printf (" Keyboard %c == %d\n", key, key);
}

glutPostRedisplay();
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

{
glColor3f (1.0, 1.0, 1.0);

glBegin (GL_LINES);
glVertex3fv (ORG); glVertex3fv (XUP);
glVertex3fv (ORG); glVertex3fv (YUP);
glVertex3fv (ORG); glVertex3fv (ZUP);
glEnd ();

glRasterPos3f (1.1, 0.0, 0.0);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'X');

glRasterPos3f (0.0, 1.1, 0.0);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'Y');

glRasterPos3f (0.0, 0.0, 1.1);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'Z');
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

void Draw_Box (void)
{

glColor3f ( 0.0, 0.7, 0.1); // Front - green
glVertex3f (-1.0, 1.0, 1.0);
glVertex3f ( 1.0, 1.0, 1.0);
glVertex3f ( 1.0, -1.0, 1.0);
glVertex3f (-1.0, -1.0, 1.0);

glColor3f ( 0.9, 1.0, 0.0); // Back - yellow
glVertex3f (-1.0, 1.0, -1.0);
glVertex3f ( 1.0, 1.0, -1.0);
glVertex3f ( 1.0, -1.0, -1.0);
glVertex3f (-1.0, -1.0, -1.0);

glColor3f ( 0.2, 0.2, 1.0); // Top - blue
glVertex3f (-1.0, 1.0, 1.0);
glVertex3f ( 1.0, 1.0, 1.0);
glVertex3f ( 1.0, 1.0, -1.0);
glVertex3f (-1.0, 1.0, -1.0);

glColor3f ( 0.7, 0.0, 0.1); // Bottom - red
glVertex3f (-1.0, -1.0, 1.0);
glVertex3f ( 1.0, -1.0, 1.0);
glVertex3f ( 1.0, -1.0, -1.0);
glVertex3f (-1.0, -1.0, -1.0);

glEnd();
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

void redraw (void)
{
int v;

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_DEPTH_TEST);

glTranslatef (0, 0, -3);
glRotatef (tippangle, 1,0,0); // Up and down arrow keys 'tip' view.
glRotatef (viewangle, 0,1,0); // Right/left arrow keys 'turn' view.

glDisable (GL_LIGHTING);

glPushMatrix ();
glTranslatef (d[0], d[1], d[2]); // Move box down X axis.
glScalef (0.2, 0.2, 0.2);
glRotatef (zAngle, 0,0,1);
glRotatef (yAngle, 0,1,0);
glRotatef (xAngle, 1,0,0);
Draw_Box ();
glPopMatrix ();

glutSwapBuffers();
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

int main (int argc, char **argv)
{
glutInit (&amp;argc, argv);
glutInitWindowSize (900, 600);
glutInitWindowPosition (300, 300);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE);

glutCreateWindow ("Orbital Font Demo");
glutDisplayFunc ( redraw );
glutKeyboardFunc ( Keyboard );
glutSpecialFunc (Special_Keys);

glClearColor (0.1, 0.0, 0.1, 1.0);

glMatrixMode (GL_PROJECTION);
gluPerspective (60, 1.5, 1, 10);
glMatrixMode (GL_MODELVIEW);
glutMainLoop ();

return 1;
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

Sindhu
02-12-2010, 02:06 AM
Hi,

Thank You So much for your replies.

i got the concept. Very very Thanks.

Sindhu
02-12-2010, 02:38 AM
Hi,

I am using Linux for execution.

How to place different images on each side of the cube in openGL(GLUT) ?

I need to place 6 images on 6 sides of the cube.

02-15-2010, 10:37 AM
Texture Mapping.

Just put all pictures into one image, load that, bind the texture and use "glTexture2f" for texture mapping ;)

mikeynovemberoscar
02-15-2010, 10:55 AM
There is an extremely simple library called SOIL. All you have to do is write:

texture = SOIL_load_OGL_texture ("MyImage.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT );

in main(void)

and

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);