Monday, March 23, 2015

Vesta Dwarf Planet Win32 Console Code


To compile you will need this 500Mb file:
http://dawndata.igpp.ucla.edu/tw.jsp?section=geometry/ShapeModels/GASKELL_CLAUDIA_2013_02_13/VEST64_DTM.raw

You will need the GLUT32 libraries.
https://www.opengl.org/resources/libraries/glut/

 "agviewer" for details of this open source api.
https://www.opengl.org/archives/resources/code/samples/glut_examples/contrib/contrib.html




#include "agviewer.h"

typedef enum {NOTALLOWED, AXES, STUFF, RING } DisplayLists;

int DrawAxes = 0;

#define ROTATEINC 0.02;

GLfloat Rotation = 0;  /* start ring flat and not spinning */
int     Rotating = 0;








  ////////////////////////////////////////////////////////////////////////////

void init1(){
int w=23041;
int h=11521;

  int ht=h+15;
  int wt=w+15;
  float scale=200.0;
  float deltaPhi=(16.0*PI/ht);
  float deltaTheta=(32.0*PI/wt);
  
  


  int length=w*h;
int size=0;

char *memblock;
unsigned short *memblock2;

  ifstream myfile;
  myfile.open ("VEST64_DTM.raw",ios::in|ios::binary|ios::ate);
  if (myfile.is_open()){
  

    size = myfile.tellg();

if(size==2*length){
cout<<size<<"=="<<2*length<<endl;

  }

    memblock=new char[2*length];
    myfile.seekg (0, ios::beg);
myfile.read(memblock,size);
myfile.close();
  }
  


  
  //unsigned short *a=new unsigned short[w][h];



  memblock2=new unsigned short[length];

for(int i=0;i<length;i++){
memblock2[i]=(memblock[i*2]<<8)+(memblock[i*2+1]);

//cout<<setw(8)<<memblock2[i];

}

delete memblock;

 unsigned short** ary = new  unsigned short*[ht];
for(int i = 0; i < ht; ++i)
    ary[i] = new unsigned short[wt];

int m,n;
for(int i=0;i<length;i++){     
 m=i%w,n=i/w;
//  cout<<setw(8)<<memblock2[i];
   ary[n][m]=memblock2[i]; 
  }
  //fill
for(int k=0;k<h;k++){
for(int j=w;j<wt;j++){

ary[k][j]=0;
}
}
for(int k=h;k<ht;k++){
for(int j=0;j<wt;j++){
ary[k][j]=0;
}
}

delete memblock2;

  unsigned short** b_ary = new  unsigned short*[ht];
for(int i = 0; i < ht; ++i)
    b_ary[i] = new unsigned short[wt/16];

for(int k=0;k<(ht);k++){
  for(int j=0;j<wt;j+=16){
 b_ary[k][j/16]=0;
 for(int l=0;l<16;l++){
 b_ary[k][j/16]+=(ary[k][j+l])/16;
// cout<<setw(8)<<b_ary[k/2][j/2];
 }
 }
}
for(int i = 0; i < ht; ++i) {
    delete [] ary[i];
    }
delete [] ary;

unsigned short** e_ary = new unsigned short*[(ht)/16];
for(int i = 0; i < (ht)/16; ++i)
    e_ary[i] = new unsigned short[(wt)/16];

  for(int k=0;k<(ht);k+=16){
  for(int j=0;j<(wt)/16;j++){
 e_ary[k/16][j]=0;
 for(int l=0;l<16;l++){
 e_ary[k/16][j]+=b_ary[k+l][j]/16;
 }
  }
}

for(int i = 0; i < (ht); ++i) {
    delete [] b_ary[i];
    }
delete [] b_ary;





  int u=0,v=0;
  glBegin(GL_TRIANGLES);
for(float theta=0;theta<2.0*PI-2.0*deltaTheta;theta+=deltaTheta){
v=0;
 for(float phi=0;phi<(PI-2.0*deltaPhi);phi+=deltaPhi){
 
 float r1= (+250.00000 - 0.3807671317E+02 + (e_ary[v][u+1])*0.1234605163E-02)/scale;
 float r2= (+250.00000 - 0.3807671317E+02 + (e_ary[v][u])*0.1234605163E-02)/scale;
 float r3= (+250.00000 - 0.3807671317E+02 + (e_ary[v+1][u])*0.1234605163E-02)/scale;
 float r4= (+250.00000 - 0.3807671317E+02 + (e_ary[v+1][u+1])*0.1234605163E-02)/scale;

 float z2=r2*cos(phi);
 float x2=r2*sin(phi)*cos(theta);
 float y2=r2*sin(phi)*sin(theta);
    
 float z1=r1*cos(phi);
 float x1=r1*sin(phi)*cos(theta+deltaTheta);
 float y1=r1*sin(phi)*sin(theta+deltaTheta);

 float z3=r3*cos(phi+deltaPhi);
 float x3=r3*sin(phi+deltaPhi)*cos(theta);
 float y3=r3*sin(phi+deltaPhi)*sin(theta);

 float z4=r4*cos(phi+deltaPhi);
 float x4=r4*sin(phi+deltaPhi)*cos(theta+deltaTheta);
 float y4=r4*sin(phi+deltaPhi)*sin(theta+deltaTheta);


  float V1x=x4-x1;
  float V1y=y4-y1;
  float V1z=z4-z1;

  float V2x=x2-x1;
  float V2y=y2-y1;
  float V2z=z2-z1;

  float normalx = V1y * V2z - V1z * V2y;
  float normaly = V2x * V1z - V2z * V1x;
  float normalz = V1x * V2y - V1y * V2x;

  float V3x=x3-x2;
  float V3y=y3-y2;
  float V3z=z3-z2;

  float V4x=x3-x4;
  float V4y=y3-y4;
  float V4z=z3-z4;

  float normalx2 = V3y * V4z - V3z * V4y;
  float normaly2 = V4x * V3z - V4z * V3x;
  float normalz2 = V3x * V4y - V3y * V4x;
  
 glNormal3f(normalx,normaly,normalz);
 
   glVertex3f( x1, y1, z1);  
  glVertex3f( x2, y2, z2);
   glVertex3f( x4, y4, z4);
  glNormal3f(normalx2,normaly2,normalz2);
 glVertex3f( x3, y3, z3);
glVertex3f( x4, y4, z4);
glVertex3f( x2, y2, z2);
 

  

 
v++;
 }
 u++;
}
glEnd();


for(int i = 0; i < ht/16; ++i) {
    delete [] e_ary[i];
    }
delete [] e_ary;


// for(int i = 0; i < h/8; ++i) {
  //  delete [] d_ary[i];
    //}
//delete [] d_ary;





//     return 0;




}








