Esempio n. 1
0
void SetUpAndRight()
{
    mRight = STVector3::Cross(mLookAt - mPosition, mUp);
    mRight.Normalize();
    mUp = STVector3::Cross(mRight, mLookAt - mPosition);
    mUp.Normalize();
}
Esempio n. 2
0
void resetUp()
{
    mUp = STVector3(0.f,1.f,0.f);
    mRight = STVector3::Cross(mLookAt - mPosition, mUp);
    mRight.Normalize();
    mUp = STVector3::Cross(mRight, mLookAt - mPosition);
    mUp.Normalize();
}
Esempio n. 3
0
bool Triangle::findPointHit(Ray r, Intersection& intersect, STTransform4 transform) const{
   
    //Transform points according to the scene object transformation
    STPoint3 v11 = transform * v1;
    STPoint3 v21 = transform * v2;
    STPoint3 v31 = transform * v3;
    
    // Using Cramer's rule
    float a = v11.x - v21.x;
    float b = v11.y - v21.y;
    float c = v11.z - v21.z;
    float d = v11.x - v31.x;
    float e = v11.y - v31.y;
    float f = v11.z - v31.z;
    float g = r.getD().x;
    float h = r.getD().y;
    float i = r.getD().z;
    float j = v11.x - r.getE().x;
    float k = v11.y - r.getE().y;
    float l = v11.z - r.getE().z;
    
    float ei_minus_hf = e * i - h * f;
    float gf_minus_di = g * f - d * i;
    float dh_minus_eg = d * h - e * g;
    float ak_minus_jb = a * k - j * b;
    float jc_minus_al = j * c - a * l;
    float bl_minus_kc = b * l - k * c;
    
    float M = a*ei_minus_hf + b*gf_minus_di + c*dh_minus_eg;
    float t = -(f*ak_minus_jb + e*jc_minus_al + d*bl_minus_kc) / M;
    if(t < r.getMinT() || t > r.getMaxT()) return false;
    float gamma = (i*ak_minus_jb + h*jc_minus_al + g*bl_minus_kc) / M;
    if(gamma < 0 || gamma > 1) return false;
    float beta = (j*ei_minus_hf + k*gf_minus_di + l*dh_minus_eg) / M;
    if(beta < 0 || beta > 1 - gamma) return false;
    
    intersect.t = t;
    intersect.intersectionPt = r.getE() + t * r.getD();
    
    //We are getting the (untransformed normal here!)
    STVector3 normal = STVector3::Cross(v2-v1,v3-v2);
    
    //Transform normal into world coordinates according to "transoform"
    // new normal = M^(-T)(normal, 0) (where normal,0 is the normal in 4D
    normal = transform.Inverse().Transpose() * normal;
    normal.Normalize();
    
    intersect.intersectionNormal = normal;
   
    //Shadow bias
    if(t < r.getMinT() || t > r.getMaxT()) return false;
    
    return true;

}
Esempio n. 4
0
void ZoomCamera(float delta_y)
{
    STVector3 direction = mLookAt - mPosition;
    float magnitude = direction.Length();
    direction.Normalize();
    float zoom_rate = 0.1f*magnitude < 0.5f ? .1f*magnitude : .5f;
    if(delta_y * zoom_rate + magnitude > 0)
    {
        mPosition += (delta_y * zoom_rate) * direction;
    }
}
Esempio n. 5
0
STColor3f DirectionalLight::sumTerm(Intersection inter, Material *material, Ray *viewingRay){
    STVector3 incomingLight = -*direction;
    incomingLight.Normalize();
    STVector3 normal = inter.normal;
    STVector3 view = -viewingRay->direction;
    
    STColor3f diffuse = material->diff * (*color) * fmax(0.0, STVector3::Dot(incomingLight, normal));
    STColor3f specular = material->spec * (*color) * pow(fmax(0, STVector3::Dot(view, Utils::reflectVector(normal, incomingLight))), material->shine);
    
    return diffuse + specular;
}
/**Populates qMatrixes based on the planes surrounding each vertex
   in the mesh m - Ankit*/
