Original Post
i''m developing an openGL program, where i have some keyboard inputs, whenever i press a key something strange happens..., see for your self.
press "w" or "s"....
#include <gl\glut.h>
#include <math.h>
int w,h;
static float upown=0.0,angulo=0.0;
static double spin=0;
//material
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 100.0 };
//luz
GLfloat light_position[] = { -1.0, 0.0, 1.0, 0.0 };
//GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_ambient[] = { 1.0 ,1.0 ,1.0 ,1.0 };
// o cubo não fica bem um cubo, deve haver um problema com as perspectivas...
// como é que faço uma " directional light " ( por ex. o sol )
void init(void){
glClearColor (0.0,0.0,0.0,0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_CULL_FACE);
//material
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
//luz 0
//glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); //quantidade de luz difusa
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); //quantidade de luz ambiente
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
//glEnable(GL_NORMALIZE);?
}
void display(void){
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable (GL_LIGHTING);
//chão
glBegin (GL_QUADS);
glColor3f(0.4,0.2,0.5);
glVertex3f(-10,0.0,10.0);
glVertex3f(10,0.0,10.0);
glVertex3f(10,0.0,-10.0);
glVertex3f(-10,0.0,-10.0);
glEnd();
//luz
glPushMatrix();
glRotated(spin, 0.0, 1.0, 0.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//glDisable (GL_LIGHTING);
glPopMatrix();
glEnable(GL_LIGHTING);
//objecto
glPushMatrix();
glTranslatef(0.0,(GLfloat) upown,0.0);
glRotatef((GLfloat) angulo,0.0, 1.0, 0.0);
glutSolidCube (2.0);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void reshape (int w,int h){
glViewport(0,0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60.0 ,(GLfloat) w/ (GLfloat)h, 1.0, 20.0);//
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0 ,3.0 ,10.0 ,0.0 ,0.0 ,0.0 ,0.0 ,1.0 ,0.0);
}
void keyboard (unsigned char key,int x, int y){
switch (key) {
case ''w'':
upown = upown + 0.5;
glutPostRedisplay();
break;
case ''s'':
upown = upown - 0.5;
glutPostRedisplay();
break;
case ''+'':
spin = spin + 2;
glutPostRedisplay();
break;
case ''-'':
spin = spin - 2;
glutPostRedisplay();
break;
}
}
void mouse(int button,int state,int x, int y){
switch (button){
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN){
angulo = angulo - 5;
glutPostRedisplay();
}
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN){
angulo = angulo + 5;
glutPostRedisplay();
}
break;
}
}
int main(int argc, char** argv){
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (800,640);
glutInitWindowPosition (100,100);
glutCreateWindow (argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}