PDA

View Full Version : Help to rotate a cube pushing a key



tirengarfio
02-07-2007, 09:45 AM
Hi,

this code below should rotate a cube when i push the key 'a', but it doesnt work.

Anyone can help me?


#include <stdafx.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <gl/glut.h>
#include <math.h>


enum change_size { GROW, SHRINK };
static GLfloat fovy, nearClip, farClip;
static GLfloat Red = 0.0, Green = 0.0, Blue = 0.0;
GLdouble eyex=0.0f, eyey=0.0f, eyez=0.0f;
GLdouble centerx=0.0, centery=0.0, centerz=0.0;
GLdouble upx=0.0, upy=1.0, upz=0.0;


GLdouble inner=0.25, outer = 0.75;
static int GrowState = GROW;
static int xx=0, yy=0;
GLdouble transx=0.0, transy=0.0, transz=0.0;
GLdouble rotax=0.0, rotay=0.0, rotaz=0.0;

void resetView( GLvoid ) {
fovy = 45.0;
nearClip = 1.0;
farClip = 100.0;
fovy = 60.0;
xx = 0; yy = 0;
Red = 0.0;
Green = 0.0;
Blue = 0.0;
}

void setEyePoint(void)
{
centerx=0.0; centery=0.0; centerz = 0.0;
inner = 0.25; outer = 0.75;
resetView();
glutPostRedisplay();
return;
}

void init (void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_SMOOTH);
resetView();
}

void drawTorus()
{
static int rotation = 0;
glPushMatrix();
//glRotatef(90.0, 1.0, 0.0, 0.0);
//glRotatef(rotation++, 1.0, 0.0, 0.0);
glutWireTorus( inner, outer, 16, 31 );
glPopMatrix();
}

void display(void) {
glClear(GL_COLOR_BUFFER_BIT );
glColor3f(Red, Green, Blue);
glLoadIdentity();

gluLookAt(eyex, eyey, eyez, // eyex,eyey,eyez, //Es la posiciĆ³n de la camara: 3 coordenadas (X,Y,Z)
eyex, eyey, 0.0, // Es el lugar hacia el que mira la camara
upx, upy, upz); // si estan a 0 los tres, estas tu mirada forma una perpendicular con el frontal del frustum.


//drawTorus();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);


glBegin(GL_QUADS); // Draw The Cube Using quads
glColor3f(0.0f,1.0f,0.0f); // Color Blue
glVertex3f( 10.0f, 10.0f,-10.0f); // Top Right Of The Quad (Top)
glVertex3f(-10.0f, 10.0f,-10.0f); // Top Left Of The Quad (Top)
glVertex3f(-10.0f, 10.0f, 10.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 10.0f, 10.0f, 10.0f); // Bottom Right Of The Quad (Top)
glColor3f(1.0f,0.5f,0.0f); // Color Orange
glVertex3f( 10.0f,-10.0f, 10.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-10.0f,-10.0f, 10.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-10.0f,-10.0f,-10.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 10.0f,-10.0f,-10.0f); // Bottom Right Of The Quad (Bottom)
glColor3f(1.0f,0.0f,0.0f); // Color Red
glVertex3f( 10.0f, 10.0f, 10.0f); // Top Right Of The Quad (Front)
glVertex3f(-10.0f, 10.0f, 10.0f); // Top Left Of The Quad (Front)
glVertex3f(-10.0f,-10.0f, 10.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 10.0f,-10.0f, 10.0f); // Bottom Right Of The Quad (Front)
glColor3f(1.0f,1.0f,0.0f); // Color Yellow
glVertex3f( 10.0f,-10.0f,-10.0f); // Top Right Of The Quad (Back)
glVertex3f(-10.0f,-10.0f,-10.0f); // Top Left Of The Quad (Back)
glVertex3f(-10.0f, 10.0f,-10.0f); // Bottom Left Of The Quad (Back)
glVertex3f( 10.0f, 10.0f,-10.0f); // Bottom Right Of The Quad (Back)
glColor3f(0.0f,0.0f,1.0f); // Color Blue
glVertex3f(-10.0f, 10.0f, 10.0f); // Top Right Of The Quad (Left)
glVertex3f(-10.0f, 10.0f,-10.0f); // Top Left Of The Quad (Left)
glVertex3f(-10.0f,-10.0f,-10.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-10.0f,-10.0f, 10.0f); // Bottom Right Of The Quad (Left)
glColor3f(1.0f,0.0f,1.0f); // Color Violet
glVertex3f( 10.0f, 10.0f,-10.0f); // Top Right Of The Quad (Right)
glVertex3f( 10.0f, 10.0f, 10.0f); // Top Left Of The Quad (Right)
glVertex3f( 10.0f,-10.0f, 10.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 10.0f,-10.0f,-10.0f); // Bottom Right Of The Quad (Right)
glEnd();


glFlush();
glutSwapBuffers();
}
void spinObject() {
glutPostRedisplay();
}

