-
Notifications
You must be signed in to change notification settings - Fork 0
/
Renderer.cpp
119 lines (111 loc) · 2.82 KB
/
Renderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "StdAfx.h"
#include "Renderer.h"
Renderer::Renderer()
{
camera.setScene(&scene);
configManager = NULL;
mcRenderer = NULL;
}
void Renderer::preview()
{
IplImage* image = cvCreateImage(cvSize(camera.width, camera.height), IPL_DEPTH_32F, 3);
vector<vec3f> eyeRays = camera.generateRays();
#pragma omp parallel for
for(int x=0; x<camera.width; x++)
{
class SmoothNormal : public KDTree::Condition
{
public:
Scene* scene;
virtual bool legal(const KDTree::Ray& ray, const KDTree::Triangle& tri, const float dist) const
{
SceneObject *intersectObject = scene->objects[((Scene::ObjSourceInformation*)tri.sourceInformation)->objID];
unsigned fi = ((Scene::ObjSourceInformation*)tri.sourceInformation)->triangleID;
bool in = ray.direction.dot(intersectObject->getWorldNormal(fi, ray.origin + ray.direction*dist))<0;
return in;
}
} condition;
for(unsigned y=0; y<camera.height; y++)
{
condition.scene = &scene;
Ray ray;
ray.direction = eyeRays[y*camera.width + x];
ray.origin = camera.position;
Scene::ObjSourceInformation osi;
float dist = scene.intersect(ray, osi, &condition);
vec3f normal = dist >= 0 ? scene.objects[osi.objID]->getWorldNormal(osi.triangleID, ray.origin + ray.direction*dist) : vec3f(0, 0, 0);
((vec3f*)image->imageData)[y*camera.width + x] = vec3f(1, 1, 1) * abs(ray.direction.dot(normal));
}
}
cvShowImage("Renderer", image);
cvReleaseImage(&image);
}
void Renderer::render()
{
scene.buildObjKDTrees();
vector<vec3f> pixelColors = mcRenderer->renderPixels(camera);
}
void Renderer::waitForCommand()
{
int key = cvWaitKey(10);
loadConfig("Data/Config.xml");
//while (key != 'q')
//{
// preview();
// key = cvWaitKey(10);
//}
//cvDestroyAllWindows();
render();
/*
while(key != 'q')
{
switch(key)
{
case 'p':
loadConfig("Data/Config.xml");
preview();
break;
case 'r':
render();
break;
}
key = cvWaitKey(10);
}
*/
}
void Renderer::loadConfig(const string& configFilePath)
{
scene.clear();
if(mcRenderer)
delete mcRenderer;
if(configManager)
delete configManager;
mcRenderer = NULL;
configManager = NULL;
configManager = new ConfigManager(this);
configManager->load(configFilePath);
/*
printf("%d\n" , scene.objects.size());
for (int i = 0; i < 3; i++)
{
printf("=================\n");
printf("%d , %d\n" , scene.objects[i]->vertexList.size() ,
scene.objects[i]->faceVertexIndexList.size());
for (int j = 0; j < scene.objects[i]->faceVertexIndexList.size(); j++)
{
printf("%d, %d, %d\n" , scene.objects[i]->faceVertexIndexList[j][0] ,
scene.objects[i]->faceVertexIndexList[j][1] , scene.objects[i]->faceVertexIndexList[j][2]);
}
}
*/
}
Renderer::~Renderer()
{
if(mcRenderer)
delete mcRenderer;
if(configManager)
delete configManager;
mcRenderer = NULL;
configManager = NULL;
cvDestroyAllWindows();
}