Example #1
0
glm::vec3 Path::GetPosition(float time)
{
	int i0 = (int)time;
	if (i0 == Points.size())
		i0 = 0;
	int i1 = i0 + 1;
	if (i1 == Points.size())
		i1 = 0;
	glm::vec3 p0 = Points[i0];
	glm::vec3 p1 = Points[i1];
	glm::vec3 m_lin = p1 - p0;
	glm::vec3 m0 = Curved ? CatmullRomTangent(i0): m_lin;
	glm::vec3 m1 = Curved ? CatmullRomTangent(i1): m_lin;
	float t = glm::fract(time);
	float t2 = t * t;
	float t3 = t2 * t;
	glm::vec3 pt =
		(1 - 3 * t2 + 2 * t3) * p0 +
		(3 * t2 - 2 * t3) * p1 +
		(t - 2 * t2 + t3) * m0 +
		(-t2 + t3) * m1;
	return pt;
}
void camera()
{
    int j=0;
    int i=move_i;
    
    p.x= CatmullRom(move, g_Splines[j].points[i].x, g_Splines[j].points[i+1].x, g_Splines[j].points[i+2].x, g_Splines[j].points[i+3].x);
    p.y= CatmullRom(move, g_Splines[j].points[i].y, g_Splines[j].points[i+1].y, g_Splines[j].points[i+2].y, g_Splines[j].points[i+3].y);
    p.z= CatmullRom(move, g_Splines[j].points[i].z, g_Splines[j].points[i+1].z, g_Splines[j].points[i+2].z, g_Splines[j].points[i+3].z);
    
    t.x= CatmullRomTangent(move, g_Splines[j].points[i].x, g_Splines[j].points[i+1].x, g_Splines[j].points[i+2].x, g_Splines[j].points[i+3].x);
    t.y= CatmullRomTangent(move, g_Splines[j].points[i].y, g_Splines[j].points[i+1].y, g_Splines[j].points[i+2].y, g_Splines[j].points[i+3].y);
    t.z= CatmullRomTangent(move, g_Splines[j].points[i].z, g_Splines[j].points[i+1].z, g_Splines[j].points[i+2].z, g_Splines[j].points[i+3].z);
    t = normalize(t);
    
    n = CrossProduct(t, v);
    n = normalize(n);
    
    b = CrossProduct(t, n);
    b = normalize(b);
    float f=0.05,f1=0.1;
    
    point c;
    
    f=0.1,f1=0.05;
    c.x = p.x +f1*n.x + f*b.x;
    c.y = p.y + f1*n.y + f*b.y;
    c.z = p.z + f1*n.z + f*b.z;
    
    
    
    gluLookAt(
              // 5.0,5.0,14.0,
              c.x,c.y,c.z,
              p.x+f1*n.x+t.x, p.y+f1*n.y+t.y, p.z+f1*n.z+t.z,
              
              b.x,b.y,b.z);
}
void displayRails()
{
    //DISPLAY RAILS ON ROLLER COASTER
    glColor3f(1.0, 1.0, 1.0);
    texture = LoadTexture("wood.jpg",256, 256);
    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
    for (int j = 0; j < g_iNumOfSplines; j++) {
        for(int i=-2;i<g_Splines[j].numControlPoints-1;i++)
        {
            for(float u=0.0;u<1.0;u+=0.05){
                
                point p,t,n,v,n1,t1,p1;
                v.x=0.0;v.y=0.0;v.z=-1.0;
                
                float f=0.1;
                //Point
                p.x= CatmullRom(u, g_Splines[j].points[i].x, g_Splines[j].points[i+1].x, g_Splines[j].points[i+2].x, g_Splines[j].points[i+3].x);
                p.y= CatmullRom(u, g_Splines[j].points[i].y, g_Splines[j].points[i+1].y, g_Splines[j].points[i+2].y, g_Splines[j].points[i+3].y);
                p.z= CatmullRom(u, g_Splines[j].points[i].z, g_Splines[j].points[i+1].z, g_Splines[j].points[i+2].z, g_Splines[j].points[i+3].z);
                //Tangent
                t.x= CatmullRomTangent(u, g_Splines[j].points[i].x, g_Splines[j].points[i+1].x, g_Splines[j].points[i+2].x, g_Splines[j].points[i+3].x);
                t.y= CatmullRomTangent(u, g_Splines[j].points[i].y, g_Splines[j].points[i+1].y, g_Splines[j].points[i+2].y, g_Splines[j].points[i+3].y);
                t.z= CatmullRomTangent(u, g_Splines[j].points[i].z, g_Splines[j].points[i+1].z, g_Splines[j].points[i+2].z, g_Splines[j].points[i+3].z);
                t = normalize(t);
                //Normal
                n = CrossProduct(t, v);
                n = normalize(n);
                
                p1.x= CatmullRom(u+0.01, g_Splines[j].points[i].x, g_Splines[j].points[i+1].x, g_Splines[j].points[i+2].x, g_Splines[j].points[i+3].x);
                p1.y= CatmullRom(u+0.01, g_Splines[j].points[i].y, g_Splines[j].points[i+1].y, g_Splines[j].points[i+2].y, g_Splines[j].points[i+3].y);
                p1.z= CatmullRom(u+0.01, g_Splines[j].points[i].z, g_Splines[j].points[i+1].z, g_Splines[j].points[i+2].z, g_Splines[j].points[i+3].z);
                
                t1.x= CatmullRomTangent(u+0.01, g_Splines[j].points[i].x, g_Splines[j].points[i+1].x, g_Splines[j].points[i+2].x, g_Splines[j].points[i+3].x);
                t1.y= CatmullRomTangent(u+0.01, g_Splines[j].points[i].y, g_Splines[j].points[i+1].y, g_Splines[j].points[i+2].y, g_Splines[j].points[i+3].y);
                t1.z= CatmullRomTangent(u+0.01, g_Splines[j].points[i].z, g_Splines[j].points[i+1].z, g_Splines[j].points[i+2].z, g_Splines[j].points[i+3].z);
                t1 = normalize(t1);
                
                n1 = CrossProduct(t1, v);
                n1 = normalize(n1);
                
                glColor3f(1.0, 1.0, 1.0);
                glTexCoord2d(0.0,0.0);glVertex3f(p.x,p.y,p.z);
                glTexCoord2d(3,0.0);glVertex3f(p.x+f*(n.x),p.y+f*(n.y),p.z+f*(n.z));
                glTexCoord2d(3,1);glVertex3f(p1.x+f*(n1.x),p1.y+f*(n1.y),p1.z+f*(n1.z));
                glTexCoord2d(0.0,1);glVertex3f(p1.x,p1.y,p1.z);
                
                glColor3f(1.0, 1.0, 1.0);
                glTexCoord2d(0.0,0.0);glVertex3f(p.x,p.y,p.z-0.003);
                glTexCoord2d(3.0,0.0);glVertex3f(p.x+f*(n.x),p.y+f*(n.y),p.z+f*(n.z)-0.003);
                glTexCoord2d(3.0,1.0);glVertex3f(p1.x+f*(n1.x),p1.y+f*(n1.y),p1.z+f*(n1.z)-0.003);
                glTexCoord2d(0.0,1.0);glVertex3f(p1.x,p1.y,p1.z-0.003);
                
                glColor3f(0.8, 0.5, 0.0);
                glTexCoord2d(0.0,0.0);glVertex3f(p.x,p.y,p.z);
                glTexCoord2d(3.0,0.0);glVertex3f(p.x+f*(n.x),p.y+f*(n.y),p.z+f*(n.z));
                glTexCoord2d(3.0,1.0);glVertex3f(p.x+f*(n.x),p.y+f*(n.y),p.z+f*(n.z)-0.003);
                glTexCoord2d(0.0,1.0);glVertex3f(p.x,p.y,p.z-0.003);
                
                glColor3f(1.0, 1.0, 1.0);
                glTexCoord2d(0.0,0.0);glVertex3f(p1.x,p1.y,p1.z);
                glTexCoord2d(3.0,0.0);glVertex3f(p1.x+f*(n1.x),p1.y+f*(n1.y),p1.z+f*(n1.z));
                glTexCoord2d(3.0,1.0);glVertex3f(p1.x+f*(n1.x),p1.y+f*(n1.y),p1.z+f*(n1.z)-0.003);
                glTexCoord2d(0.0,1.0);glVertex3f(p1.x,p1.y,p1.z-0.003);
                
                
            }
        }
    }
    glEnd();
    glDisable(GL_TEXTURE_2D);
}
void displayRollerCoaster()
{
    //DISPLAY THE ROLLER COASTER
    float a=0.01,f=0.1;
    texture = LoadTexture("wood.jpg",256, 256);
    glEnable(GL_TEXTURE_2D);
    for(float r=0;r<2;r++){
        glBegin(GL_QUADS);
        for (int j = 0; j < g_iNumOfSplines; j++) {
            for(int i=-2;i<g_Splines[j].numControlPoints-1;i++)
            {
                for(float u=0.0;u<1.0;u+=0.02){
                    //Point
                    p.x= CatmullRom(u, g_Splines[j].points[i].x, g_Splines[j].points[i+1].x, g_Splines[j].points[i+2].x, g_Splines[j].points[i+3].x);
                    p.y= CatmullRom(u, g_Splines[j].points[i].y, g_Splines[j].points[i+1].y, g_Splines[j].points[i+2].y, g_Splines[j].points[i+3].y);
                    p.z= CatmullRom(u, g_Splines[j].points[i].z, g_Splines[j].points[i+1].z, g_Splines[j].points[i+2].z, g_Splines[j].points[i+3].z);
                    //Tangent
                    t.x= CatmullRomTangent(u, g_Splines[j].points[i].x, g_Splines[j].points[i+1].x, g_Splines[j].points[i+2].x, g_Splines[j].points[i+3].x);
                    t.y= CatmullRomTangent(u, g_Splines[j].points[i].y, g_Splines[j].points[i+1].y, g_Splines[j].points[i+2].y, g_Splines[j].points[i+3].y);
                    t.z= CatmullRomTangent(u, g_Splines[j].points[i].z, g_Splines[j].points[i+1].z, g_Splines[j].points[i+2].z, g_Splines[j].points[i+3].z);
                    t = normalize(t);
                    //Normal
                    n = CrossProduct(t, v);
                    n = normalize(n);
                    //Binormal
                    b = CrossProduct(t, n);
                    b = normalize(b);
                    
                    p1.x= CatmullRom(u+0.02, g_Splines[j].points[i].x, g_Splines[j].points[i+1].x, g_Splines[j].points[i+2].x, g_Splines[j].points[i+3].x);
                    p1.y= CatmullRom(u+0.02, g_Splines[j].points[i].y, g_Splines[j].points[i+1].y, g_Splines[j].points[i+2].y, g_Splines[j].points[i+3].y);
                    p1.z= CatmullRom(u+0.02, g_Splines[j].points[i].z, g_Splines[j].points[i+1].z, g_Splines[j].points[i+2].z, g_Splines[j].points[i+3].z);
                    
                    t1.x= CatmullRomTangent(u+0.02, g_Splines[j].points[i].x, g_Splines[j].points[i+1].x, g_Splines[j].points[i+2].x, g_Splines[j].points[i+3].x);
                    t1.y= CatmullRomTangent(u+0.02, g_Splines[j].points[i].y, g_Splines[j].points[i+1].y, g_Splines[j].points[i+2].y, g_Splines[j].points[i+3].y);
                    t1.z= CatmullRomTangent(u+0.02, g_Splines[j].points[i].z, g_Splines[j].points[i+1].z, g_Splines[j].points[i+2].z, g_Splines[j].points[i+3].z);
                    t1 = normalize(t1);
                    
                    n1 = CrossProduct(t1, v);
                    n1 = normalize(n1);
                    b1 = CrossProduct(t1, n1);
                    b1 = normalize(b1);
                    
                    if(r==1)
                    {
                        p.x+=f*n.x;
                        p.y+=f*n.y;
                        p.z+=f*n.z;
                        
                        p1.x+=f*n1.x;
                        p1.y+=f*n1.y;
                        p1.z+=f*n1.z;
                        
                    }
                    
                    glColor3f(1.0, 0.0, 0.0);
                    //V1
                    glTexCoord2d(0.0,0.0);glVertex3f(p.x+a*(n.x-b.x),p.y+a*(n.y-b.y),p.z+a*(n.z-b.z));
                    //V2
                    glTexCoord2d(1.0,0.0);glVertex3f(p.x+a*(n.x+b.x),p.y+a*(n.y+b.y),p.z+a*(n.z+b.z));
                    //V3
                    glTexCoord2d(1.0,1.0);glVertex3f(p.x+a*(-n.x+b.x),p.y+a*(-n.y+b.y),p.z+a*(-n.z+b.z));
                    //V4
                    glTexCoord2d(0.0,1.0);glVertex3f(p.x+a*(-n.x-b.x),p.y+a*(-n.y-b.y),p.z+a*(-n.z-b.z));
                    
                    //V5
                    glTexCoord2d(0.0,0.0);glVertex3f(p1.x+a*(n1.x-b1.x),p1.y+a*(n1.y-b1.y),p1.z+a*(n1.z-b1.z));
                    //V6
                    glTexCoord2d(1.0,0.0);glVertex3f(p1.x+a*(n1.x+b1.x),p1.y+a*(n1.y+b1.y),p1.z+a*(n1.z+b1.z));
                    //V7
                    glTexCoord2d(1.0,1.0);glVertex3f(p1.x+a*(-n1.x+b1.x),p1.y+a*(-n1.y+b1.y),p1.z+a*(-n1.z+b1.z));
                    //V8
                    glTexCoord2d(0.0,1.0);glVertex3f(p1.x+a*(-n1.x-b1.x),p1.y+a*(-n1.y-b1.y),p1.z+a*(-n1.z-b1.z));
                    
                    //V1
                    glTexCoord2d(0.0,0.0);glVertex3f(p.x+a*(n.x-b.x),p.y+a*(n.y-b.y),p.z+a*(n.z-b.z));
                    //V4
                    glTexCoord2d(0.0,1.0);glVertex3f(p.x+a*(-n.x-b.x),p.y+a*(-n.y-b.y),p.z+a*(-n.z-b.z));
                    //V8
                    glTexCoord2d(0.0,1.0);glVertex3f(p1.x+a*(-n1.x-b1.x),p1.y+a*(-n1.y-b1.y),p1.z+a*(-n1.z-b1.z));
                    //V5
                    glTexCoord2d(0.0,0.0);glVertex3f(p1.x+a*(n1.x-b1.x),p1.y+a*(n1.y-b1.y),p1.z+a*(n1.z-b1.z));
                    
                    //V3
                    glTexCoord2d(1.0,1.0);glVertex3f(p.x+a*(-n.x+b.x),p.y+a*(-n.y+b.y),p.z+a*(-n.z+b.z));
                    //V4
                    glTexCoord2d(0.0,1.0);glVertex3f(p.x+a*(-n.x-b.x),p.y+a*(-n.y-b.y),p.z+a*(-n.z-b.z));
                    //V8
                    glTexCoord2d(0.0,1.0);glVertex3f(p1.x+a*(-n1.x-b1.x),p1.y+a*(-n1.y-b1.y),p1.z+a*(-n1.z-b1.z));
                    //V7
                    glTexCoord2d(1.0,1.0);glVertex3f(p1.x+a*(-n1.x+b1.x),p1.y+a*(-n1.y+b1.y),p1.z+a*(-n1.z+b1.z));
                    
                    //V2
                    glTexCoord2d(1.0,0.0);glVertex3f(p.x+a*(n.x+b.x),p.y+a*(n.y+b.y),p.z+a*(n.z+b.z));
                    //V3
                    glTexCoord2d(1.0,1.0);glVertex3f(p.x+a*(-n.x+b.x),p.y+a*(-n.y+b.y),p.z+a*(-n.z+b.z));
                    //V7
                    glTexCoord2d(1.0,1.0);glVertex3f(p1.x+a*(-n1.x+b1.x),p1.y+a*(-n1.y+b1.y),p1.z+a*(-n1.z+b1.z));
                    //V6
                    glTexCoord2d(1.0,0.0);glVertex3f(p1.x+a*(n1.x+b1.x),p1.y+a*(n1.y+b1.y),p1.z+a*(n1.z+b1.z));
                    
                    //V1
                    glTexCoord2d(0.0,0.0);glVertex3f(p.x+a*(n.x-b.x),p.y+a*(n.y-b.y),p.z+a*(n.z-b.z));
                    //V2
                    glTexCoord2d(1.0,0.0);glVertex3f(p.x+a*(n.x+b.x),p.y+a*(n.y+b.y),p.z+a*(n.z+b.z));
                    //V6
                    glTexCoord2d(1.0,0.0);glVertex3f(p1.x+a*(n1.x+b1.x),p1.y+a*(n1.y+b1.y),p1.z+a*(n1.z+b1.z));
                    //V5
                    glTexCoord2d(0.0,0.0);glVertex3f(p1.x+a*(n1.x-b1.x),p1.y+a*(n1.y-b1.y),p1.z+a*(n1.z-b1.z));
                }
            }
        }
        glEnd();
    }
    glDisable(GL_TEXTURE_2D);
}