-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application5.cpp
332 lines (271 loc) · 8.63 KB
/
Application5.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Application5.cpp: implementation of the Application5 class.
//
//////////////////////////////////////////////////////////////////////
/*
* application test code for homework assignment #5
*/
#include "stdafx.h"
#include "CS580HW.h"
#include "Application5.h"
#include "Gz.h"
#include "disp.h"
#include "rend.h"
#include "LEE.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define INFILE "ppot.asc"
#define OUTFILE "output.ppm"
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 */
void shade(GzCoord norm, GzCoord color);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Application5::Application5()
{
}
Application5::~Application5()
{
Clean();
}
int Application5::Initialize()
{
GzCamera camera;
int xRes, yRes; /* display parameters */
GzToken nameListShader[9]; /* shader attribute names */
GzPointer valueListShader[9]; /* shader attribute pointers */
GzToken nameListLights[10]; /* light info */
GzPointer valueListLights[10];
GzToken nameListAA[1]; /* Anti-Aliasing*/
GzPointer valueListAA[1];
int shaderType, interpStyle;
float specpower;
int status;
status = 0;
/*
* Allocate memory for user input
*/
m_pUserInput = new GzInput;
/*
* initialize the display and the renderer
*/
m_nWidth = 256; // frame buffer and display width
m_nHeight = 256; // frame buffer and display height
status |= GzNewFrameBuffer(&m_pFrameBuffer, m_nWidth, m_nHeight);
status |= GzNewDisplay(&m_pDisplay, m_nWidth, m_nHeight);
status |= GzGetDisplayParams(m_pDisplay, &xRes, &yRes);
status |= GzNewRender(&m_pRender, m_pDisplay);
/* Translation matrix */
GzMatrix scale =
{
3.25, 0.0, 0.0, 0.0,
0.0, 3.25, 0.0, -3.25,
0.0, 0.0, 3.25, 3.5,
0.0, 0.0, 0.0, 1.0
};
GzMatrix rotateX =
{
1.0, 0.0, 0.0, 0.0,
0.0, .7071, .7071, 0.0,
0.0, -.7071, .7071, 0.0,
0.0, 0.0, 0.0, 1.0
};
GzMatrix rotateY =
{
.866, 0.0, -0.5, 0.0,
0.0, 1.0, 0.0, 0.0,
0.5, 0.0, .866, 0.0,
0.0, 0.0, 0.0, 1.0
};
#if 1 /* set up app-defined camera if desired, else use camera defaults */
camera.position[X] = -3;
camera.position[Y] = -25;
camera.position[Z] = -4;
camera.lookat[X] = 7.8;
camera.lookat[Y] = 0.7;
camera.lookat[Z] = 6.5;
camera.worldup[X] = -0.2;
camera.worldup[Y] = 1.0;
camera.worldup[Z] = 0.0;
camera.FOV = 63.7; /* degrees * /* degrees */
status |= GzPutCamera(m_pRender, &camera);
#endif
/* Start Renderer */
status |= GzBeginRender(m_pRender);
/* Light */
GzLight light1 = { {-0.7071, 0.7071, 0}, {0.5, 0.5, 0.9} };
GzLight light2 = { {0, -0.7071, -0.7071}, {0.9, 0.2, 0.3} };
GzLight light3 = { {0.7071, 0.0, -0.7071}, {0.2, 0.7, 0.3} };
GzLight ambientlight = { {0, 0, 0}, {0.3, 0.3, 0.3} };
/* Material property */
GzColor specularCoefficient = { 0.3, 0.3, 0.3 };
GzColor ambientCoefficient = { 0.1, 0.1, 0.1 };
GzColor diffuseCoefficient = {0.7, 0.7, 0.7};
/*
renderer is ready for frame --- define lights and shader at start of frame
*/
/*
* Tokens associated with light parameters
*/
nameListLights[0] = GZ_DIRECTIONAL_LIGHT;
valueListLights[0] = (GzPointer)&light1;
nameListLights[1] = GZ_DIRECTIONAL_LIGHT;
valueListLights[1] = (GzPointer)&light2;
nameListLights[2] = GZ_DIRECTIONAL_LIGHT;
valueListLights[2] = (GzPointer)&light3;
status |= GzPutAttribute(m_pRender, 3, nameListLights, valueListLights);
nameListLights[0] = GZ_AMBIENT_LIGHT;
valueListLights[0] = (GzPointer)&ambientlight;
status |= GzPutAttribute(m_pRender, 1, nameListLights, valueListLights);
/*
* Tokens associated with shading
*/
nameListShader[0] = GZ_DIFFUSE_COEFFICIENT;
valueListShader[0] = (GzPointer)diffuseCoefficient;
/*
* Select either GZ_COLOR or GZ_NORMALS as interpolation mode
*/
nameListShader[1] = GZ_INTERPOLATE;
interpStyle = GZ_NORMALS; /* Phong shading */
valueListShader[1] = (GzPointer)&interpStyle;
nameListShader[2] = GZ_AMBIENT_COEFFICIENT;
valueListShader[2] = (GzPointer)ambientCoefficient;
nameListShader[3] = GZ_SPECULAR_COEFFICIENT;
valueListShader[3] = (GzPointer)specularCoefficient;
nameListShader[4] = GZ_DISTRIBUTION_COEFFICIENT;
specpower = 32;
valueListShader[4] = (GzPointer)&specpower;
nameListShader[5] = GZ_TEXTURE_MAP;
#if 0 /* set up null texture function or valid pointer */
valueListShader[5] = (GzPointer)0;
#else
valueListShader[5] = (GzPointer)(tex_fun); /* or use ptex_fun */
#endif
status |= GzPutAttribute(m_pRender, 6, nameListShader, valueListShader);
#if 0
AAFilter aaFilter[AAKERNEL_SIZE] = {
{ 0., 0., 0.128 },
{ 0., 0., 0.119 },
{ 0., 0., 0.294 },
{ 0., 0., 0.249 },
{ 0., 0., 0.104 },
{ 0., 0., 0.106 }
};
#else
AAFilter aaFilter[AAKERNEL_SIZE] = {
{-0.52, 0.38, 0.128},
{0.41, 0.56, 0.119},
{0.27, 0.08, 0.294},
{-0.17, -0.29, 0.249},
{0.58, -0.55, 0.104},
{-0.31, -0.71, 0.106}
};
#endif // 0
nameListAA[0] = GZ_ANTIALISING_FILTER;
valueListAA[0] = (GzPointer)(aaFilter);
status |= GzPutAttribute(m_pRender, 1, nameListAA, valueListAA);
status |= GzPushMatrix(m_pRender, scale);
status |= GzPushMatrix(m_pRender, rotateY);
status |= GzPushMatrix(m_pRender, rotateX);
if (status) exit(GZ_FAILURE);
if (status)
return(GZ_FAILURE);
else
return(GZ_SUCCESS);
}
int Application5::Render()
{
GzToken nameListTriangle[3]; /* vertex attribute names */
GzPointer valueListTriangle[3]; /* vertex attribute pointers */
GzCoord vertexList[3]; /* vertex position coordinates */
GzCoord normalList[3]; /* vertex normals */
GzTextureIndex uvList[3]; /* vertex texture map indices */
char dummy[256];
int status;
/* Initialize Display */
status |= GzInitDisplay(m_pDisplay);
/*
* Tokens associated with triangle vertex values
*/
nameListTriangle[0] = GZ_POSITION;
nameListTriangle[1] = GZ_NORMAL;
nameListTriangle[2] = GZ_TEXTURE_INDEX;
// I/O File open
FILE *infile;
if( (infile = fopen( INFILE , "r" )) == NULL )
{
AfxMessageBox( "The input file was not opened\n" );
return GZ_FAILURE;
}
FILE *outfile;
if( (outfile = fopen( OUTFILE , "wb" )) == NULL )
{
AfxMessageBox( "The output file was not opened\n" );
return GZ_FAILURE;
}
/*
* Walk through the list of triangles, set color
* and render each triangle
*/
while( fscanf(infile, "%s", dummy) == 1) { /* read in tri word */
fscanf(infile, "%f %f %f %f %f %f %f %f",
&(vertexList[0][0]), &(vertexList[0][1]),
&(vertexList[0][2]),
&(normalList[0][0]), &(normalList[0][1]),
&(normalList[0][2]),
&(uvList[0][0]), &(uvList[0][1]) );
fscanf(infile, "%f %f %f %f %f %f %f %f",
&(vertexList[1][0]), &(vertexList[1][1]),
&(vertexList[1][2]),
&(normalList[1][0]), &(normalList[1][1]),
&(normalList[1][2]),
&(uvList[1][0]), &(uvList[1][1]) );
fscanf(infile, "%f %f %f %f %f %f %f %f",
&(vertexList[2][0]), &(vertexList[2][1]),
&(vertexList[2][2]),
&(normalList[2][0]), &(normalList[2][1]),
&(normalList[2][2]),
&(uvList[2][0]), &(uvList[2][1]) );
/*
* Set the value pointers to the first vertex of the
* triangle, then feed it to the renderer
* NOTE: this sequence matches the nameList token sequence
*/
valueListTriangle[0] = (GzPointer)vertexList;
valueListTriangle[1] = (GzPointer)normalList;
valueListTriangle[2] = (GzPointer)uvList;
GzPutTriangle(m_pRender, 3, nameListTriangle, valueListTriangle);
}
if(LEE::getLEE()->getAA()!=NULL)
LEE::getLEE()->getAA()->setOutput(m_pDisplay);
GzFlushDisplay2File(outfile, m_pDisplay); /* write out or update display to file*/
GzFlushDisplay2FrameBuffer(m_pFrameBuffer, m_pDisplay); // 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( "The output file was not closed\n" );
if (status)
return(GZ_FAILURE);
else
return(GZ_SUCCESS);
}
int Application5::Clean()
{
/*
* Clean up and exit
*/
int status = 0;
status |= GzFreeRender(m_pRender);
status |= GzFreeDisplay(m_pDisplay);
status |= GzFreeTexture();
if (status)
return(GZ_FAILURE);
else
return(GZ_SUCCESS);
}