void QuadricErrorSimplification::generateQMatrices(STTriangleMesh* mesh){
	STVector3 i, j, k;
	STVector3 n;
	float d = 0;
	float area = 0;
	STFace* f;
	STVertex* vert;
	
	for (size_t v = 0; v < mesh->mVertices.size(); ++v)
	{
	    qMatrixes.push_back(new STMatrix4());
	}
	for (size_t v = 0; v < mesh->mVertices.size(); ++v)
	{
	        //memset(m_vertices[v].m_Q, 0, 10 * sizeof(Float));
	        vert = mesh->mVertices[v];
		//find all the faces that contain this vertex
		for (size_t itT = 0; itT < mesh->mFaces.size(); ++itT)
		{
		        f = mesh->mFaces[itT];
			if (vertexEquals(f->v[0], vert) || vertexEquals(f->v[1], vert) || vertexEquals(f->v[2], vert)){
			  i = vertex2Vector3(f->v[0]);
			  j = vertex2Vector3(f->v[1]);
			  k = vertex2Vector3(f->v[2]);
			  n = STVector3::Cross(j - i, k - i);
			  area = n.Length();
			  n.Normalize();
			  d = -(STVector3::Dot(vertex2Vector3(mesh->mVertices[v]), n));
			  
			  qMatrixes[v]->table[0][0] += area * (n.x * n.x);
			  qMatrixes[v]->table[0][1] += area * (n.x * n.y);
			  qMatrixes[v]->table[0][2] += area * (n.x * n.z);
			  qMatrixes[v]->table[0][3] += area * (n.x * d);
			  qMatrixes[v]->table[1][0] += area * (n.y * n.x);
			  qMatrixes[v]->table[1][1] += area * (n.y * n.y);
			  qMatrixes[v]->table[1][2] += area * (n.y * n.z);
			  qMatrixes[v]->table[1][3] += area * (n.y * d);
			  qMatrixes[v]->table[2][0] += area * (n.z * n.x);
			  qMatrixes[v]->table[2][1] += area * (n.z * n.y);
			  qMatrixes[v]->table[2][2] += area * (n.z * n.z);
			  qMatrixes[v]->table[2][3] += area * (n.z * d);
			  qMatrixes[v]->table[3][0] += area * (d * n.x);
			  qMatrixes[v]->table[3][1] += area * (d * n.y);
			  qMatrixes[v]->table[3][2] += area * (d * n.z);
			  qMatrixes[v]->table[3][3] += area * (d * d);
			}
		}
	}
}
Esempio n. 7
0
void Camera::createOrthonormalBasis(const STPoint3& eye, const STVector3& up, const STPoint3& lookAt)
{
    position = eye;
    
    // Create orthonormal basis
    STVector3 a = lookAt - eye;
    STVector3 b = up;
    
    a.Normalize();
    w = a;
    
    u = STVector3::Cross(b, w);
    u.Normalize();
    
    v = STVector3::Cross(w, u);
}
Esempio n. 8
0
void handleInput() {
    
    // this handles setting keyboard booleans for motion
    go_forward = Input.IsKeyDown(sf::Key::W);
    go_backward = Input.IsKeyDown(sf::Key::S);
    go_left = Input.IsKeyDown(sf::Key::A);
    go_right = Input.IsKeyDown(sf::Key::D);
    go_up = Input.IsKeyDown(sf::Key::Space);
    go_down = Input.IsKeyDown(sf::Key::C);
    
    
    // Event loop, for processing user input, etc.  For more info, see:
    // http://www.sfml-dev.org/tutorials/1.6/window-events.php
    sf::Event evt;
    while (window.GetEvent(evt)) {
        
        switch (evt.Type) {
            case sf::Event::Closed: 
                // Close the window.  This will cause the game loop to exit,
                // because the IsOpened() function will no longer return true.
                window.Close(); 
                break;
            case sf::Event::Resized: 
                // If the window is resized, then we need to change the perspective
                // transformation and viewport
                glViewport(0, 0, evt.Size.Width, evt.Size.Height);
                break;
                
                // Mouse Movement
            case sf::Event::MouseMoved:
                if (!mouseInit) {
                    cur_mouse = STPoint2(evt.MouseMove.X, evt.MouseMove.Y);
                    last_mouse = cur_mouse;
                    mouseInit = true;
                }
                else {
                    cur_mouse = STPoint2(evt.MouseMove.X, evt.MouseMove.Y);
                }
                mouse_move = cur_mouse - last_mouse;
                
                h_angle += .01 * mouse_move.x;
                v_angle += .01 * mouse_move.y;
                if (v_angle < 0) {
                    v_angle = 0.01;
                }
                else if (v_angle > MY_PI) {
                    v_angle = MY_PI-.01;
                }
                
                current_forward = STVector3(sin(v_angle)*cos(h_angle),cos(v_angle),sin(v_angle)*sin(h_angle));
                current_forward.Normalize();
                current_right = STVector3::Cross(current_forward, Up);
                current_right.Normalize();
                
                last_mouse = cur_mouse;
                break;
                
                // Key presses
            case sf::Event::KeyPressed:
                switch (evt.Key.Code) {
                    case sf::Key::Escape:
                        window.Close();
                        break;
                        
                    default:
                        break;
                }
                
            default: 
                break;
        }
    }
}
Esempio n. 9
0
STVector3 Sphere::CalcNormal(STVector3 surface_pt, Ray unused){
    STVector3 v = surface_pt - center;
    v.Normalize();
    return v;
}
Esempio n. 10
0
void DrawManipulator(void)
{

    // color
    RGBR_f colorRed(1, 0, 0, 1);
    RGBR_f colorGreen(0, 1, 0, 1);
    RGBR_f colorBlue(0, 0, 1, 1);
    RGBR_f colorWhite(1,1,1,1);
    RGBR_f colorMagenta(1.0f,0.3f,1.0f, 1.0f);
    RGBR_f colorGrey(0.2f,0.2f,0.2f, 1.0f);

    // a, y and z axis
    STVector3 xaxis = STVector3::eX;
    STVector3 yaxis = STVector3::eY;
    STVector3 zaxis = STVector3::eZ;

    // camera basis
    STVector3 up = pScene->GetCamera()->Up();
    STVector3 right = pScene->GetCamera()->Right();
    STVector3 dir = pScene->GetCamera()->LookAt();
    STVector3 front;
    front = front.Cross(right, xaxis);
    front.Normalize();

    // camera plane
    float colx[4] = {1, 0, 0, 1};
    float coly[4] = {0, 1, 0, 1};
    float colz[4] = {0, 0, 1, 1};
    float colall[4] = {1, 1, 1, 1};

    float fct = 0.05f;
    float fct2 = 0.83f;

    STMatrix4 curModelMatrix;
    curModelMatrix.EncodeI();

    //------------------------------------------------------------------------------
    // TO DO: Proj3_4 OpenGL
    // Update the matrix transformation for the manipulator geometry
    // Update curModelMatrix
    //------------------------------------------------------------------------------
    curModelMatrix.EncodeS(0.1, 0.1, 0.1);
    //-------------------------------------------------------------------------------

    // screen projection
    ViewProjectionScreenSpace(curModelMatrix);

    STVector3 origin(0,0,0);
    curModelMatrix.GetT(&origin.x, &origin.y, &origin.z);

    if(pScene->CurrentManipMode() == LOCAL) {
        xaxis.Transform(curModelMatrix);
        yaxis.Transform(curModelMatrix);
        zaxis.Transform(curModelMatrix);
        xaxis.Normalize();
        yaxis.Normalize();
        zaxis.Normalize();
    }


    // Rotations
    if((pScene->CurrentManipGeometryState() == AXIS_ALL) || 
        (pScene->CurrentManipGeometryState() == AXIS_ROTATIONALL)) {


        STVector3 X_UP;
        STVector3 X_RIGHT;
        STVector3 X_FRONT;
        STVector3 X_UP_sc;
        STVector3 X_RIGHT_sc;
        STVector3 X_FRONT_sc;


        STVector3 planenorm(pScene->GetCamera()->Position() - origin);
        planenorm.Normalize();
        STVector4 camplane = vector4(planenorm,0);
        
        // duplicate
        X_RIGHT   =   right    * ScreenFactor();
        X_UP      =   up       * ScreenFactor();
        if(pScene->CurrentManipMotion() == ROTATE_DUPLICATE)
            DrawCircle(origin, colorWhite, X_RIGHT, X_UP);
        else
            DrawCircle(origin, colorGrey, X_RIGHT, X_UP);


        // screen rot
        X_UP_sc    =  up * 1.2f       * ScreenFactor();
        X_RIGHT_sc =  right * 1.2f    * ScreenFactor();
        if(pScene->CurrentManipMotion() == ROTATE_SCREEN) 
            DrawCircle(origin, colorWhite, X_UP_sc, X_RIGHT_sc);
        else 
            DrawCircle(origin, colorMagenta, X_UP_sc, X_RIGHT_sc);


        // x rot
        right.Cross(dir, xaxis);
        right.Normalize();
        front.Cross(right, xaxis);
        front.Normalize();
        X_RIGHT   =   right    * ScreenFactor();
        X_FRONT   =   front    * ScreenFactor();
        if(pScene->CurrentManipMotion() == ROTATE_Y)
            DrawCircleHalf(origin, colorWhite, X_RIGHT, X_FRONT, camplane);
        else
            DrawCircleHalf(origin, colorRed, X_RIGHT, X_FRONT, camplane);


        // y rot
        right.Cross(dir, yaxis);
        right.Normalize();
        front.Cross(right, yaxis);
        front.Normalize();
        X_RIGHT   =   right    * ScreenFactor();
        X_FRONT   =   front    * ScreenFactor();
        if(pScene->CurrentManipMotion() == ROTATE_Y)
            DrawCircleHalf(origin, colorWhite, X_RIGHT, X_FRONT, camplane);
        else
            DrawCircleHalf(origin, colorGreen, X_RIGHT, X_FRONT, camplane);

        // z rot
        right.Cross(dir, zaxis);
        right.Normalize();
        front.Cross(right, zaxis);
        front.Normalize();
        X_RIGHT   =   right    * ScreenFactor();
        X_FRONT   =   front    * ScreenFactor();
        if(pScene->CurrentManipMotion() == ROTATE_Z)
            DrawCircleHalf(origin, colorWhite, X_RIGHT, X_FRONT, camplane);
        else
            DrawCircleHalf(origin, colorBlue, X_RIGHT, X_FRONT, camplane);
    }


    // cam
    if ((pScene->CurrentManipMotion() !=  MANIP_NONE) && ((pScene->CurrentManipMotion() != ROTATE_DUPLICATE)))
        DrawCam(origin, gblXVert*ScreenFactor(), gblYVert *ScreenFactor(), -Ng2);



    // draw translation manupulator
    //-----------------------------------------------
 
    // Translations
    if((pScene->CurrentManipGeometryState() == AXIS_ALL) ||
        (pScene->CurrentManipGeometryState() == AXIS_TRANSXYZ)) {

        DrawQuadrant(origin, 0.5f*ScreenFactor(), (pScene->CurrentManipMotion() == TRANS_XZ), xaxis, zaxis);
        DrawQuadrant(origin, 0.5f*ScreenFactor(), (pScene->CurrentManipMotion() == TRANS_XY), xaxis, yaxis);
        DrawQuadrant(origin, 0.5f*ScreenFactor(), (pScene->CurrentManipMotion() == TRANS_YZ), yaxis, zaxis);

        xaxis *= ScreenFactor();
        yaxis *= ScreenFactor();
        zaxis *= ScreenFactor();


        if(pScene->CurrentManipMotion() == TRANS_X)
            DrawAxis(origin, colorRed, xaxis, yaxis, zaxis, fct, fct2, colall);
        else
            DrawAxis(origin, colorRed, xaxis, yaxis, zaxis, fct, fct2, colx);


        // y axis
        if(pScene->CurrentManipMotion() == TRANS_Y) 
            DrawAxis(origin, colorGreen, yaxis, xaxis, zaxis, fct, fct2, colall);
        else
            DrawAxis(origin, colorGreen, yaxis, xaxis, zaxis, fct, fct2, coly);

    
        // z axis
        if(pScene->CurrentManipMotion() == TRANS_Z) 
            DrawAxis(origin, colorBlue, zaxis, xaxis, yaxis, fct, fct2, colall);
        else
            DrawAxis(origin, colorBlue, zaxis, xaxis, yaxis, fct, fct2, colz);
    }


}