#include <stdlib.h>
#include <stdio.h>
#include <cave_ogl.h>
#include <unistd.h>

#include "VidAv.h"

#define SPEED 5.0f

void ogl_init_fn();
void update_funct(void);
void drawframe(); 

void drawplane();   
void drawrow();  
void drawfloor();  
void navigate(void);



void main(int argc,char **argv)
{
   
   va_PreInit();   /*** does avatar initialization, must be   ****/
		   /*** called before CAVEInit();             ****/
   CAVEInit();
   CAVEInitApplication(ogl_init_fn,0);
   CAVEDisplay(drawframe,0);
   CAVEFrameFunction(update_funct, 0);
 
   while(!CAVEgetbutton(CAVE_ESCKEY))
    {
      navigate();
      sginap(1);  /* Make this loop run about 100 iterations/second */
    }
   CAVEExit();
}

/* initialize the graphics  */
void ogl_init_fn()
{
    
    glClearColor(0.0, 0.0, 0.0, 0.0);
       
    /***  va_InitTex initializes avatar's texture mapping  ***/
 
    va_InitTex();
   
}


void drawframe(void)
{
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);  
  
  glPushMatrix();
    CAVENavTransform();
  
      drawfloor();
	  
      va_DrawAllAvatars();  /*** draws all enabled avatars ***/
	  
  glPopMatrix(); 
  

}



void update_funct(void)
{
    static int OnFlag=1;
    float t=*CAVETime;
    static float prevtime=*CAVETime;
    static Button1Val, Button2Val;
    static float RemoteAvatarScale=1.0;
    
   if (CAVEBUTTON1 && (CAVEBUTTON1!=Button1Val)) printf("Remote avatars are enabled: %d\n", va_RemoteAvatarsAreEnabled());
   
   if (CAVEBUTTON2 && (CAVEBUTTON2!=Button2Val)) 
    {
	OnFlag=(OnFlag+1)%2;
	printf("OnFlag: %d\n", OnFlag);
	if (OnFlag) va_EnableRemoteAvatars();
	else va_DisableRemoteAvatars();
    }
    
    
    Button1Val=CAVEBUTTON1;
    Button2Val=CAVEBUTTON2;
    
    if (CAVEgetbutton(CAVE_NINEKEY)) 
    {
	RemoteAvatarScale-=.1;
	if (RemoteAvatarScale<0.0) RemoteAvatarScale=0.0;
	va_SetRemoteAvatarScale(RemoteAvatarScale);
    }
    
    if (CAVEgetbutton(CAVE_ZEROKEY)) 
    {
	RemoteAvatarScale+=.1;
	va_SetRemoteAvatarScale(RemoteAvatarScale);
    }

    /*** va_UpdateAllAvatars updates all avatars appropriately ***/
    
    va_UpdateAllAvatars();
    
    prevtime=t;
}

void navigate(void)
{
    float jx=CAVE_JOYSTICK_X,jy=CAVE_JOYSTICK_Y,dt,t;
    static float prevtime = 0;
    t = *CAVETime;
    dt = t - prevtime;
    prevtime = t;
    if (fabs(jy)>0.2)
    {
    float wandFront[3];
    CAVEGetVector(CAVE_WAND_FRONT,wandFront);
    CAVENavTranslate(wandFront[0]*jy*SPEED*dt, 0.0,
		    wandFront[2]*jy*SPEED*dt);
    }
    if (fabs(jx)>0.2)
	CAVENavRot(-jx*90.0f*dt,'y');
}


void drawplane()
{
	
   const  float	point1[3] = {-0.5, 0., -0.5};
   const  float	point2[3] = {0.5, 0., -0.5}; 
   const  float	point3[3] = {0.5, 0., 0.5};
   const  float	point4[3] = {-0.5, 0., 0.5}; 

   const  float normal[3] = {0., 1., 0}; 

   glBegin(GL_POLYGON);
     glNormal3fv(normal); 
     glVertex3fv(point4);
     glNormal3fv(normal); 
     glVertex3fv(point3);
     glNormal3fv(normal);
     glVertex3fv(point2);
     glNormal3fv(normal);
     glVertex3fv(point1);
   glEnd();
   				
}

void drawrow(void)
{
     glPushMatrix();
	glColor4f(1,0,0, 1);
	glTranslatef(-15, 0, 0);
	glScalef(10, 1., 10);
	drawplane();
    glPopMatrix();
    glPushMatrix();
	glColor4f(0,1,0, 1);
	glTranslatef(-5, 0, 0);
	glScalef(10, 1., 10);
	drawplane();
    glPopMatrix();
    glPushMatrix();
	glColor4f(0,0,1, 1);
	glTranslatef(5, 0, 0);
	glScalef(10, 1., 10);
	drawplane();
    glPopMatrix();
    glPushMatrix();
	glColor4f(1,1,0, 1);
	glTranslatef(15, 0, 0);
	glScalef(10, 1., 10);
	drawplane();
    glPopMatrix();   
}

void drawfloor(void)
{
      glPushMatrix();
	glTranslatef(0, 0, -15); 
	drawrow(); 
      glPopMatrix();
      glPushMatrix();
	glTranslatef(0, 0, -5);
	glRotatef(180, 0, 1, 0); 
	drawrow(); 
      glPopMatrix();
      glPushMatrix();
	glTranslatef(0, 0, 5); 
	drawrow(); 
      glPopMatrix();
      glPushMatrix();
	glTranslatef(0, 0, 15); 
	glRotatef(180, 0, 1, 0);
	drawrow(); 
      glPopMatrix();
}


Back to VideoAvatar