Foros del Web » Programación para mayores de 30 ;) » C/C++ »

duda codigo

Estas en el tema de duda codigo en el foro de C/C++ en Foros del Web. Hola buenas. Tengo un programa en C++(programa A), el cual muestra en un entorno grafico unos cubos y segun la tecla que pulses suena un ...
  #1 (permalink)  
Antiguo 13/12/2010, 12:56
 
Fecha de Ingreso: enero-2008
Mensajes: 13
Antigüedad: 16 años, 3 meses
Puntos: 1
duda codigo

Hola buenas. Tengo un programa en C++(programa A), el cual muestra en un entorno grafico unos cubos y segun la tecla que pulses suena un sonido, se mueve posicion del cubo...
Quiero incorporar un menu en este programa(que tengo en programa B) para que segun la opcion que elijas realice ciertas acciones sobre los cubos, pero no se como hacerlo. En el programa A primero compruebo que se le pasen 4 argumentos y luego creo entorno gráfico glut, a continuacion defino las funciones. Lo que quiero es que una vez esten los cubos en el torno graficos, poder elegir desde el terminal a que objeto aplico lo que desee hacer(cambiar pitch, gain, posicion...) Os paso los 2 programanas que tengo a ver si me podeis echar una mano, porque voy perdidisimo y no se donde mas mirar. Adjunto el programa A, como no me deja adjuntar programa B por exceso de caracteres lo adjunto en otro nuevo tema

PROGRAMA A

#include <iostream>
#include <stdio.h>
#include <stdlib.h> // Per al 'exit'
#include <GL/glut.h>
#include <OpenAL/alut.h>
#include <AL/alut.h>
// function prototypes ---------------------------------------------
void init(char *s1, char *s2, char *s3, char *s4);
void display();
void reshape(int w, int h);
void keyboard(unsigned char key, int x, int y);
void specialKeys(int key, int x, int y);
// definir numero de buffers y fuentes a utilizar (iguales en este caso).Ddefinir numero de ambientes de sonido ---------------------------------------------
#define NUM_BUFFERS 4
#define NUM_SOURCES 4
#define NUM_ENVIRONMENTS 1
/* definir la posicion velocidad y orientacion del receptor */
ALfloat listenerPos[]={0.0,0.0,4.0};
ALfloat listenerVel[]={0.0,0.0,0.0};
ALfloat listenerOri[]={0.0,0.0,1.0, 0.0,1.0,0.0};
/* definir las posiciones, velocidaded y orientaciones de cada emisor */
ALfloat source0Pos[]={ -2.0, 0.0, 0.0};
ALfloat source0Vel[]={ 0.0, 0.0, 0.0};
ALfloat source1Pos[]={ 2.0, 0.0, 0.0};
ALfloat source1Vel[]={ 0.0, 0.0, 0.0};
ALfloat source2Pos[]={ 0.0, 0.0, -4.0};
ALfloat source2Vel[]={ 0.0, 0.0, 0.0};
ALfloat source3Pos[]={ 0.0, 0.0, 8.0};
ALfloat source3Vel[]={ 0.0, 0.0, 0.0};
/* reservar buffers de sonido */
ALuint buffer[NUM_BUFFERS];
ALuint source[NUM_SOURCES];
ALuint environment[NUM_ENVIRONMENTS];
int GLwin;
ALsizei size,freq;
ALenum format;
ALvoid *data;
int ch;

int main(int argc, char** argv) //finaly the main function
{
if (argc < 4)
{ printf( "Faltan los tres argumentos que indiquen qué ficheros de audio han de sonar asociados a las tres fuentes de audio que se definen en la escena\n\n\
Por ejemplo:\n\
%s /usr/share/sounds/KDE_Beep_Bottles.wav /usr/share/sounds/KDE_Beep_Beep.wav /usr/share/sounds/KDE_Beep_Ahem.wav /usr/share/sounds/EXAMPLE.wav\n", argv[0]);exit( 1 );
}
else
{
// Menú: opciones
printf( "Las fuentes de sonido se activan con las teclas '1', '2' , '3' y '7'\n\
Las fuentes de sonido se desactivan con las teclas '4', '5' , '6' y '8'\n\
La posición del oyente se modifica con las teclas del cursor (también con 'a', 's', 'q' y 'z').\n\
ESC para terminar\n\n\n");
}
//initialise glut
glutInit(&argc, argv) ;
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) ;
glutInitWindowSize(400,400) ;
//initialise openAL
alutInit(&argc, argv);
GLwin = glutCreateWindow("PIGE - OpenAL Example") ;
init(argv[1], argv[2], argv[3], argv[4]) ;
glutDisplayFunc(display) ;
glutKeyboardFunc(keyboard) ;
glutSpecialFunc(specialKeys);
glutReshapeFunc(reshape) ;

