PDA

View Full Version : How to use left and right keys to move a teapot?



Aditi
02-17-2009, 07:54 PM
Hello..
In this program, I have already created a flat floor, placed 3 teapots on it and used "f" key to toggle the fog effect. Now, i need to move the front teapot up and down using arrow keys.If i keep pressing the left key then the teapot should keep moving to the left and with right key press, it should start moving to the right.

I am unable to get this done.Here is my code.I know that for left and right keys, i need to use glutSpecialFunc()..Please let me know how can i do this?..Thanks

#include <stdafx.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>

#define checkImageWidth 128
#define checkImageHeight 128

static GLint fogMode;
static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
static int flag = GL_TRUE;
float FogCol[3]={0.8f,0.8f,0.8f};

void drawCheckImage(void)
{
int i, j, c;

for (i = 0; i < checkImageHeight; i++) {
for (j = 0; j < checkImageWidth; j++) {
c = ((((i&0x8)==0)^((j&0x8))==0))*255;
checkImage[i][j][0] = (GLubyte) c;
checkImage[i][j][1] = (GLubyte) c;
checkImage[i][j][2] = (GLubyte) c;
checkImage[i][j][3] = (GLubyte) 255;
}
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight,
0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);

glFrontFace (GL_CW);
}

static void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);

drawCheckImage();

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
glEnable(GL_COLOR_MATERIAL);

float black[4]={0,0,0,0};
glMaterialfv(GL_FRONT,GL_AMBIENT,black); //ambient and specular properties set to black
glMaterialfv(GL_FRONT,GL_SPECULAR,black);
}

static void renderTeapot (GLfloat x, GLfloat y, GLfloat z)
{
glPushMatrix();
glTranslatef (x, y, z);
glutSolidTeapot(1.8);
glPopMatrix();
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDisable(GL_TEXTURE_2D);
glColor3f(0.4,0.0,0.8);
renderTeapot (-5, 0.8, 5.0);

glColor3f(0.1,0.5,0.3);
renderTeapot (0, 0.8, -5.0);
glColor3f(0.6,0.0,0.0);
renderTeapot (5, 0.8, -15.0);

glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

glColor3f(0.1, 0.1, 0.1);
glBegin(GL_QUADS);

glNormal3f(0, 1, 0);
glTexCoord2f(0, 0);
glVertex3f(-17.0, 0, 22.0);
glTexCoord2f(0, 1);
glVertex3f(-17.0, 0, -22.0);
glTexCoord2f(1, 1);
glVertex3f(17.0, 0, -22.0);
glTexCoord2f(1, 0);
glVertex3f(17.0,0, 22.0);

glEnd();

if (flag) {
glEnable(GL_FOG);
{
GLfloat fogColor[4] = {0.8, 0.8, 0.8, 1.0};
fogMode = GL_EXP;
glFogi (GL_FOG_MODE, fogMode);
glFogfv (GL_FOG_COLOR, fogColor);
glFogf (GL_FOG_DENSITY, 0.03);
glHint (GL_FOG_HINT, GL_DONT_CARE);
glClearColor (0.7, 0.7, 0.7, 1.0);
}
}
else
{
glDisable(GL_FOG);
glClearColor(0.0, 0.0, 0.0, 0.0);
}
glutSwapBuffers();
}


void reshape(int w, int h)
{
glViewport(0, 0,(GLsizei) w,(GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w/(float) h, 1.0, 200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -40.0f);
glRotatef(30, 1, 0, 0);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'f':
case 'F':
flag = !flag;
glutPostRedisplay();
break;

case 27:
exit(0);
break;
default:
break;
}
}


int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(750, 750);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}

todayman
02-17-2009, 08:30 PM
I would create a two-element array to track the translation of a teapot (perhaps GLfloat translation[2]) and initialize both elements to 0. In your glutSpecialFunc callback, modify the first element when the left/right buttons are called, and the second element when the up/down buttons are pressed, then glutPostRedisplay(). In the display function, I would add the translation to the arguments to renderTeapot. Instead of just passing the coordinates, I would call something like renderTeapot(0.4+translation[0], 0.8+translation[1], 5.0)

Aditi
02-17-2009, 09:16 PM
hey..
This is a good idea..Are 2 elements in an array sufficient if i want one of the teapot to move up, down, left and right?..Or do i need to create GLfloat translation[4] array to keep track of individual movement?..Thanks again

Aditi
02-18-2009, 12:10 PM
Hey..
Thanks for your reply..
I was able to get the desired movement :)