forked from jlwangPoincare/RayTracingEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationEngine.cpp
More file actions
214 lines (190 loc) · 8.25 KB
/
ApplicationEngine.cpp
File metadata and controls
214 lines (190 loc) · 8.25 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// ApplicationEngine.cpp: implementation of the ApplicationEngine class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RayTracingEngine.h"
#include "ApplicationEngine.h"
#include "gz.h"
//#include "GzDisplay.h"
//#include "GzRender.h"
#include <cstdio>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define INFILE "rects"
#define OUTFILE "output.ppm"
// Might be useful. But I think we'll have better way.
//extern int tex_fun(float u, float v, GzColor color); // image texture function
//extern int ptex_fun(float u, float v, GzColor color); // procedural texture function
ApplicationEngine::ApplicationEngine()
{
}
ApplicationEngine::~ApplicationEngine()
{
Clean();
}
int ApplicationEngine::Initialize()
{
// Tokens need to be redesigned.
//GzToken nameListShader[9]; // shader attribute names
//GzPointer valueListShader[9]; // shader attribute pointers
//GzToken nameListLights[10]; // light info
//GzPointer valueListLights[10];
//int shaderType, interpStyle;
//float specpower;
int status(GZ_SUCCESS);
try
{
// m_nWidth and m_nHeight are variables inherited from Application class.
m_nWidth = 256; // frame buffer and display width
m_nHeight = 256; // frame buffer and display height
// Initialize frame buffer and display
status = status || GzNewFrameBuffer(m_pFrameBuffer, m_nWidth, m_nHeight);
status = status || GzNewDisplay(m_pDisplay, m_nWidth, m_nHeight);
GzCamera *p_camera = new GzCamera(GzVector3(0.0f, 10.0f, -10.0f), GzVector3(0.0f, 5.0f, 0.0f), GzVector3(0.0f, 1.0f, 0.0f), 116.0f);
GzLight **g_lights = new GzLight*[1];
//g_lights[0] = new GzLight(GzVector3(-15.0f, 25.0f, 5.0f), GzVector3(0, 0, -10.0f), GzVector3(0, -10.0f, 0.0f));
//g_lights[0] = new GzLight(DIR_LIGHT, GzVector3(0.0f, 20.0f, 0.0f), GzColor::WHITE);
//g_lights[0] = new GzLight(DIR_LIGHT, GzVector3(0, 1.0f, 0), GzColor::WHITE);
g_lights[0] = new GzLight(POINT_LIGHT, GzVector3(-15, 20.0f, 0), GzColor::WHITE);
//g_lights[1] = new GzLight(POINT_LIGHT, GzVector3(-10.0f, 20.0f, 0), GzColor::RED);
//g_lights[0] = new GzLight(POINT_LIGHT, GzVector3(-10.0f, 20.0f, 0), GzColor::WHITE);
//GzMaterial mTemp1;
//GzMaterial mTemp2(GzColor::RED, GzColor::BLACK, GzColor::WHITE, 15, 0);
// We'll do AA in renderer directly.
//
//**********************
// Same routine. Set up camera, lights for our renderer
// in this initialize function, like
//```
m_pRender = new GzRender(m_pDisplay);
status = status || m_pRender->putCamera(p_camera);
status = status || m_pRender->putLights(g_lights, 1);
//GzVector3 *kernel4 = new GzVector3[4];
//kernel4[0] = GzVector3(-0.25f, 0.25f, 0.25f);
//kernel4[1] = GzVector3(0.25f, 0.25f, 0.25f);
//kernel4[2] = GzVector3(-0.25f, -0.25f, 0.25f);
//kernel4[3] = GzVector3(0.25f, -0.25f, 0.25f);
GzAASetting *p_9SAA = new GzAASetting(3);
status = status || m_pRender->putAASetting(p_9SAA); //optional
//status = status || renderer.putAttribute(refractionmode); //optional
//status = status || renderer.putAttribute(diffusemode); //optional
//status = status || renderer.putAttribute(arealightmode); //optional
//```
// Still need tokens. But might be quite different from original.
// Pass pointers and boolean values to renderer. And do
// rendering inside Render() function.
//************************
}
catch (GzException)
{
status = GZ_FAILURE;
}
if (status)
{
AfxMessageBox(_T("Initiating went wrong!\n"));
}
return(status);
}
int ApplicationEngine::Render()
{
int status(GZ_SUCCESS);
try
{
// Initialize Display
m_pDisplay->init(GzColor(0.4f, 0.8f, 1.0f));
// I/O File open. Temporary
//
//FILE *infile;
//if( (infile = fopen( INFILE , "r" )) == NULL )
//{
//AfxMessageBox( "The input file was not opened\n" );
//return GZ_FAILURE;
//}
//
FILE *outfile;
if( (outfile = std::fopen( OUTFILE , "wb" )) == NULL )
{
AfxMessageBox(_T("The output file was not opened\n"));
return GZ_FAILURE;
}
// Test display
// Test Sphere
//Sphere s0(GzVector3(0.0f, 0.0f, 10.0f), 2.0f);
//GzCamera cam; // Test with default camera
//for (int j = 0; j < m_nHeight; ++j)
//{
//for (int i = 0; i < m_nWidth; ++i)
//{
//int yj = j;
//int xi = i;
//float ndcx = xi * 2.0f / m_nWidth - 1;
//float ndcy = -(yj * 2.0f / m_nHeight - 1);
//GzRay rForPixel = cam.generateRay(ndcx, ndcy);
//if (s0.intersect(rForPixel).p_geometry)
//{
//m_pDisplay->putDisplay(xi, yj, (GzColor::BLUE + GzColor::RED) * 0.25);
//}
//}
//}
//*******************************
//Sphere s0(GzVector3(0.0f, 0.0f, 10.0f), 2.0f);
//GzMaterial mat(GzTexture(&GzTexture::checker_ptex_func), 15, 0.2f);
//GzGeometry ** p_geos = new GzGeometry*[3];
//p_geos[0] = new Sphere(GzVector3(5.2f, 5.0f, 0.0f), 5.0f, GzMaterial(GzColor(0.5f, 0.5f, 0.5f), 16.0f, 0.8f));
//p_geos[1] = new Sphere(GzVector3(-5.2f, 5.0f, 0.0f), 5.0f, GzMaterial(GzColor(0.5f, 0.5f, 0.5f), 16.0f, 0.8f));
//p_geos[2] = new Plane(GzVector3(0.0f, 1.0f, 0.0f), 0.0f, GzVector3(0.0f, 0.0f, 1.0f), mat);
//GzMaterial mat(GzTexture(&GzTexture::checker_ptex_func), 0.4f);
//GzMaterial mat2(GzTexture(&GzTexture::checker_ptex_func2), 0.4f);
//GzGeometry ** p_geos = new GzGeometry*[3];
//p_geos[0] = new Sphere(GzVector3(0.0f, 5.0f, 0.0f), 5.0f, GzMaterial(GzColor::RED, 16.0f, 1.1f, 0.05f, 0.95f));
//p_geos[1] = new Plane(GzVector3(0.0f, 1.0f, 0.0f), 0.0f, GzVector3(0.0f, 0.0f, 1.0f), mat);
//p_geos[2] = new Rec(GzVector3(-10.0f,1.0f,-10.0f), GzVector3(0,0,15), GzVector3(15, 0, 0), GzMaterial(GzColor::RED, 0)); // base, x dir , y dir
const char* file = "tex_normal_golf.ppm";
GzMaterial mat(GzTexture(&GzTexture::checker_ptex_func, 1), 0.2f);
GzMaterial mat2(GzTexture(&GzTexture::checker_ptex_func, 5), 0.2f);
GzMaterial mat_norm(GzColor::CYAN, GzTexture(file, &GzTexture::image_tex_func, 0), 16.0f, 1.12f, 0.5f, 0.0f);
GzGeometry ** p_geos = new GzGeometry*[3];
p_geos[0] = new Sphere(GzVector3(5.2f, 5.0f, 0.0f), 5.0f, mat_norm);
p_geos[1] = new Sphere(GzVector3(-5.2f, 5.0f, 0.0f), 5.0f, GzMaterial(GzColor(0.5f, 0.5f, 0.5f), 16.0f, 1.12f, 0.5f, 0.5f));
p_geos[2] = new Plane(GzVector3(0.0f, 1.0f, 0.0f), 0.0f, GzVector3(0.0f, 0.0f, 1.0f), mat);
GzGeometry * p_unionGeometry = new Union(3, p_geos);
//Sphere s0(GzVector3(0.0f, 0.0f, 10.0f), 5.0f);
//p_s0->material = mat;
//GzGeometry scene = constructScene(inFile);
status = status || m_pRender->putScene(p_unionGeometry);
status = status || m_pRender->renderToDisplay();
//*******************************
//GzFlushDisplay2File(outfile, m_pDisplay); /* */
//GzFlushDisplay2FrameBuffer(m_pFrameBuffer, m_pDisplay); // write out or update display to frame buffer
m_pDisplay->flush2File(outfile); //write out or update display to file
m_pDisplay->flush2FrameBuffer(m_pFrameBuffer); //write out or update display to frame buffer
//
// Close file
//
//if( fclose( infile ) )
//AfxMessageBox( "The input file was not closed\n" );
if( fclose( outfile ) )
{
AfxMessageBox(_T("The output file was not closed\n"));
}
}
catch (GzException)
{
status = GZ_FAILURE;
}
if (status)
{
AfxMessageBox(_T("Rendering went wrong!\n"));
}
return(status);
}
void ApplicationEngine::Clean()
{
// Might need to clean renderer and texture and other objects
delete m_pRender;
m_pRender = nullptr;
delete m_pDisplay;
m_pDisplay = nullptr;
//status |= GzFreeTexture();
}