void myGLInit(void)
{

  GLfloat mat_ambuse[] = { 0.6, 0.0, 0.0, 1.0 };
  GLfloat mat_specular[] = { 0.4, 0.4, 0.4, 1.0 };

  GLfloat light0_position[] = { 0.6, 0.4, 0.3, 0.0 };

  glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);

  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_ambuse);
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  glMaterialf(GL_FRONT, GL_SHININESS, 25.0);
  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glEnable(GL_NORMALIZE);

  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);

  glShadeModel(GL_SMOOTH);

  glFlush();



void MakeDisplayLists(void)
{
  glNewList(STUFF, GL_COMPILE);
  glPushMatrix();
    glutSolidCube(1.0);
    glTranslatef(2, 0, 0);
    glutSolidSphere(0.5, 10, 10);
    glTranslatef(-2, 0, 3);
    glRotatef(-90, 1, 0, 0);
    glutSolidCone(0.5, 1.0, 8, 8);
  glPopMatrix();
  glEndList();

  glNewList(RING, GL_COMPILE);
    init1();
  glEndList();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  gluPerspective(60, 1, 0.01, 100);

    /* so this replaces gluLookAt or equiv */
  agvViewTransform();

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

    /* we call agvMakeAxesList() to make this display list */
  if (DrawAxes)
    glCallList(AXES);

 // glCallList(STUFF);

 // glTranslatef(-2, 1, -2);
  glRotatef(Rotation, 1, 0, 0);
  glCallList(RING);

  glutSwapBuffers();
  glFlush();
}

//////////////////////////////////////////////////////////////////////////////////////////////////
  /* rotate the axis and adjust position if nec. */
void rotatethering(void)

  Rotation += ROTATEINC;

  if (agvMoving)   /* we since we are the only idle function, we must */
    agvMove();     /* give AGV the chance to update the eye position */

  glutPostRedisplay();
}

typedef enum { MENU_AXES, MENU_QUIT, MENU_RING } MenuChoices;

void handlemenu(int value)
{
  switch (value) {
    case MENU_AXES:
      DrawAxes = !DrawAxes;
      break;
    case MENU_QUIT:
      exit(0);
      break;
    case MENU_RING:
      Rotating = !Rotating;
      if (Rotating) {
glutIdleFunc(rotatethering);    /* install our idle function */
agvSetAllowIdle(0);             /* and tell AGV to not */
      } else {
glutIdleFunc(NULL);    /* uninstall our idle function      */
agvSetAllowIdle(1);    /* and tell AGV it can mess with it */
      }
      break;
    }
  glutPostRedisplay();
}

void visible(int v)
{
  if (v == GLUT_VISIBLE) {
    if (Rotating) {
      glutIdleFunc(rotatethering);
      agvSetAllowIdle(0);
    } else {
      glutIdleFunc(NULL);
      agvSetAllowIdle(1);      
    }
  } else {
      glutIdleFunc(NULL);
      agvSetAllowIdle(0);
  }
}


void MenuInit(void)
{
  int sub2 = glutCreateMenu(agvSwitchMoveMode);   /* pass these right to */
  glutAddMenuEntry("Flying move",  FLYING);       /* agvSwitchMoveMode() */
  glutAddMenuEntry("Polar move",   POLAR);

  glutCreateMenu(handlemenu);
  glutAddSubMenu("Movement", sub2);
  glutAddMenuEntry("Toggle Axes", MENU_AXES);
  glutAddMenuEntry("Toggle ring rotation", MENU_RING);
  glutAddMenuEntry("Quit", MENU_QUIT);
  glutAttachMenu(GLUT_RIGHT_BUTTON);
}

//////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitWindowSize(512, 512);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutCreateWindow("AGV example");
  
  glutVisibilityFunc(visible);

  if (Rotating)
    glutIdleFunc(rotatethering);

    /*
     * let AGV know if it can mess with the idle function (if we've
     * just installed an idle function, we tell AGV it can't touch it)
     */

  agvInit(!Rotating);
    
    /* 
     * agvInit() installs mouse, motion, and keyboard handles, but 
     * we don't care for this example cause we only use right button menu
     */

  agvMakeAxesList(AXES);  /* create AGV axes */

  myGLInit(); 
  MakeDisplayLists();
  MenuInit();

  glutDisplayFunc(display);

  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}



No comments:

Post a Comment