Ejemplo n.º 1
0
Vector4f get_color(Vector4f viewer, Vector4f normal, Vector4f intersect){
    Vector4f R = objects[hit_index].ka;
    MatrixXf None(4,2); None<<0,0,0,0,0,0,0,0;
    Ray testshadow;
    MatrixXf is_shadow;
    
    if (pt_light_counter != 0) {
        for (int l = 0; l < pt_light_counter; l++) {
            Vector4f pt_light_xyz = pt_xyz[l];
            Vector4f pt_light_rgb = pt_rgb[l];
            Vector4f light = pt_light_xyz - intersect;
            light.normalize();
            
            testshadow = Ray(intersect, light);
            is_shadow = Find_nearest(testshadow, objects);
            
            if(is_shadow==None){
                Vector4f diffuse = diffuseTerm(kd, pt_light_rgb, normal, light);
                
                Vector4f specular = specularTerm(ks, pt_light_rgb, normal, light, viewer, p);
                
                Vector4f ambient = times(ka,pt_light_rgb);
                
                R = R + (diffuse + specular + ambient);
            }
        }
    }
    
    if (dl_light_counter != 0) {
        for (int l = 0; l < dl_light_counter; l++) {
            Vector4f dl_light_rgb = dl_rgb[l];
            Vector4f light = dl_xyz[l];
            light.normalize();
            
            testshadow = Ray(intersect, light);
            is_shadow = Find_nearest(testshadow, objects);
            
            if(is_shadow==None){
                
                Vector4f diffuse = diffuseTerm(kd, dl_light_rgb, normal, light);
                
                Vector4f specular = specularTerm(ks, dl_light_rgb, normal, light, viewer, p);
                
                Vector4f ambient = times(ka,dl_light_rgb);
                
                R = R + (diffuse + specular + ambient);
            }
            
        }
    }
    return R;
};
	void DeferredDirectionalLighting::bind(const DirectionalLight& light, const Matrix4f& modelView)
	{
		_program.bind();
		
		//tell the shader about uniforms
		GLint program = _program.getProgram();


		Vector3f lightDir = light.getDirection();

		//light in view space
		Vector4f lightDirection(lightDir.x(),
								lightDir.y(),
								lightDir.z(),
								0);
		Vector4f lightDirectionView = (modelView) * lightDirection;
		lightDirectionView.normalize();
		glUniform3f(glGetUniformLocation(program, "lightdir"),
					lightDirectionView.x(),
					lightDirectionView.y(),
					lightDirectionView.z());
	}
