此类是一个全景摄像机视角,书上介绍了详细原理。直接给实现代码。

类声明:

#pragma once
#ifndef __SPHERICAL_HEADER__
#define __SPHERICAL_HEADER__ #include "camera.h" class Spherical :public Camera {
public:
Spherical();
~Spherical();
Spherical(const Spherical& sp);
void set_fov(const ldouble hfov, const ldouble vfov);
void set_angle(const ldouble deg);
Vector3 ray_direction(const Point3& pp, const integer hres, const integer vres, const ldouble s) const;
virtual Camera* clone() const;
virtual void render_scene(World& w);
Spherical& operator=(const Spherical& sp);
private:
ldouble lambda_max, psi_max;
};
#endif

类实现:

#include "pch.h"
#include "spherical.h"
#include "../utilities/world.h"
#include "../utilities/viewplane.h"
#include "../samplers/sampler.h"
#include "../tracers/tracer.h" Spherical::Spherical() :Camera(), lambda_max(180), psi_max(180) {} Spherical::~Spherical() {} Spherical::Spherical(const Spherical& sp) : Camera(sp), lambda_max(sp.lambda_max), psi_max(sp.psi_max) {} void Spherical::set_fov(const ldouble hfov, const ldouble vfov) {
lambda_max = hfov / 2;
psi_max = vfov / 2;
} void Spherical::set_angle(const ldouble deg) {
ldouble rad = radian(deg);
up = Point3(std::cos(rad) * up.x - std::sin(rad) * up.y,
std::sin(rad) * up.x + std::cos(rad) * up.y, up.z);
} Vector3 Spherical::ray_direction(const Point3& pp, const integer hres, const integer vres, const ldouble s) const {
Point3 pn(2.0 / (s * hres) * pp.x, 2.0 / (s * vres) * pp.y, 0);
ldouble lambda = pn.x * radian(lambda_max),
psi = pn.y * radian(psi_max);
ldouble phi = M_PI - lambda,
theta = 0.5 * M_PI - psi;
ldouble sin_phi = std::sin(phi), cos_phi = std::cos(phi), sin_theta = std::sin(theta), cos_theta = std::cos(theta);
Vector3 dir = sin_theta * sin_phi * u + cos_theta * v + sin_theta * cos_phi * w;
return dir;
} Camera* Spherical::clone() const {
return new Spherical(*this);
} void Spherical::render_scene(World& w) {
Ray ray;
ViewPlane vp(w.vp);
integer depth = 0;
Point3 sp, pp;
w.open_window(vp.hres, vp.vres);
ray.o = eye;
vp.s = 1 / vp.s;
for (integer r = vp.vres - 1; r >= 0; r--)//render from left-corner to right-corner
for (integer c = 0; c < vp.hres; c++) {
RGBColor color;
for (integer p = 0; p < vp.nsamples; p++) {
sp = vp.sampler->sample_unit_square();
pp.x = (c - 0.5 * vp.hres + sp.x) * vp.s;
pp.y = (r - 0.5 * vp.vres + sp.y) * vp.s;
ray.d = ray_direction(pp, vp.hres, vp.vres, vp.s);
color += w.tracer_ptr->trace_ray(ray);
}
color /= vp.nsamples;
color *= exposure_time;
w.display_pixel(r, c, color);
}
} Spherical& Spherical::operator=(const Spherical& sp) {
if (this == &sp)
return*this;
Camera::operator=(sp);
lambda_max = sp.lambda_max;
psi_max = sp.psi_max;
return *this;
}

测试效果图(等之后加了材质后,再看看效果):

