forked from chr11031/CS312_Graphics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.cpp
More file actions
200 lines (183 loc) · 6.38 KB
/
pipeline.cpp
File metadata and controls
200 lines (183 loc) · 6.38 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
#include "definitions.h"
#include "coursefunctions.h"
/***********************************************
* CLEAR_SCREEN
* Sets the screen to the indicated color value.
**********************************************/
void clearScreen(Buffer2D<PIXEL> & frame, PIXEL color = 0xff000000)
{
int h = frame.height();
int w = frame.width();
for(int y = 0; y < h; y++)
{
for(int x = 0; x < w; x++)
{
frame[y][x] = color;
}
}
}
/************************************************************
* UPDATE_SCREEN
* Blits pixels from RAM to VRAM for rendering.
***********************************************************/
void SendFrame(SDL_Texture* GPU_OUTPUT, SDL_Renderer * ren, SDL_Surface* frameBuf)
{
SDL_UpdateTexture(GPU_OUTPUT, NULL, frameBuf->pixels, frameBuf->pitch);
SDL_RenderClear(ren);
SDL_RenderCopy(ren, GPU_OUTPUT, NULL, NULL);
SDL_RenderPresent(ren);
}
/*************************************************************
* POLL_CONTROLS
* Updates the state of the application based on:
* keyboard, mouse, touch screen, gamepad inputs.
************************************************************/
void processUserInputs(bool & running)
{
SDL_Event e;
int mouseX;
int mouseY;
while(SDL_PollEvent(&e))
{
if(e.type == SDL_QUIT)
{
running = false;
}
if(e.key.keysym.sym == 'q' && e.type == SDL_KEYDOWN)
{
running = false;
}
}
}
/****************************************
* DRAW_POINT
* Renders a point to the screen with the
* appropriate coloring.
***************************************/
void DrawPoint(Buffer2D<PIXEL> & target, Vertex* v, Attributes* attrs, Attributes * const uniforms, FragmentShader* const frag)
{
// Your code goes here
}
/****************************************
* DRAW_TRIANGLE
* Renders a line to the screen.
***************************************/
void DrawLine(Buffer2D<PIXEL> & target, Vertex* const triangle, Attributes* const attrs, Attributes* const uniforms, FragmentShader* const frag)
{
// Your code goes here
}
/*************************************************************
* DRAW_TRIANGLE
* Renders a triangle to the target buffer. Essential
* building block for most of drawing.
************************************************************/
void DrawTriangle(Buffer2D<PIXEL> & target, Vertex* const triangle, Attributes* const attrs, Attributes* const uniforms, FragmentShader* const frag)
{
// Your code goes here
}
/**************************************************************
* VERTEX_SHADER_EXECUTE_VERTICES
* Executes the vertex shader on inputs, yielding transformed
* outputs.
*************************************************************/
void VertexShaderExecuteVertices(const VertexShader* vert, Vertex const inputVerts[], Attributes const inputAttrs[], const int& numIn,
Attributes* const uniforms, Vertex transformedVerts[], Attributes transformedAttrs[])
{
// Defaults to pass-through behavior
if(vert == NULL)
{
for(int i = 0; i < numIn; i++)
{
transformedVerts[i] = inputVerts[i];
transformedAttrs[i] = inputAttrs[i];
}
}
}
/***************************************************************************
* DRAW_PRIMITIVE
* Processes the indicated PRIMITIVES type through pipeline stages of:
* 1) Vertex Transformation
* 2) Clipping
* 3) Normalization
* 4) ViewPort transform
* 5) Rasterization & Fragment Shading
**************************************************************************/
void DrawPrimitive(PRIMITIVES prim,
Buffer2D<PIXEL>& target,
const Vertex inputVerts[],
const Attributes inputAttrs[],
Attributes* const uniforms,
FragmentShader* const frag,
VertexShader* const vert,
Buffer2D<double>* zBuf)
{
// Setup count for vertices & attributes
int numIn = 0;
switch(prim)
{
case POINT:
numIn = 1;
break;
case LINE:
numIn = 2;
break;
case TRIANGLE:
numIn = 3;
break;
}
// Vertex shader
Vertex transformedVerts[MAX_VERTICES];
Attributes transformedAttrs[MAX_VERTICES];
VertexShaderExecuteVertices(vert, inputVerts, inputAttrs, numIn, uniforms, transformedVerts, transformedAttrs);
// Vertex Interpolation & Fragment Drawing
switch(prim)
{
case POINT:
DrawPoint(target, transformedVerts, transformedAttrs, uniforms, frag);
break;
case LINE:
DrawLine(target, transformedVerts, transformedAttrs, uniforms, frag);
break;
case TRIANGLE:
DrawTriangle(target, transformedVerts, transformedAttrs, uniforms, frag);
}
}
/*************************************************************
* MAIN:
* Main game loop, initialization, memory management
************************************************************/
int main()
{
// -----------------------DATA TYPES----------------------
SDL_Window* WIN; // Our Window
SDL_Renderer* REN; // Interfaces CPU with GPU
SDL_Texture* GPU_OUTPUT; // GPU buffer image (GPU Memory)
SDL_Surface* FRAME_BUF; // CPU buffer image (Main Memory)
// ------------------------INITIALIZATION-------------------
SDL_Init(SDL_INIT_EVERYTHING);
WIN = SDL_CreateWindow(WINDOW_NAME, 200, 200, S_WIDTH, S_HEIGHT, 0);
REN = SDL_CreateRenderer(WIN, -1, SDL_RENDERER_SOFTWARE);
FRAME_BUF = SDL_CreateRGBSurface(0, S_WIDTH, S_HEIGHT, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
FRAME_BUF = SDL_ConvertSurface(SDL_GetWindowSurface(WIN), SDL_GetWindowSurface(WIN)->format, 0);
GPU_OUTPUT = SDL_CreateTextureFromSurface(REN, FRAME_BUF);
BufferImage frame(FRAME_BUF);
// Draw loop
bool running = true;
while(running)
{
// Handle user inputs
processUserInputs(running);
// Refresh Screen
clearScreen(frame);
// Your code goes here
// Push to the GPU
SendFrame(GPU_OUTPUT, REN, FRAME_BUF);
}
// Cleanup
SDL_FreeSurface(FRAME_BUF);
SDL_DestroyTexture(GPU_OUTPUT);
SDL_DestroyRenderer(REN);
SDL_DestroyWindow(WIN);
SDL_Quit();
return 0;
}