[Tutorial] II.5 - glutSpecialFunc()
Página 1 de 1.
[Tutorial] II.5 - glutSpecialFunc()
II.5 Recoge la tecla pulsada, sea la que sea: glutSpecialFunc()
Novedades:
Hay teclas que no detecta el glutKeyboardFunc(), como las flechas, las teclas F1, F2... y otras.
El funcionamiento de glutSpecialFunc(ControlFlechas); es casi idéntico al de glutKeyobardFunc(). En vez de obtener un char, obtienes un int. Puedes ver la lista de correspondencias de valor para int key en glutspecialFunc() en el Sumario de los tutoriales II.
En este caso:
Novedades:
- glutSpecialFunc()
Hay teclas que no detecta el glutKeyboardFunc(), como las flechas, las teclas F1, F2... y otras.
- Código:
#include <GL/glut.h>
float xpos, ypos;
float rojo, verde, azul, dis;
void IniciarGLUT() {
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Practica II,5 de OpenGL");
}
void PintarEscena() {
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(0.0f,0.0f,0.4f);
glRectf(-10.0f,-10.0f,10.0f,10.0f);
glColor3f(0.0f,0.0f,0.0f);
glRectf(xpos-0.5f+dis, ypos-0.5f-dis, xpos+0.5f+dis, ypos+0.5f-dis);
glColor3f(rojo,verde,azul);
glRectf(xpos-0.5f, ypos-0.5f, xpos+0.5f, ypos+0.5f);
glutSwapBuffers();
}
void ReProyectar(int w, int h) {
GLfloat formato;
if(h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
formato = (GLfloat)w / (GLfloat)h;
if (w <= h) glOrtho (-10.0f, 10.0f, -10.0f / formato, 10.0f / formato, 1.0f, -1.0f);
else glOrtho (-10.0f * formato, 10.0f * formato, -10.0f, 10.0f, 1.0f, -1.0f);
}
void ControlTeclado(unsigned char key, int x, int y) {
switch (key)
{
case 114: // 114 en ASCII es: r
if (rojo==1.0f) rojo = 0.0f;
else rojo = 1.0f;
break;
case 118: // 118 en ASCII es: v
if (verde==1.0f) verde = 0.0f;
else verde = 1.0f;
break;
case 98: // 98 en ASCII es: b
if (azul==1.0f) azul = 0.0f;
else azul = 1.0f;
break;
}
glutPostRedisplay();
}
void ControlFlechas(int key, int x, int y) {
switch (key)
{
case GLUT_KEY_RIGHT:
xpos = xpos + 0.08f;
if (xpos > 10.0f-0.5f) xpos = 9.5f;
break;
case GLUT_KEY_LEFT:
xpos = xpos - 0.08f;
if (xpos < -10.0f+0.5f) xpos = -9.5f;
break;
case GLUT_KEY_UP:
ypos = ypos + 0.08f;
if (ypos > 10.0f-0.5f) ypos = 9.5f;
break;
case GLUT_KEY_DOWN:
ypos = ypos - 0.08f;
if (ypos < -10.0f+0.5f) ypos = -9.5f;
break;
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv); //Solo necesario en Linux
IniciarGLUT();
glutReshapeFunc(ReProyectar);
glutDisplayFunc(PintarEscena);
glutSpecialFunc(ControlFlechas);
glutKeyboardFunc(ControlTeclado);
glutMainLoop();
return 0;
}
El funcionamiento de glutSpecialFunc(ControlFlechas); es casi idéntico al de glutKeyobardFunc(). En vez de obtener un char, obtienes un int. Puedes ver la lista de correspondencias de valor para int key en glutspecialFunc() en el Sumario de los tutoriales II.
En este caso:
- Código:
void ControlFlechas(int key, int x, int y) {
switch (key)
{
case GLUT_KEY_RIGHT:
xpos = xpos + 0.08f;
if (xpos > 10.0f-0.5f) xpos = 9.5f;
break;
case GLUT_KEY_LEFT:
xpos = xpos - 0.08f;
if (xpos < -10.0f+0.5f) xpos = -9.5f;
break;
case GLUT_KEY_UP:
ypos = ypos + 0.08f;
if (ypos > 10.0f-0.5f) ypos = 9.5f;
break;
case GLUT_KEY_DOWN:
ypos = ypos - 0.08f;
if (ypos < -10.0f+0.5f) ypos = -9.5f;
break;
}
glutPostRedisplay();
}
Temas similares
» [Tutorial] II.4 - glutKeyboardFunc()
» [Tutorial C] IV.3 - Estructuras
» [Tutorial] II.3 - glutTimerFunc()
» [Tutorial] II.7 - glutIdleFunc(), glutKeyboardUpFunc() y glutSpecialUpFunc()
» [Tutorial] II.2 - glutReshapeFunc()
» [Tutorial C] IV.3 - Estructuras
» [Tutorial] II.3 - glutTimerFunc()
» [Tutorial] II.7 - glutIdleFunc(), glutKeyboardUpFunc() y glutSpecialUpFunc()
» [Tutorial] II.2 - glutReshapeFunc()
Página 1 de 1.
Permisos de este foro:
No puedes responder a temas en este foro.
|
|