PDA

View Full Version : BINARY TREE IMPLEMENTATION



ammykks
05-31-2009, 12:45 AM
I'm planning to implement a binary tree in 3d. To start off with, this is what Ive done.

#include<GL/glut.h>
#include<stdio.h>

void myinit()
{
GLfloat mat_ambient[]={0.3,0.3,0.3,1.0};
GLfloat mat_diffuse[]={0.6,0.6,0.6,1.0};
GLfloat mat_specular[]={0.9,0.9,0.9,1.0};
GLfloat mat_shininess[]={100.0};

glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambi ent);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diff use);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_spe cular);
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_sh ininess);

GLfloat light0_ambient[]={0.5,0.5,0.5,1.0};
GLfloat light0_diffuse[]={1.0,1.0,1.0,1.0};
GLfloat light0_specular[]={1.0,1.0,1.0,1.0};
GLfloat light0_position[]={5.0,5.0,5.0,0.0};

glLightfv(GL_LIGHT0,GL_AMBIENT,light0_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,light0_specular);
glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE,light0_ambi ent);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
}

void sphere()
{
glPushMatrix();
glutSolidSphere(0.5,100,100);
glPopMatrix();
}
static GLfloat theta[]={0.0,0.0,0.0};
static GLint axis=2.0;
static GLfloat translate[]={0.0,0.0,0.0};

void myspecialkeyfunc(int key,int x,int y)
{
switch(key)
{
case GLUT_KEY_RIGHT: translate[0]+=0.1;
break;
case GLUT_KEY_UP: translate[1]+=0.1;
break;
case GLUT_KEY_LEFT: translate[0]-=0.1;
break;
case GLUT_KEY_DOWN: translate[1]-=0.1;
break;
}
}


void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(translate[0],translate[1],translate[2]);
glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);
sphere();
glFlush();
glutSwapBuffers();
}

void spinsphere()
{
theta[axis]+=10.0;
if(theta[axis]>360.0)
theta[axis]-=360.0;
glutPostRedisplay();
}

void myreshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPT H);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("TEST");
glutSpecialFunc(myspecialkeyfunc);
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
glutIdleFunc(spinsphere);
myinit();
glutMainLoop();
return 0;
}


My requirements are:-
1. I'd like to have the output window size= size of monitor screen and not 500x500...when I use maximize, the image is distorted.
2. I want the sphere to rotate along y axis, my spin()'s rotation is not noticable.
3. I have a simple nurbs program that I'd like to include in this. Its a half pot object and so I'd like to show spheres popping from it(behind the half pot) while I'm inserting new nodes(spheres) onto the tree. The pot is currently in the middle of the screen and I want it in the bottom left corner.
4. How do I construct a binary tree and how many max nodes should I allow? How to make the program interactive?
5. The movement of spheres is in increments of 0.1, how to make them move to the correct position in the tree when inserting? Should I hard code the positions of nodes and movements of sphere? If not, how to enable the user to move them to the postions?