-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application5.cpp
308 lines (248 loc) · 8.3 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
// 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 "rend.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define INFILE "POT4.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 */
extern int GzFreeTexture();
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];
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 = 1024; // frame buffer and display width
m_nHeight = 1024; // frame buffer and display height
m_pRender = new GzRender(m_nWidth, m_nHeight);
m_pRender->GzDefault();
m_pFrameBuffer = m_pRender->framebuffer;
/* 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] = 13.2;
camera.position[Y] = -8.7;
camera.position[Z] = -14.8;
camera.lookat[X] = 0.8;
camera.lookat[Y] = 0.7;
camera.lookat[Z] = 4.5;
camera.worldup[X] = -0.2;
camera.worldup[Y] = 1.0;
camera.worldup[Z] = 0.0;
camera.FOV = 53.7; /* degrees */
status |= m_pRender->GzPutCamera(camera);
#endif
/* Start Renderer */
status |= m_pRender->GzBeginRender();
/* 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 light_toon = { {0.609539, -0.401742, -0.683422}, {1, 1, 1} };
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;*/
nameListLights[0] = GZ_DIRECTIONAL_LIGHT;
valueListLights[0] = (GzPointer)&light_toon;
status |= m_pRender->GzPutAttribute(1, nameListLights, valueListLights);
nameListLights[0] = GZ_AMBIENT_LIGHT;
valueListLights[0] = (GzPointer)&ambientlight;
status |= m_pRender->GzPutAttribute(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_COLOR; /* Gouraud shading */
//interpStyle = GZ_NORMALS; /* Phong shading */
interpStyle = GZ_TOON;
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 1 /* set up null texture function or valid pointer */
valueListShader[5] = (GzPointer)0;
#else
valueListShader[5] = (GzPointer)(ptex_fun); /* or use ptex_fun */
#endif
status |= m_pRender->GzPutAttribute(6, nameListShader, valueListShader);
status |= m_pRender->GzPushMatrix(scale);
status |= m_pRender->GzPushMatrix(rotateY);
status |= m_pRender->GzPushMatrix(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 |= m_pRender->GzDefault(); /* init for new frame */
/*
* 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;
m_pRender->GzPutTriangle(3, nameListTriangle, valueListTriangle);
}
m_pRender->GzSobelEdgeDetection();
m_pRender->GzFlushDisplay2File(outfile); /* write out or update display to file*/
m_pRender->GzFlushDisplay2FrameBuffer(); // write out or update display to frame buffer
/*
* Close file
*/
if( fclose( infile ) )
AfxMessageBox(_T( "The input file was not closed\n" ));
if( fclose( outfile ) )
AfxMessageBox(_T( "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;
delete(m_pRender);
status |= GzFreeTexture();
if (status)
return(GZ_FAILURE);
else
return(GZ_SUCCESS);
}