Spherical类定义和实现的更多相关文章

  1. Python笔记——类定义

    Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...

  2. 几种常用的JS类定义方法

    几种常用的JS类定义方法   // 方法1 对象直接量var obj1 = {    v1 : "",    get_v1 : function() {        return ...

  3. Js 类定义的几种方式

    提起面向对象我们就能想到类,对象,封装,继承,多态.在<javaScript高级程序设计>(人民邮电出版社,曹力.张欣译.英文名字是:Professional JavaScript for ...

  4. 为什么C++类定义中,数据成员不能被指定为自身类型,但可以是指向自身类型的指针或引用?为什么在类体内可以定义将静态成员声明为其所属类的类型呢 ?

    static的成员变量,不是存储在Bar实例之中的,因而不会有递归定义的问题. 类声明: class Screen: //Screen类的声明 1 类定义: class Screen{ //Scree ...

  5. C++学了这么多年,你也许不知道为什么类定义要放在.h文件,类实现放在cpp文件。它们如何关联?

    原文  http://blog.csdn.net/ithzhang/article/details/8119286 主题 C++  C++学了这么多年你知道为什么定义类时,类的定义放在.h文件中,而类 ...

  6. YTU 2602: 熟悉题型——类设计( 矩形类定义【C++】)

    2602: 熟悉题型--类设计( 矩形类定义[C++]) 时间限制: 1 Sec  内存限制: 128 MB 提交: 183  解决: 119 题目描述 定义一个矩形类,数据成员包括左下角和右上角坐标 ...

  7. Objective-c 类接口 (@interface) (类定义)

    在Objective-c中如何定义一个类呢?我们可以使用下面的格式进行表示: @interface 类名:父类名{ 变量定义; } 方法定义: @end; 下面给出一个实例: @interface P ...

  8. 只能从脚本中调用在类定义上有[ScriptService]属性的Web服务问题的解决方案

    ajax调用webservice中的接口时, 会出现[只能从脚本中调用在类定义上有[ScriptService]属性的...]的异常. 这是因为, 在.net3.5中, 访问web服务, 要对web服 ...

  9. 开涛spring3(12.4) - 零配置 之 12.4 基于Java类定义Bean配置元数据

    12.4  基于Java类定义Bean配置元数据 12.4.1  概述 基于Java类定义Bean配置元数据,其实就是通过Java类定义Spring配置元数据,且直接消除XML配置文件. 基于Java ...

随机推荐

  1. 每天一个 HTTP 状态码 203

    203 Non-Authoritative Information 203 Non-Authoritative Information 'Non-Authoritative Informative' ...

  2. Abp Vnext源码解析系列文章01---EventBus

    一.简介 BP vNext 封装了两种事件总线结构,第一种是 ABP vNext 自己实现的本地事件总线,这种事件总线无法跨项目发布和订阅.第二种则是分布式事件总线,ABP vNext 自己封装了一个 ...

  3. 【抬杠.NET】如何进行IL代码的开发(续)

    背景 之前写了一篇文 [抬杠.NET]如何进行IL代码的开发 介绍了几种IL代码的开发方式. 创建IL项目 C#项目混合编译IL 使用InlineIL.Fody 使用DynamicMethod(ILG ...

  4. sql语句中 int(1)与int(10)有什么区别?资深开发竟然能理解错

    过完春节该投入战斗了,上班第一天发现了一个挺有意思的知识点给大家分享一下:一直以来的的误区我们都认为了int后面的跟的数字为最大显示宽度会对后面插入的参数会有限制,其实倒不是这样的 # 困惑 最近遇到 ...

  5. 回流&重绘

    浏览器加载解析页面:把HTML解析为DOM树,解析CSS生成CSSOM树,HTML DOM树和CSSOM树组合构建render树,首次触发回流和重绘后将页面结构信息发送给GPU进行绘制渲染. 回流:当 ...

  6. Linux文件拷贝脚本

    在工作中,我们经常遇到要从Linux服务器拷贝日志至本地或者定期清理日志的需求,在服务器上,大型系统的日志是按模块存储的,这就导致日志的文件目录较多且层级不统一.我们从众多的目录手工筛选要下载或者删除 ...

  7. .NET中测试代码运行时间

    更新记录 本文迁移自Panda666原博客,原发布时间:2021年6月29日. 计算代码运行的时间,除了呆萌地用秒表去计时,或者可以通过Visual Studio来查看,还可以在.NET代码中使用St ...

  8. BUUCTF-后门查杀

    后门查杀 后门查杀这种题最好还是整个D盾直接扫描目录方便. 查看文件得到flag

  9. Vue回炉重造之封装一个实用的人脸识别组件

    前言 人脸识别技术现在越来越火,那么我们今天教大家实现一个人脸识别组件. 资源 element UI Vue.js tracking-min.js face-min.js 源码 由于我们的电脑有的有摄 ...

  10. 【python基础】第04回 变量常量

    本章内容概要 1. python 语法注释 2. python 语法之变量常量 3. python 基本数据类型(整型(int),浮点型(float),字符串(str)) 本章内容详解 1. pyth ...