Ejemplo n.º 3
0
bool Quaternion::Update(){
	if(_mOmega->getIsValided()){
		Vector3f omega = _mOmega->getOmega() * MathTools::RADIAN_PER_DEGREE;
		Vector4f q;
		q << 0, omega[0], omega[1], omega[2];
		Vector4f t;
		t[0] = -q[1]*_Quaternion[1]-q[2]*_Quaternion[2]-q[3]*_Quaternion[3];
		t[1] = q[1]*_Quaternion[0]+q[2]*_Quaternion[3]-q[3]*_Quaternion[2];
		t[2] = -q[1]*_Quaternion[3]+q[2]*_Quaternion[0]+q[3]*_Quaternion[1];
		t[3] = q[1]*_Quaternion[2]-q[2]*_Quaternion[1]+q[3]*_Quaternion[0];
		Interval = App::mApp->mTicks->getTicks() - PrevTick;
		PrevTick = App::mApp->mTicks->getTicks();
		Interval /= 1000.0f;
		if(Interval <= 0){
			Valid = false;
			return false;
		}
		t *= 0.5f * Interval;
		q = _Quaternion + 0.5f * (t + PrevT);
		PrevT = t;
		q.normalize();
		Vector3f e = QuaternionToEuler(q);
		bool valid = true;
		bool AccValid = _mAcceleration->getIsValided() && (_mAcceleration->getAcc().norm() > Acceleration::Gravity * 0.95f) && (_mAcceleration->getAcc().norm() < Acceleration::Gravity * 1.05f);
		Vector3f angle;
		bool MagValid;

		if(IsUseCompass){
			MagValid = _mCompass->getIsValided() && _mCompass->getMag().norm() > 0.9f && _mCompass->getMag().norm() < 1.1f;
		}
		if(AccValid){
			angle = _mAcceleration->getAngle();
			if(IsUseCompass && MagValid){
				angle[2] = _mCompass->getAngle()[2];
			}
			else if(IsUseEncoderYaw){
				angle[2] = _mEncoderYaw->getYaw();
			}
			else{
				angle[2] = e[2];
			}
		}
		else{
			angle[0] = e[0];
			angle[1] = e[1];
			if(IsUseCompass && MagValid){
				angle[2] = _mCompass->getAngle()[2];
			}
			else if(IsUseEncoderYaw){
				angle[2] = _mEncoderYaw->getYaw();
			}
			else{
				angle[2] = e[2];
			}
		}

		Matrix3f A;
		A.setIdentity();
		Matrix3f H;
		H.setIdentity();
		if(valid && AccValid && _QuaternionKalman->Filtering(A, e, H, angle)){
			angle = _QuaternionKalman->getCorrectedData();
			for(int i = 0; i < 3; i++){
				if(angle[i] != angle[i]){
					Valid = false;
					return false;
				}
			}
			if(IsUseEncoderYaw && fabs(fabs(_mEncoderYaw->getYaw()) - MathTools::PI) < 0.01f){
				angle[2] = _mEncoderYaw->getYaw();
			}
			_Euler = angle;
		}
		else{
			_Euler = e;
		}
		_Quaternion = EulerToQuaternion(_Euler);
		return true;
	}
	else{
		Valid = false;
		return false;
	}
}
Ejemplo n.º 4
0
int main(int args, char* argv[]){




    FreeImage_Initialise();

    FIBITMAP *bitmap = FreeImage_Allocate(WIDTH, HEIGHT, BPP);
    RGBQUAD color;

    if (!bitmap)
        exit(1);


    float aspect_ratio = WIDTH/HEIGHT;
    float fovV = fov/180.0f*PI;
    float vertical_offset = tan(fov/2);
    float horizontal_offset = vertical_offset*aspect_ratio;


    Vector4f dd = camara_position + camara_looking_direction
    Vector4f tt 


    // float fovW = fov/360.0f*PI;
    // float fovH = fovW * (float)HEIGHT/(float)WIDTH;
    // float tanFovW = tan(fovW);
    // float tanFovH = tan(fovH);
    // float zNear = -1.0f;
    // float zFar = -1000.0f;

    // printf("problem2\n");


    // used for testing
    test_translate << 1.0f, 0.0f, 0.0f, 0.0f,
                    0.0f, 1.0f, 0.0f, 0.0f, 
                    0.0f, 0.0f, 1.0f, 0.0f,
                    0.0f, 0.0f, 0.0f, 1.0f;
    test_ka = Vector4f(1.0, 0.0, 0.0, 0.0);
    test_center = Vector4f(0.0, 0.0, 0.0, 1.0);
    test_radius = 1.0f;

    test_sphere = Sphere(test_ka, test_translate, test_center, test_radius);

    // printf("problem3\n");

    obj_counter = 1;
    objects.push_back(test_sphere);

    pt_light_counter = 0;
    // pt_xyz[0] = Vector4f(1.0f, 1.0f, 1.0f, 1.0f);
    // pt_rgb[0] = Vector4f(1.0f, 1.0f, 1.0f, 0.0f);

    dl_light_counter = 0;

    // printf("problem4\n");

    bool gogo = true;

    if (gogo){

    for (int i=0; i<WIDTH; i++){
        for (int j=0; j<HEIGHT; j++){
        //construct a ray

            if (i == 0 && j == 0){
                printf("Ray Tracer Initialized, System Rendering. . .\n");
            }

            if (i == 0 && j == HEIGHT/2){
                printf("Ray Tracing Running, 12.5 percent completed. . .\n");
            }

            if (i == 0 && j == HEIGHT){
                printf("Ray Tracing Running, 25 percent completed. . .\n");
            }

            if (i == WIDTH/2 && j == 0){
                printf("Ray Tracing Running, 37.5 percent completed. . .\n");
            }

            if (i == WIDTH/2 && j == HEIGHT/2){
                printf("Ray Tracing Running, 50 percent completed. . .\n");
            }

            if (i == WIDTH/2 && j == HEIGHT){
                printf("Ray Tracing Running, 62.5 percent completed. . .\n");
            }

            if (i == WIDTH && j == 0){
                printf("Ray Tracing Running, 75 percent completed. . .\n");
            }

            if (i == WIDTH && j == HEIGHT/2){
                printf("Ray Tracing Running, 86.5 percent completed. . .\n");
            }

            if (i == WIDTH && j == HEIGHT){
                printf("Ray Tracing Running, 100 percent completed. . .\n");
            }


            float x = ((2.0f * (float) j) - (float) WIDTH) / (float)WIDTH * tanFovW;
            float y = ((2.0f * (float) i) - (float) HEIGHT) / (float)HEIGHT * tanFovH;

            Vector4f target = Vector4f(x, y, zNear, 0);
            target.normalize();
            Ray initialRay = Ray(camara_position, target);
            
            // if ((initialRay.direction)[3] != 0 || (initialRay.origin)[3] != 1){
            //     cout << "\n";
            //     cout << "fuckfuck f**k f**k";
            //     cout << initialRay.origin << endl;
            //     cout << initialRay.direction << endl;
            //     cout << "\n";
            // }
            
            // printf("    sub_problem1\n");
            Vector4f result = trace(initialRay, objects, TraceDepth);
            // printf("    sub_problem2\n");
            color.rgbRed = result[2]*255;
            color.rgbGreen = result[1]*255;
            color.rgbBlue = result[0]*255;

            // if (color.rgbRed != 0 || color.rgbGreen != 0 || color.rgbBlue != 0){
            //     printf("weird, isn't it? but at least you got values");
            // }

            FreeImage_SetPixelColor (bitmap, i, j, &color);
        }
    }
    }

    // printf("problem5\n");

    // delete [] objects;
    
    // printf("problem6\n");


    if (FreeImage_Save(FIF_PNG, bitmap, "test.png", 0))
        cout << "Image successfully saved!" << endl;
    
    FreeImage_DeInitialise();
}