void reshape(int w, int h) {
GLdouble aspect;
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
aspect = (GLdouble) w / (GLdouble) h ;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective( fovy, aspect, nearClip, farClip );

//glOrtho( -40.0, 40.0, -40.0, 40.0, nearClip, farClip );

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glRotatef(rotax, eyex, eyey, eyez);

}

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



case GLUT_KEY_LEFT: eyex--; break;
case GLUT_KEY_RIGHT: eyex++; break;

case GLUT_KEY_DOWN: eyez--; break;
case GLUT_KEY_UP: eyez++; break;



//case 'c': centerx++; break;

case GLUT_KEY_PAGE_UP: centerz++; break;
case GLUT_KEY_PAGE_DOWN:centerz--; break;
case GLUT_KEY_HOME: setEyePoint(); break;
case GLUT_KEY_F1:


if (GrowState == GROW ) {
inner += 0.01; outer += 0.01;
}

else {
inner -= 0.01; outer -= 0.01;
}

break;

case GLUT_KEY_END: exit(0); break;

default: break;
}

glutPostRedisplay();

}


void mousefunc( int button, int state, int x, int y)
{
if(button == GLUT_RIGHT_BUTTON &amp;&amp; state == GLUT_UP)
glutIdleFunc(spinObject);

if(button == GLUT_RIGHT_BUTTON &amp;&amp; state == GLUT_DOWN)
glutIdleFunc(NULL);

if(button == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN)

{
if (GrowState == GROW)
{
inner += (xx + x )/200;
outer += (yy + y )/200;
xx += x/200; yy += y/200;
}
else
{
inner -= (xx + x )/200;
outer -= (yy + y )/200;
xx += x/200; yy += y/200;
}

}
return;
}

void myKeyboard( unsigned char key, int x, int y)
{
if (glutGetModifiers() == GLUT_ACTIVE_SHIFT)
key = key + 32;

switch( key )
{

//case 'x':eyex=5.0; eyey=0.0; eyez = 0.0;break;
case 'o': eyey--; break;
case 'l': eyey++; break;

case 'a': upz=upz+0.2; break;

case 'r': rotax++; break;


case 'y': eyex=0.1; eyey=5.0; eyez = 0.0;break;
case 'z': eyex=0.0; eyey=0.0; eyez = 5.0;break;
case 's': GrowState = SHRINK;break;
case 'g': GrowState = GROW;break;
default:break;

}
glutPostRedisplay();
}

void passiveMotion( int x, int y)
{
GLfloat X, Y;
X = x; Y = y;
Blue = X/Y;
glutPostRedisplay();
}


void motionfunc( int x, int y) {
GLfloat X, Y;
X = x; Y = y;
Red = X/500; Green = Y/500; Blue = 0.0;
glutPostRedisplay();
}

int main(int argc, char** argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition( 100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutSpecialFunc( specialkeys );
glutKeyboardFunc( myKeyboard );
glutReshapeFunc(reshape);
glutMouseFunc( mousefunc );
glutMotionFunc( motionfunc );
glutPassiveMotionFunc( passiveMotion );
glutIdleFunc( spinObject );
glutMainLoop();
return 0;
}

Debugger
02-08-2007, 04:33 AM
Hi
I think you should first learn the basic knowledge of OpenGL and also learn someting about how to programming. it seems your idear is not clear enough.