-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvas.cpp
404 lines (351 loc) · 9.13 KB
/
Canvas.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//
// Canvas.cpp
//
// Created by Warren R. Carithers 2016/09/30.
// Based on a C++ version created by Joe Geigel.
// Copyright 2016 Rochester Institute of Technology. All rights reserved.
//
// Routines for adding points to create a new mesh.
//
// This file should not be modified by students.
//
#include <cstdlib>
#include <iostream>
#include <iomanip>
// Canvas.h includes all the OpenGL/GLFW/etc. header files for us
#include "Canvas.h"
///
// Constructor
//
// @param w width of canvas
// @param h height of canvas
///
Canvas::Canvas( int w, int h ) : width(w), height(h) {
currentColor[0] = 0.0f;
currentColor[1] = 0.0f;
currentColor[2] = 0.0f;
pointArray = 0;
colorArray = 0;
normalArray = 0;
uvArray = 0;
elemArray = 0;
numElements = 0;
}
///
// Destructor
///
Canvas::~Canvas( void ) {
clear();
}
///
// clear the canvas
///
void Canvas::clear( void )
{
if( pointArray ) {
delete [] pointArray;
pointArray = 0;
}
if( normalArray ) {
delete [] normalArray;
normalArray = 0;
}
if( uvArray ) {
delete [] uvArray;
uvArray = 0;
}
if( elemArray ) {
delete [] elemArray;
elemArray = 0;
}
if( colorArray ) {
delete [] colorArray;
colorArray = 0;
}
points.clear();
normals.clear();
uv.clear();
colors.clear();
numElements = 0;
currentColor[0] = 0.0f;
currentColor[1] = 0.0f;
currentColor[2] = 0.0f;
}
///
// adds a triangle to the current shape
//
// @param p0 first triangle vertex
// @param p1 second triangle vertex
// @param p2 final triangle vertex
///
void Canvas::addTriangle( glm::vec3 p0, glm::vec3 p1, glm::vec3 p2 )
{
points.push_back( p0.x );
points.push_back( p0.y );
points.push_back( p0.z );
points.push_back( 1.0f );
points.push_back( p1.x );
points.push_back( p1.y );
points.push_back( p1.z );
points.push_back( 1.0f );
points.push_back( p2.x );
points.push_back( p2.y );
points.push_back( p2.z );
points.push_back( 1.0f );
numElements += 3; // three vertices per triangle
}
///
// adds a triangle to the current shape, along with (u,v) data
//
// @param p0 first triangle vertex
// @param uv0 first triangle (u,v) data
// @param p1 second triangle vertex
// @param uv1 second triangle (u,v) data
// @param p2 final triangle vertex
// @param uv2 final triangle (u,v) data
///
void Canvas::addTriangleWithUV( glm::vec3 p0, glm::vec3 uv0,
glm::vec3 p1, glm::vec3 uv1,
glm::vec3 p2, glm::vec3 uv2 )
{
// calculate the normal
float ux = p1.x - p0.x;
float uy = p1.y - p0.y;
float uz = p1.z - p0.z;
float vx = p2.x - p0.x;
float vy = p2.y - p0.y;
float vz = p2.z - p0.z;
glm::vec3 nn;
nn.x = (uy * vz) - (uz * vy);
nn.y = (uz * vx) - (ux * vz);
nn.z = (ux * vy) - (uy * vx);
// Attach the normal to all 3 vertices
addTriangleWithNorms( p0, nn, p1, nn, p2, nn );
// Attach the texture coordinates
uv.push_back( uv0.x ); // note use of (x,y) vs. (u,v)
uv.push_back( uv0.y ); // see Vertex.h for details
uv.push_back( uv1.x );
uv.push_back( uv1.y );
uv.push_back( uv2.x );
uv.push_back( uv2.y );
}
///
// adds a triangle to the current shape, along with normal data
//
// @param p0 first triangle vertex
// @param n0 first triangle normal data
// @param p1 second triangle vertex
// @param n1 second triangle normal data
// @param p2 final triangle vertex
// @param n2 final triangle normal data
///
void Canvas::addTriangleWithNorms( glm::vec3 p0, glm::vec3 n0,
glm::vec3 p1, glm::vec3 n1,
glm::vec3 p2, glm::vec3 n2 )
{
points.push_back( p0.x );
points.push_back( p0.y );
points.push_back( p0.z );
points.push_back( 1.0f );
normals.push_back( n0.x );
normals.push_back( n0.y );
normals.push_back( n0.z );
points.push_back( p1.x );
points.push_back( p1.y );
points.push_back( p1.z );
points.push_back( 1.0f );
normals.push_back( n1.x );
normals.push_back( n1.y );
normals.push_back( n1.z );
points.push_back( p2.x );
points.push_back( p2.y );
points.push_back( p2.z );
points.push_back( 1.0f );
normals.push_back( n2.x );
normals.push_back( n2.y );
normals.push_back( n2.z );
numElements += 3; // three vertices per triangle
}
///
// adds a triangle to the current shape, along with (u,v) and normal data
//
// @param p0 first triangle vertex
// @param n0 first triangle normal data
// @param uv0 first triangle (u,v) data
// @param p1 second triangle vertex
// @param n1 second triangle normal data
// @param uv1 second triangle (u,v) data
// @param p2 final triangle vertex
// @param n2 final triangle normal data
// @param uv2 final triangle (u,v) data
///
void Canvas::addTriangleWithNormsUV( glm::vec3 p0, glm::vec3 n0, glm::vec3 uv0,
glm::vec3 p1, glm::vec3 n1, glm::vec3 uv1,
glm::vec3 p2, glm::vec3 n2, glm::vec3 uv2 )
{
addTriangleWithNorms( p0, n0, p1, n1, p2, n2 );
uv.push_back( uv0.x );
uv.push_back( uv0.y );
uv.push_back( uv1.x );
uv.push_back( uv1.y );
uv.push_back( uv2.x );
uv.push_back( uv2.y );
}
///
// change the current drawing color
//
// @param r The red component of the new color (between 0-1)
// @param g The green component of the new color (between 0-1)
// @param b The blue component of the new color (between 0-1);
///
void Canvas::setColor( float r, float g, float b )
{
currentColor[0] = r;
currentColor[1] = g;
currentColor[2] = b;
}
///
// set a pixel to the current drawing color
//
// @param x The x coord of the pixel to be set
// @param y The y coord of the pixel to be set
///
void Canvas::setPixel( int x0, int y0 )
{
points.push_back( (float) x0 );
points.push_back( (float) y0 );
points.push_back( -1.0f ); // fixed Z depth
points.push_back( 1.0f );
colors.push_back( currentColor[0] );
colors.push_back( currentColor[1] );
colors.push_back( currentColor[2] );
colors.push_back( 1.0f ); // alpha channel
numElements += 1;
}
///
// gets the array of vertices for the current shape
///
float *Canvas::getVertices( void )
{
// delete the old point array if we have one
if( pointArray ) {
delete [] pointArray;
pointArray = 0;
}
int n = points.size();
if( n > 0 ) {
// create and fill a new point array
pointArray = new float[ n ];
if( pointArray == 0 ) {
cerr << "point allocation failure" << endl;
exit( 1 );
}
for( int i = 0; i < n; i++ ) {
pointArray[i] = points[i];
}
}
return pointArray;
}
///
// gets the array of normals for the current shape
///
float *Canvas::getNormals( void )
{
// delete the old normal array if we have one
if( normalArray ) {
delete [] normalArray;
normalArray = 0;
}
int n = normals.size();
if( n > 0 ) {
// create and fill a new normal array
normalArray = new float[ n ];
if( normalArray == 0 ) {
cerr << "normal allocation failure" << endl;
exit( 1 );
}
for( int i = 0; i < n; i++ ) {
normalArray[i] = normals[i];
}
}
return normalArray;
}
///
// gets the array of texture coordinates for the current shape
///
float *Canvas::getUV( void )
{
// delete the old texture coordinate array if we have one
if( uvArray ) {
delete [] uvArray;
uvArray = 0;
}
int n = uv.size();
if( n > 0 ) {
// create and fill a new texture coordinate array
uvArray = new float[ n ];
if( uvArray == 0 ) {
cerr << "uv allocation failure" << endl;
exit( 1 );
}
for( int i = 0; i < n; i++ ) {
uvArray[i] = uv[i];
}
}
return uvArray;
}
///
// gets the array of elements for the current shape
///
GLuint *Canvas::getElements( void )
{
// delete the old element array if we have one
if( elemArray ) {
delete [] elemArray;
elemArray = 0;
}
int n = numElements;
if( n > 0 ) {
// create and fill a new element array
elemArray = new GLuint[ n ];
if( elemArray == 0 ) {
cerr << "element allocation failure" << endl;
exit( 1 );
}
for( int i = 0; i < n; i++ ) {
elemArray[i] = i;
}
}
return elemArray;
}
///
// gets the array of colors for the current shape
///
float *Canvas::getColors( void )
{
// delete the old color array if we have one
if( colorArray ) {
delete [] colorArray;
colorArray = 0;
}
int n = colors.size();
if( n > 0 ) {
// create and fill a new color array
colorArray = new float[ n ];
if( colorArray == 0 ) {
cerr << "color allocation failure" << endl;
exit( 1 );
}
for( int i = 0; i < n; i++ ) {
colorArray[i] = colors[i];
}
}
return colorArray;
}
///
// returns number of vertices in current shape
///
int Canvas::numVertices( void )
{
return numElements;
}