glutMainLoop() ;
return 0;
}

void init( char *s1, char *s2, char *s3, char *s4) //void)
{
alutInit(0, NULL);
/* asignar posicion, velocidad y orientacion al receptor */
alListenerfv(AL_POSITION,listenerPos);
alListenerfv(AL_VELOCITY,listenerVel);
alListenerfv(AL_ORIENTATION,listenerOri);
alGetError(); // clear any error messages
if(alGetError() != AL_NO_ERROR)
{printf("- Error creating buffers !!\n");
exit(1);}
else
{printf("init() - No errors yet.");}

// Generate buffers, or else no sound will happen!.
alGenBuffers(NUM_BUFFERS, buffer);
/*cargar archivos de sonido y asignarlos a los emisores */
printf( "Assignant buffer 1 a %s\n", s1 );
buffer[0] = alutCreateBufferFromFile( s1 );
printf( "Assignant buffer 2 a %s\n", s2 );
buffer[1] = alutCreateBufferFromFile( s2 );
printf( "Assignant buffer 3 a %s\n", s3 );
buffer[2] = alutCreateBufferFromFile( s3 );
printf( "Assignant buffer 4 a %s\n", s4 );
buffer[2] = alutCreateBufferFromFile( s4 );

alGetError(); /* clear error */
alGenSources(NUM_SOURCES, source);

if(alGetError() != AL_NO_ERROR)
{printf("- Error creating sources !!\n");
exit(2);}
else
{printf("init - no errors after alGenSources\n");}


/* definir parametros de control del sonido para cada emisor */
alSourcef(source[0],AL_PITCH,1.0f);
alSourcef(source[0],AL_GAIN,1.0f);
alSourcefv(source[0],AL_POSITION,source0Pos);
alSourcefv(source[0],AL_VELOCITY,source0Vel);
alSourcei(source[0],AL_BUFFER,buffer[0]);
alSourcei(source[0],AL_LOOPING,AL_TRUE);
alSourcef(source[1],AL_PITCH,1.0f);
alSourcef(source[1],AL_GAIN,1.0f);
alSourcefv(source[1],AL_POSITION,source1Pos);
alSourcefv(source[1],AL_VELOCITY,source1Vel);
alSourcei(source[1],AL_BUFFER,buffer[1]);
alSourcei(source[1],AL_LOOPING,AL_TRUE);
alSourcef(source[2],AL_PITCH,1.0f);
alSourcef(source[2],AL_GAIN,1.0f);
alSourcefv(source[2],AL_POSITION,source2Pos);
alSourcefv(source[2],AL_VELOCITY,source2Vel);
alSourcei(source[2],AL_BUFFER,buffer[2]);
alSourcei(source[2],AL_LOOPING,AL_TRUE);
alSourcef(source[3],AL_PITCH,1.0f);
alSourcef(source[3],AL_GAIN,1.0f);
alSourcefv(source[3],AL_POSITION,source3Pos);
alSourcefv(source[3],AL_VELOCITY,source3Vel);
alSourcei(source[3],AL_BUFFER,buffer[3]);
alSourcei(source[3],AL_LOOPING,AL_TRUE);
}

