📄 camera.cpp
字号:
/* Copyright (c) 2007 Massachusetts Institute of Technology * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *//** * camera.cpp - Implementation for routines of the Camera class * @author Brian Sweatt * * As most of these methods are small, inlining may seem to be a natural choice to raise performance. * However, these methods are called at most once per frame and thus, will not show any appreciable * increase in performance over a frame if inlined. */extern "C" {#include <cross_product3_v.h>}#include "camera.h"#include <math.h>#include <stdio.h>Camera :: Camera(const vector float &c, const vector float &d, const vector float &u, float a){ vector float direction = _normalize3(d); vector float center = c; vector float horizontal = _normalize3(_cross_product3(direction, u)); float tanAngle2 = 2.0f * tanf(a / 2.0f); vector unsigned char splat_word_0 = (vector unsigned char){0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3}; vector unsigned char splat_word_1 = (vector unsigned char){4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7}; vector unsigned char splat_word_2 = (vector unsigned char){8, 9,10,11, 8, 9,10,11, 8, 9,10,11, 8, 9,10,11}; Hx = spu_shuffle(horizontal, horizontal, splat_word_0); Hy = spu_shuffle(horizontal, horizontal, splat_word_1); Hz = spu_shuffle(horizontal, horizontal, splat_word_2); vector float up = _normalize3(_cross_product3(horizontal, direction)); Cx = spu_shuffle(center, center, splat_word_0); Cy = spu_shuffle(center, center, splat_word_1); Cz = spu_shuffle(center, center, splat_word_2); Ux = spu_shuffle(up, up, splat_word_0); Uy = spu_shuffle(up, up, splat_word_1); Uz = spu_shuffle(up, up, splat_word_2); size = spu_splats(tanAngle2); vector float negHalfSize = spu_mul(size, spu_splats(-0.5f)); vector float lowerLeft = spu_add(up, horizontal); vector float vCenter = spu_add(center, direction); lowerLeft = spu_madd(lowerLeft, negHalfSize, vCenter); xLL = spu_shuffle(lowerLeft, lowerLeft, splat_word_0); yLL = spu_shuffle(lowerLeft, lowerLeft, splat_word_1); zLL = spu_shuffle(lowerLeft, lowerLeft, splat_word_2); printf("Camera center: %f %f %f\n", spu_extract(Cx, 0), spu_extract(Cy, 0), spu_extract(Cz, 0));}void Camera::setCenter(const vector float ¢er) { vector unsigned char splat_word_0 = (vector unsigned char){0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3}; vector unsigned char splat_word_1 = (vector unsigned char){4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7}; vector unsigned char splat_word_2 = (vector unsigned char){8, 9,10,11, 8, 9,10,11, 8, 9,10,11, 8, 9,10,11}; vector float newcx = spu_shuffle(center, center, splat_word_0); vector float newcy = spu_shuffle(center, center, splat_word_1); vector float newcz = spu_shuffle(center, center, splat_word_2); vector float deltax = spu_sub(newcx, Cx); vector float deltay = spu_sub(newcy, Cy); vector float deltaz = spu_sub(newcz, Cz); Cx = newcx; Cy = newcy; Cz = newcz; xLL = spu_add(xLL, deltax); yLL = spu_add(yLL, deltay); zLL = spu_add(zLL, deltaz); //printf("Camera center: %f %f %f\n", spu_extract(center, 0), spu_extract(center, 1), spu_extract(center, 2));}vector float Camera::getDirection() { vector float dx, dy, dz; _cross_product3_v(&dx, &dy, &dz, Ux, Uy, Uz, Hx, Hy, Hz); return (vector float) {spu_extract(dx, 0), spu_extract(dy, 0), spu_extract(dz, 0), 0};}void Camera::setDirection(const vector float &d, const vector float &u) { vector float direction = _normalize3(d); //vector unsigned int usame = spu_cmpeq(newU, (vector foat) {0.0f, 0.0f, 0.0f, 0.0f}); // Use the currently stored values for the center (this will not change) and up (this may change) //vector float u = (vector float) {spu_extract(Ux, 0), spu_extract(Uy, 0), spu_extract(Uz, 0), 0}; //u = spu_sel(newU, u, usame); vector float center = (vector float) {spu_extract(Cx, 0), spu_extract(Cy, 0), spu_extract(Cz, 0), 0}; vector float horizontal = _normalize3(_cross_product3(direction, u)); vector unsigned char splat_word_0 = (vector unsigned char){0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3}; vector unsigned char splat_word_1 = (vector unsigned char){4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7}; vector unsigned char splat_word_2 = (vector unsigned char){8, 9,10,11, 8, 9,10,11, 8, 9,10,11, 8, 9,10,11}; Hx = spu_shuffle(horizontal, horizontal, splat_word_0); Hy = spu_shuffle(horizontal, horizontal, splat_word_1); Hz = spu_shuffle(horizontal, horizontal, splat_word_2); vector float up = _normalize3(_cross_product3(horizontal, direction)); Ux = spu_shuffle(up, up, splat_word_0); Uy = spu_shuffle(up, up, splat_word_1); Uz = spu_shuffle(up, up, splat_word_2); vector float negHalfSize = spu_mul(size, spu_splats(-0.5f)); vector float lowerLeft = spu_add(up, horizontal); vector float vCenter = spu_add(center, direction); lowerLeft = spu_madd(lowerLeft, negHalfSize, vCenter); xLL = spu_shuffle(lowerLeft, lowerLeft, splat_word_0); yLL = spu_shuffle(lowerLeft, lowerLeft, splat_word_1); zLL = spu_shuffle(lowerLeft, lowerLeft, splat_word_2);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -