Voxel
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Macros Pages
renderer_state_host.h
1 #pragma once
2 
3 #ifndef RENDERER_STATE_HOST_H
4 #define RENDERER_STATE_HOST_H
5 
6 
7 
8 
9 #include "renderer_state_base.h"
10 #include "renderer_detail_trace.h"
11 #include "renderer_detail_normal_mrf.h"
12 #include "renderer_camera.h"
13 #include "synchronized.h"
14 #include "platform.h"
15 #include "octree.h"
16 #include "vector.h"
17 #include "helpers.h"
18 #include "settings.h"
19 
20 #include <array>
21 #include <algorithm>
22 #include <limits>
23 #include <cmath>
24 
25 #include <assert.h>
26 #include <stdint.h>
27 
28 
29 
30 
31 namespace Renderer {
32 
33 
34 
35 
36 /*==============================================================================
37  State< PLATFORM_HOST > specialization
38 ==============================================================================*/
39 
40 
41 template<>
42 struct State< PLATFORM_HOST > {
43 
44  inline State();
45 
46 
47  template< int t_ColorFormat, int t_Platform >
48  void TraceScreen(
49  uint8_t* const colors,
50  Vector< unsigned int, 2 > const dimension, // width, height
51  unsigned int const pitch, // bytes per row
52  Synchronized::Heap< t_Platform > const& heap, // heap containing the octree
53  Synchronized::TextureHeap< t_ColorFormat, t_Platform > const& textureHeap, // texture heap containing the octree's textures
54  typename Synchronized::Heap< t_Platform >::template Pointer< Octree::Node< t_ColorFormat, t_Platform > > const& pRoot, // root of the octree
55  Camera const& camera // camera matrix (x,y,z vectors, and position)
56  ) const;
57 };
58 
59 
60 
61 
62 /*==============================================================================
63  State< PLATFORM_HOST > methods
64 ==============================================================================*/
65 
66 
67 State< PLATFORM_HOST >::State() {
68 }
69 
70 
71 template< int t_ColorFormat, int t_Platform >
72 void State< PLATFORM_HOST >::TraceScreen(
73  uint8_t* const colors,
74  Vector< unsigned int, 2 > const dimension,
75  unsigned int const pitch,
76  Synchronized::Heap< t_Platform > const& heap,
77  Synchronized::TextureHeap< t_ColorFormat, t_Platform > const& textureHeap,
78  typename Synchronized::Heap< t_Platform >::template Pointer< Octree::Node< t_ColorFormat, t_Platform > > const& pRoot,
79  Camera const& camera
80 ) const
81 {
82  Vector< double, 3 > const& xx = camera.VectorX();
83  Vector< double, 3 > const& yy = camera.VectorY();
84  Vector< double, 3 > const& zz = camera.VectorZ();
85  Vector< double, 3 > const& position = camera.Position();
86  Vector< double, 2 > const fieldOfView = dimension * ( camera.Zoom() / dimension.Maximum() );
87 
88  double const spread = 2 * ( fieldOfView / dimension ).Minimum(); // x2 because for fov=1, the screen is 2 units wide for every 1 unit of distance
89 
90  std::unique_ptr< float[] > depths( new float[ dimension.Product() ] );
91 
92  #pragma omp parallel for
93  for ( int ii = 0; ii < static_cast< int >( dimension[ 1 ] ); ++ii ) {
94 
95  unsigned int const offset = ii * pitch;
96  for ( int jj = 0; jj < static_cast< int >( dimension[ 0 ] ); ++jj ) {
97 
98  Vector< int, 2 > const coordinates = { { jj, ii } };
99  Vector< double, 2 > const direction = ( ( coordinates + 0.5 ) * 2 / dimension - 1 ) * fieldOfView;
100  Vector< double, 3 > const ray = xx * direction[ 0 ] - yy * direction[ 1 ] - zz;
101 
102  uint8_t* const colorDestination = colors + offset + jj * 4;
103  colorDestination[ 0 ] = 0x00;
104  colorDestination[ 1 ] = 0x00;
105  colorDestination[ 2 ] = 0x00;
106  colorDestination[ 3 ] = 0xff;
107 
108  float* const depthDestination = depths.get() + coordinates[ 1 ] * dimension[ 0 ] + coordinates[ 0 ];
109  *depthDestination = std::numeric_limits< float >::infinity();
110 
111  detail::Trace< t_ColorFormat, t_Platform >( colorDestination, depthDestination, pRoot, spread, position, ray );
112  }
113  }
114 }
115 
116 
117 
118 
119 } // namespace Renderer
120 
121 
122 
123 
124 #endif // RENDERER_STATE_HOST_H