void display(void)
{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;
glPushMatrix() ;
glRotatef(20.0,1.0,1.0,0.0) ;

glPushMatrix() ;
glTranslatef(source0Pos[0],source0Pos[1],source0Pos[2]) ;
glColor3f(1.0,0.0,0.0) ;
glutWireCube(0.5) ;
glPopMatrix() ;

glPushMatrix() ;
glTranslatef(source2Pos[0],source2Pos[1],source2Pos[2]) ;
glColor3f(0.0,0.0,1.0) ;
glutWireCube(0.5) ;
glPopMatrix() ;

glPushMatrix() ;
glTranslatef(source1Pos[0],source0Pos[1],source0Pos[2]) ;
glColor3f(0.0,1.0,0.0) ;
glutWireCube(0.5) ;
glPopMatrix() ;

glPushMatrix() ;
glTranslatef(source3Pos[0],source3Pos[1],source3Pos[2]) ;
glColor3f(0.0,1.0,0.0) ;
glutWireCube(0.5) ;
glPopMatrix() ;
//the listener
glPushMatrix() ;
glTranslatef(listenerPos[0],listenerPos[1],listenerPos[2]) ;
glColor3f(1.0,1.0,1.0) ;
glutWireCube(0.5) ;
glPopMatrix() ;

glPopMatrix() ;
glutSwapBuffers() ;
}

void reshape(int w, int h) // the reshape function
{glViewport(0,0,(GLsizei)w,(GLsizei)h) ;
glMatrixMode(GL_PROJECTION) ;
glLoadIdentity() ;
gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,30.0) ;
glMatrixMode(GL_MODELVIEW) ;
glLoadIdentity() ;
glTranslatef(0.0,0.0,-6.6) ;
}

void keyboard(unsigned char key, int x, int y)
{
switch(key){
case 'h':
case 'H': // Menú: opciones
printf(" 1 on 1, 4 off 1\n 2 on 2, 5 off 2\n 3 on 3, 6 off 3\n 7 on 7, 8 off 7 La posició del oient es canvia en les tecles del cursor o en 'asqz'\n");break;
case '1':
alSourcePlay(source[0]);
printf("1\n");break;
case '2':
alSourcePlay(source[1]);
printf("2\n");break;
case '3':
alSourcePlay(source[2]);
printf("3\n");break;
case '7':
alSourcePlay(source[3]);
printf("7\n");break;
case '4':
alSourceStop(source[0]);
printf("4\n");break;
case '5':
alSourceStop(source[1]);
printf("5\n");break;
case '6':
alSourceStop(source[2]);
printf("6\n");
break;
case '8':
alSourceStop(source[3]);
printf("8\n");break;
case 'a':
case 'A':
listenerPos[0] -= 0.1 ;
alListenerfv(AL_POSITION,listenerPos);break ;
case 's':
case 'S':
listenerPos[0] += 0.1 ;
alListenerfv(AL_POSITION,listenerPos);break ;
case 'q':
case 'Q':
listenerPos[2] -= 0.1 ;
alListenerfv(AL_POSITION,listenerPos);break ;
case 'z':
case 'Z':
listenerPos[2] += 0.1 ;
alListenerfv(AL_POSITION,listenerPos);break ;
case 27:
alSourceStop(source[2]);
alSourceStop(source[1]);
alSourceStop(source[0]);
alutExit();
glutDestroyWindow(GLwin) ;
exit(0) ;break ;
default: break;}
glutPostRedisplay() ;
}
void specialKeys(int key, int x, int y){
switch(key){
case GLUT_KEY_RIGHT:
listenerPos[0] += 0.1 ;
alListenerfv(AL_POSITION,listenerPos);
glutPostRedisplay() ;break;
case GLUT_KEY_LEFT:
listenerPos[0] -= 0.1 ;
alListenerfv(AL_POSITION,listenerPos);
glutPostRedisplay() ;break;
case GLUT_KEY_UP:
listenerPos[2] -= 0.1 ;
alListenerfv(AL_POSITION,listenerPos);
glutPostRedisplay() ;break;
case GLUT_KEY_DOWN:
listenerPos[2] += 0.1 ;
alListenerfv(AL_POSITION,listenerPos);
glutPostRedisplay() ;break;}}
  #2 (permalink)  
Antiguo 13/12/2010, 12:58
 
Fecha de Ingreso: enero-2008
Mensajes: 13
Antigüedad: 16 años, 3 meses
Puntos: 1
Respuesta: duda codigo

este es el programa B:

PROGRAMA B

#include <stdio.h>
#include <stdlib.h> // Per al 'exit'
#define GLUT_DISABLE_ATEXIT_HACK
//#include <GLUT/glut.h>
#include <GL/glut.h>
//#include <OpenAL/alut.h>
#include <AL/alut.h>

int generar();
void version();
void dispositivos();
void extension();

int main (int argc, char **argv)
{char c;
do{
printf("Elija una de las siguientes opciones: \n\n");
printf("a.Generar señales simples.\n\n");
printf("b.Obtener version openAL y ALUT.\n\n");
printf("c.Mostrar dispositivos de audio.\n\n");
printf("d.Mostrar extensiones OPENAL.\n\n");
printf("e.Modificar PITCH de cada fuente.\n\n");
printf("f.Modificar GAIN de cada fuente y del oyente.\n\n");
printf("g.Cambiar la orientación del oyente.\n\n");
printf("h.Cambiar posicion espacial y orientacion de cada fuente\n\n");
printf("i. Modificar DOPPLER de cada fuente\n\n");
printf("j.Salir.\n\n");
c = getchar();

switch ( c ){
case 'a':
generar();break;
case 'b':
version();break;
case 'c':
dispositivos();break;
case 'd':
extension();break;
case 'e':
printf("Has elegido la Opción e");break;
case 'f':
printf("Has elegido la Opción f");break;
case 'g':
printf("Has elegido la Opción g");break;
case 'h':
printf("Has elegido la Opción h");break;
case 'i':
printf("Has elegido la Opción i");break;}// switch
}while ((c != 'j') && (c != 'J'));}

int generar(){
int argc;
char **argv;
ALuint buffers[6], fuente;
char p;
alutInit (&argc, argv);
p = 'h';
buffers[0] = alutCreateBufferHelloWorld ();
buffers[1] = alutCreateBufferWaveform(ALUT_WAVEFORM_SINE, 440.0, 0.0, 1.0);
buffers[2] = alutCreateBufferWaveform(ALUT_WAVEFORM_SQUARE, 440.0, 0.0, 1.0);
buffers[3] = alutCreateBufferWaveform(ALUT_WAVEFORM_SAWTOOTH, 440.0, 0.0, 1.0);
buffers[4] = alutCreateBufferWaveform(ALUT_WAVEFORM_WHITENOISE, 440.0, 0.0, 1.0);
buffers[5] = alutCreateBufferWaveform(ALUT_WAVEFORM_IMPULSE, 440.0, 0.0, 1.0);
alGenSources (1, &fuente);
printf("'h'ellow, 's'ine, sq'u'are, sa'w'tooh, white'n'oise, 'i'mpulse ('q' para salir): \n");
do{
switch ( p ){
case 'h': alSourcei (fuente, AL_BUFFER, buffers[0]);
break;
case 's': alSourcei (fuente, AL_BUFFER, buffers[1]);
break;
case 'u': alSourcei (fuente, AL_BUFFER, buffers[2]);
break;
case 'w': alSourcei (fuente, AL_BUFFER, buffers[3]);
break;
case 'n': alSourcei (fuente, AL_BUFFER, buffers[4]);
break;
case 'i': alSourcei (fuente, AL_BUFFER, buffers[5]);
break;}// switch
alSourcePlay (fuente);
alutSleep (1);
p = getch();
printf("%c\r", p); fflush(stdout);
} while ((p != 'q') && (p != 'Q'));
printf("\nTerminando ... \n");
alDeleteSources(1, &fuente);
alDeleteBuffers(6, buffers);
alutExit ();
return EXIT_SUCCESS;}
void version(){

int argc;
char **argv;
const ALchar *versions;
alutInit (&argc, argv);
versions = alGetString(AL_VERSION);
printf("OpenAL Version is '%s' \n",versions);
printf("ALUT version: %d.%d \n", alutGetMajorVersion (), alutGetMinorVersion () );
}
void dispositivos(){

const ALCchar *devices;
const ALCchar *defaultDeviceName;
devices = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
// devices contiene el nombre de los dispositivos, separados por NULL
// and terminado con dos NULLS consecutivos.
defaultDeviceName = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
printf("Los dispositivos son:'%s\n%s' \n",devices,defaultDeviceName);
// defaultDeviceName contiene el nombre de el dispositivo por defecto
}
void extension(){
int argc;
char **argv;
const ALchar *extensions;
alutInit (&argc, argv);
extensions = alGetString(AL_EXTENSIONS);
printf("OpenAL entension is '%s' \n",extensions);
}
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:23.