forked from chr11031/CS312_Graphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoursefunctions.h
442 lines (368 loc) · 16.7 KB
/
coursefunctions.h
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include "definitions.h"
#ifndef COURSE_FUNCTIONS_H
#define COURSE_FUNCTIONS_H
/***************************************************
* Team Activity for week #1.
* When working on this activity be sure to
* comment out the following function calls in
* pipeline.cpp:main():
* 1) processUserInputs(running);
* 2) clearScreen(frame);
* 3) Any draw calls that are being made there
*
* When you finish this activity be sure to
* uncomment these functions again!!!
**************************************************/
void GameOfLife(Buffer2D<PIXEL> & target)
{
// 'Static's are initialized exactly once
static bool isSetup = true;
static bool holdDown = false;
static int w = target.width();
static int h = target.height();
static int scaleFactor = 8;
static int gridW = 64;
static int gridH = 64;
static int grid[64][64];
static int gridTmp[64][64];
// Setup small grid, temporary grid from previous iteration
for(int y = 0; y < gridH; y++)
{
for(int x = 0; x < gridW; x++)
{
grid[y][x] = (target[y*scaleFactor][x*scaleFactor] == 0xffff0000) ? 1 : 0;
gridTmp[y][x] = grid[y][x];
}
}
//Parse for inputs
SDL_Event e;
while(SDL_PollEvent(&e))
{
int mouseX;
int mouseY;
if(e.type == SDL_MOUSEBUTTONDOWN)
{
holdDown = true;
}
if(e.type == SDL_MOUSEBUTTONUP)
{
holdDown = false;
}
if(e.key.keysym.sym == 'g' && e.type == SDL_KEYDOWN)
{
isSetup = !isSetup;
}
if(holdDown && isSetup)
{
// Clicking the mouse changes a pixel's color
SDL_GetMouseState(&mouseX, &mouseY);
int gridX = mouseX / scaleFactor;
int gridY = mouseY / scaleFactor;
if(grid[gridY][gridX] == 1)
{
// Dead
grid[gridY][gridX] = 0;
}
else
{
// Alive
grid[gridY][gridX] = 1;
}
}
}
// Advance the simulation after pressing 'g'
if(!isSetup)
{
// Your Code goes here
// Wait a half-second between iterations
SDL_Delay(500);
}
// Upscale/blit to screen
for(int y = 0; y < h; y++)
{
for(int x = 0; x < w; x++)
{
int yScal = y/scaleFactor;
int xScal = x/scaleFactor;
if(grid[yScal][xScal] == 0)
{
// Dead Color
target[y][x] = 0xff000000;
}
else
{
// Alive color
target[y][x] = 0xffff0000;
}
}
}
}
/***************************************************
* Create a 3D View like in a CAD program
* NOTE: Assumes that the resolution is an even
* value in both dimensions.
**************************************************/
void CADView(Buffer2D<PIXEL> & target)
{
// Each CAD Quadrant
static int halfWid = target.width()/2;
static int halfHgt = target.height()/2;
static Buffer2D<PIXEL> topLeft(halfWid, halfHgt);
static Buffer2D<PIXEL> topRight(halfWid, halfHgt);
static Buffer2D<PIXEL> botLeft(halfWid, halfHgt);
static Buffer2D<PIXEL> botRight(halfWid, halfHgt);
// Your code goes here
// Feel free to copy from other test functions to get started!
// Blit four panels to target
int yStartSrc = 0;
int xStartSrc = 0;
int yLimitSrc = topLeft.height();
int xLimitSrc = topLeft.width();
for(int ySrc = yStartSrc; ySrc < yLimitSrc; ySrc++)
{
for(int xSrc = xStartSrc; xSrc < xLimitSrc; xSrc++)
{
target[ySrc][xSrc] = botLeft[ySrc][xSrc];
target[ySrc][xSrc+halfWid] = botRight[ySrc][xSrc];
target[ySrc+halfHgt][xSrc] = topLeft[ySrc][xSrc];
target[ySrc+halfHgt][xSrc+halfWid] = topRight[ySrc][xSrc];
}
}
}
/***************************************************
* Demonstrate pixel drawing for project 01.
**************************************************/
void TestDrawPixel(Buffer2D<PIXEL> & target)
{
Vertex vert = {10, 502, 1, 1};
Attributes pointAttributes;
PIXEL color = 0xffff0000;
// Your Code goes here for 'pointAttributes'
DrawPrimitive(POINT, target, &vert, &pointAttributes);
}
/***********************************************
* Demonstrate Triangle Drawing for Project 02.
**********************************************/
void TestDrawTriangle(Buffer2D<PIXEL> & target)
{
/**************************************************
* 6 Flat color triangles below
*************************************************/
Vertex verts[3];
Attributes attr[3];
verts[0] = {100, 362, 1, 1};
verts[1] = {150, 452, 1, 1};
verts[2] = {50, 452, 1, 1};
PIXEL colors1[3] = {0xffff0000, 0xffff0000, 0xffff0000};
// Your color code goes here for 'attr'
DrawPrimitive(TRIANGLE, target, verts, attr);
verts[0] = {300, 402, 1, 1};
verts[1] = {250, 452, 1, 1};
verts[2] = {250, 362, 1, 1};
PIXEL colors2[3] = {0xffff0000, 0xffff0000, 0xffff0000};
// Your color code goes here for 'attr'
DrawPrimitive(TRIANGLE, target, verts, attr);
verts[0] = {450, 362, 1, 1};
verts[1] = {450, 452, 1, 1};
verts[2] = {350, 402, 1, 1};
PIXEL colors3[3] = {0xff00ff00, 0xff00ff00, 0xff00ff00};
// Your color code goes here for 'attr'
DrawPrimitive(TRIANGLE, target, verts, attr);
verts[0] = {110, 262, 1, 1};
verts[1] = {60, 162, 1, 1};
verts[2] = {150, 162, 1, 1};
PIXEL colors4[3] = {0xff00ff00, 0xff00ff00, 0xff00ff00};
// Your color code goes here for 'attr'
DrawPrimitive(TRIANGLE, target, verts, attr);
verts[0] = {210, 252, 1, 1};
verts[1] = {260, 172, 1, 1};
verts[2] = {310, 202, 1, 1};
PIXEL colors5[3] = {0xff00ff00, 0xff00ff00, 0xff00ff00};
// Your color code goes here for 'attr'
DrawPrimitive(TRIANGLE, target, verts, attr);
verts[0] = {370, 202, 1, 1};
verts[1] = {430, 162, 1, 1};
verts[2] = {470, 252, 1, 1};
PIXEL colors6[3] = {0xff00ff00, 0xff00ff00, 0xff00ff00};
// Your color code goes here for 'attr'
DrawPrimitive(TRIANGLE, target, verts, attr);
}
/***********************************************
* Demonstrate Fragment Shader, linear VBO
* interpolation for Project 03.
**********************************************/
void TestDrawFragments(Buffer2D<PIXEL> & target)
{
/**************************************************
* 1. Interpolated color triangle
*************************************************/
Vertex colorTriangle[3];
Attributes colorAttributes[3];
colorTriangle[0] = {250, 112, 1, 1};
colorTriangle[1] = {450, 452, 1, 1};
colorTriangle[2] = {50, 452, 1, 1};
PIXEL colors[3] = {0xffff0000, 0xff00ff00, 0xff0000ff}; // Or {{1.0,0.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}}
// Your color code goes here for 'colorAttributes'
FragmentShader myColorFragShader;
// Your code for the color fragment shader goes here
Attributes colorUniforms;
// Your code for the uniform goes here, if any (don't pass NULL here)
DrawPrimitive(TRIANGLE, target, colorTriangle, colorAttributes, &colorUniforms, &myColorFragShader);
/****************************************************
* 2. Interpolated image triangle
****************************************************/
Vertex imageTriangle[3];
Attributes imageAttributes[3];
imageTriangle[0] = {425, 112, 1, 1};
imageTriangle[1] = {500, 252, 1, 1};
imageTriangle[2] = {350, 252, 1, 1};
double coordinates[3][2] = { {1,0}, {1,1}, {0,1} };
// Your texture coordinate code goes here for 'imageAttributes'
BufferImage myImage("image.bmp");
// Provide an image in this directory that you would like to use (powers of 2 dimensions)
Attributes imageUniforms;
// Your code for the uniform goes here
FragmentShader myImageFragShader;
// Your code for the image fragment shader goes here
DrawPrimitive(TRIANGLE, target, imageTriangle, imageAttributes, &imageUniforms, &myImageFragShader);
}
/************************************************
* Demonstrate Perspective correct interpolation
* for Project 04.
***********************************************/
void TestDrawPerspectiveCorrect(Buffer2D<PIXEL> & target)
{
/**************************************************
* 1. Image quad (2 TRIs) Code (texture interpolated)
**************************************************/
// Artificially projected, viewport transformed
double divA = 6;
double divB = 40;
Vertex quad[] = {{(-1200 / divA) + (S_WIDTH/2), (-1500 / divA) + (S_HEIGHT/2), divA, 1.0/divA },
{(1200 / divA) + (S_WIDTH/2), (-1500 / divA) + (S_HEIGHT/2), divA, 1.0/divA },
{(1200 / divB) + (S_WIDTH/2), (1500 / divB) + (S_HEIGHT/2), divB, 1.0/divB },
{(-1200 / divB) + (S_WIDTH/2), (1500 / divB) + (S_HEIGHT/2), divB, 1.0/divB }};
Vertex verticesImgA[3];
Attributes imageAttributesA[3];
verticesImgA[0] = quad[0];
verticesImgA[1] = quad[1];
verticesImgA[2] = quad[2];
Vertex verticesImgB[3];
Attributes imageAttributesB[3];
verticesImgB[0] = quad[2];
verticesImgB[1] = quad[3];
verticesImgB[2] = quad[0];
double coordinates[4][2] = { {0/divA,0/divA}, {1/divA,0/divA}, {1/divB,1/divB}, {0/divB,1/divB} };
// Your texture coordinate code goes here for 'imageAttributesA, imageAttributesB'
BufferImage myImage("checker.bmp");
// Ensure the checkboard image is in this directory
Attributes imageUniforms;
// Your code for the uniform goes here
FragmentShader fragImg;
// Your code for the image fragment shader goes here
// Draw image triangle
DrawPrimitive(TRIANGLE, target, verticesImgA, imageAttributesA, &imageUniforms, &fragImg);
DrawPrimitive(TRIANGLE, target, verticesImgB, imageAttributesB, &imageUniforms, &fragImg);
}
/************************************************
* Demonstrate simple transformations for
* Project 05 in the vertex shader callback.
***********************************************/
void TestVertexShader(Buffer2D<PIXEL> & target)
{
/**************************************************
* 1. Interpolated color triangle
*************************************************/
Vertex colorTriangle[3];
Attributes colorAttributes[3];
colorTriangle[0] = { 350, 112, 1, 1};
colorTriangle[1] = { 400, 200, 1, 1};
colorTriangle[2] = { 300, 200, 1, 1};
PIXEL colors[3] = {0xffff0000, 0xff00ff00, 0xff0000ff};
// Your code for 'colorAttributes' goes here
FragmentShader myColorFragShader;
Attributes colorUniforms;
// Your code for the uniform goes here, if any (don't pass NULL here)
VertexShader myColorVertexShader;
// Your code for the vertex shader goes here
/******************************************************************
* TRANSLATE (move +100 in the X direction, +50 in the Y direction)
*****************************************************************/
// Your translating code that integrates with 'colorUniforms', used by 'myColorVertexShader' goes here
DrawPrimitive(TRIANGLE, target, colorTriangle, colorAttributes, &colorUniforms, &myColorFragShader, &myColorVertexShader);
/***********************************
* SCALE (scale by a factor of 0.5)
***********************************/
// Your scaling code that integrates with 'colorUniforms', used by 'myColorVertexShader' goes here
DrawPrimitive(TRIANGLE, target, colorTriangle, colorAttributes, &colorUniforms, &myColorFragShader, &myColorVertexShader);
/**********************************************
* ROTATE 45 degrees in the X-Y plane around Z
*********************************************/
// Your rotation code that integrates with 'colorUniforms', used by 'myColorVertexShader' goes here
DrawPrimitive(TRIANGLE, target, colorTriangle, colorAttributes, &colorUniforms, &myColorFragShader, &myColorVertexShader);
/*************************************************
* SCALE-TRANSLATE-ROTATE in left-to-right order
* the previous transformations concatenated.
************************************************/
// Your scale-translate-rotation code that integrates with 'colorUniforms', used by 'myColorVertexShader' goes here
DrawPrimitive(TRIANGLE, target, colorTriangle, colorAttributes, &colorUniforms, &myColorFragShader, &myColorVertexShader);
}
/********************************************
* Verify that the whole pipeline works. By
* the end of week 07 you should be able to
* run this code successfully.
*******************************************/
void TestPipeline(Buffer2D<PIXEL> & target)
{
// This is similar to TestDrawPerspectiveCorrect
// except that:
// 1) perspective projection is expected from
// the programmer in the vertex shader.
// 2) Clipping/normalization must be turned on.
// 3) The ViewPort Transform must be applied.
// 4) The Z-Buffer is incorporated into drawing.
// 5) You may want to involve camera variables:
// i) camYaw
// ii) camPitch
// iii) camRoll,
// iv) camX
// v) camY
// vi) camZ
// To incorporate a view transform (add movement)
static Buffer2D<double> zBuf(target.width(), target.height());
// Will need to be cleared every frame, like the screen
/**************************************************
* 1. Image quad (2 TRIs) Code (texture interpolated)
**************************************************/
Vertex quad[] = { {-20,-20, 50, 1},
{20, -20, 50, 1},
{20, 20, 50, 1},
{-20,20, 50, 1}};
Vertex verticesImgA[3];
Attributes imageAttributesA[3];
verticesImgA[0] = quad[0];
verticesImgA[1] = quad[1];
verticesImgA[2] = quad[2];
Vertex verticesImgB[3];
Attributes imageAttributesB[3];
verticesImgB[0] = quad[2];
verticesImgB[1] = quad[3];
verticesImgB[2] = quad[0];
double coordinates[4][2] = { {0,0}, {1,0}, {1,1}, {0,1} };
// Your texture coordinate code goes here for 'imageAttributesA, imageAttributesB'
BufferImage myImage("checker.bmp");
// Ensure the checkboard image is in this directory, you can use another image though
Attributes imageUniforms;
// Your code for the uniform goes here
FragmentShader fragImg;
// Your code for the image fragment shader goes here
VertexShader vertImg;
// Your code for the image vertex shader goes here
// NOTE: This must include the at least the
// projection matrix if not more transformations
// Draw image triangle
DrawPrimitive(TRIANGLE, target, verticesImgA, imageAttributesA, &imageUniforms, &fragImg, &vertImg, &zBuf);
DrawPrimitive(TRIANGLE, target, verticesImgB, imageAttributesB, &imageUniforms, &fragImg, &vertImg, &zBuf);
// NOTE: To test the Z-Buffer additinonal draw calls/geometry need to be called into this scene
}
#endif