• Camera类定义和实现


    类声明:

    #pragma once
    #ifndef __CAMERA_HEADER__
    #define __CAMERA_HEADER__
    
    #include "../utilities/geometry.h"
    
    class World;
    
    class Camera {
    public:
    	Camera();
    	Camera(const Camera& cam);
    	~Camera();
    	void set_eye(const Point3& p);
    	void set_lookat(const Point3& p);
    	void set_up(const Vector3& v);
    	void set_roll(const ldouble a);
    	void set_exposure_time(const ldouble t);
    	void compute_uvw();
    	virtual Camera* clone() const = 0;
    	virtual void render_scene(World& w) = 0;
    	Camera& operator=(const Camera& cam);
    protected:
    	Point3 eye, lookat;
    	Vector3 up, u, v, w;
    	ldouble exposure_time, ra;
    };
    
    #endif  

    类实现:

    #include "pch.h"
    #include "camera.h"
    
    Camera::Camera()
    	:eye(0, 0, 1), lookat(0), ra(0), up(0, 1, 0), u(1, 0, 0), v(0, 1, 0), w(0, 0, 1), exposure_time(1) {}
    
    Camera::Camera(const Camera& cam)
    	: eye(cam.eye), lookat(cam.lookat), ra(cam.ra), up(cam.up), 
    	u(cam.u), v(cam.v), w(cam.w), exposure_time(cam.exposure_time) {}
    
    Camera::~Camera() {}
    
    void Camera::set_eye(const Point3& p) {
    	eye = p;
    }
    
    void Camera::set_lookat(const Point3& p) {
    	lookat = p;
    }
    
    void Camera::set_up(const Vector3& v) {
    	up = v;
    }
    
    void Camera::set_roll(const ldouble a) {
    	ra = a;
    }
    
    void Camera::set_exposure_time(const ldouble t) {
    	exposure_time = t;
    }
    
    void Camera::compute_uvw() {
    	w = eye - lookat;
    	w.normalize();
    	u = up ^ w;
    	u.normalize();
    	v = w ^ u;
    	if (eye.x == lookat.x && eye.z == lookat.z && eye.y > lookat.y) { // camera looking vertically down
    		u = Vector3(0, 0, 1);
    		v = Vector3(1, 0, 0);
    		w = Vector3(0, 1, 0);
    	}
    	if (eye.x == lookat.x && eye.z == lookat.z && eye.y < lookat.y) { // camera looking vertically up
    		u = Vector3(1, 0, 0);
    		v = Vector3(0, 0, 1);
    		w = Vector3(0, -1, 0);
    	}
    }
    
    Camera& Camera::operator=(const Camera& cam) {
    	if (this == &cam)
    		return *this;
    	eye = cam.eye;
    	lookat = cam.lookat;
    	ra = cam.ra;
    	up = cam.up;
    	u = cam.u;
    	v = cam.v;
    	w = cam.w;
    	exposure_time = cam.exposure_time;
    	return *this;
    }
    

     

  • 相关阅读:
    迭代器和生成器
    小练习函数
    python学习(函数)
    python学习(文件的操作)
    【Python基础】数据结构
    【Python基础】break 和 continue 语句, 以及循环中的 else 子句
    【Python算法】汉诺塔
    【Python基础】序列(字符串、列表、元组)
    【Python基础】正则表达式
    【Python练习】分类数据
  • 原文地址:https://www.cnblogs.com/dalgleish/p/12650806.html
Copyright © 2020-2023  润新知